Write a Python Program to Print Even Numbers in a Set. The if statement (if(val % 2 == 0)) tests whether the Set item is divisible by two returns zero. If True, print that Even number in a Set.
# Set Even Numbers evSet = {15, 26, 19, 78, 64, 35, 66} print("Even Set Items = ", evSet) print("\nThe Even Numbers in this evSet Set are:") for val in evSet: if(val % 2 == 0): print(val, end = " ")
Print Even numbers in a Python set output
Even Set Items = {64, 66, 35, 19, 26, 78, 15}
The Even Numbers in this evSet Set are:
64 66 26 78
This Python Set Program allows to enter the set items and Print the Even Numbers.
# Set Even Numbers evSet = set() number = int(input("Enter the Total Even Set Items = ")) for i in range(1, number + 1): value = int(input("Enter the %d Set Item = " %i)) evSet.add(value) print("Even Set Items = ", evSet) print("\nThe Even Numbers in this evSet Set are:") for val in evSet: if(val % 2 == 0): print(val, end = " ")
In this Python Set example, we created a setEvenNumbers function that prints the Even numbers.
# Set Even Numbers def setEvenNumbers(evset): for val in evSet: if(val % 2 == 0): print(val, end = " ") evSet = set() number = int(input("Enter the Total Even Set Items = ")) for i in range(1, number + 1): value = int(input("Enter the %d Set Item = " %i)) evSet.add(value) print("Even Set Items = ", evSet) print("\nThe Even Numbers in this evSet Set are:") setEvenNumbers(evSet)
Enter the Total Even Set Items = 7
Enter the 1 Set Item = 22
Enter the 2 Set Item = 33
Enter the 3 Set Item = 44
Enter the 4 Set Item = 55
Enter the 5 Set Item = 66
Enter the 6 Set Item = 77
Enter the 7 Set Item = 89
Even Set Items = {33, 66, 44, 77, 22, 55, 89}
The Even Numbers in this evSet Set are:
66 44 22