Summary: In this tutorial, we will learn what the range() function does in Python and how can we use it to generate the sequence of numbers in Python.

What is range() in Python?

The range() is a Python function that generates a sequence of numbers from the starting number to the ending number.

The general syntax of the range() is:

range(start, stop , step)

where,

  • start (optional) is the starting number of the sequence. Since it is the optional parameter, its default value is 0.
  • stop (required) is the number where sequence stops. It is never included in the sequence generated by the range().
  • step (optional) is the incremental difference between two consequetive numbers in the sequence generated by range(). Its default value is 1.

Since the start and step parameters are optional, if we provide only one parameter to the range() method, it will accept it as a stop value (i.e., range(5) is equivalent to range(stop=5)).

How to use range() in Python?

Although the range() function generates the sequence of numbers, it does not return it directly. Instead, it returns the reference of the range class.

>>> type(range(5))
<class 'range'>

To access the values returned by the range(), we need to iterate through the sequence generated using for loop as follows:

>>> for x in range(5):
...     print(x, end=' ')
0 1 2 3 4

In this example, we have only provided 5 as the stop value, the range by default uses 0 and 1 as the start and step value respectively.

Alternatively, we can transform the generated sequence into a list data structure by using the list() constructor in Python.

>>> l = list(range(5))
>>> print(l)
[0, 1, 2, 3, 4]

Let’s see some more examples using range() in Python.

Examples using range() function

  1. range(stop): Generate numbers from 0 to stop.
>>> for x in range(10):
...     print(x, end=' ')
0 1 2 3 4 5 6 7 8 9

2. range(start, stop): Generate numbers from start to stop.

>>> for x in range(5, 10):
...     print(x, end=' ')
5 6 7 8 9

3. range(start, stop, step): Generate numbers from start to stop with a specified step size.

>>> for x in range(1, 10, 2):
...     print(x, end=' ')
1 3 5 7 9

Note: In all the above examples, the stop value is not included in the sequence generated by the range() function.

Decreasing range() function

So far, we have only generated increasing sequences using the range() method in Python. We can also generate decreasing sequence of numbers with a negative step value.

For example, in the following code, we are printing a decreasing sequence starting from 10 to 1 using the step value of -1.

>>> for x in range(10, 0, -1):
...     print(x, end= ' ')
... 
10 9 8 7 6 5 4 3 2 1

When using a negative step value in the range() method, we should wisely provide the start value greater than the stop value otherwise, the range() method will not generate any sequence.

>>> l = list(range(10, 15, -1))
>>> print(l)
[]

How range() function is useful?

Using the range() function, we can easily generate a sequence of numbers of any interval and size in Python.

One most common situation where the range() function is useful is in the iteration of data structures on the basis of index values.

For instance, consider the following Python snippet:

>>> lang = ['Python', 'C++', 'Java']
>>> for i in range(len(lang)):
...     print(i , ': ', lang[i])
... 
0 :  Python
1 :  C++
2 :  Java

Here, we are generating the sequence of index values for the given list using the len() and range() methods.

The len(list) computes the size of the list which is used by the range() as the stop value to generate index values.

Conclusion

The range() is a function in Python that is used to generate a sequence of numbers.

We can generate any linear sequence of numbers using the appropriate value for start, stop and step in the range(start, stop, step) function.

Leave a Reply