Python Mastery – From Basics to Brilliance / Operators in Python

Identity & Membership Operators

✅ Identity Operators

These operators compare memory locations — not just the values.

Operator

Meaning

Returns True If...

is

Same object

Both variables point to the same memory location

is not

Not same object

They point to different memory locations


💡 Example:

a = [1, 2]   # a is a list
b = a        # b refers to the same list as a

print(a is b)       # ✅ True – both a and b point to the same object
print(a is not b)   # ❌ False – they are not different objects

✅ Membership Operators

These operators are used to check if a value is present in a sequence like a list, string, tuple, etc.

Operator

Meaning

Returns True If...

in

Value exists

The value is inside the sequence

not in

Value doesn’t exist

The value is not inside the sequence


💡 Example:

colors = ["red", "green"]

print("red" in colors)        # ✅ True – "red" is in the list
print("blue" not in colors)   # ✅ True – "blue" is not in the list