Skip to main content
How to Reverse a Range in Python

How to Reverse a Range in Python

In this tutorial, you’ll learn how to use Python to reverse a range. The various ways to reverse lists and list ranges in Python are covered in this tutorial.

Python range()

The python range() function helps to create a series of numbers that fall within the specified range. You need to define arguments : You can choose where that sequence of numbers will begin and end, Define the difference will be between one number and the next.

Syntax:

range(start, stop, step)

Parameters:

  • A start argument is the starting number of the series.
  • A stop argument is an ending number.
  • The step is the difference between each number in the output. The default value of the step is 1 if not specified.

Example of range():

for i in range(0, 10, 2):
    print(f"{i}")

Output:

0
2
4
6
8

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Python range reverse

Python provides us with a built-in reversed() function. This method helps to create reverse a range using range() method. You need to wrap the range() method inside the reversed() method, you can print the integers in reverse order.

The sample code to reverse Number:

for i in reversed(range(10)):
  print(i)

Output:

9
8
7
6
5
4
3
2
1
0

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Leave a Reply

Your email address will not be published. Required fields are marked *