This guide explains Identity Operators: is vs == through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
== asks whether two objects compare equal in value; is asks whether they are the exact same object. Use is None to test for the singleton None, not is to compare ordinary numbers or strings.
When You Would Use This
A shared list and an independent list can contain the same items without sharing identity.
Step-by-Step Example
original_values = [10, 20]
shared_values = original_values
copied_values = [10, 20]
print(original_values == copied_values)
print(original_values is copied_values)
print(original_values is shared_values)
print(None is None)
How the Example Works
Equal contents make the first comparison true. The independently created list is not the same object. The alias references the original object, so its identity check is true.
Example Output
True
False
True
True
Try It Yourself
Make a list alias and explain what happens when you append through the alias.
Common Beginner Mistakes
- Using
isinstead of==to compare strings or numbers. - Assuming two independent equal lists share one identity.
- Testing
value == Nonewherevalue is Noneis more direct and idiomatic.
Key Takeaways
==asks whether two objects compare equal in value;isasks whether they are the exact same object.- Equal contents make the first comparison true.
- Avoid this mistake: Using
isinstead of==to compare strings or numbers.