Skip to content

Print even numbers in Python using a range | Example code

  • by

You can set the lower and upper limits using the range() function to print even numbers in Python. Use if statement to check the condition for even numbers is applied to filter all the odd numbers.

This method takes O(n) + constant time of comparison.

Example Print even numbers in Python using a range

A simple example code prints all the even numbers in the given range. Iterate from the start till the range in the list using the for loop and check if num % 2 == 0. If the condition is satisfied, then only print the number.

start, end = 10, 20

# iteration
for num in range(start, end + 1):
    # check
    if num % 2 == 0:
        print(num, end=" ")

Output:

Print even numbers in Python using a range

Take input of the lower limit and upper limit of the range.

ll = int(input("Enter lower limit "))
ul = int(input("Enter upper limit "))

for i in range(ll, ul):
    if i % 2 == 0:
        print(i, end=" ")

Output:

Enter lower limit 5
Enter upper limit 10
6 8

Do comment if you have any doubts or suggestions on this Python number topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading