• 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 / 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.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 2.  How to Filter pandas DataFrame by Date
  • 3.  Using Python to Remove Last Character from String
  • 4.  Get Month Name from Datetime in pandas DataFrame
  • 5.  Using Python to Generate Random String of Specific Length
  • 6.  Find Magnitude of Complex Number in Python
  • 7.  pandas head – Return First n Rows from DataFrame
  • 8.  islower Python – Check if All Letters in String Are Lowercase
  • 9.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 10.  Python Check if Attribute Exists in Object with hasattr() Function

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