Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sum_tuples(first_tuple, second_tuple):
- """
- Function that takes in two tuples as arguments, and returns a new tuple that is the result of the summing of the two input tuples.
- If the tuples have different length, it will sum the values with the same index and fill the rest with zeroes.
- Args:
- first_tuple (tuple): The first tuple to be summed
- second_tuple (tuple): The second tuple to be summed
- Returns:
- tuple: A new tuple that is the result of the summing of the two input tuples
- Example:
- result = sum_tuples((1, 2, 3), (4, 5, 6))
- # result: (5, 7, 9)
- result = sum_tuples((1, 2, 3), (4, 5))
- # result: (5, 7, 3)
- """
- # Create a new empty list to store the sum of the tuples
- summed_list = []
- # Iterate through the elements of the first tuple
- for i in range(max(len(first_tuple), len(second_tuple))):
- try:
- # Sum the elements of the tuples with the same index
- summed_list.
Advertisement
Add Comment
Please, Sign In to add comment