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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Python Decrement Counter with -= Decrement Operator

Python Decrement Counter with -= Decrement Operator

September 12, 2022 Leave a Comment

To decrement a counter in Python, you can use the -= decrement operator and subtract a number from the counter.

i = 0
i -= 1

print(i)

#Output:
-1

The decrement operator is the equivalent of subtracting a number via simple subtraction.

i = 0
i = i - 1

print(i)

#Output:
-1

In Python, counters are frequently used when iterating over an object or keeping track of a count for later use.

There are many useful operators in Python and one such operator which makes counting easy is the decrement operator -=.

You can use the -= to subtract a number to another number and decrement a counter.

For example, if you want to subtract one from a number and decrement a counter, you can do so with the following Python code.

i = 0
i -= 1

print(i)

#Output:
-1

This is the equivalent as using simple subtraction to subtract from a number.

i = 0
i = i + 1

print(i)

#Output:
1

You can subtract any number with the decrement operator including floats.

i = 0
i -= 2

print(i)

i -= 2.0

print(i)

#Output:
-2
-4.0

Increment Counter in Python with Increment Operator +=

If you want to go the other way and increment a counter, then you can use the Python increment operator +=.

The increment operator works the same as the decrement operator.

Below shows you a simple example of how you can increment a counter in Python with the increment operator.

i = 0
i += 1

print(i)

#Output:
1

Hopefully this article has been useful for you to learn how to use the decrement operator and how to decrement a counter in Python.

Other Articles You'll Also Like:

  • 1.  How to Read CSV File from AWS S3 Bucket Using Python
  • 2.  Using Python to Remove Apostrophe From String
  • 3.  Draw Star in Python Using turtle Module
  • 4.  Flatten List of Tuples in Python
  • 5.  PROC LIFETEST Equivalent in Python
  • 6.  Get Substring from String in Python with String Slicing
  • 7.  Python min() function – Get Minimum of List or Dictionary with min() Function
  • 8.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 9.  Using Python to Get Day of Week
  • 10.  Using Python to Count Even Numbers in List

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