Nearby lessons
40 of 108Python - Identity Operators
Introduction
Identity operators are used to compare the identity of objects.
They check whether two variables refer to the same object or different objects in memory.
Identity operators do not compare the values stored inside the objects.
Identity Operators List
| Operator | Meaning |
|---|---|
is |
Returns True if both variables refer to the same object. |
is not |
Returns True if both variables refer to different objects. |
Important Note
Identity operators compare the memory location (object identity), not the value stored inside the object.
Difference Between == and is
| == Operator | is Operator |
|---|---|
| Compares values. | Compares object identity. |
| Checks content equality. | Checks memory reference. |
Example - Using == Operator
main.py
True
Explanation
Both variables contain the same value.
Therefore, a == b returns True.
Example - Using is Operator
main.py
True
Explanation
Both variables refer to the same object in memory.
Therefore, a is b returns True.
Checking Object Identity Using id()
Python provides the id() function to check the identity (memory reference) of an object.
Example - id() Function
main.py
140723456100 140723456100
Important Observation
If both IDs are the same, both variables refer to the same object.
Example - Different Objects
main.py
True False
Explanation
a == b returns True because both lists contain the same values.
a is b returns False because they are different objects.
Checking IDs of Different Objects
main.py
140723451000 140723452000
Important Observation
The IDs are different, so both variables refer to different objects.
Identity Operator with Strings
Python may reuse immutable objects such as strings to improve memory usage.
Example - String Identity
main.py
True
Explanation
Python may store both variables in the same memory location because strings are immutable.
Identity Operator with Mutable Objects
If two variables refer to the same mutable object, the identity operator returns True.
Example - Same List Object
main.py
True
Explanation
Both variables refer to the same list object.
Modifying a Shared Object
main.py
[10, 20, 30]
Why Did the Output Change?
Both variables refer to the same list object.
When one variable modifies the list, the change is visible through the other variable.
Identity Operator - is not
The is not operator checks whether two variables refer to different objects.
Example - is not Operator
main.py
True
Another Example - is not
main.py
False
Difference Between == and is
| Expression | Meaning | Result |
|---|---|---|
a == b |
Values are equal. | True |
a is b |
Same object. | False |
Example - == vs is
main.py
True False
Real World Usage of Identity Operators
Identity operators are useful for:
- Comparing object references.
- Checking
Nonevalues. - Validating memory references.
Checking None Using is
The recommended way to check whether a variable contains None is by using the is operator.
Example - Checking None
main.py
True
Why is This Recommended?
None is a singleton object in Python.
Using is None is the recommended and preferred style.
Less Preferred Style
main.py
True
Important Notes
iscompares object identity.==compares values.id()returns the identity (memory reference) of an object.- Mutable objects usually have different identities even if their values are equal.
- Immutable objects may share the same memory location.
Quick Summary
| Operator | Purpose |
|---|---|
is |
Checks whether two variables refer to the same object. |
is not |
Checks whether two variables refer to different objects. |
== |
Checks whether two values are equal. |