Write a Python Program to Print Even Numbers in Tuple using for loop range. The if statement (if(evTuple[i] % 2 == 0)) checks whether each Tuple item is divisible by two. If True, print that Tuple even number.
# Tuple Even Numbers evTuple = (3, 78, 67, 34, 88, 11, 23, 19) print("Tuple Items = ", evTuple) print("\nThe Even Numbers in this evTuple Tuple are:") for i in range(len(evTuple)): if(evTuple[i] % 2 == 0): print(evTuple[i], end = " ")
Tuple Items = (3, 78, 67, 34, 88, 11, 23, 19)
The Even Numbers in this evTuple Tuple are:
78 34 88
Python Program to Print Even Numbers in Tuple using the For Loop.
In this Python even numbers example, we used the for loop (for tup in evTuple) to iterate the actual tuple items.
# Tuple Even Numbers evTuple = (19, 25, 32, 44, 17, 66, 11, 98) print("Tuple Items = ", evTuple) print("\nThe Even Numbers in this evTuple Tuple are:") for tup in evTuple: if(tup % 2 == 0): print(tup, end = " ")
Tuple Items = (19, 25, 32, 44, 17, 66, 11, 98)
The Even Numbers in this evTuple Tuple are:
32 44 66 98
Python Program to return or display Even Numbers in Tuple using the While Loop.
# Tuple Even Numbers evTuple = (25, 12, 19, 44, 66, 79, 89, 22, 46) print("Tuple Items = ", evTuple) i = 0 print("\nThe Even Numbers in this evTuple Tuple are:") while (i < len(evTuple)): if(evTuple[i] % 2 == 0): print(evTuple[i], end = " ") i = i + 1
Tuple Items = (25, 12, 19, 44, 66, 79, 89, 22, 46)
The Even Numbers in this evTuple Tuple are:
12 44 66 22 46
In this Python Tuple example, we created a function (tupleEvenNumbers(evTuple)) that finds and prints the Even numbers.
# Tuple Even Numbers def tupleEvenNumbers(evTuple): for tup in evTuple: if(tup % 2 == 0): print(tup, end = " ") evTuple = (19, 32, 25, 77, 44, 56, 89, 45, 98, 122, 55, 12) print("Tuple Items = ", evTuple) print("\nThe Even Numbers in this evTuple Tuple are:") tupleEvenNumbers(evTuple)