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, 40Create a function called
is_even that returns True if a number is evenUse the
filter() function with is_even and ages to get only even agesConvert the filter result to a list called
even_agesPrint 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