a = "123"
b = "12" + "3"
c = str(100+20+3)
print(a is b, b is c)
Why would the output be ‘False’ in the second case?
On
The purpose of the is operator is to say whether two variables reference the same object. The only scenario where this is guaranteed is when they have been explicitly set up that way.
>>> a = "random string"
>>> b = a
>>> a is b
True
There are other scenarios wher Python might notice that two unrelated variables refer to the same value, and have them pass this test; but that is not guaranteed, merely an implementation detail which should not, and indeed cannot, be relied on.
>>> c = 1
>>> d = 1
>>> c is d
True # or not
In Python,
isoperator is called the identity operator. Python interpreter assigns each object in the computer's memory a unique identification number (id). The identity operator returns true if theid()of two objects is the same else false. So,isoperator tests if two variables point to the same object and not if two variables have the same value.To illustrate
Use
==operator to test if two values are the same.