Python Exercise: Print the even numbers from a given list
10. Print Even Numbers from a Given List
Write a Python program to print the even numbers from a given list.
Sample Solution:
Python Code:
Sample Output:
[2, 4, 6, 8]
Explanation:
In the exercise above the code defines a function named "is_even_num()" that takes a list of numbers as input and returns a new list containing only the even numbers from the input list. It iterates through the input list and checks whether each number is even. If a number is even, it adds it to a new list called 'enum'. Finally, it tests the function by calling it with a list [1, 2, 3, 4, 5, 6, 7, 8, 9] and prints the resulting list of even numbers.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python function that iterates over a list and prints elements that are even, using the modulo operator.
- Write a Python function that uses list comprehension to create and return a new list containing only the even numbers.
- Write a Python function that filters a list using the filter() function with a lambda to keep even numbers.
- Write a Python function that sorts a list and then extracts and prints the even numbers using slicing with a step.
Go to:
Previous: Write a Python function that takes a number as a parameter and check the number is prime or not.
Next: Write a Python function to check whether a number is perfect or not.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.