Write a Python Program to find the sum of all the items in a tuple. Here, we used the Tuple sum function to return the sum of all tuple items.
# Tuple Sum of All Items numTuple = (20, 40, 65, 75, 80, 220) print("Tuple Items = ", numTuple) tupleSum = sum(numTuple) print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Tuple Items = (20, 40, 65, 75, 80, 220)
The Sum of numTuple Tuple Items = 500
Python Program to Find the Sum of Tuple Items
In this Python example, the for loop (for tup in numTuple) iterate all the tuple items. Within the loop (tupleSum = tupleSum + tup), we added each tuple item to tupleSum and printing the same.
# Tuple Sum of All Items numTuple = (11, 22, 33, 44, 55, 66, 77, 88, 99) print("Odd Tuple Items = ", numTuple) tupleSum = 0 for tup in numTuple: tupleSum = tupleSum + tup print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Odd Tuple Items = (11, 22, 33, 44, 55, 66, 77, 88, 99)
The Sum of numTuple Tuple Items = 495
Python Program to calculate the Sum of all the Tuple Items using the While loop.
# Tuple Sum of All Items numTuple = (25, 45, 65, 75, 95, 125, 256, 725) print("Odd Tuple Items = ", numTuple) tupleSum = 0 i = 0 while (i < len(numTuple)): tupleSum = tupleSum + numTuple[i] i = i + 1 print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Odd Tuple Items = (25, 45, 65, 75, 95, 125, 256, 725)
The Sum of numTuple Tuple Items = 1411
In this Python Tuple example, we created a sumOfTupleItems(numTuple) function that will calculate and returns the sum of all tuple items.
# Tuple Sum def sumOfTupleItems(numTuple): tupleSum = 0 for tup in numTuple: tupleSum = tupleSum + tup return tupleSum numTuple = (16, 31, 24, 98, 123, 78, 56, 67, 22) print("Tuple Items = ", numTuple) tSum = sumOfTupleItems(numTuple) print("\nThe Sum of numTuple Tuple Items = ", tSum)