Write a Python Program to find Sum of Even and Odd Numbers in a List using For Loop, While Loop, and Functions with a practical example.

## Python Program to find Sum of Even and Odd Numbers in a List using For Loop

In this python program, we are using For Loop to iterate each element in a given List. Inside the Python loop, we used the If statement to check and find the Sum of Even and odd numbers.

# Python Program to find Sum of Even and Odd Numbers in a List NumList = [] Even_Sum = 0 Odd_Sum = 0 Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) for j in range(Number): if(NumList[j] % 2 == 0): Even_Sum = Even_Sum + NumList[j] else: Odd_Sum = Odd_Sum + NumList[j] print("\nThe Sum of Even Numbers in this List = ", Even_Sum) print("The Sum of Odd Numbers in this List = ", Odd_Sum)

In this python program to find Sum of Even and Odd Numbers in a List, User entered items = [2, 3, 4, 5], Even_Sum = 0, Odd_Sum = 0.

For Loop – First Iteration: for 0 in range(0, 4)

The condition is True. So, it enters into the If Statement

if(NumList[0] % 2 == 0) => if(2 % 2 == 0) – Condition is True

Even_Sum = Even_Sum + NumList[0] => 0 + 2 = 2

Second Iteration: for 1 in range(0, 4) – Condition is True

if(NumList[1] % 2 == 0) => if(3 % 2 == 0) – Condition is False, so it enters into the Else block.

Odd_Sum = Odd_Sum + NumList[1] => 0 + 3 = 3

Third Iteration: for 2 in range(0, 4) – Condition is True

if(NumList[2] % 2 == 0) => if(4 % 2 == 0) – Condition is True

Even_Sum = 2 + 4 = 6

Fourth Iteration: for 3 in range(0, 4) – Condition is True

if(5 % 2 == 0) – Condition is False, so it enters into Else block.

Odd_Sum = 3 + 5 = 8

Fifth Iteration: for 4 in range(4) – Condition is False. So, Python exits from For Loop

## Python Program to find Sum of Even and Odd Numbers in a List using While loop

This Python program for calculating the sum of even and odd numbers is the same as the above. We just replaced the For Loop with While loop.

# Python Program to find Sum of Even and Odd Numbers in a List NumList = [] Even_Sum = 0 Odd_Sum = 0 j = 0 Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) while(j < Number): if(NumList[j] % 2 == 0): Even_Sum = Even_Sum + NumList[j] else: Odd_Sum = Odd_Sum + NumList[j] j = j+ 1 print("\nThe Sum of Even Numbers in this List = ", Even_Sum) print("The Sum of Odd Numbers in this List = ", Odd_Sum)

The sum of even and odd numbers in a Python List using while loop output

```
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 22
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 55
Please enter the Value of 5 Element : 99
The Sum of Even Numbers in this List = 66
The Sum of Odd Numbers in this List = 187
```

## Python Program to Calculate Sum of Even and Odd Numbers in a List using Functions

This Python sum of even and odd list numbers program is the same as the first example. However, we separated the logic using Functions

# Python Program to find Sum of Even and Odd Numbers in a List def even_sum(NumList): Even_Sum = 0 for j in range(Number): if(NumList[j] % 2 == 0): Even_Sum = Even_Sum + NumList[j] return Even_Sum def odd_sum(NumList): Odd_Sum = 0 for j in range(Number): if(NumList[j] % 2 != 0): Odd_Sum = Odd_Sum + NumList[j] return Odd_Sum NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) Even_Sum = even_sum(NumList) Odd_Sum = odd_sum(NumList) print("\nThe Sum of Even Numbers in this List = ", Even_Sum) print("The Sum of Odd Numbers in this List = ", Odd_Sum)

The sum of even and odd numbers in a List using functions output

```
Please enter the Total Number of List Elements: 7
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 9
Please enter the Value of 3 Element : 21
Please enter the Value of 4 Element : 13
Please enter the Value of 5 Element : 87
Please enter the Value of 6 Element : 14
Please enter the Value of 7 Element : 66
The Sum of Even Numbers in this List = 92
The Sum of Odd Numbers in this List = 130
```