Python Program to Find Sum of Even and Odd Numbers in Set

Write a Python Program to find the Sum of Even and Odd Numbers in a Set. The if condition (if(eoVal % 2 == 0)) examines the Set item divisible by two equals zero. If True, add that set value to sEvenSum; otherwise, add to the sOddSum.

# Sum of Set Even and Odd Numbers

evenoddSet = {78, 64, 11, 95, 36, 66, 77, 151}
print("Even and Odd Set Items = ", evenoddSet)

sEvenSum = sOddSum = 0

for eoVal in evenoddSet:
    if(eoVal % 2 == 0):
        sEvenSum = sEvenSum + eoVal
    else:
        sOddSum = sOddSum + eoVal

print("The Sum of Even Numbers in evenoddSet = ", sEvenSum)
print("The Sum of Odd  Numbers in evenoddSet = ", sOddSum)
Python Program to Find Sum of Even and Odd in Set 1

Python Program to Find Sum of Even and Odd Numbers in Set

This Python even and odd sum example allows to enter the set items.

# Sum of Set Even and Odd Numbers

evenoddSet = set()

number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    evenoddSet.add(value)

print("Even and Odd Set Items = ", evenoddSet)

sEvenSum = sOddSum = 0

for eoVal in evenoddSet:
    if(eoVal % 2 == 0):
        sEvenSum = sEvenSum + eoVal
    else:
        sOddSum = sOddSum + eoVal

print("The Sum of Even Numbers in evenoddSet = ", sEvenSum)
print("The Sum of Odd  Numbers in evenoddSet = ", sOddSum)

The Sum of Even and Odd Numbers in a Python Set output

Enter the Total Even Odd Set Items = 7
Enter the 1 Set Item = 22
Enter the 2 Set Item = 44
Enter the 3 Set Item = 87
Enter the 4 Set Item = 99
Enter the 5 Set Item = 122
Enter the 6 Set Item = 321
Enter the 7 Set Item = 439
Even and Odd Set Items =  {321, 439, 99, 44, 22, 87, 122}
The Sum of Even Numbers in evenoddSet =  188
The Sum of Odd  Numbers in evenoddSet =  946

In this Python Set example, we created a sumOfSetEvenandOddNumbers function that returns the sum of Even and Odd numbers in a Set.

# Sum of Set Even and Odd Numbers

def sumOfSetEvenandOddNumbers(evenoddSet):
    sEvenSum = sOddSum = 0

    for eoVal in evenoddSet:
        if(eoVal % 2 == 0):
            sEvenSum = sEvenSum + eoVal
        else:
            sOddSum = sOddSum + eoVal
    return sEvenSum, sOddSum


evenoddSet = set()

number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    evenoddSet.add(value)

print("Even and Odd Set Items = ", evenoddSet)

sESum, sOSum = sumOfSetEvenandOddNumbers(evenoddSet)
print("The Sum of Even Numbers in evenoddSet = ", sESum)
print("The Sum of Odd  Numbers in evenoddSet = ", sOSum)
Enter the Total Even Odd Set Items = 5
Enter the 1 Set Item = 12
Enter the 2 Set Item = 33
Enter the 3 Set Item = 44
Enter the 4 Set Item = 86
Enter the 5 Set Item = 99
Even and Odd Set Items =  {33, 99, 12, 44, 86}
The Sum of Even Numbers in evenoddSet =  142
The Sum of Odd  Numbers in evenoddSet =  132