How to print range() in reverse order in Python

 

Introduction to Python range reverse use case

The python range method creates a collection of numbers on the fly, for example, 0, 1, 2, 3, 4, and so on. This is very useful since the numbers can be used to index into collections such as strings. In this tutorial, we will learn about how we can reverse the range method and print the range of numbers in reverse order.

We will cover different scenarios including reversing a range by the step size, reversing a range by default, and many more. In general, we will be discussing two different methods to reverse the Python range method by taking various examples and different scenarios. In a nutshell, this tutorial will contain all the necessary information and important examples that you need to know in order to start working with Python range reverse.

Advertisement

 

Getting started with Python range reverse use case

As we already discussed Python range reverse is to return the range in reverse order. In the upcoming sections, we will be using two different methods to reverse the Python range. See the diagram below which shows the pictorial representation of Python range reverse.

Python range reverse

Notice that the reverse of Python range is simplifying creating a new range but this time the last element of the previous range will be the first element and will return all other elements in reverse order.

 

What is the Python range() method?

Before going into the reverse of the Python range, first, let us understand the range() method. The range() method is a python build-in method that returns a range of numbers. It can take three arguments but the two of them are optional. The following is the simple syntax of the Python range() method.

range(start, stop, step)

The start argument is the starting number of the series or in simple words, it is the lower limit. If we will not specify that starting number, then by default it will be zero. The stop argument is the upper limit of the series and the range method will not include this number in the result. The step is the difference between each number in the output. By default, the step size is 1.

 

Examples of Python range() method

Now let us take some examples of the python range() method by considering different scenarios. See the example below in which declare a variable and call range method and print that variable.

# storing range method in a variable
myRange = range(8)
# printing 
print(myRange)

Output:

Advertisement
range(0, 8)

Notice that this variable does not contain explicitly a range of numbers but is an iterable object. Let us now print the type of this variable.

# storing range method in a variable
myRange = range(8)
# printing the type
print(type(myRange))

Output:

<class 'range'>

Notice the type of the variable is range. If we want to print the numbers in the given range, we need to iterate over this variable and print. See the example below:

# storing range method in a variable
myRange = range(8)
# for loop 
for i in myRange:
    # printing
    print(i)

Output:

0
1
2
3
4
5
6
7

Notice that this time we get a range of numbers but the starting number is 0 and the step size is 1 by default because we did not specify in our program. Now let us specify the starting and step size and print a range of numbers. See the example below:

# storing range method in a variable
myRange = range(2,17,3)
# for loop 
for i in myRange:
    # printing
    print(i)

Output:

2
5
8
11
14

You can see in the above output that the range of numbers is starting from 2 as specified and the step size is 3.

 

Python range reverse using the reversed() method

The Python reversed() function allows us to process the items in a sequence in reverse order. It accepts a sequence and returns an iterator. The following is the simple syntax of the python reversed() method.

Advertisement
reversed(sequence)

The reversed method takes sequences as an argument and returns an iterator in reversed order. In this section, we will use the reversed method to return the range of numbers in reversed order by taking various examples.

 

Example-1 Python range reverse using reversed() method

We already have seen how we can get a range of numbers using the Python range function. Let us now learn how we can reverse the range of numbers using the reversed method. See the example below:

# Python range 
myRange = range(5)
# printing
print("Range before reversing:")
# for loop to iterate
for num in myRange:
    print(num)
# printing
print("Range after reversing :")
# Python range reverse and for loop
for num in reversed(myRange):
    print(num)

Output:

Range before reversing:
0
1
2
3
4
Range after reversing :
4
3
2
1
0

Notice that the reversed method had changed the order of the sequence and then we simply print all the elements using for loop.

 

Example-2 Using reversed() method by providing two arguments to range

Now let us take an example by providing starting and ending points to the range method and then reversing the sequence. The Python code will be similar to the above one. See the example below:

# Python range 
myRange = range(5, 10)
# printing
print("Range before reversing:")
# for loop to iterate
for num in myRange:
    print(num)
# printing
print("Range after reversing :")
# Python range reverse and for loop
for num in reversed(myRange):
    print(num)

Output:

Range before reversing:
5
6
7
8
9
Range after reversing :
9
8
7
6
5

Notice that the range started with 5 and ends at 9 and the reverse starts from 9 and ends at 5.

 

Example-3 Using reversed() method by providing 3 arguments to range

We have already discussed the Python range with three arguments. In this section, we will reverse the sequence returned by Python range with three arguments. See the example below:

Advertisement
# Python range with three arguments
myRange = range(5, 10, 2)
# printing
print("Range before reversing:")
# for loop to iterate
for num in myRange:
    print(num)
# printing
print("Range after reversing :")
# Python range reverse and for loop
for num in reversed(myRange):
    print(num)

Output:

Range before reversing:
5
7
9
Range after reversing :
9
7
5

The third argument is used to define the step size. In the above example, you can see that the range of numbers returned by the range method was reversed after applying the reversed method.

 

Python range reverse by range() method

We can use the Python range method itself to return a sequence in reverse order, rather than in increasing order.  This method is useful because we are not calling to any other function. In this section, we will take various examples and see how we can use the range method itself to produce a sequence in reverse order.

 

Example-1 Python range reverse using the negative argument

To get the range in reverse order, we have to provide starting point greater than the stop point and then pass the step size equal to -1. See the example below:

# Python range reverse with three arguments in reverse order
myRange = range(5, 0, -1)
# printing
print("The Range in reverse order is :")
# for loop to iterate
for num in myRange:
    print(num)

Output:

The Range in reverse order is :
5
4
3
2
1

So the range will start the sequence with the first argument and each time it will return a number less than the previous because the step size is -1, until it reaches the stop point.

 

Example-2 Python range reverse by providing negative step size

Now let us provide the step size other than one. In the following Python program, we will print a range of numbers in reverse order with a step size of 3. See the example below:

# Python range reverse with three arguments in reverse order
myRange = range(10, 0, -3)
# printing
print("The Range in reverse order is :")
# for loop to iterate
for num in myRange:
    print(num)

Output:

The Range in reverse order is :
10
7
4
1

Notice that we get a range of numbers in reverse order with a step size of 3.

 

Python range reverse of numbers that satisfy a condition

We can use any of the above methods that we have used to reverse a range of numbers and return only the elements that satisfy a given condition. For example, in the following example, we will print a sequence of numbers in the reverse order that will satisfy the given condition.

# Python range reverse with three arguments in reverse order
myRange = range(20, 0, -1)
# printing
print("The Range in reverse order is :")
# for loop to iterate
for num in myRange:
    # condition 
    if (num%4==0):
        print(num)

Output:

The Range in reverse order is :
20
16
12
8
4

Notice that we get the sequence of numbers in the reverse order that satisfies a condition.

 

Summary

The python range method is the core in-built function that is used to perform an action until the specified number of times. Usually, it is used for a sequence of numbers. In this tutorial, we learn about Python reverse range using different methods. We covered two methods to reverse range by taking various examples. Moreover, we also discussed how we can print a sequence of numbers in the reverse order that satisfies a given condition. To summarize, this tutorial contains all the necessary details and methods that are used to find the reverse of a range.

 

Further Reading

Python range method
Python reverse method
Python for loop 

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment