Advertisement
ganiyuisholaafeez

Tuples additional dealings

Feb 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. # Unpacking with tuples
  2. job_opening = ("Software Engineer", "New York City", 100000)
  3. position, city, salary = job_opening
  4. print(position)
  5. print(city)
  6. print(salary)
  7.  
  8. # Using asterisk with tuples
  9. address = ("35 Elm Street", "San Francisco", "CA", "94107")
  10. street, *city_and_state, zip_code = address
  11. print(street)
  12. print(zip_code)
  13. print(city_and_state)
  14.  
  15. # This is a function that returns the sum of all the even numbers on the left side
  16. # and the sum of all the odd numbers of a tuples
  17. def sum_of_evens_and_odds(*numbers):
  18.     sum_evens = 0
  19.     sum_odds = 0
  20.     for number in numbers:
  21.         if number % 2 == 0:
  22.             sum_evens += number
  23.         else:
  24.             sum_odds += number
  25.     return(sum_evens, sum_odds)
  26.  
  27.  
  28. print(sum_of_evens_and_odds(1, 2, 3, 4))
  29. print(sum_of_evens_and_odds(1, 3, 5))
  30. print(sum_of_evens_and_odds(2, 4, 6))
  31.  
  32.  
  33. Software Engineer
  34. # New York City
  35. # 100000
  36. # 35 Elm Street
  37. # 94107
  38. # ['San Francisco', 'CA']
  39. # (6, 4)
  40. # (0, 9)
  41. # (12, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement