A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ below:

Print all even numbers in a range - Python

Print all even numbers in a range - Python

Last Updated : 11 Jul, 2025

Our task is to print all even numbers within a given range. The simplest way to achieve this is by using a loop to iterate through the range and check each number for evenness. Let's explore some methods to do so.

Using Loop

We can use a for loop with if conditional to check if a number is even.

Python
s = 1
e = 10

for i in range(s, e + 1):
    if i % 2 == 0:
        print(i)

Explanation:

Using List Comprehension

List comprehension provides a concise way to filter even numbers and print them directly.

Python
s = 1
e = 10

res = [i for i in range(s, e + 1) if i % 2 == 0]
print(res)

Explanation:

Using range() with Step

The built-in range() function can also be used efficiently to print even numbers by setting a step value of 2. This avoids the need for an extra if condition.

Python
s = 1
e = 10

for i in range(2, e + 1, 2):
    print(i)

Explanation:

Related Articles:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4