Python Range() Function

We will be learning about a built-in Python function called `range()` function. It is a very popular & widely used function in Python, especially when we are working with predominantly `for` loops & sometimes with `while` loops. It returns a sequence of numbers & is immutable (whose value is fixed). The range function takes one or at most three arguments, namely the start & a stop value along with a step size.

Range function was introduced only in Python3, while in Python2, a similar function `xrange()` was used, & it used to return a generator object & consumed less memory. The `range()` function, on the other hand, returns a list or sequence of numbers & consumes more memory than `xrange()`.

Since the `range()` function only stores the start, stop, & step values, it consumes less amount of memory irrespective of the range it represents when compared to a list or tuple.

The `range()` function can be represented in three different ways, or you can think of them as three range parameters:

• range(stop_value) : This by default considers the starting point as zero
• range(start_value, stop_value) : This generates the sequence based on the start & stop value
• range(start_value, stop_value, step_size): It generates the sequence by incrementing the start value using the step size until it reaches the stop value

Let’s first check the type of the `range()` function.

Let’s start with a simple example of printing a sequence of ten numbers, which will cover our first range parameter.

• To achieve this, we will be just passing in the stop value. Since Python works on zero-based indexing, hence, the sequence will start with zero & stop at the specified number, i.e., n−1, where n is the specified number in the range function.

As expected, the above cell returns a sequence of numbers starting with 0 & ending at 9.

We could also use the range function as an argument to a list in which case it would result in a list of numbers with a length equal to the stop value as shown below:

• Next, let’s look at the second way of working with the range function. Here we will specify both start & the stop value.

Similarly, we can use the `range` function to print the negative integer values as well.

• Let’s now add the third parameter, i.e., the step size to the range function, & find out how it affects the output. We will specify the start point as 50, the end/stop value as 1000 with a step size of 100. The below range function should output a sequence starting from 50 incrementing with a step of 100.

We will notice that it will print all even numbers.

It is important to note that the `range()` function can only work when the specified value is an integer or a whole number. It does not support the float data type & the string data type. However, we can pass in both positive & negative integer values to it.

Let’s see what happens when we try to pass float values.

• We would have scratched our head at least once while trying to reverse a linked list of integer values in C language. However, in python, it can be achieved with the range function with just interchanging the start & stop along with adding a negative step size.

Say we have a list of integer values, & we would like to find the sum of the list, but using the `range()` function. Let's find out how it can be done.

First, we will define the list consisting of integer values. Then initialize a counter in which we will store the value each time we iterate over the list & also add the current list value with the old count value.

To access the elements from the list, we will apply the range function on the `length` of the list & then access the list elements bypassing the index `i` which will start from zero & stop at the length of the list.

We could also concatenate two or more range functions using the `itertools` package class called `chain`. And not just the `range` function, we could even concatenate list, tuples, etc. Remember that `chain` method returns a generator object, & to access the elements from that generator object, we can either use a `for` loop or use `list` & pass the generator object as an argument to it.

We can apply equality comparisons between range functions. Given two range functions, if they represent the same sequence of values, then they are considered to be equal. Having said that, two equal range functions don’t need to have the same start, stop, & step attributes.

Let’s understand it with an example.

As we can observe from the above outputs, even though the parameters of the range function are different, they are still considered to be equal since the sequence of both the functions is the same. While in the second example, changing the step size makes the comparison False.

https://pynative.com/python-range-function/

Data Scientist & Machine Learning Engineer

More from Jason Joseph

Data Scientist & Machine Learning Engineer