Python Identity Operators
1. What are Identity Operators?
Identity operators are used to check whether two variables point to the same object in memory.
In simple words:
π They check βAre both variables the same thing?β
Python has two identity operators:
-
is -
is not
2. Identity vs Equality (Very Important)
-
==checks value -
ischecks identity (memory location)
Example
3. The is Operator
is returns True if both variables refer to the same object.
Example
Output:
Here:
-
ypoints to the same list asx
4. The is not Operator
is not returns True if both variables do not refer to the same object.
Example
Output:
Even though values look same, they are different objects.
5. Identity with Numbers (Simple Note)
Small numbers may behave the same because Python saves memory.
Example
Output:
π Do not depend on this behavior.
6. Identity with Lists (Important)
Lists with same values are not the same object.
Example
7. Identity Operator in if Statement
Example
π This is a very common and correct use of is.
8. Common Beginner Mistakes
β Using is Instead of ==
β Correct:
β Comparing Values Instead of Objects
It works, but it is not good practice.
9. Simple Practice Examples
Example 1: Check None
Example 2: Same List Check
Example 3: Different Objects
10. Summary (Identity Operators)
β Identity operators check memory location
β is means same object
β is not means different object
β Use == for value check
β Use is for None check
π Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
Python Membership Operators
-
Operator Exercises
-
ifβelse statements
-
MCQs with answers
Just tell me π
