Understanding the built-in Python range() function

Python Range()

One of Python’s most commonly used functions is the range(), which is essential to learn before going any further with Python.

Python range() is a built-in function widely used for traversing through any iterable using for-loops or list comprehensions. Rather than being a function, the range() is actually an immutable sequence type. This function returns a sequence of numbers within a given range.

By the end of this article, you will have a solid understanding of the range() function, its syntax, parameters, and how to use it. let’s get started!

Syntax & Parameters of Python range() function

The syntax & parameters of the Python range() function are given below.

Syntax:

range(start, stop[, step])

Parameters:

  • start(optional): It is the starting number from which the sequence generation would start. It is included in the sequence and if not mentioned, by default it is 0,
  • stop: It is the number before which the sequence generation would stop(exclusive),
  • step(optional): It is the jump the function would take while the sequence generation. If not provided, it is by default considered 1.

Note: The range() function in Python 3 is the same as xrange() function in Python 2.

Using the range() function in Python

Now let us look at the various ways we can actually use the range() function to return a specific sequence of integer numbers.

1. range() with the Stop Argument 

The range() function takes three arguments, step, start and stop. The first two arguments, step and start are optional and are by default set to 1 and 0 respectively. But for sequence generation, the stop argument is mandatory.

When only stop is mentioned, the range() function creates a sequence of numbers starting from 0 to (stop – 1) with step 1. 

Example:

#range() with one parameter

print("Type of object returned by range: ", type(range(5)))

list1 = list(range(5))
print("sequence generated by range() with 1 parameter: ", list1)

Here the range ends at 5 meaning we only get the number less than 5, not including 5.

Output:

Type of object returned by range:  <class 'range'>
sequence generated by range() with 1 parameter:  [0, 1, 2, 3, 4]

As we can see, the type of sequence generated by the function is a range object of the class range. Type-casting the range(), the output gives us a list of numbers containing the values 0 to 4(i.e. 5-1) with step 1, as default.

2. range() with Start and Stop Arguments 

Similarly, we can use the range() function with two arguments, start and stop. In this case, the step value is by default set to 1. Below is an example of how to use that.

Example:

#range() with two parameter

list1 = list(range(3,7))
print("sequence generated by range() with 2 parameter: ", list1)

Here the returns sequence of numbers will begin and end with start and stop respectively.

Output:

sequence generated by range() with 2 parameter:  [3, 4, 5, 6]

3. range() with Start, Stop and Step Arguments

When all the arguments are passed, the range() function generates a sequence ranging from start to stop-1. The value of each element after the start value is calculated as the sum of the previous element and the step. The example below illustrates this fact very well.

Example:

#range() with three parameter

list1 = list(range(3,20,3))
print("sequence generated by range() with 3 parameter: ", list1)

Output:

sequence generated by range() with 3 parameter:  [3, 6, 9, 12, 15, 18]

The output clearly shows that the sequence is generated with values in the range of 3 to 19(20-1). For the last element, just because 18+3=21 exceeds the stop(20) the sequence generation is terminated at 18.

For Loop using range() with Start and Stop Arguments

As we mentioned earlier, range() is widely used with for loop. Let us look at an easy example of range() using a for loop.

Example:

#range() with for loop

for i in range(1,5):
    for j in range(1,i+1):
        print(j , end="")
    print()

Output:

1
12
123
1234

In the above code, we try to print a pattern with each row having the numbers from the sequence returned by the range(1,i+1) function in the inner loop. For the last iteration of the outer loop(i=4) the inner loop iterates for values of j from 1 to (4+1)-1 =4.

Conclusion

In this tutorial, we have learned the syntax and parameters of the range() function in Python. We have demonstrated range() functions with one, two and three arguments with examples that definitely help you to understand it better. Hope you enjoyed reading this tutorial.

References