+ 1

What is difference between '==' and 'is' in python.

28th Oct 2017, 3:07 AM
Pankaj9310
Pankaj9310 - avatar
2 Answers
+ 2
the "==" test for value equality . the "is " test for object identity print(None == None)------(True) print(None is None)------>(True) print(True is True)-------> (True) print([] == [])------->(True) print([] is [])--------->(False) print("Python" is "Python")----->(True) there is only one None and one True object. That is why True is equal and also identical to True. There is only one truth out there, anyway. The empty list [] is equal to another empty list []; but they are not identical. Python has put them into two different memory locations. They are two distinct objects. Hence the is keyword returns False. On the other hand, "Python" is "Python" returns True. This is because of optimization. If two string literals are equal, they have been put to same memory location. A string is an immutable entity and therefore, no harm can be done. it is safe to use the "==" when comparing two things.
28th Oct 2017, 4:43 AM
jay
+ 1
for more details on this and more check this link : http://zetcode.com/lang/JUMP_LINK__&&__python__&&__JUMP_LINK/keywords/
28th Oct 2017, 4:50 AM
jay