4 Ways to Decrement for loop in Python

We have learned about the For Loop in Python and must have used it, too. We must have used a loop value’s increment and decrement operations inside it. In this tutorial, we will learn about the concept of a decrement in a for loop in Python only. We will know more about using the decrement in for loop in Python in detail with the ways explained with examples in detail.

What is For Loop?

A For Loop is used to iterate over the sequence: a list, a tuple, a dictionary, a set, or a string.

What is the Decrement operator?

There is no decrement operator in Python. We can only decrement the value in Python, but there is no such operator in Python.

The decrement operator is present in c and C++ but not in Python.

Syntax

For <variable> in range(start index, stop index, step) 

Parameter

  • Variable: A variable is a value that starts from the start index and ends at the stop index in the for loop.
  • Start index: It is an optional value. If not passed, it starts from 0. It is the integer value from which the for loop value starts iterating.
  • Stop index: It is the integer value from which the for loop value stops iterating.
  • Step: It is the optional value. If not passed, it increments the value by 1. It is an integer value that defines the increment and decrement of the loop.

Examples for using decrement in For loop

In these examples, we will learn how to decrease the value in the for loop in Python. We will be explaining all the examples in detail.

1. Using Start, Stop Index, and step to Decrement for loop in Python

In this example, we will use the start index and stop index, decreasing the value in the for loop. We will set the start index’s value greater than the stop index so that the value will be decremented individually at each iteration. Let us look at the example for understanding the concept in detail.

#Start index and stop index value
startindex = 10
stopindex = 0
step = -1

#applying for loop
for i in range(startindex, stopindex, step):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

Explanation:

  • Firstly, we will assign the start index value more significant than the stop index, i.e., 10.
  • Then, we have assigned the value of the stop index as 0.
  • After that, the value of the step is -1, at which point we will decrease the value in the for loop.
  • And then, we have applied the for loop with variable ‘i’ and range as the start index, stop index, and step.
  • At last, we have printed the output.
  • Hence, we can see the output in reverse order.

2. Using Start, stop, and step in for loop only to Decrement for loop in Python

In this example, we will set the start index value, stop index, step inside the for loop only, and see the output. The start index’s value will be greater than the stop index so that the value gets decremented. Let us look at the example for understanding the concept in detail.

#applying for loop
for i in range(10,0,-2):
    print('output : ',i)

Output:

output :  10
output :  8
output :  6
output :  4
output :  2

Explanation:

  • In this example, we have applied all the values inside the range() function in for loop.
  • The start index is set as 10, the stop index is set as 0, and the step is set as -2, which will decrement the value by 2 at each iteration until 0.
  • At last, we have printed the value of variable i.
  • Hence, you can see the output.

3. Using reversed() Function to Decrement for loop in Python

In this example, we will use the reversed() function inside to pass the range() function and see the output. Reversed() function is used to loop over a sequence in reverse order. Let us look at the example for understanding the concept in detail.

#applying reversed() function 
for i in reversed(range(5)):
    print('Output : ',i)

Output:

Output :  4
Output :  3
Output :  2
Output :  1
Output :  0

Explanation:

  • In this example, we have applied the reversed() function, which prints the reverse order values.
  • We have applied the for loop directly and applied reversed() function inside it.
  • Inside the reversed() function, we have also applied the range() function with 5.
  • At last, we have printed the value of variable i.
  • Hence, you can see the output.

4. Using while and -= operation

In this example, we will use the while loop and i= operation to decrement the value one by one or according to your requirement. We will set the while condition and then decrement the value inside the while function afterward. Let us look at the example for understanding the concept in detail.

#applying while and -= operation
s = 10
while s>0:
    print("Output : ",s)
    s -= 1

Output:

Output :  10
Output :  9
Output :  8
Output :  7
Output :  6
Output :  5
Output :  4
Output :  3
Output :  2
Output :  1

Explanation:

  • Firstly, we will take an input variable s and assign it the value we want to decrease (s = 10).
  • Then, we will apply a condition such that s > 0.
  • Inside the while condition, we will print the value of s, and after printing the value, we will decrease the value of s by 1 (s -= 1).
  • Hence, you can see the output.

Conclusion

In this tutorial, we have learned about the concept of the Decrement in for loop and how to use it. We have also seen the syntax for decrementing the value in for loop. We have also seen the multiple ways through which we can print the values in reverse order. You can use any of the methods according to your need in the program.

However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Madhan
Madhan
2 years ago

These answers are not working for decrement …do we have any version pre requisites

Pratik Kinage
Admin
2 years ago
Reply to  Madhan

No, this should work on any Python3 version. Is there any specific decrement you’re looking for?

Regards,
Pratik