The iteration of numbers is done by looping techniques in python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backwards in alternate way and having shorthands to do so can be quite useful. Let’s discuss certain ways in which this can be done.
Method #1 : Using reversed()
The simplest way to perform this is to use the reversed function for the for loop and the iteration will start occurring from the rear side than the conventional counting.
# Python3 code to demonstrate # Alternate Rear iteration # using reversed() # Initializing number from which # iteration begins N = 6 # using reversed() to perform the Alternate Rear iteration print ("The reversed numbers are : ", end = "") for num in reversed(range(0, N + 1, 2)) : print (num, end = " ") |
The reversed numbers are : 6 4 2 0
Method #2 : Using range(N, -1, -2)
This particular task can also be performed using the conventional range function which, if provided with the third argument performs the alternate skip and second argument is used to start from backwards.
# Python3 code to demonstrate # Alternate Rear iteration # using range(N, -1, -2) # Initializing number from which # iteration begins N = 6 # using reversed() to perform the Alternate Rear iteration print ("The reversed numbers are : ", end = "") for num in range(N, -1, -2) : print (num, end = " ") |
The reversed numbers are : 6 4 2 0
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Alternate front - rear Sum
- Python | Front and rear range deletion in a list
- Python | Append at front and remove from rear
- Access front and rear element of Python tuple
- Python | Strings with similar front and rear character
- Python | Replace rear word in String
- Python - Remove rear element from list
- Python - Rear element extraction from list of tuples records
- Python - Test rear digit match in all list elements
- Python | Rear Addition of Record
- Python | Rear elements from Tuple Strings
- Python - Words with Particular Rear letter
- Python - Rear shift K in List
- Python | Shift from Front to Rear in List
- Python | Retain K Front and Rear elements
- Python | Kth Non-None String from Rear
- Python - Remove Rear K characters from String List
- Python - Rear character String categorization
- Python | First character occurrence from rear String
- Python - Pair elements with Rear element in Matrix Row
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

