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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.