Equality compares values according to the object’s equality implementation. Identity asks whether two references designate the same object.
Before you start
You should know Python functions, collections and exceptions. Use a small isolated script or interactive session to trace the example. Pay attention to when objects are created and when work executes; iteration, binding and mutation can happen at different points in a program.
The practical goal is to reason through this situation: Two separate lists with equal elements compare equal but are not identical. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Compare object references
Is tests whether references designate the same object.
Step 2: Compare domain values
Double-equals delegates to equality behavior rather than object identity.
Step 3: Use singleton checks
Is None expresses the intended missing-value check without invoking custom equality.
Worked scenario
Two separate lists with equal elements compare equal but are not identical.
a = [1, 2]
b = [1, 2]
print(a == b) # True
print(a is b) # False
print(None is None) # TrueEqual contents do not merge separate list objects. Interning of some values is an implementation behavior, not a substitute for value comparison.
Common mistake
Interned small values can make identity comparisons appear to work.
Verify the behavior
Test equal lists, a shared alias and None; avoid identity assertions for ordinary numeric or string values.
Interview exercise
Test whether a result is missing.
Answer and reasoning
Use is None for the singleton and use equality for domain values rather than relying on incidental interning.
Continue learning
Compare the scenario with the Python interview questions and test your understanding with the Python MCQs. For terminology and implementation details, consult the reference material.