Junior — Middle
Explain the difference between the operators == and is in the context of comparison in Python.
sobes.tech AI
Answer from AI
In Python, the == operator compares the values of objects, meaning it checks whether the data stored in the objects are equal. The is operator checks whether both variables point to the same object in memory (i.e., it compares object identity).
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a == c) # True, because the contents of the lists are the same
print(a is c) # False, because they are different objects
print(a is b) # True, because b refers to the same object as a
Thus, == is for value equality, and is is for reference equality (identity).