Python Range Exercises

Let’s check out some exercises that will help understand range() function better.

Exercise 14-a

Create a range from 0 to 50, excluding 50.


range() function can be used to create range type data.

rng = range (50)
or rng = range(0,50)

Exercise 14-b

Create a range from 0 to 10 with steps of 2.


range() function can take 3 parameters. range(start, stop, step)

rng = range(0,10,2)

Exercise 14-c

Create a range from 100 to 160 with steps of 10. Then print it as a list.


range() function can take 3 parameters. range(start, stop, step)

rng = range(100,160,10)
print(list(rng))

Exercise 14-d

Can you you create a list from 1300 to 700 with descending steps of 100? Is your stop value included in the list?


range() function can take negative step as well. range(start, stop, -step)

rng = range(1300,700,-100)

Exercise 14-e

Can you you create a list from 1300 to 700 with descending steps of 100, this time including 700?


range() function can take negative step as well. range(start, stop, -step)

rng = range(1300,699,-100)