is vs == comparison confusion
Quick summary
You used 'a is b' when you meant 'a == b' — 'is' checks if two variables point to the same object, not if their values are equal.
Why this happens
You used 'is' to compare values like numbers or strings, which works for small integers due to caching but fails for larger ones
You used 'is not' for value inequality instead of '!=', getting identity comparison instead of value comparison
You compared immutable objects (strings, tuples) with 'is' expecting value equality but got identity comparison
Minimal example
a = 1000
b = 1000
print(a is b) # False — different objects!
print(a == b) # True — same valuea = 1000
b = 1000
print(a == b) # True — value equality
# Use 'is' only for None checks:
if a is not None:
print(a)Python caches small integers (-5 to 256) so 'is' works by coincidence there, but for larger numbers each is a separate object requiring == for value comparison.
How to diagnose
Replace 'is' with '==' and check if the result changes — if it does, you were comparing identity not value
Print id(a) and id(b) to see if the objects are the same instance in memory
Test with larger numbers (>256) or dynamically created strings where caching does not apply
How to fix
Use == for value equality comparisons and reserve 'is' for None checks and identity comparisons
Use 'is not None' for None checks and '!= None' is discouraged but '== value' for general comparisons
Use 'a is not b' only when you specifically need identity comparison, otherwise use 'a != b'
How to prevent
Use == for all value comparisons and 'is' only for None, True, False, and sentinel comparisons
Enable flake8 warnings (E711, E712) that detect comparison to None/True/False with == instead of is