🔹 Identity & Membership Operators
Last Updated: 16th August 2025
- Membership operators check if a value exists inside a sequence (string, tuple,etc).
- Identity operators check if two variables refer to the same object in memory.
Hinglish Tip 🗣: Identity = “kya dono same cheez ko point kar rahe hain?”
Membership = “kya yeh value sequence ke andar hai?”
🔎 Membership Operators
# String membership (case-sensitive)
name = "Python"
print("P" in name) # True
print("py" in name) # False (case matters)
print("z" not in name) # True
# Tuple membership
nums = (10, 20, 30)
print(20 in nums) # True
print(25 in nums) # False
Hinglish Tip 🗣: Membership exact match hota hai; strings me uppercase/lowercase ka farq padta hai.
🔐 Identity Operators
# Best practice: check None with 'is'
x = None
print(x is None) # True
print(x is not None) # False
# Identity vs equality
a = (1, 2) # tuple
b = (1, 2) # another tuple with same values
c = a # c points to the same object as a
print(a == b) # True (values equal)
print(a is b) # False (different objects)
print(a is c) # True (same object)
# Memory address Printing ,Every object has a same memory address
print(id(a), id(b), id(c)
Hinglish Tip 🗣: == values compare karta hai, is object identity (memory) compare karta hai.
💡 Quick Practice
- For word = "developer", check: "dev" in word, "D" in word, "z" not in word.
- Create code = None and print whether code is None.
- Exercises