Categories: category

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.

Separate odd and even number from a list

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

for loop in Python language

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

 

 

 

Calculate power of a number using recursion in C language
Calculate sum of odd and even in an array in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago