Python Program to Print Numbers in an Interval

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
  
for num in range(l, u + 1, 2):
    print(num)

chevron_right


Output:

10
12
14
16
18
20


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.