Computer >> Computer tutorials >  >> Programming >> Python

What is behavior of ++ and -- operators in Python?


In C/C++ and Java etc, ++ and -- operators are defined as increment and decrement operators. In Python they are not defined as operators.

In Python objects are stored in memory. Variables are just the labels. Numeric objects are immutable. Hence they can't be incremented or decremented.

However, prefix ++ or -- doesn't give error but doesn't perform either.

>>> a=5
>>> b=6
>>> ++a
5
>>> --b
6

Postfix ++ or -- produce errors

>>> a=5
>>> b=6
>>> a++
SyntaxError: invalid syntax
>>> b--
SyntaxError: invalid syntax