Write A Python Program To Count The Number Of Even And Odd Numbers From A Series Of Numbers
In this Python example post, you will learn to write a python program to check if a number is odd or even from a series of numbers. We will show you the different examples so that you can choose your preferred solution.
Example 1:
1. Write A Python Program To Count The Number Of Even And Odd Numbers From A Series Of Numbers
In the following code, you can see the python program to count the number of even and odd numbers from the given series of numbers.
Numbers =(1,2,3,4,5,6,7,8,9)
count_odd_num =0
count_even_num =0
for x in numbers:
if not x % 2:
count_even_num+=1
else:
count_odd_num+=1
print("Number of even numbers :",count_even_num)
print("Number of odd numbers :",count_odd_num)
Example 2:
2. Python program to Count Even And Odd numbers in a List
In this other example, you can see the example of a python program to count even and odd numbers in a list.
list1
=
[
10
,
21
,
4
,
45
,
66
,
93
,
1
]
even_count, odd_count
=
0
,
0
# iterating each number in list
for
num
in
list1:
# checking condition
if
num
%
2
=
=
0
:
even_count
+
=
1
else
:
odd_count
+
=
1
("Even numbers
in
the
list
: ", even_count)
("Odd numbers
in
the
list
: ", odd_count)
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Example 3:
3. Python program to Count Even And Odd numbers in a List using While loop
Write a program in python to count even and odd numbers using while loop.
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] even_count, odd_count = 0, 0 num = 0 # using while loop while(num < len(list1)): # checking condition if list1[num] % 2 == 0: even_count += 1 else: odd_count += 1 # increment num num += 1 print("Even numbers in the list: ", even_count) print("Odd numbers in the list: ", odd_count)
Example 4 :
4. Python program to Count Even And Odd numbers in a List Using Python Lambda Expressions
Write a program in python to count even and odd numbers using python lambda expressions.
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] odd_count = len(list(filter(lambda x: (x%2 != 0) , list1))) # we can also do len(list1) - odd_count even_count = len(list(filter(lambda x: (x%2 == 0) , list1))) print("Even numbers in the list: ", even_count) print("Odd numbers in the list: ", odd_count)
Example 5:
5. Python program to Count Even And Odd numbers in a List Using List Comprehension
Write a program in python to count even and odd numbers in list using list comprehension
# Python program to print odd Numbers in a List # list of numbers list1 = [10, 21, 4, 45, 66, 93, 11] only_odd = [num for num in list1 if num % 2 == 1] odd_count = len(only_odd) print("Even numbers in the list: ", len(list1) - odd_count) print("Odd numbers in the list: ", odd_count)
Example 6:
6. Python program to Count Even And Odd numbers in a List Using Recursion
if (n < 2): return (n % 2 == 0) return (check(n - 2)) n=int(input("Enter number:")) if(check(n)==True): print("Number is even!") else: print("Number is odd!")