We all have learned about the For Loop in python and must have used them too. Inside it, we must have used the increment and decrement operations of a loop value. In this tutorial, we will be learning about the concept of a decrement in 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.
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 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 gets starts iterating.
- Stop index: It is the integer value from which the for loop value gets stops iterating.
- Step: It is the optional value. If not passed it increment 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 know about how we can decrement the value in for loop in python. We will be explaining all the examples with explanation in detail.
1. Using Start, Stop Index, and step to Decrement for loop in Python
In this example, we will be using the start index and stop index, by which we will decrement the value in the for loop. We will set the start index’s value greater than the stop index so that value will be decremented one by one 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 value of the start index greater than the stop index, i.e., 10.
- Then, we have assigned the value of the stop index as 0.
- After that, the value of step as -1 at which we will decrement 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 be using 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 is used to print 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 be using the while loop and i= operation for decrementing the value one by one or according to your requirement. We will set the while condition and then afterward decrement the value inside the while function. 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 decrement the value(s = 10).
- Then, we will apply while condition such that s > 0.
- Inside the while condition, we will print the value of s, and after printing the value, we will decrement 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, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
These answers are not working for decrement …do we have any version pre requisites
No, this should work on any Python3 version. Is there any specific decrement you’re looking for?
Regards,
Pratik