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?

Leave a Reply

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