In this python program, we will learn how to count the even and odd numbers in a given list. In this program, we will identify the even and odd numbers and display the total number of even and odd numbers in a given list.
Here is the source code of the program to count the even and odd numbers in a list.
# Python Program to Count Even and Odd Numbers in a List Using For Loop
NumList = []
Even_count = 0
Odd_count = 0
# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("nList Entered By the User: ",NumList)
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)
Enter the Total Number of List Elements: 5
Enter the Value of 1 Element: 23
Enter the Value of 2 Element: 15
Enter the Value of 3 Element: 22
Enter the Value of 4 Element: 55
Enter the Value of 5 Element: 65
List Entered By the User: [23, 15, 22, 55, 65]
Total Number of Even Numbers in this List = 1
Total Number of Odd Numbers in this List = 4
# Python Program to Count Even and Odd Numbers in a List Using While Loop
NumList = []
Even_count = 0
Odd_count = 0
j = 0
# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList Entered By the User: ",NumList)
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)
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 45
Enter the Value of 2 Element: 25
Enter the Value of 3 Element: 66
Enter the Value of 4 Element: 44
Enter the Value of 5 Element: 13
Enter the Value of 6 Element: 52
List Entered By the User: [45, 25, 66, 44, 13, 52]
Total Number of Even Numbers in this List = 3
Total Number of Odd Numbers in this List = 3
Program 3: Python Program to Count Even and Odd Numbers in a List By Defining functions
# Python Program to Count Even and Odd Numbers in a List By Defining functions
# Define a function
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
# Define a Function
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("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList Entered By the User: ",NumList)
even_count = count_even(NumList)
odd_count = count_odd(NumList)
print("\nTotal Number of Even Numbers in this List = ", even_count)
print("Total Number of Odd Numbers in this List = ", odd_count)
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 5
Enter the Value of 2 Element: 2
Enter the Value of 3 Element: 4
Enter the Value of 4 Element: 6
Enter the Value of 5 Element: 2
Enter the Value of 6 Element: 7
List Entered By the User: [5, 2, 4, 6, 2, 7]
Total Number of Even Numbers in this List = 4
Total Number of Odd Numbers in this List = 2
Comments