• 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 Increment Counter with += Increment Operator

Python Increment Counter with += Increment Operator

September 12, 2022 Leave a Comment

To increment a counter in Python, you can use the += increment operator and add a number to the counter.

i = 0
i += 1

print(i)

#Output:
1

The increment operator is the equivalent of adding a number via simple addition.

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 increment operator +=.

You can use the += to add a number to another number and increment a counter.

For example, if you want to add one to a number and increment 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 addition to add to a number.

i = 0
i = i + 1

print(i)

#Output:
1

You can add any number with the increment operator including floats.

i = 0
i += 2

print(i)

i += 2.0

print(i)

#Output:
2
4.0

Decrement Counter in Python with Decrement Operator -=

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

The decrement operator works the same as the increment operator.

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

i = 0
i -= 1

print(i)

#Output:
-1

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

Other Articles You'll Also Like:

  • 1.  Using Python to Remove First Character from String
  • 2.  How to Group By Columns and Find Mean in pandas DataFrame
  • 3.  Reverse a List in Python Without Reverse Function
  • 4.  Draw Circle in Python Using turtle circle() Function
  • 5.  Initialize Multiple Variables in Python
  • 6.  Remove Decimal from Float in Python
  • 7.  Symmetric Difference of Two Sets in Python
  • 8.  How to Group By Columns and Find Standard Deviation in pandas
  • 9.  Selenium maximize_window() Function to Maximize Window in Python
  • 10.  How to Multiply All Elements in List Using 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