# Backward iteration in Python

• Difficulty Level : Easy
• Last Updated : 04 Jul, 2022

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 backward 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

 `# Python3 code to demonstrate``# backward iteration``# using reversed()` `# Initializing number from which``# iteration begins``N ``=` `6` `# using reversed() to perform the back iteration``print` `(``"The reversed numbers are : "``, end ``=` `"")``for` `num ``in` `reversed``(``range``(N ``+` `1``)) :``    ``print``(num, end ``=` `" "``)`

Output

`The reversed numbers are : 6 5 4 3 2 1 0 `

Method #2 : Using range(N, -1, -1) This particular task can also be performed using the conventional range function which, if provided with the third argument performs the skip and second argument is used to start from backwards.

## Python3

 `# Python3 code to demonstrate``# backward iteration``# using range(N, -1, -1)` `# Initializing number from which``# iteration begins``N ``=` `6` `# using reversed() to perform the back iteration``print` `(``"The reversed numbers are : "``, end ``=` `"")``for` `num ``in` `range``(N, ``-``1``, ``-``1``) :``    ``print``(num, end ``=` `" "``)`

Output

`The reversed numbers are : 6 5 4 3 2 1 0 `

Method#3: Using slice syntax This particular task can also be performed using the slice syntax which, is used for reversing the list. We used it for reversing the range class in the for loop then we perform the backward iteration.

## Python3

 `# # Python3 code to demonstrate``# # backward iteration``# # using slice syntax` `# # Initializing number from which``# # iteration begins``N ``=` `6`  `# Using slice syntax perform the backward iteration``k ``=` `range``(N``+``1``)[::``-``1``]``print``(``"The reversed numbers are : "``,end``=``'')``for` `i ``in` `k:``    ``print``(i, end``=``' '``)`

Output

`The reversed numbers are : 6 5 4 3 2 1 0 `

My Personal Notes arrow_drop_up