Identity vs Equality
Learn the difference between identity and equality in Python. Identity checks whether two variables refer to the same object in memory, while equality compares whether two objects hold the same value. Grasping this distinction is key to writing accurate conditionals, validations, and data-processing logic.
What is is?
ischecks object identity, whether two variables point to the same object in memory;==checks equality of values, whether two objects have the same contents.
12345678# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True β same items print(cart_today is cart_yesterday) # False β two separate cart objects print(cart_today is shared_cart) # True β both refer to the same cart
When to Use is and When to Avoid It
- Use
isin comparison with singletons such asNone;
12345# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
is notis the negated identity check;
12345# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
- Avoid using
isto check for equality between numbers or strings. Due to internal caching/interning, identity may appear to "work" sometimes, but it's not reliable across different runs and environments, use==instead;
123456789101112# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True β same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True β same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
- For booleans, prefer truthiness checks.
12345# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
1. Fill in the blanks with is or ==:
2. What's the correct way to test for "no value"?
3. Which statement is recommended?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about when to use `is` versus `==` in real-world scenarios?
What are some common mistakes people make with `is` and `==` in Python?
Could you provide more examples of identity and equality checks?
Awesome!
Completion rate improved to 3.45
Identity vs Equality
Swipe to show menu
Learn the difference between identity and equality in Python. Identity checks whether two variables refer to the same object in memory, while equality compares whether two objects hold the same value. Grasping this distinction is key to writing accurate conditionals, validations, and data-processing logic.
What is is?
ischecks object identity, whether two variables point to the same object in memory;==checks equality of values, whether two objects have the same contents.
12345678# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True β same items print(cart_today is cart_yesterday) # False β two separate cart objects print(cart_today is shared_cart) # True β both refer to the same cart
When to Use is and When to Avoid It
- Use
isin comparison with singletons such asNone;
12345# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
is notis the negated identity check;
12345# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
- Avoid using
isto check for equality between numbers or strings. Due to internal caching/interning, identity may appear to "work" sometimes, but it's not reliable across different runs and environments, use==instead;
123456789101112# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True β same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True β same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
- For booleans, prefer truthiness checks.
12345# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
1. Fill in the blanks with is or ==:
2. What's the correct way to test for "no value"?
3. Which statement is recommended?
Thanks for your feedback!