An Even Number is an integer divisible by 2 without a remainder. Examples include 0, 2, 4, 6, and 8.
In Python, determining even numbers within a specified range is a common task that can be accomplished efficiently using various techniques. This article explores different methods to identify even numbers within a given range, including using the modulo operator, loops, and list comprehensions.
Each approach is explained with code examples and a discussion of its efficiency and readability. By understanding these methods, you can choose the most suitable approach for your specific needs and coding style.
Table of contents
1. How to find even numbers in a range
Steps to find even numbers in a range using a for loop:
- Define the range
Set the
startandendvalues to specify the range of numbers to check. For example, start is 10 and stop is 30. - Initialize storage
Create an empty list,
even_numbers, to store the even numbers found. - Iterate through the range
Use a
forloop withrange(start, end + 1)to go through each number in the range, including the end value. - Check for even numbers
Inside the loop, use the modulo operator (
%) to check if a number is even with the conditionnum % 2 == 0. - Store even numbers
If the condition is satisfied, append the number to the
even_numberslist. - Print the result
Print the list of even numbers after the loop to display the output.
Code Example
Output:
Even numbers between 10 and 30 are: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]Code language: Python (python)
2. Using List Comprehension
List comprehension provides a concise way to find even numbers in a range.
It allows you to generate a new list by applying an expression to each item in an existing iterable (like a list, tuple, or range) within a single line of code
Code Example
Explanation
- List comprehension: Combines the loop and condition in a single line to generate the list of even numbers.
- Condition/expression: Filters numbers in the range using
num % 2 == 0.
3. Using Python’s filter() Function with Lambda
In Python, the filter() function filters elements from an iterable (like a list, tuple, or string) based on a condition specified by a function.
A lambda in Python is a small, anonymous (nameless) function defined using the lambda keyword. It is typically used for short, simple operations that don’t require a full function definition.
Uisng filter() and lambda makes the approach concise and efficient for filtering specific values from a sequence.
Code Example
Explanation
filter()function: Filters elements from the range based on the condition provided by the lambda function.- Lambda function: Iterate on each number in the range and check if it is an even number using the condition:
num % 2 == 0. list()function: Converts the filtered results into a list.
4. Using a Generator
A generator in Python is a special type of function that produces items one at a time using the yield keyword instead of returning all results at once.
Using a generator to find even numbers is memory-efficient because it does not store the entire list of even numbers in memory but yields each even number when needed. It is suitable for large ranges.
Code Example
Explanation
- Generator function: Uses
yieldto produce even numbers one at a time. It checks if a number is even using the modulo operator (%), wherenum % 2 == 0ensures that the number is divisible by 2 without leaving a remainder. range()function: Iterates through the range and checks for even numbers.list()function: Converts the iterable produced by the generator into a concrete list of values, allowing the data to be stored, manipulated, or printed easily.
5. Using NumPy
NumPy provides an efficient and optimized approach to finding even numbers in a given range, which is especially useful for handling large datasets. Its built-in functions allow for concise and highly performant implementations.
Note: To use NumPy we need to install NumPy library, as it does not come by default.
Code Example:
Explanation:
np.arange(): This function generates numbers within a specified range. For example, if the range starts at 10 and ends at 30,np.arange(10, 31, 2)will generate the sequence [10, 12, 14, …, 30], including only even numbers by stepping over odd numbers.- Start Adjustment: The expression
start + start % 2ensures the starting value is even. For example, if the starting value is 9, adding9 % 2(which equals 1) results in 10, making it even. If the starting value is already even, such as 10,10 % 2equals 0, so the starting value remains unchanged. - Step Size: The third argument in
np.arange()specifies the step size. Setting this to 2 ensures only even numbers are included.
Summary
Each method provides its unique benefits depending on the use case.
- The
forloop and list comprehension are great for simplicity and readability. - Methods like generators and NumPy are ideal for performance-critical tasks or large datasets.

Leave a Reply