Python program to list even and odd numbers in a list
In this post, you will learn different ways to write Python programs to print even and odd numbers in a list. To check whether a number is odd or even, you can either 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 a program is generally asked in interviews and examinations. This can also be helpful to improve our programming skills.
Python program to print even and odd in the list using the for loop
In the given Python program, we are using the for loop to print the even and odd items in the list. In each iteration of the list, we apply the condition to check the even numbers, i.e., to check their remainder after dividing by 2. If the remainder is 0 (i% 2 == 0), add it to the even list; otherwise, add it to the odd list.
nums = [43, 20, 53, 12, 53, 5, 3, 2]
even = []
odd = []
for i in nums:
if(i % 2 == 0):
even.append(i)
else:
odd.append(i)
print("Even List: ",even)
print("Odd List: ",odd)
Output of the above code-
Even List: [20, 12, 2]
Odd List: [43, 53, 53, 5, 3]
Python program to print even and odd in the list using while loop
The given Python program for printing the even and odd numbers is the same as the above. We just replace the for loop with a while loop.
# Check and count odd and even numbers using while loop
# list of numbers
numlist = [11, 30, 28, 41, 22, 85, 7]
even_count = []
odd_count = []
num = 0
# using while loop
while(num < len(numlist)):
# checking condition
if numlist[num] % 2 == 0:
even_count.append(numlist[num])
else:
odd_count.append(numlist[num])
# increment num
num += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
Output of the above code -
Even numbers in the list: [30, 28, 22]
Odd numbers in the list: [11, 41, 85, 7]
Python program to print even and odd in list using lambda function
A lambda function is a small anonymous function that takes any number of arguments, but can only have one expression. It is useful when writing a function inside a function. In the given Python program, we have easily found the even and odd numbers in the list using a lambda function. The argument and the function are surrounded by parentheses. We have created a new list 'even_numbers' and used the filter() function with a lambda function to filter even numbers from the 'numberlist'. Again, we have created a new list 'odd_numbers' and used the filter() function with a lambda function to filter odd numbers from the 'numberlist'.
# Check and count odd and even numbers using lambda function
# list of numbers
numberlist = [11, 30, 28, 41, 22, 85, 7]
# print Even numbers in a list using Lambda function
even_numbers = list(filter(lambda x: x % 2 == 0,numberlist))
print("Total Even numbers in the list: ", even_numbers)
# print Odd numbers in a list using Lambda function
odd_numbers = list(filter(lambda x: x % 2 == 1,numberlist))
print("Total Odd numbers in the list: ", odd_numbers)
Output of the above code -
Total Even numbers in the list: [30, 28, 22]
Total Odd numbers in the list: [11, 41, 85, 7]
Find even and odd using function
Here is the Python program for listing the even and odd numbers from the specified range.
def find_even_odd(numbers):
odd_numbers = [number for number in numbers if number % 2 == 1]
even_numbers = [number for number in numbers if number % 2 == 0]
return odd_numbers, even_numbers
numbers = range(1,40)
print("The odd numbers are {}".format(find_even_odd(numbers)[0]))
print("The even numbers are {}".format(find_even_odd(numbers)[1]))
Output of the above code:
The odd numbers are [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
The even numbers are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary
Python program to print all even numbers between 1 to 100
Python program to multiply two numbers
Program to find area of triangle in Python
Python program to sort words in alphabetical order