Write a Python Program to Count Even and Odd Numbers in a List using For Loop, While Loop, and Functions with a practical example.
Python Program to Count Even and Odd Numbers in a List using For Loop
In this program, we are using For Loop to iterate every element in a given List. Inside the loop, we use the If statement to check and count Even and odd numbers.
NumList = []
Even_count = 0
Odd_count = 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_count = Even_count + 1
else:
Odd_count = Odd_count + 1
print("\nTotal Number of Even Numbers in this List = ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)

In this python program, the User entered List elements = [2, 3, 5, 7], Even_count = 0, Odd_count = 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_count = Even_count + 1 => 0 + 1 = 1
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_count = Odd_count + 1 => 0 + 1 = 1
Third Iteration: for 2 in range(0, 4) – Condition is True.
if(NumList[2] % 2 == 0) => if(5 % 2 == 0) – Condition is False and enters into the Else block.
Odd_count = 1 + 1 = 2
Fourth Iteration: for 3 in range(0, 4) – Condition is True.
if(7 % 2 == 0) – Condition is False and enters into the Else block.
Odd_count = 2 + 1 = 3
Fifth Iteration: for 4 in range(4) – Condition is False. So, it exits from For Loop
Python Program to Count Even and Odd Numbers in a List using While loop
This program for counting is the same as the above. We just replaced the Python For Loop with the While loop.
NumList = []
Even_count = 0
Odd_count = 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_count = Even_count + 1
else:
Odd_count = Odd_count + 1
j = j + 1
print("\nTotal Number of Even Numbers in this List = ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 13
Please enter the Value of 3 Element : 14
Please enter the Value of 4 Element : 15
Please enter the Value of 5 Element : 44
Total Number of Even Numbers in this List = 3
Total Number of Odd Numbers in this List = 2
Program to Count Even and Odd Numbers in a List using Functions
This count even and odd list numbers program is the same as the first example. However, we separated the logic using Functions.
def count_even(NumList):
Even_count = 0
for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
return Even_count
def count_odd(NumList):
Odd_count = 0
for j in range(Number):
if(NumList[j] % 2 != 0):
Odd_count = Odd_count + 1
return Odd_count
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_cnt = count_even(NumList)
odd_cnt = count_odd(NumList)
print("\nTotal Number of Even Numbers in this List = ", even_cnt)
print("Total Number of Odd Numbers in this List = ", odd_cnt)
Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 22
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 44
Please enter the Value of 5 Element : 55
Please enter the Value of 6 Element : 66
Total Number of Even Numbers in this List = 4
Total Number of Odd Numbers in this List = 2