w3resource

Python Exercise: Print the even numbers from a given list

Python Functions: Exercise-10 with Solution

Write a Python program to print the even numbers from a given list.

Python Even Numbers from 1 to 100

Sample Solution:

Python Code:

# Define a function named 'is_even_num' that takes a list 'l' as input and returns a list of even numbers
def is_even_num(l):
    # Create an empty list 'enum' to store even numbers
    enum = []
    
    # Iterate through each number 'n' in the input list 'l'
    for n in l:
        # Check if the number 'n' is even (divisible by 2 without a remainder)
        if n % 2 == 0:
            # If 'n' is even, append it to the 'enum' list
            enum.append(n)
    
    # Return the list 'enum' containing even numbers
    return enum

# Print the result of calling the 'is_even_num' function with a list of numbers
print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9])) 
 

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:

Flowchart: Python exercises: Print the even numbers from a given list.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-functions-exercise-10.php