Increment and Decrement operators in Python [6 Examples]

In this Python article, I will explain what is increment and decrement operators in Python. I will also explian the difference between increment and decrement operator in Python.

In many programming languages like C, C++, and Java, there are operators known as increment and decrement operators (++ and –) which are used to increase or decrease the value of a variable by one.

However, Python does not have specific increment (++) and decrement (–) operators like those found in other languages. Python uses augmented assignment operators, which combine the assignment operator (=) with a mathematical operation, such as addition or subtraction to be addition assignment operator (+=) or substraction assignment operator (-=).

This addition assignment operator (+=) and substraction assignment operator (-=) is called increment and decrement operator in Python respectivly.

Let’s explaore them with the help of some examples:

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 in Python.

The syntax of the addition assignment operator in Python:

Variable += Value

or

Variable = Variable + Value

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

Let’s see some examples:

1. Python increment operator

Here is the basic use of 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

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

increment in python

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

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

what is +=1 in python

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.

Here is an example of Python increment operator with for loop:

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

After executing the code in Pycharm, one can see the output in the below screenshot.

python increment counter

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

Seeing this we can say, the vaule is getting decremented by the variable.

Here are some examples:

1. Python decrease by 1

Here is the basic uses of 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

Below is a screenshot displaying the output following the implementation of the code in the Pycharm editor.

increment and decrement operators 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'

Below is a screenshot showcasing the output, captured after the code was executed in the Pycharm editor.

increment and decrement operator in python

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

Following the implementation of the code in the Pycharm editor, the screenshot below has been provided.

increment or decrement operator in python

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.
Syntaxx += 1‘ or ‘x = x + 1y -= 1‘ or ‘y = y – 1
UsageTypically used in loops, counters, or variable updates.Commonly used in loops, counters, or variable updates.
Can we used with two strings in Python but with str and numbers.Throw ValueError, Cannot be used with two strings in Python or string with numbers.
List of difference between increment and decrement operators in Python

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 arithmatic expression operator with the assignment operator.

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

I hope you understand this clerly, and will use the increment and decrement opertors in Python programs of yours.

You may also like to read: