Python Program To Print Even Numbers In List Example
Jul 20, 2021
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....
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?