How to find even numbers in a list python

num_list=[] n=int(input("Enter the Starting of the range:")) k=int(input("Enter the Ending of the range:")) for i in range(n,k): num_list.append(i) print("Original Number List:", num_list) even_list=[] odd_list=[] for i in range(len(num_list)): if(num_list[i]%2==0): even_list.append(num_list[i]) else: odd_list.append(num_list[i]) print("Even Numbers List:", even_list) print("Odd Numbers List:", odd_list)

Output:

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]

Approach to print even numbers in a list

To execute this program we will follow the approach of traversing the list and checking each element if it is an even number or not. If it is an even number, print the number or add it to another list and print this list as output.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will check for all even numbers in a list

Step 2- Declare a list that will store all the even numbers in a list

Step 3- Run a loop for all elements in the list

Step 4- Check if the element is divisible by 2 or not

Step 5- If yes, then add the number to the new list

Step 6- Return new list as the output of the function

Step 7- Take input of a list using loop

Step 8- Call the function and print the result

Python Program

Look at the program to understand the implementation of the above-mentioned approach. We have used the append() function to add an element to a list. It is a built-in function.

#even numbers in list #function def even(list): new_list=[] for i in list: if i%2==0: new_list.append(i) return new_list #input li=[] n=int(input("Enter size of list ")) for i in range(0,n): e=int(input("Enter element of list ")) li.append(e) print("Even numbers in ",li) print(even(li))

Enter size of list 5 Enter element of list 3 Enter element of list 6 Enter element of list 1 Enter element of list 2 Enter element of list 9 Even numbers in [3, 6, 1, 2, 9]

[6, 2]

Conclusion

In this tutorial, we have learned how to find and print all the even numbers in a list. We can do the above-mentioned program in a more simple way by just printing the element which satisfies the if condition. Then we will not need to declare a new list and add the even numbers in that list.

In this post, you will learn different ways to write Python programs to list 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 type of program is generally asked during interviews and in the examination. This can also be helpful to improve our programming skills.

Print even and odd in the list using the for loop

In the given example, we are using the for loop to list 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]

Find 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 example, 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.

# 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 in the list using while loop

This Python program for listing 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]

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 array
Convert 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








Video liên quan

Postingan terbaru

LIHAT SEMUA