Skip to Content

Python ++ How To Increment A Variable (Code Examples)

Python ++ How To Increment A Variable (Code Examples)

How do you increment an integer variable in Python? Many other languages use the double plus sign operator on the variable to increment by 1, but what is Python’s plus plus operator equivalent?

Python does not yet (as of version 3.9) have the ++ operator. Instead to increment an integer variable in Python by 1 use the operator assignment syntax i += 1.

Is There A ++ In Python?

As of Python version 3.9 there is no double plus operator. This type of operation in other languages increments an integer variable by one.

Take for example the following JavaScript code in a browser’s console panel:

> let i = 1;
> i++;
> console.log(i);

As you can see from the above example in a browser console window to increment an integer variable, such as i by 1 you can just apply the plus plus operator ++ to perform the operation.

Performing the same operation in Python (as of version 3.9.7) produces the following SyntaxError in the REPL:

>>> i = 1
>>> i++
  File "<stdin>", line 1
SyntaxError: invalid syntax

As you can see from the error output by the Python REPL it is signalling the location of the invalid syntax by the caret symbol ^ using it as a pointer for where the problem can be found in your code.

So if you can’t use the double plus operator in Python to increment a variable by 1 in Python, what can you use?

How Do You Use ++ In Python?

If you can’t directly use the ++ operator in Python what can you use in its place? If the outcome of what you’re trying to achieve is to increment the value of an integer variable by 1 then there are alternatives.

It goes without saying that the most obvious way to increment a variable by 1 is to just use the most obvious form of code which most coding languages also do:

>>> i = 1
>>> i = i + 1
>>> print(i)

Python isn’t going to win any awards with this code, but at least it’s clear what is being achieved by the code written.

Besides the obvious form above, the other lesser known form is to use the assignment operator += which itself isn’t unique to Python and can be found in other languages too.

Here is a demonstration of the += assignment operator in Python REPL:

>>> i = 1
>>> i += 1
>>> print(i)

As demonstrated above the assignment operator += does exactly the same as what the ++ operator does.


To increment a variable by 1 in Python use the assignment operator += 1 as the plus plus operator common in other languages is not recognised in Python code.