Computer Science

Two objects (say a and b) when compared using == ,return True. But Python gives False when compared using is operator. Why? (i.e., a == b is True but why is a is b False?)

Python Data Handling

24 Likes

Answer

As equality (==) operator returns True, it means that a and b have the same value but as is operator returns False, it means that variables a and b point to different objects in memory. For example, consider the below Python statements:

>>> a = 'abc'
>>> b = input("Enter a string: ")
Enter a string: abc
>>> a == b
True
>>> a is b
False

Here, both a and b have the same value 'abc' but they point to different objects.

Answered By

15 Likes


Related Questions