Increment and Decrement Operators in Python

Recently, I was working on a project where I had to manage counters in a simulation model. Coming from a C++ background, I instinctively typed i++, only to realize Python doesn’t support it.

This got me thinking: many beginners who switch to Python from other languages face the same confusion. In Python, there are no ++ or — operators. Instead, we use a more explicit and readable approach.

In this tutorial, I’ll show you different ways to increment and decrement values in Python. I’ll also share some real-world examples so you can see how these operators are applied in everyday coding.

Increment Operator in Python

We can increment in Python using the += operator. This operator adds the value on the right-hand side to the variable on the left-hand side. In this section, we will see what is increment operator is in Python.

The syntax of the addition assignment operator in Python:

Variable += Value

or

Variable = Variable + Value

Seeing this, we can say the value is getting incremented in the variable on the right-hand side(for iterables).

Let’s see some examples:

1. Python Increment Operator

Here is the basic use of the Python increment operator:

USA_Gdp = 23.32
USA_Gdp += 4.65
print("USA GDP after increment:", USA_Gdp)

Population = 333287557
Population = Population + 2605681
print("Population of USA after increment: ", Population)

Output:

USA GDP after increment: 27.97
Population of USA after increment:  335893238

I executed the above example code and added the screenshot below.

increment in python

This code demonstrates how to increment numeric variables in Python using both += and + assignment methods.

2. Python Variable Increment Operator

Let’s see how to use the increment operator with a string variable in Python.

USA = "United States"
USA = USA + " America"
print("USA:", USA)

P = "Python"
P += "Guides"
print("Our Site Name is:", P)

Output:

USA: United States America
Our Site Name is: PythonGuides

I executed the above example code and added the screenshot below.

what is +=1 in python

This code shows how to increment string variables in Python by concatenating additional text using + and +=.

3. Python Increment Operator with for Loop

When it comes to incrementing variables within a loop in Python, the for loop syntax provides a convenient way to iterate over a sequence of items without the need for a separate increment operation.

total_cars = 0
for x in range(10):
    total_cars += 1
print("Total number of cars passed through the toll booth:", total_cars)

Output: Within the loop, we increment the total_cars variable by 1 for each car passing through using the += operator in Python.

Total number of cars passed through the toll booth: 10

I executed the above example code and added the screenshot below.

python increment counter

This code demonstrates incrementing a counter variable inside a for loop to count items or events efficiently.

Decrement Operator in Python

Similarly, to decrement a variable in Python, we can use the subtraction assignment operator (-=). This operator subtracts the value on the right-hand side from the variable on the left-hand side.

The syntax of the subtraction assignment operator in Python:

Variable -= Value

or,

Variable = Variable - Value

Here are some examples:

1. Python Decrease by 1

Here are the basic uses of the Python subtraction assignment operator:

customers_waiting = 20
customers_waiting -= 1
print("Updated number of customers waiting in line:", customers_waiting)

customers_waiting = 10
customers_waiting -= 1
print("Updated number of customers waiting in line:", customers_waiting)

Output:

Updated number of customers waiting in line: 19
Updated number of customers waiting in line: 9

I executed the above example code and added the screenshot below.

increment and decrement operators in python

This code shows how to decrease a variable’s value by 1 using the subtraction assignment operator (-=) in Python.

2. Python Decrement Operator on String Variable

Let’s see how to use the decrement operator with a string variable in Python.

NYC = "New York City"
NYC -= "City"
print("The NYC stands for:", NYC)

Output: We will get the TypeError as strings don’t support the -= operation in Python.

Traceback (most recent call last):
  File "C:\Users\kumar\PycharmProjects\pythonProject1\main.py", line 2, in <module>
    NYC = NYC - "City"
TypeError: unsupported operand type(s) for -: 'str' and 'str'

I executed the above example code and added the screenshot below.

increment and decrement operator in python

This code will raise an error because Python does not support the decrement (-=) operator with strings.

3. Decrement Operator in Python Within Loops

Decrement operations are commonly used within loops to control the iteration in Python.

Let’s see an instance that will demonstrate the use of the decrement operator in Python within loops:

item_quantity = 10
for _ in range(5):
    item_quantity -= 1
print("Updated quantity of the item in the warehouse:", item_quantity)

item_quantity = 15
for _ in range(5):
    item_quantity = item_quantity - 1
print("Updated quantity of the item in the warehouse:", item_quantity)

Output:

Updated quantity of the item in the warehouse: 5
Updated quantity of the item in the warehouse: 10

I executed the above example code and added the screenshot below.

increment or decrement operator in python

Python does not allow using the decrement (-=) operator with strings, so this code results in a TypeError.

Difference Between Increment and Decrement Operators in Python

Here’s a comparison between increment and decrement operators in Python presented in tabular form:

AspectIncrement Operator in PythonDecrement Operator in Python
Operators UsedWe use ‘+=’ or ‘+’ operators as increment operator in PythonWe use ‘-=’ or ‘-‘ operators as decrement operator in Python
EffectsIncreases the variable by a specified value.Decreases the variable by a specified value.
Syntax‘x += 1’ or ‘x = x + 1’‘y -= 1’ or ‘y = y – 1’
UsageTypically used in loops, counters, or variable updates.Commonly used in loops, counters, or variable updates.
Can we use two strings in Python, but with str and numbers?Can we use two strings in Python, but with str and numbers?

This table provides a clear difference between increment and decrement operators in Python, highlighting their usage, effect, and common practices.

Conclusion

In summary, while Python lacks specific increment and decrement operators (++ and –), we can achieve similar functionality using the +=, -= operators, or the arithmetic expression operator with the assignment operator.

And this is what we call increment and decrement operators in Python. I have taken different examples with a clear difference between increment and decrement operators in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.