Python program to print the even numbers from a given list

Logic: To do this, we will simply go through the list and check whether the number is divisible by 2 or not, if it is divisible by 2, then the number is EVEN otherwise it is ODD.

example 1:-

def is_even_num(l):
    evennum = []
    for num in l:
        if num % 2 == 0:
            evennum.append(num)
    return evennum
print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9]))
output:-

[2, 4, 6, 8]

>>> 

example 2:-

list1 = [11,23,45,23,64,22,11,24]
# iteration
for num in list1:
   # check
   if num % 2 == 0:
      print(num, end = " ")
output:-

64 22 24 

>>> 

example 3:

num_list=[]
n=int(input("Enter the Starting of the range:"))
k=int(input("Enter the Ending of the range:"))
for i in range(n,k):
  num_list.append(i)
print("Original Number List:", num_list)
even_list=[]
odd_list=[]
for i in range(len(num_list)):
  if(num_list[i]%2==0):
    even_list.append(num_list[i])
  else:
    odd_list.append(num_list[i])
print("Even Numbers List:", even_list)
print("Odd Numbers List:", odd_list)
output:-

Enter the Starting of the range:1 

Enter the Ending of the range:50

Original Number List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

Even Numbers List: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]

Odd Numbers List: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]

>>> 

example 4:-

# Give number of elements present in list
n=int(input())

# list
l= list(map(int,input().strip().split(" ")))

# the number will be odd if on diving the number by 2 
# its remainder is one otherwise number will be even
odd=[]
even=[]

for i in l:
    if(i%2!=0):
        odd.append(i)
    else:
        even.append(i)

print("list of odd number is:",odd)
print("list of even number is:",even)
output:
4
12 3 5 6 78
list of odd number is: [3, 5]
list of even number is: [12, 6, 78]
>>> 

Post a Comment

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

Previous Post Next Post