You are currently viewing What is Range Function in Python? What does it do?

What is Range Function in Python? What does it do?

In this article, we will see what is range function and what does it do? We hear a lot about Range() function in Python, basically at the time of using a loop.

Range function generates the list of number which we can use for iterate through loops.

The range function  can have three types of parameters:

a.) range(stop)

b.) range(start,stop)

c.) range(start,stop,step)

range(stop): It will tell when to stop.

Example:

for i in range(10):

print(i)

Here the range has 10 value means starting from 0 to 9.

Above for-loop will run 10 times and it will print the value of i from 0 to 9. It means range() is defining where you have to stop.

range(start, stop): It tells where to start and where to end.  it takes two value starting and ending value.

Example: 

for i in range(1,10):

print(i)

Here the range has a starting value that is 1 and ending value that is 10. It means the loop will run 9 times.

Above for-loop will run 9 times and it will print the value of i from 1 to 9. It means range() is defining where you have to start and where to end.

range(start, stop, step):  It tells where to start and where to end and it takes the third value that is step it is very interesting, suppose you want to skip certain value after the interval at that time we can use step. Let’s say you want to print the value from 1 to 10 but like 1,3,5,7,9 in this, you can see after every two-step the value is skipped, this is the use of step.

Example:
for i in range(1,10,2):

print(i)

Here the range has a starting value that is 1 and ending value that is 10. It means the loop will run 9 times.

Above for-loop will run 9 times and it will print the value of i from 1 to 9. But it will skip the value after every 2 steps.

Hope this article has helped you to understand the basics of range function in Python. If you are new to Python, Check my article on how to install Python on Windows Machine and writing your first Python program.

Leave a Reply