After completing this chapter, you will be able to:
- Understand Identity Operators.
- Explain why identity operators are used.
- Learn the
isandis notoperators. - Differentiate between identity and equality.
- Write programs using identity operators.
- Understand object references in Python.
In Python, two variables may contain the same value, but they may or may not refer to the same object in memory.
To determine whether two variables refer to the same object, Python provides Identity Operators.
Identity operators compare the memory location (object identity) of objects rather than just comparing their values.
Identity Operators are used to check whether two variables refer to the same object in memory.
Python provides two identity operators.
isis not
Identity operators help to:
- Compare object references.
- Check whether two variables refer to the same object.
- Compare objects with
None. - Understand Python's memory model.
- Debug object references.
| Operator | Description |
|---|---|
is |
Returns True if both variables refer to the same object |
is not |
Returns True if both variables refer to different objects |
Example
a = [10, 20]
b = a
c = [10, 20] a
│
▼
+-------------+
| 10 | 20 |
+-------------+
▲
│
b
c
│
▼
+-------------+
| 10 | 20 |
+-------------+
Here,
aandbrefer to the same object.crefers to a different object, even though it has the same value.
The is operator returns True if two variables refer to the same object.
variable1 is variable2a = [10, 20]
b = a
print(a is b)True
The is not operator returns True if two variables refer to different objects.
variable1 is not variable2a = [10, 20]
b = [10, 20]
print(a is not b)True
Identity operators compare memory locations, while equality operators compare values.
Example
a = [10, 20]
b = [10, 20]
print(a == b)
print(a is b)True
False
Explanation
==compares values.iscompares object identity.
Write a Python program to compare two variables using identity operators.
a = [1, 2, 3]
b = a
print("a is b :", a is b)
print("a is not b :", a is not b)a is b : True
a is not b : False
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)True
False
value = None
print(value is None)
print(value is not None)True
False
Identity operators are used in:
- Checking
Nonevalues. - Object reference comparison.
- Memory optimization.
- Python debugging.
- Object caching analysis.
- Compares object references directly.
- Useful when checking
None. - Improves code clarity.
- Helps understand Python's memory management.
| == | is |
|---|---|
| Compares values | Compares object identity |
| Checks equality | Checks memory reference |
| Used for value comparison | Used for object comparison |
❌ Incorrect
a = [10, 20]
b = [10, 20]
print(a is b)Output
False
Although both lists contain the same values, they are different objects.
✅ Correct
print(a == b)Output
True
Instead of
if value == None:
print("No Value")Prefer
if value is None:
print("No Value")This is the recommended Python practice.
Identity operators compare whether two variables refer to the same object in memory.
isis not
==compares values.iscompares object identity (memory reference).
The is operator is commonly used to compare a variable with None.
Yes.
Two variables can store equal values but refer to different objects in memory.
is→ Same object.is not→ Different objects.- Identity operators compare object references.
- Equality (
==) compares values. - Use
is Noneto check forNone.
In this chapter, you learned:
- What Identity Operators are.
- The
isoperator. - The
is notoperator. - Identity vs Equality.
- Practical programs.
- Real-world applications.
- Common mistakes.
- Interview questions.