Decrementing for loops in Python (with code examples)

avatar

Borislav Hadzhiev

Last updated: Jul 3, 2022

banner

Photo from Unsplash

Decrementing for loops in Python (with code examples) #

Use the range() class to get a decrementing for loop in Python, e.g. for num in range(10, 0, -1):. When passed a negative step argument, the start value of the range is decremented on each iteration of the for loop.

main.py
# ✅ decrementing for loop for num in range(10, 0, -1): print(num) # 👉️ 10 9 8 ... 3 2 1 # 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] print(list(range(10, 0, -1))) # 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(list(range(10, -1, -1))) # 👇️ [10, 8, 6, 4, 2] print(list(range(10, 0, -2))) # ------------------- # ✅ decrementing while loop num = 10 stop = 0 while num > stop: print(num) # 👉️ 10 9 8 ... 3 2 1 num -= 1

We used the range() class to implement a decrementing for loop.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)
When the value for the step argument is negative, the start value of the range is decremented on each iteration of the for loop.
main.py
for num in range(10, 0, -1): print(num) # 👉️ 10 9 8 ... 3 2 1

If the step argument is omitted, it defaults to 1, which means that the start value of the range is incremented on each iteration.

Note that the value for the start parameter is inclusive, whereas the stop value is exclusive (up to but not including).

main.py
for num in range(10, 0, -1): print(num) # 👉️ 10 9 8 ... 3 2 1 # 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] print(list(range(10, 0, -1))) # 👇️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(list(range(10, -1, -1)))

You can also specify a different step for your decrementing for loop.

main.py
for num in range(10, 0, -2): print(num) # 👉️ 10 8 6 4 2 # 👇️ [10, 8, 6, 4, 2] print(list(range(10, 0, -2))) # 👇️ [10, 8, 6, 4, 2, 0] print(list(range(10, -1, -2)))
The example uses -2 for the step argument, which means that the start value of the range is decremented by 2 on each iteration of the for loop.

If the value for the start argument is lower than the value for the stop argument and a negative step is provided, the range will be empty.

main.py
for num in range(0, 10, -2): print(num) # 👇️ [] print(list(range(0, 10, -2)))

You can also use a decrementing while loop.

main.py
num = 10 stop = 0 while num > stop: print(num) # 👉️ 10 9 8 ... 3 2 1 num -= 1 # 👉️ num is now 0
On each iteration of the while loop, we decrement the value of the num variable until the condition is no longer met.

Make sure to specify a base case that has to be met to exit the while loop, otherwise you might end up with an infinite loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.