A range is a sequence of numbers like from 1 to 10. Reversing this range means to set it up so that the sequence starts from 10 and ends in 1. Let us learn how to do so in Python!

But first, why do we need ranges in Python? The most common use of ranges is to setup a for loop or other iteration. For instance, suppose we wish to print the squares of numbers from 1 to 10. We can use the range() function like so:

In this program we have a for loop and begins at 1 (the first argument of the range function) and incrementally updates this index till we exceed 11 (the second argument of the range function). Thus, do not confused by the 11 - this range starts at 1 and ends at 10. Within the for loop, we compute the square of the index and print a helpful message. The output is:

It is not necessary to use the range() function in a for loop. You can also do something simple like:

The output will be:

which is not very descriptive. You can apply the list() function on the range to render the range’s elements in a list:

The output will now be:

Let us now learn how to reverse a range in Python.

Method 1: Use a negative step size to reverse the range

The first idea is to specify a third argument, which is the step size (by default: 1) and this time makes the step size to be -1. Of course, you have to flip the start and ending values to make the range start at a higher value and count down to the lower value.

In the program above note that we have inverted the first two arguments to range(). The second argument is made to be one less than what we need, and thus it is zero. Then we have given a third argument which specifies a negative step size. Running this program will give us the desired range in reverse:

Method 2: Use the reversed() function

The second way to reverse a range is to use the reversed() function which takes a sequence or range or any iterable object as input and returns it in reverse order:

The output is:

What happened? We had forgotten to convert the range to a list for ease of printing. Let us update our program:

The output is now what we desired:

Method 3: Use slicing operators

A third way to obtain a reverse range is to first construct it the normal way and then use slicing operators to traverse the range in the opposite direction.

Note that in the last line we are using square brackets (“[“ and “]”) to index into the range variable called “myrange”. The step size of -1 is how you communicate to Python that you are traversing it in the opposite direction. The output is as we expected:

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.