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