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=" ")
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=" ")
Enter lower limit 5
Enter upper limit 10
Do comment if you have any doubts or suggestions on this Python number topic.
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.