Skip to content
Related Articles

Related Articles

Python Program to Print Numbers in an Interval
  • 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



10
11
12
13
14
15
16
17
18
19
20

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

10
12
14
16
18
20

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :