Python Increment Operation

Python Increment Operation

Increment operation is used to increase the value of a variable by adding 1 to it. Languages like C, C++, Java, etc. have “++” for this purpose. If you’re coming from a language where the “++” operator exists, you may also want to implement the increment functionality to Python.

In Python, if you try to use the “++” operator, it may result in a syntax error. The “++” operator is not present to perform increment operations in Python, so how can we do increment operations in Python?

In this tutorial, we will learn to perform increment operations in Python & see what the alternative is to the “++” operator in Python.

Python Increment a Value

Before going with the exact differences, we’ll look at how to increment a variable in Python.

The below code shows how almost all programmers increment integers or similar variables in Python.

>>> a = 10
>>> print(a)
10

>>> a += 1
>>> print(a)
11

>>> a += 100
>>> print(a)
111

We have incremented the integer variable a in successive steps here. Also, since the + operator also stands for concatenation with respect to strings, we can also append to a string in place!

>>> b = 'Hello'
>>> b += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

>>> b += 'from AskPython'
>>> b
'Hello from AskPython'

Can we post-increment a by 1, using a++?

>>> a++
  File "<stdin>", line 1
    a++
      ^
SyntaxError: invalid syntax

Well, there is a problem here. Python, by design, does not allow the use of the “++” operator. The team “++”  is called the increment operator in many programming languages and does not have a place in Python.

Why is there no ++ operator in Python?

If you want to understand this in more detail, you need to have some background in programming language design.

The option of not including the ++ operator in Python is a design decision. People who are responsible for creating features in the Python language felt that there was no need to introduce a CPP-style increment operator. Python uses its own philosophy, known as the “Pythonic way,” which emphasizes readability and simplicity of code. 

When the Python interpreter parses the a++ symbol from our input, it is interpreted in the following fashion:

  • Since the binary + operator is the addition operator, a++ will be treated as a, +, and +. But Python expects a number after the first + operator. Therefore, it will give a syntax error on a++, since the second + is not a number.

Similarly, the pre-increment ++a will be treated like this:

  • The unary + operator in Python refers to the identity operator. This simply returns the integer after it. This is why it is an identity operation on the integer
  • For example, the value of +5 is simply 5, and for +-5, it is -5. This is a unary operator, which works on real numbers
  • The ++a will be parsed as + and +a, but the second +a is again treated as (+a), which is simply a
  • Therefore, +(+(a)) simply evaluates to a.

So, even though we wanted to increment the value of a by 1, we cannot achieve this using the ++ symbols, since this kind of operator does not exist in Python. We must, therefore, use the += operator to increment a value in Python.

a += 1
a -= 1

The logic is the same for both the increment and decrement operators in Python.

How is the augmented assignment operator “+=” evaluated?

You may think that since there is a = symbol, it could be an assignment statement. However, this is not a regular assignment statement. This is called an augmented assignment statement.

In a regular assignment statement, the right-hand side is evaluated first before assigning it to the left-hand side.

# 2 + 3 is evaluated to 5, before assigning to a
a = 2 + 3

However, in this augmented assignment statement, the left side is evaluated first before evaluating the right side. This is done so that the updated value can be written to the left side in place.

# Reads the value of a, before adding 3 to it in-place
a += 3

This is the only way to increment a variable, without using a reassigning statement like a = a + 1. But here, overall, the option doesn’t matter anyway, since the interpreter will optimize the code at runtime.

Conclusion

Increment operation is performed by using increment operator “++” which is used to increment by one, but this operator don’t work in Python, it doesn’t exist here instead, Python uses something called augmented assignment operator “+=” to add one or any other value to a variable. Hope this tutorial helped you to learn about Python increment operation.

References