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.
Python3
# 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
Python3
# 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
Python3
# 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
Python3
# 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]Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article
Python program to count Even and Odd numbers in a List
Given a list of numbers, write a Python program to count Even and Odd numbers in a List.
Example:
Input: list1 = [2, 7, 5, 64, 14] Output: Even = 3, odd = 2 Input: list2 = [12, 14, 95, 3] Output: Even = 2, odd = 2Example 1: count Even and Odd numbers from given list using for loop
Iterate each element in the list using for loop and check if num % 2 == 0, the condition to check even numbers. If the condition satisfies, then increase even count else increase odd count.
# Python program to count Even # and Odd numbers in a List
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# iterating each number in list for num in list1:
# checking condition if num % 2 == 0: even_count += 1
else: odd_count += 1
print["Even numbers in the list: ", even_count] print["Odd numbers in the list: ", odd_count] |
Output:
Even numbers in the list: 3 Odd numbers in the list: 4
Example 2: Using while loop
# Python program to count Even and Odd numbers in a List
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11]
even_count, odd_count = 0, 0 num = 0
# using while loop while[num < len[list1]]:
# checking condition if list1[num] % 2 == 0: even_count += 1 else: odd_count += 1
# increment num num += 1
print["Even numbers in the list: ", even_count] print["Odd numbers in the list: ", odd_count] |
Output: Even numbers in the list: 3 Odd numbers in the list: 4
Example 3 : Using Python Lambda Expressions
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11]
odd_count = len[list[filter[lambda x: [x%2 != 0] , list1]]]
# we can also do len[list1] - odd_count even_count = len[list[filter[lambda x: [x%2 == 0] , list1]]]
print["Even numbers in the list: ", even_count] print["Odd numbers in the list: ", odd_count] |
Output: Even numbers in the list: 3 Odd numbers in the list: 4
Example 4 : Using List Comprehension
# Python program to print odd Numbers in a List
# list of numbers list1 = [10, 21, 4, 45, 66, 93, 11]
only_odd = [num for num in list1 if num % 2 == 1] odd_count = len[only_odd]
print["Even numbers in the list: ", len[list1] - odd_count] print["Odd numbers in the list: ", odd_count] |
Output: Even numbers in the list: 3 Odd numbers in the list: 4
Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-lambda
python-list
Practice Tags :
python-list
Read Full Article
Python program to find odd and even numbers from a list
Python program to find odd and even number from list
In this tutorial, we are going to discuss how to find odd and even numbers from a list in the Python program.
Even number produces zero balance when the number is divided by two.
Odd number produces one as balance when the number is divided by two.
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
A modular operator is used to separate odd or even numbers.
if n%2==0, n is a even number
if n%2==1, n is a odd number
Program 1
numbers=[23,56,76,78,90,35,12,45] #defined a list for i in numbers: //for loop use to iterates each number if[i%2]==0: print i, "is even" //print even number else: print i, "is odd" //print odd numberWhen the above code is executed, it produces the following results
23 is odd 56 is even 76 is even 78 is even 90 is even 35 is odd 12 is even 45 is oddApproach
- To begin with, a user can use input[] function to create a length of list for the numbers.
- Then, it is initialized by a list called “NumberList=[] ” which has no numbers.
- Next, an input is received for each number in the list using the “for loop”.
- The “For loop” appends each number to the list, NumberList=[]
- Then, the second “for loop” checks whether the number is even or odd in the list using the modular operator.
- Finally, The odd and even numbers are displayed on the screen
if n%2==0, n is an even number
if n%2==1, n is an odd number
- finally even and odd numbers are displayed on the screen
Program 2
NumberList=[] size=int[input["Enter the total number of list elemnts: "]] for i in range[size]: element=int[input["please Enter the value of %d elements: "%i]] NumberList.append[element] print["find odd and even here"] for i in range[size]: if[NumberList[i]%2]==0: print NumberList[i], "is even" else: print NumberList[i], "is odd"When the above code is executed, it produces the following results
Enter the total number of list elemnts: 7 please Enter the value of 0 elements: 12 please Enter the value of 1 elements: 23 please Enter the value of 2 elements: 34 please Enter the value of 3 elements: 45 please Enter the value of 4 elements: 56 please Enter the value of 5 elements: 67 please Enter the value of 6 elements: 78 find odd and even here 12 is even 23 is odd 34 is even 45 is odd 56 is even 67 is odd 78 is evenSuggested for you
for loop in Python language
if statements in Python language
Similar post
Python program check whether a number is odd or even
Python program to check a number is even or odd using the function
Python program to display even and odd numbers without if
Python program to display even and odd number in the given range
Python code to display all even and odd numbers from 1 to n
Calculate power of a number using recursion in C language
Calculate sum of odd and even in an array in C++
Related posts:
Python program to find the sum of two numbers using recursion
Find product of two numbers using recursion in Python
Calculate the sum of natural numbers in Python
Print char array in C++ language
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.
How Do You Print Odd Numbers From a List in Python?
To print odd numbers from a list of numbers you can use the Python modulo operator, related to the mathematical concept of remainder.
When you divide an odd number by 2 the remainder of the division is 1. When you divide an even number by 2 the remainder of the division is 0.
Let’s use this concept and a Python for loop to print odd numbers from a list.
def get_odd_numbers[numbers]: odd_numbers = [] for number in numbers: if number % 2 == 1: odd_numbers.append[number] return odd_numbersBefore starting the for loop we define an empty list, then at every iteration of the for loop we add odd numbers to the list.
numbers = [2, 3, 6, 8, 13, 45, 67, 88, 99, 100] print[get_odd_numbers[numbers]] [output] [3, 13, 45, 67, 99]Python Program to Check if a Number is Odd or Even
In this example, you will learn to check whether a number entered by the user is even or odd.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python Operators
- Python if...else Statement
A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.
Python Program to Check if a Number is Odd or Even
Odd and Even numbers:
If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number.
Even number examples: 2, 4, 6, 8, 10, etc.
Odd number examples:1, 3, 5, 7, 9 etc.
See this example:
Output:
Next TopicPython Check Leap Year
← prev next →
Python program to print even numbers in a list
PythonServer Side ProgrammingProgramming
In this article, we will learn about the solution and approach to solve the given problem statement.