Write a Python Program to find the Sum of Even and Odd Numbers in a Tuple using the for loop range. The if condition (if(evenOddTuple[i] % 2 == 0)) checks whether the Tuple item divisible by two equals zero. If True, add that tuple Item to even sum; otherwise, add to the odd sum.
# Sum of Tuple Even and Odd Numbers evenOddTuple = (9, 22, 33, 45, 56, 77, 89, 90) print("Even and Odd Tuple Items = ", evenOddTuple) tEvenSum = tOddSum = 0 for i in range(len(evenOddTuple)): if(evenOddTuple[i] % 2 == 0): tEvenSum = tEvenSum + evenOddTuple[i] else: tOddSum = tOddSum + evenOddTuple[i] print("The Sum of Even Numbers in evenOddTuple = ", tEvenSum) print("The Sum of Odd Numbers in evenOddTuple = ", tOddSum)
Even and Odd Tuple Items = (9, 22, 33, 45, 56, 77, 89, 90)
The Sum of Even Numbers in evenOddTuple = 168
The Sum of Odd Numbers in evenOddTuple = 253
Python Program to Find the Sum of Even and Odd Numbers in Tuple
In this Python example, we used the for loop (for eotup in evenOddTuple) and If statement (if(eotup % 2 == 0)) to inspect whether tuple items are divisible by two equal to zero.
# Sum of Tuple Even and Odd Numbers evenOddTuple = (11, 22, 33, 44, 55, 66, 77, 88, 99) print("Even and Odd Tuple Items = ", evenOddTuple) tEvenSum = tOddSum = 0 for eotup in evenOddTuple: if(eotup % 2 == 0): tEvenSum = tEvenSum + eotup else: tOddSum = tOddSum + eotup print("The Sum of Even Numbers in evenOddTuple = ", tEvenSum) print("The Sum of Odd Numbers in evenOddTuple = ", tOddSum)
Python Tuple Program to Find the Sum of Even and Odd using the While Loop.
# Sum of Tuple Even and Odd Numbers evenOddTuple = (16, 25, 42, 35, 56, 55, 37, 95, 122, 205) print("Even and Odd Tuple Items = ", evenOddTuple) tEvenSum = tOddSum = 0 i = 0 while (i < len(evenOddTuple)): if(evenOddTuple[i] % 2 == 0): tEvenSum = tEvenSum + evenOddTuple[i] else: tOddSum = tOddSum + evenOddTuple[i] i = i + 1 print("The Sum of Even Numbers in evenOddTuple = ", tEvenSum) print("The Sum of Odd Numbers in evenOddTuple = ", tOddSum)
Even and Odd Tuple Items = (16, 25, 42, 35, 56, 55, 37, 95, 122, 205)
The Sum of Even Numbers in evenOddTuple = 236
The Sum of Odd Numbers in evenOddTuple = 452
In this Python Tuple example, we created a sumOfEvenOddOddNumbers function that returns the Even and Odd Sum.
# Sum of Tuple Even and Odd Numbers def sumOfEvenOddOddNumbers(evenOddTuple): tEvenSum = tOddSum = 0 for eotup in evenOddTuple: if(eotup % 2 == 0): tEvenSum = tEvenSum + eotup else: tOddSum = tOddSum + eotup return tEvenSum, tOddSum evenOddTuple = (10, 23, 20, 33, 43, 40, 60, 93, 120, 83) print("Even and Odd Tuple Items = ", evenOddTuple) evenSum, oddSum = sumOfEvenOddOddNumbers(evenOddTuple) print("The Sum of Even Numbers in evenOddTuple = ", evenSum) print("The Sum of Odd Numbers in evenOddTuple = ", oddSum)
Even and Odd Tuple Items = (10, 23, 20, 33, 43, 40, 60, 93, 120, 83)
The Sum of Even Numbers in evenOddTuple = 250
The Sum of Odd Numbers in evenOddTuple = 275