In Python range() generates sequence of numbers which we can iterate using for loop.

for val in range(4):

In the above loop, range(4) will generate numbers from 0 to 4 i.e; 0,1,2,3,4
There are 3 ways to define a range

  1. range(stop) #from 0 to stop-1, incrementing by 1
  2. range(start,stop) #from start to stop-1, incrementing by 1
  3. range(start,stop,step) #from start to stop-1, incrementing by step

Let’s see some examples of range() and it’s output as below.

range(5) => 0,1,2,3,4
range(5,10) =>5,6,7,8,9
range(5,12,2) =>5,7,9,11

Let’s write a program using range() as below

Program : Print cubes of numbers from range 2 to 10.

#print cubes of numbers from 2 to 10
for i in range(2,10):
  print("cube of",i,"is ",i**3)