What does 'is' operator do in Python?

PythonServer Side ProgrammingProgramming

In Python, is and is not operators are called identity operators. Each object in computer's memory is assigned a unique identification number (id) by Python interpreter. Identity operators check if id() of two objects is same. 'is' operator returns false of id() values are different and true if they are same.

>>> a=10
>>> b=a
>>> id(a), id(b)
(490067904, 490067904)
>>> a is b
True
>>> a=10
>>> b=20
>>> id(a), id(b)
(490067904, 490068064)
>>> a is b
False
raja
Published on 13-Feb-2018 14:16:26
Advertisements