Skip to content
Related Articles

Related Articles

Python Program to Print Numbers in an Interval

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 11 May, 2020

Given a range of numbers, find all the numbers between them.

Input : l = 2, u = 5
Output : 2 3 4 5

Input : l = 10, u = 20
Output : 10 11 12 13 14 15 16 17 18 19 20

The idea is to use range function in Python.




# Python program to print all the numbers within an interval
l = 10
u = 20
  
for num in range(l, u + 1):
    print(num)

Output:

10
11
12
13
14
15
16
17
18
19
20

We can also print alternate numbers or numbers with given steps.




# Python program to print all EVEN numbers within an interval
l = 10
u = 20
if l%2==0:
    for num in range(l, u + 1, 2):
        print(num)
else:
    for num in range(l+1, u + 1, 2):
        print(num)

Output:

10
12
14
16
18
20
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!