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.

# Example 1

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.

# Using For Loop

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.

# Uisng Functions

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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.