Senior
What is the difference between 'is' and '==' in Python?
sobes.tech AI
Answer from AI
In Python, the is operator checks whether two variables point to the same object in memory (i.e., it compares object identity). Meanwhile, the == operator checks for value equality of objects, meaning it calls the __eq__ method and compares the contents.
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, because b is a reference to the same object
print(a == b) # True, values are equal
print(a is c) # False, different objects in memory
print(a == c) # True, list values are the same