• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Decrement For Loop with range() in Python

Decrement For Loop with range() in Python

July 25, 2022 Leave a Comment

To decrement a for loop in Python, the easiest way is to use range() and pass “-1” as the third argument to step by -1 after each iteration.

for i in range(5,0,-1):
    print(i)

#Output:
5
4
3
2
1

When working in Python, the ability to loop over objects and perform an action multiple times efficiently is very important.

Using loops in our programs allows us to create complex operations.

One such case where you might need to do a little more work is if you want to go in reverse when looping. You can easily decrement in a for loop with the range() function.

By default, when you use range() in a for loop, you work sequentially from the beginning to the end in order. To go in reverse, we can pass a third argument to range() and decrement the index of the for loop.

To decrement a for loop in Python, the easiest way is to use range() and pass “-1” as the third argument to step by -1 after each iteration.

Below is a simple example which decrements a for loop in Python.

for i in range(5,0,-1):
    print(i)

#Output:
5
4
3
2
1

How to Decrement while Loop in Python

If you want to use a while loop instead of a for loop in your Python program, you just need to keep track of the index and subtract 1 from it after each iteration.

Below is a simple example of how you can decrement a while loop in Python.

i = 5

while i > 0:
    print(i)
    i = i - 1

#Output:
5
4
3
2
1

Hopefully this article has been useful for you to learn how to decrement a for loop in Python using range().

Other Articles You'll Also Like:

  • 1.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 2.  Using Python to Calculate Average of List of Numbers
  • 3.  Using Python to Read File Word by Word
  • 4.  Get Substring Between Two Characters with Python
  • 5.  pandas max – Find Maximum Value of Series or DataFrame
  • 6.  Time Difference in Seconds Between Datetimes in Python
  • 7.  Using Lambda Expression with max() in Python
  • 8.  Count Letters in Word in Python
  • 9.  Add Tuple to List in Python
  • 10.  How to Remove NaN from List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy