Skip to content

Print even numbers in Python using a range | Example code

  • by

You can set the lower and upper limits by using the range() function to print even numbers in Python. Use if statement to check 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

Simple example code print all the even numbers in the given range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, 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 (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

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.