20-Jul-2021
.
Admin
Hello Friends,
In this blog, I will learn you how to print even numbers in list using python. We will talk about python program to print even numbers in list example. This article will give you simple example of how to display even number in list using python.
In this example I am using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.
Here i will give you three example for python program to print even numbers in list. So let's see below example:
Example 1
# Python program to print even numbers
# declare list
my_list = [1,2,3,4,5,6,7,8,9]
# for loop
for num in my_list:
# checking condition
if num % 2 == 0:
print(num)
Output:
2
4
6
8
Example 2
# Python program to print even numbers
# declare list
my_list = [1,2,3,4,5,6,7,8,9]
# list of numbers
num = 0
# using while loop
while(num < len(my_list)):
# checking condition
if my_list[num] % 2 == 0:
print(my_list[num])
# increment num
num += 1
Output:
2
4
6
8
Example 3
# Python program to print even numbers
# declare list
my_list = [1,2,3,4,5,6,7,8,9]
# using list comprehension
even_nos = [num for num in my_list if num % 2 == 0]
print("Even numbers is : ", even_nos)
Output:
('Even numbers is : ', [2, 4, 6, 8])
It will help you....
#Python