Python Program to Count Even and Odd Numbers in a List

This article is created to cover some programs in Python, that counts total number of even numbers and odd numbers available in a given list entered by user at run-time. Here are the list of programs covered in this article:

• Count Even/Odd Numbers in a List of 10 Elements
• Count Even/Odd Numbers in a List of n Elements

Count Even/Odd in a List of 10 Elements

The question is, write a Python program to count total number of even and odd numbers present in a list of 10 numbers entered by user. Here is its answer:

nums = []
totEven = 0
totOdd = 0

print("Enter 10 Numbers: ")
for i in range(10):
nums.insert(i, int(input()))

for i in range(10):
if nums[i]%2==0:
totEven = totEven+1
else:
totOdd = totOdd+1

print("\nEven Number: ")
print(totEven)
print("Odd Number: ")
print(totOdd)

Here is its sample run:

Now supply the input as any 10 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and then press ENTER key to count and print how many even and odd numbers available in the given list as shown in the snapshot given below:

Count Even/Odd Numbers in List of n Elements

This program allows user to define the size of list. The question is, write a Python program to count even/odd numbers in a list of n size. The following program is the answer to this question:

nums = []
totEven = 0
totOdd = 0

print(end="Enter the Size: ")
size = int(input())

print(end="Enter " +str(size)+ " Numbers: ")
for i in range(size):
nums.insert(i, int(input()))

for i in range(size):
if nums[i]%2==0:
totEven = totEven+1
else:
totOdd = totOdd+1

print("\nEven Number: " +str(totEven))
print("Odd Number: " +str(totOdd))

Here is its sample run with user input 5 as size, and 1, 2, 3, 4, 5 as five numbers:

Python Online Test

« Previous Program Next Program »