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

Updated on: 26-Feb-2020

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements