0
0
Pythonprogramming~15 mins

filter() function in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the filter() function to select even numbers
๐Ÿ“– Scenario: Imagine you have a list of numbers representing ages of people in a group. You want to find only the even ages to organize a special event for them.
๐ŸŽฏ Goal: You will build a small program that uses the filter() function to select even numbers from a list of ages.
๐Ÿ“‹ What You'll Learn
Create a list called ages with the exact values: 12, 17, 20, 25, 30, 33, 40
Create a function called is_even that returns True if a number is even
Use the filter() function with is_even and ages to get only even ages
Convert the filter result to a list called even_ages
Print the even_ages list
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Filtering data is common when you want to select only certain items from a list, like customers above a certain age or products in stock.
๐Ÿ’ผ Career
Understanding filter() helps in data cleaning, processing lists, and working efficiently with collections in many programming jobs.
Progress0 / 4 steps
1
Create the list of ages
Create a list called ages with these exact values: 12, 17, 20, 25, 30, 33, 40
Python
Need a hint?

Use square brackets [] to create a list and separate numbers with commas.

2
Define the function to check even numbers
Define a function called is_even that takes one parameter num and returns True if num % 2 == 0, otherwise returns False
Python
Need a hint?

The % operator gives the remainder. Even numbers have remainder 0 when divided by 2.

3
Use filter() to select even ages
Use the filter() function with is_even and ages, then convert the result to a list called even_ages
Python
Need a hint?

Remember to wrap filter() with list() to get a list.

4
Print the list of even ages
Print the list even_ages using print(even_ages)
Python
Need a hint?

The output should show only the even numbers from the list.