How do you perform a Python increment operation? If you’re coming from a language like C++ or Java, you may want to try extending a similar increment functionality to Python also.
But, as we will see in this article, this is not quite the same. Let’s look at how we can try to use similar functionality of the Increment (
++) operation in Python.
Before going with the exact differences, we’ll look at how we can 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 >>> 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'
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!
Can we post-increment
a by 1, using
>>> 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
++ term, is called the increment operator in C++ / Java, 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 on 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.
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
+. 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, and for
+-5, it is
-5. This is a unary operator, which works on real numbers
++awill be parsed as + and
+a, but the second
+ais again treated as
(+a), which is simply
+(+(a))simply evaluates to
So, even though we wanted to increment the value of
a by one, we cannot achieve this using the
++ symbols, since this kind of operator, does not exist.
We must, therefore, use the
+= operator to do this kind of increment.
a += 1 a -= 1
The same logic holds true for the decrement operation also.
How is the += operation evaluated?
You may think that since there is an
= 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 this 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 for incrementing 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.
In this article, we learned about how we can use the increment operation in Python, and why the
-- operators are not supported.