Lesson guide
What this Python exercise practices
Filter Even Numbers is a beginner practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
Summary
Return a new list containing only the even numbers from the input list, preserving order.
Problem statement
Write a function filter_evens(nums) that takes a list of integers nums and returns a new list containing only the even integers from nums in the same order they appeared. If there are no even numbers, return an empty list.
Task
Practice iterating over a list, applying a condition, and building a new list.
Examples
Mixed numbers
Input
filter_evens([1, 2, 3, 4, 5, 6])
Output
[2, 4, 6]
Explanation
Only even numbers 2, 4, 6 are kept, in the original order.
Input format
A single list of integers, e.g. [0, 1, 2].
Output format
A list of integers containing only even values, e.g. [0, 2].
Constraints
Do not use external libraries. Aim for O(n) time and O(n) output space (new list).
Samples
Sample input 0
filter_evens([0, -2, 7])
Sample output 0
[0, -2]
Explanation 0
0 and -2 are even; 7 is odd and removed.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.