Python range function

Python range function detailed tutorial in 2022

In this tutorial, I will explain what is python range function is and how to use it effectively. I will also explain how to perform various operations using the range function. so let’s get started.

Python range Function

Python range function

The Python range function returns a series of numbers, in a given range. The numbers will start from 0 and increment by the step defined.

Syntax of Python range function

Below is the syntax of the range function

range(start, stop, step)

Parameter:
start[optional]	:start value of the sequence
stop		:end value of the sequence
step[optional]	:an integer number specifying the step to increment.
Python range examples

Python range function example

Now let’s use the range function to print the numbers from 1 to 5

# create a sequence of numbers from 1 to 5
for i in range(1,5):
  print(i)
Python range function example

By default, the step is 1 in the python range function.

Python range step example

So far we have not passed any step into the range function. in this session, we will understand how to use the step-in range function in python.

# print number from 0 to 11,increment by 2
for i in range(0,11,2):
  print(i)
Python range step
  • the first argument is start which is 0
  • the second argument is stop which is 11
  • the third argument is step when we passed as 2

Python range inclusive

As we know that in the range function the last value is not included while calculating the range. A user can define the range function in the below format to include the end number as well.

#calculate the range from 0 to 10
start=0
end=10
step=1
for i in range(start,end+1,step):
  print(i)

Python range inclusive

Python range reverse

A user can also use the reverse function along with the range to print the values in a reverse manner. Now let’s print the number from 10 to 0 by using the range function

# print the number from 10 to 0 in reverse order
for i in reversed(range(0,11,1)):
  print(i)
Python range reverse

A user can also use -1 as step to print the number in reverse order.

# Print the number from 10 to 1 in reverse order
for i in range(10,0,-1):
  print(i)
Python range reverse

Python range skip values

A user can also skip a number of values in the python range function. Let’s say we want to print the number from 1 to 20 but skip 10 and 15.

#  print the number from 1 to 20 but skip 10 and 15
for i in range(1,21):
  if i not in [10,15]:
    print(i)
Python range skip values

Python range float

We can not use the range function to print the float values as the step can only accept integer values. Let’s print the float values starting from 0 and incrementing by 0.2 till 2.

for i in range(1,2,0.2):
  print(i)

Python range float

Instead, a user can use NumPy arrange function to achieve the same.

# print the float values starting from 0 and incrementing by 0.2 till 2.
import numpy as np
for i in np.arange(1,2,0.2):
  print(i)
Python range float

Python range list

A user can also use the python range function to get the list of values. Get the list of 10 numbers from 0 to 10 using the range function.

# get the list of 10 numbers from 0 to 10 
my_list = list(range(1,11,1))
print(my_list)
Python range list

Conclusion

I hope you have liked this small tutorial about the python range function. Please do let me know if you are facing any issues while following along.

More to Explore?

Python substring

Python request post and get

Python square root

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top