How to decrement for loop in python

How to decrement for loop in Python

We use the for loop widely in the programming world. Most programs, whether highly complex or not, contain this loop. Sometimes, depending on the conditions, we need to decrement the loop.

This article focuses on the ways to decrement the for loop in Python.

What is for loop in Python?

The for loop is a conditional iterative statement that executes a block of code until we satisfy a given condition. It can loop through different iterables also in Python.

Ways to decrement the for loop in Python

We need to decrement a for loop when we wish to execute something in reverse order. Python does not make use of the decrement operator in the for loop. However, we can emulate the use for decrementing a for loop using other different methods.

Let us now discuss how to decrement the for loop in Python.

Using the start, stop and step parameters in range() function

The range() function returns a series of numbers within a given starting and ending index. The range() function is quite essential in the use of the for loop.

We can use the start, stop, and step parameters in this function. The start parameter indicates the starting value of the sequence, and the stop parameter tells the last value. The step parameter tells how much to increment or decrement between the values in the series.

To decrement a value in the for loop, the start value should be greater than the stop value, and the step parameter should be a negative integer.

For example,

Output:

7
6
5
4
3
2
1

In the above example,

  • We define the value for the start, stop, and step parameters.
  • The start index value needs to be higher than the stop index as we must traverse in the reverse order.
  • We take the value of the step parameter to be -1, which helps to decrement the value before the next number in the series.
  • Finally, the print() function displays the output.

Using the reversed() function

The reversed() function reverses the order of the provided sequence. It takes only one parameter, which is the sequence whose order we need to reverse.

Since we usually decrement a value in the for loop to do something in reverse order, we use this function to reverse the given sequence directly.

The range() function within the for loop can be passed as a parameter to the reversed() function to implement the desired output.

See the code below.

Output:

7
6
5
4
3
2
1

In the above example,

  • The range() function generates a series of numbers.
  • The reversed() function reverses the order of the given range.
  • We loop through the sequence and display it.

Using the while loop

As you know, the while loop is another popular iterative statement that can execute a given set of statements several times.

This method is an alternative solution to the given problem, wherein we use the while loop instead of the for loop. But still, it achieves the same results in the program.

In a while loop, we increment or decrement the value separately.

For example,

Output:

7
6
5
4
3
2
1

Conclusion

In this tutorial, different ways by which we can decrement the for loop in Python have been discussed. Decrementing a loop is very common and is relatively easy to understand and implement with the proper use of the right functions. Also, this tutorial demonstrates an alternative to the problem by using the while loop instead of the for loop.

That’s all about how to decrement for loop in Python.


import_contacts

You may also like:

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.