➕ Operators & Expressions
Module 3 · Topic 6

Identity & Membership Operators

Test object identity with is/is not and check membership with in/not in — two operator categories unique to Python.

Theory

Identity Operators:

| Operator | Description | Example |
|----------|-------------|--------|
| is | Same object in memory | x is y |
| is not | Different objects | x is not y |

  • Tests object identity, not value equality
  • Use for comparing with None: if x is None
  • Small integers (-5 to 256) and short strings are cached (interned)

Membership Operators:

| Operator | Description | Example |
|----------|-------------|--------|
| in | Value exists in sequence | "a" in "abc" → True |
| not in | Value not in sequence | 5 not in [1,2,3] → True |

  • Works with: str, list, tuple, set, dict (checks keys), range
  • Sets have O(1) lookup, lists have O(n) — use sets for large collections
Syntax
# Identity
x is None
x is not None

# Membership
"a" in "abc"         # True
5 in [1, 2, 3, 4, 5] # True
"key" in my_dict      # Checks dict keys
x not in my_set       # Not in set