Skip to content

How to Decrement a Loop in Python

If you need to decrement a loop in Python, then you can do the following:

How to Decrement a loop in Python using -1

The optional third argument you can pass to the range function is the order.

The default order is to count up / increment, while the -1 value, is to count down / decrement.

for i in range(3, , -1): print(i)
Code language: Python (python)

Output: 3 2 1

How to Decrement a loop in Python using while

You can also use a while loop and set the value to the upper bound, and then decrement while in the loop itself.

i = 3 while (i > ): print(i) i = i-1
Code language: Python (python)

Output: 3 2 1

See also  Counting Method VarArgs in Java
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x