Python program to count the number of even and odd numbers from a series of numbers

Example 1:- Count even or odd using for loop

numbers = (2,4,6,3,1,9,8,11)
count_odd = 0
count_even = 0
for x in numbers:
        if not x % 2:
             count_even+=1
        else:
             count_odd+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)

output:-

Number of even numbers : 4

Number of odd numbers : 4

>>> 

Example 2:-

numbers = [10,2,4,3,5,68,93,3,9,4] 

even, odd= 0, 0

for i in numbers: 

    if i % 2 == 0: 

        even += 1

    else: 

        odd+= 1          

print("Even : ", even) 

print("Odd : ", odd)

output:-
Even :  5
Odd :  5
>>> 

Count Even and Odd in List Using Lambda Function

Example 3-

numbers = [1,4,2,7,8,11,2,3,66]

even_list = list(filter(lambda x: (x % 2 == 0), numbers))

odd_list = list(filter(lambda x: (x % 2 != 0), numbers))

print("{0} even numbes and {1} odds.".format(len(even_list), len(odd_list)))

output:-

5 even numbes and 4 odds.
>>> 

Python Program to Count Even and Odd Numbers in a List


Example 4:-

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)
output:-

Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 3
Please enter the Value of 2 Element : 1
Please enter the Value of 3 Element : 6
Please enter the Value of 4 Element : 8

Total Number of Even Numbers in this List =   2
Total Number of Odd Numbers in this List =  2
>>> 

Python Program to Count Even and Odd Numbers in a List using 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)
output:-
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 3
Please enter the Value of 2 Element : 7
Please enter the Value of 3 Element : 8
Please enter the Value of 4 Element : 1

Total Number of Even Numbers in this List =   1
Total Number of Odd Numbers in this List =  3
>>> 

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post