PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Program to find Even Numbers in Range

Python Program to find Even Numbers in Range

Updated on: March 27, 2025 | Leave a Comment

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
  • 2. Using List Comprehension
  • 3. Using Python’s filter() Function with Lambda
  • 4. Using a Generator
  • 5. Using NumPy
  • Summary

1. How to find even numbers in a range

Steps to find even numbers in a range using a for loop:

  1. Define the range

    Set the start and end values to specify the range of numbers to check. For example, start is 10 and stop is 30.

  2. Initialize storage

    Create an empty list, even_numbers, to store the even numbers found.

  3. Iterate through the range

    Use a for loop with range(start, end + 1) to go through each number in the range, including the end value.

  4. Check for even numbers

    Inside the loop, use the modulo operator (%) to check if a number is even with the condition num % 2 == 0.

  5. Store even numbers

    If the condition is satisfied, append the number to the even_numbers list.

  6. Print the result

    Print the list of even numbers after the loop to display the output.

Code Example

start = 10
end = 30

# List to store even numbers
even_numbers = []

for num in range(start, end + 1):
    # Check if the number is even
    if num % 2 == 0:
        even_numbers.append(num)  # Add even number to the list

print(f"Even numbers between {start} and {end} are: {even_numbers}")Code language: Python (python)

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

# Using list comprehension to find even numbers between 10 to 30
even_numbers = [num for num in range(10, 31) if num % 2 == 0]
print(f"Even numbers between {start} and {end} are: {even_numbers}")

# Output:
Even numbers between 10 and 30 are: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]Code language: Python (python)

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

start = 10
end = 30

# Using filter and lambda to find even numbers
even_numbers = list(filter(lambda num: num % 2 == 0, range(start, end + 1)))
print(f"Even numbers between {start} and {end} are: {even_numbers}")

# Output:
Even numbers between 10 and 30 are: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]Code language: Python (python)

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

# Define a generator function to find even numbers
def find_even_numbers(start, end):
    for num in range(start, end + 1):
        if num % 2 == 0:
            yield num  # Yield even numbers one at a time

# Define the range
start = 10
end = 30
# Generate even numbers and convert to a list
even_numbers = list(find_even_numbers(start, end))
print(f"Even numbers between {start} and {end} are: {even_numbers}")

# Output:
Even numbers between 10 and 30 are: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]Code language: Python (python)

Explanation

  • Generator function: Uses yield to produce even numbers one at a time. It checks if a number is even using the modulo operator (%), where num % 2 == 0 ensures 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:

import numpy as np

# Using NumPy to find even numbers in a range
def find_even_numbers(start, end):
    return np.arange(start + start % 2, end + 1, 2)

# Define the range
start = 10
end = 30

# Generate even numbers and convert to a list
even_numbers = list(find_even_numbers(start, end))
# Print the result
print(f"Even numbers between {start} and {end} are: {even_numbers}")

# Output:
Even numbers between 10 and 30 are: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]Code language: Python (python)

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 % 2 ensures the starting value is even. For example, if the starting value is 9, adding 9 % 2 (which equals 1) results in 10, making it even. If the starting value is already even, such as 10, 10 % 2 equals 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 for loop and list comprehension are great for simplicity and readability.
  • Methods like generators and NumPy are ideal for performance-critical tasks or large datasets.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com