Python program to print even numbers in a list
Given a list of numbers, write a Python program to print all even numbers in the given list.
Example:
Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12, 14, 95, 3] Output: [12, 14]Method 1: Using for loop
Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print[num, end=" "]
|
Output:
10, 4, 66Method 2: Using while loop
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 24, 4, 45, 66, 93]
num = 0
# using while loop
while[num < len[list1]]:
# checking condition
if list1[num] % 2 == 0:
print[list1[num], end=" "]
# increment num
num += 1
|
Output:
10, 4, 66Method 3: Using list comprehension
# Python program to print even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# using list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print["Even numbers in the list: ", even_nos]
|
Output:
Even numbers in the list: [10, 4, 66]Method 4: Using lambda expressions
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print even no's using lambda exp.
even_nos = list[filter[lambda x: [x % 2 == 0], list1]]
print["Even numbers in the list: ", even_nos]
|
Output:
Even numbers in the list: [10, 4, 66]Python program to print even numbers in a list
In this article, we will learn about the solution and approach to solve the given problem statement.
Python program to print even numbers in a list
In this tutorial, you will learn to write a program that will print all the even numbers in a list. Even numbers are the numbers that are divisible by 2. We will use this property of even numbers in our program. The concept of loops in Python and conditional statements in Python will be used in our program.
For a given list of numbers, the task is to find and print all the even numbers in the list.
Input: [7, 4, 9, 3, 5, 1, 2, 12]
Output: [4, 2, 12]
Input: [13, 17, 9, 8, 15, 29]
Output: [8]
Python Program to Print Even Numbers in a List using For Loop
In thispython program, we are usingFor Loopto iterate each element in this List. Inside thePythonloop, we are using the If statement to check even numbers.
# Python Program to Print Even Numbers in a List NumList = [] Number = int[input["Please enter the Total Number of List Elements: "]] for i in range[1, Number + 1]: value = int[input["Please enter the Value of %d Element : " %i]] NumList.append[value] print["\nEven Numbers in this List are : "] for j in range[Number]: if[NumList[j] % 2 == 0]: print[NumList[j], end = ' ']In this In thispython program,User entered list elements = [22, 56, 7, 87]
For Loop – First Iteration:for 0 in range[0, 4]
The For Loop condition is True. So, Pythonenters into the If Statement
if[NumList[0] % 2 == 0] => if[22 % 2 == 0] – Condition True
This Number printed.
Second Iteration: for 1 in range[0, 4] –Condition is True
if[NumList[1] % 2 == 0] => if[56 % 2 == 0] – Condition True
This Number printed.
Third Iteration: for 2 in range[0, 4] –Condition is True
if[NumList[2] % 2 == 0] => if[7 % 2 == 0] – Condition is False
This Number is skipped.
Fourth Iteration: for 3 in range[0, 4] –Condition is True
if[NumList[3] % 2 == 0] => if[87 % 2 == 0] – Condition is False
This Number skipped.
Fifth Iteration: for 4 in range[0, 4] –Condition is False
So, it exits from the For Loop
Introduction
In this section of python programming, we will understand the python code to print all the even numbers in the given list.
Expression: num % 2 == 0
Python program to list even and odd numbers of a list
In this post, you will learn different ways to write Python programs to list even and odd numbers of a list. To check whether a number is odd or even, either you can ask the user to enter a number or you can provide a list from which the program can check whether the number is odd or even. Such type of program is generally asked during interviews and in the examination. This can also be helpful to improve our programming skill.