🧠 Python Identity Operators (is, is not) – Explained with Examples
Python’s identity operators—is and is not—are often misunderstood by beginners. While they look similar to comparison operators, they behave differently by checking whether two variables point to the same memory location, not just equivalent values.
🔍 What Are Identity Operators in Python?
Identity operators compare the memory location of two objects, not their values. They are:
| 🔑 Operator | 🧠 Description |
|---|---|
is | Returns True if both variables refer to the same object (i.e., same memory address). |
is not | Returns True if they do not refer to the same object. |
✅ Example 1 – Same Reference
a = [1, 2, 3]
b = a
print(a is b) # ✅ True – both refer to the same object
print(a == b) # ✅ True – values are also equal
✅ Explanation:
a is b: ✔ True, becausebpoints to the same list asa.a == b: ✔ True, because the list values are the same.
⚠️ Example 2 – Different Objects with Same Content
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # ❌ False – different memory addresses
print(x == y) # ✅ True – contents are equal
⚠️ Explanation:
x is y: ❌ False becausexandyare two different list objects.x == y: ✔ True because they have the same values.
💡 Example 3 – Identity with Immutable Types
a = 100
b = 100
print(a is b) # ✅ True (cached by Python for small integers)
a = 5000
b = 5000
print(a is b) # ❌ False (not always cached)
💡 Tip:
- Python internally caches small integers and strings, so
ismight returnTruefor those, but it should not be relied upon for comparisons.
📘 Best Practices
- ✅ Use
isandis notfor:- Comparing with
None(if x is None) - Checking if two variables refer to the same object (identity check)
- Comparing with
- ⚠️ Do NOT use
isfor:- Value comparison (use
==instead)
- Value comparison (use
📊 Comparison: is vs ==
| Check Type | is | == |
|---|---|---|
| Compares | Memory location | Values |
Returns True | If same object | If values are the same |
| Example | x is y | x == y |
| Use Case | if x is None: | if x == 0: |
❓FAQs
❓What is the difference between is and == in Python?
is checks identity (same memory location), while == checks equality (same value).
❓Should I use is to compare strings or numbers?
No. Use == for value comparisons. Python might cache small strings or numbers, but that’s not guaranteed across all Python implementations.
❓Why does None require is instead of ==?
Because None is a singleton in Python. Checking with is ensures you’re referring to the actual None object.
🔚 Summary
- Use
is/is notto check object identity. - Use
==/!=to check value equality. - Common in conditional checks involving
None.
✅ FAQ – Python Identity Operators
❓What are identity operators in Python?
Identity operators (is and is not) are used to check if two variables reference the same object in memory, not just if they have the same value.
❓What is the difference between is and == in Python?
ischecks whether two variables point to the same memory address.==checks if the values are equal, regardless of memory address.
❓When should I use is in Python?
Use is when you want to check identity, such as:
if x is None:
This ensures you’re comparing with the singleton None object.
❓Why does a is b return False even if a == b is True?
Because they might have the same value but are stored in different memory locations, especially with mutable data types like lists and dictionaries.
❓Is it safe to use is to compare strings and integers?
Only for small values. Python caches small integers and some strings, so is might work for those—but it’s not guaranteed. Always use == for value comparison.
Share Now :
