Extract Even and odd number from a given list in Python

Separating EVEN and ODD numbers from the list: Here, we will figure out how to extricate Even and odd number from a given list in Python programming language?

In this issue, we are given a list by the client which might be the blend of even and odd numbers and dependent on the idea of even and odd, we will part the list into two lists and one will contain just even numbers and another will contain just odd numbers. Prior to going to carry out this responsibility, we will figure out how to check the given number is even or odd in Python?

What are the even and odd number?

The number that can be completely separated by 2 is known as an even number and on the off chance that the number isn’t detachable by 2, at that point it is known as an odd number.

Python program to check even or odd number:

# taking input from the user 
n=int(input('Enter the number: '))

# checking whether it is EVEN or ODD
if n%2==0:
    print('{} is an even number.'.format(n))
else:
    print('{} is an odd number.'.format(n))

Output:

RUN 1:
Enter the number: 63734
63734 is an even number.

RUN 2:
Enter the number: 9568405
9568405 is an odd number.

Calculation to separate even and odd number from the given list:

  1. Take the contribution to the type of a list.
  2. Make two void lists to store the even and odd number which will be removed from the given list.
  3. Check every component of the given list.
  4. In the event that it is an EVEN number, at that point add this to one of the lists from the above-made list by utilizing the added strategy.
  5. In the event that it is an ODD number, at that point add this to another list from the above-made list by utilizing the added strategy.
  6. Print the two lists which will be our necessary list.

Python program to check even or odd number from the given list:

# input the list
A=list(map(int,input('Enter elements of List: ').split()))

# create two empty lists to store EVEN and ODD elements
B=[]
c=[]

for j in A:
    if j%2==0:
        B.append(j)
    else:
        c.append(j)
        
print('List of even number: ',B)
print('List of odd number: ',c)

Output:

Enter elements of List: 6 4 7 45 7 6 7 9 2 1
List of even number: [6, 4, 6, 2]
List of odd number: [7, 45, 7, 7, 9, 1]

append() strategy:

The capacity adds a user to add a number to a current list. Here, we have utilized the affix capacity to add a considerable number to list B and odd numbers to list C.

Leave a Comment

error: Alert: Content is protected!!