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
22 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
14 Likes
Related Questions
What is an atom in Python? What is an expression?
What is the difference between implicit type conversion and explicit type conversion?
If you give the following for str1 = "Hello", why does Python report error?
str1[2] = 'p'
Given str1 = "Hello", what will be the values of:
(a) str1[0]
(b) str1[1]
(c) str1[-5]
(d) str1[-4]
(e) str1[5]