Python program to find odd and even numbers from a list
Python program to find odd and even number from list
In this tutorial, we are going to discuss how to find odd and even numbers from a list in the Python program.
Even number produces zero balance when the number is divided by two.
Odd number produces one as balance when the number is divided by two.
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
A modular operator is used to separate odd or even numbers.
if n%2==0, n is a even number
if n%2==1, n is a odd number
Program 1
numbers=[23,56,76,78,90,35,12,45] #defined a list for i in numbers: //for loop use to iterates each number if(i%2)==0: print i, "is even" //print even number else: print i, "is odd" //print odd number
When the above code is executed, it produces the following results
23 is odd 56 is even 76 is even 78 is even 90 is even 35 is odd 12 is even 45 is odd
Approach
- To begin with, a user can use input() function to create a length of list for the numbers.
- Then, it is initialized by a list called “NumberList=[] ” which has no numbers.
- Next, an input is received for each number in the list using the “for loop”.
- The “For loop” appends each number to the list, NumberList=[]
- Then, the second “for loop” checks whether the number is even or odd in the list using the modular operator.
- Finally, The odd and even numbers are displayed on the screen
if n%2==0, n is an even number
if n%2==1, n is an odd number
- finally even and odd numbers are displayed on the screen
Program 2
NumberList=[] size=int(input("Enter the total number of list elemnts: ")) for i in range(size): element=int(input("please Enter the value of %d elements: "%i)) NumberList.append(element) print("find odd and even here") for i in range(size): if(NumberList[i]%2)==0: print NumberList[i], "is even" else: print NumberList[i], "is odd"
When the above code is executed, it produces the following results
Enter the total number of list elemnts: 7 please Enter the value of 0 elements: 12 please Enter the value of 1 elements: 23 please Enter the value of 2 elements: 34 please Enter the value of 3 elements: 45 please Enter the value of 4 elements: 56 please Enter the value of 5 elements: 67 please Enter the value of 6 elements: 78 find odd and even here 12 is even 23 is odd 34 is even 45 is odd 56 is even 67 is odd 78 is even
Suggested for you
if statements in Python language
Similar post
Python program check whether a number is odd or even
Python program to check a number is even or odd using the function
Python program to display even and odd numbers without if
Python program to display even and odd number in the given range
Python code to display all even and odd numbers from 1 to n