Advertisement
Blessing988

Untitled

Feb 29th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. job_opening = ("Software Engineer", "New York City", 100000)
  2.  
  3. position, city, salary  =  job_opening           #destructuring values in job_opening tuple and assigning them to new variables
  4.  
  5. #Testing
  6. print(position)
  7. print(city)
  8. print(salary)
  9.  
  10.  
  11.  
  12. address = ("35 Elm Street", "San Francisco", "CA", "94107")
  13.  
  14. street, *city_and_state, zip_code = address     #destructuring values in address tuple and assigning them to new variables
  15.  
  16. #Testing
  17. print(street)
  18. print(zip_code)
  19. print(city_and_state)
  20.  
  21.  
  22.  
  23. #This program declares a function that accepts a tuple of elements(numbers) and returns a tuple with two numeric values: (sum of the even numbers, sum of the odd numbers)
  24.  
  25. #function
  26.  
  27. def sum_of_evens_and_odds(*a_tuple_of_numbers):  #asterisks(*) should be introduced to demonstrate that it would accept a tuple
  28.  
  29.     even_numbers = sum(tuple(filter(lambda x : x%2 ==0 , a_tuple_of_numbers))) #this would store the sum of all the even numbers in the tuple in the even_numbers variable
  30.  
  31.     odd_numbers =  sum(tuple(filter(lambda x : x%2 ==1 , a_tuple_of_numbers)))  #this would store the sum of all the odd numbers in the tuple in the odd_numbers variable
  32.  
  33.     return(even_numbers, odd_numbers)
  34.  
  35. #calling the function
  36. print(sum_of_evens_and_odds(1, 2, 3, 4))   # Example (the argument must be a tuple consisting of numbers)
  37. print(sum_of_evens_and_odds(2, 4, 6))      # Example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement