Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Identity vs Equality | Booleans and Comparisons
Data Types in Python

bookIdentity 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?

  • is checks 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
copy

When to Use is and When to Avoid It

  • Use is in comparison with singletons such as None;
12345
# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
copy
  • is not is 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}")
copy
  • Avoid using is to 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)
copy
  • 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")
copy

1. Fill in the blanks with is or ==:

2. What's the correct way to test for "no value"?

3. Which statement is recommended?

question-icon

Fill in the blanks with is or ==:

Use to check if two variables point to the same object.
Use
to check if two values have the same contents.

Click or drag`n`drop items and fill in the blanks

question mark

What's the correct way to test for "no value"?

Select the correct answer

question mark

Which statement is recommended?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

bookIdentity 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?

  • is checks 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
copy

When to Use is and When to Avoid It

  • Use is in comparison with singletons such as None;
12345
# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
copy
  • is not is 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}")
copy
  • Avoid using is to 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)
copy
  • 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")
copy

1. Fill in the blanks with is or ==:

2. What's the correct way to test for "no value"?

3. Which statement is recommended?

question-icon

Fill in the blanks with is or ==:

Use to check if two variables point to the same object.
Use
to check if two values have the same contents.

Click or drag`n`drop items and fill in the blanks

question mark

What's the correct way to test for "no value"?

Select the correct answer

question mark

Which statement is recommended?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt