Reversing a range is a common task in programming that involves creating a sequence of numbers in the reverse order. A range can be reversed to iterate over a list backward, to generate a countdown, or to reverse the order of digits.
In Python, different built-in functions are used to reverse a range, such as reversed(), sorted(), etc. In this article, we will explore how to reverse a Python range using appropriate examples.
- Method 1: Using range() With a Negative Step
- Method 2: Using reversed()
- Method 3: Using sorted() Function
Let’s discuss all of these methods using numerous examples:
Method 1: Using range() With a Negative Step
The built-in “range()” function is utilized to create/generate an immutable numbers sequence. The stated function is normally used with the for loops to iterate over the sequences of numbers. The syntax of this function is shown below:
range(start, stop, step)
The parameter start, stop and step specify the starting number, ending number, and the step size of the sequences.
The below code is utilized to reverse the range using the negative step parameter of the range() function:
Code:
for num in range(25, 10, -3):
print(num, end=" ")
- The “range()” function takes three parameters: start, end, and step.
- These parameters state that the range starts from “25” and ends at “10” with the negative step size of “-3”.
- The negative step size “-3” states that the number decreased by “3” each time.
Output:
The specified range has been reversed.
Method 2: Using reversed()
The reversed() function is used to reverse an iterable sequence according to the specified values. The below code uses the reversed function to reverse a range in Python:
Code:
for num in reversed(range(0, 15, 3)):
print(num, end=" ")
- The “range()” function is used to create the range of specified numbers by taking the parameter values.
- The “reversed()” function takes the return value of the range() function and presents it in reverse order.
Output:
The given range has been reversed.
Method 3: Using sorted() Function
A Python built-in function, sorted(), retrieves a sorted list from an iterable object. To reverse a range using the sorted() function, pass the “reverse=True” as a parameter to the stated function. The below example code is used to reverse a range in Python:
Code:
range_value = range(5, 20)
print(sorted(range_value, reverse=True))
- The “range()” function takes the starting and stopping number as an argument and creates the sequence.
- The “sorted()” function takes the range and “reverse=True” as an argument and reverses the given range.
Output:
The given range has been reversed successfully.
Conclusion
The “range() with a negative step”, “reversed()” function, and “sorted()” function can be used to reverse a Python range. The “range()” function is used along with the for loop to reverse a range. The “reversed()” function takes the “range()” and “reverse=True” as arguments and returns the reverse iterator. By using numerous examples, this tutorial demonstrated various ways to reverse a range in Python.