What does this print?
a = [1, 2]
b = [1, 2]
print(a == b, a is b)Options
- True True
- True False (Correct option)
- False False
- False True
Answer
True False
Why
== asks: do both objects contain the same value? is asks: are they literally the same object in memory?
a and b are two different lists that happen to contain the same numbers, so == is True and is is False.
Use is only to compare with singletons like None: if x is None:.
Remember
== compares values, is compares identity.