 # Backward iteration in Python

## Backward iteration in Python

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

Last Updated on October 28, 2021 by admin

## Underscore (_) in PythonUnderscore (_) in Python

Underscore (_) in Python Single Underscore   In Interpreter: _ returns the value of last executed expression value in Python Prompt/Interpreter   >>> a = 10 >>> b = 10 >>> _ Traceback (most recent call last): File "<stdin>", line

## Render HTML Forms (GET & POST) in DjangoRender HTML Forms (GET & POST) in Django

Render HTML Forms (GET & POST) in Django Django is often called “Batteries Included Framework” because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a

## Python – os.path.dirname() methodPython – os.path.dirname() method

Python | os.path.dirname() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python

## Binary to decimal and vice-versa in pythonBinary to decimal and vice-versa in python

Binary to decimal and vice-versa in python Write Python code for converting a decimal number to it’s binary equivalent and vice-versa. Example: From decimal to binary Input : 8 Output : 1 0 0 0 From binary to decimal Input

## Python – os.path.exists() methodPython – os.path.exists() method

Python | os.path.exists() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python

## Difference between loc() and iloc() in Pandas DataFrameDifference between loc() and iloc() in Pandas DataFrame

Difference between loc() and iloc() in Pandas DataFrame   Pandas library of python is very useful for the manipulation of mathematical data and is widely used in the field of machine learning. It comprises of many methods for its proper

## Python – Check if a variable is stringPython – Check if a variable is string

Python | Check if a variable is string While working with different datatypes, we might come across a time, where we need to test the datatype for its nature. This article gives ways to test a variable against the data