Learn Python
>>> operators.op_identity_membership

Identity & Membership Operators in Python

#concept

Identity operators (is, is not) check whether two names point to the exact same object in memory — not just an equal value. Membership operators (in, not in) check whether a value exists inside a collection like a list, string, or dictionary. Identity is easy to confuse with == (equality), but the two answer genuinely different questions.

#how it works
1

is checks identity

True only when two names refer to the literal same object in memory, not just an equal value.

2

is not is the opposite

True when the two names refer to different objects.

3

Equal values are not always the same object

[1, 2] == [1, 2] is True, but [1, 2] is [1, 2] is usually False — two separate list objects that happen to hold equal contents.

4

in checks membership

True when a value appears inside a sequence or collection.

5

not in is the opposite

True when the value is absent from the collection.

#example
a = [1, 2, 3]
b = [1, 2, 3]
c = a
 
print(a == b)
print(a is b)
print(a is c)
print(2 in a)
print(5 not in a)
Output
True
False
True
True
True
1a == b is True — they contain equal values.
2a is b is False — they are two separate list objects in memory, even though their contents match.
3a is c is True — c was assigned directly from a, so both names point at the exact same object.
42 in a checks membership — 2 is one of the elements in the list.
55 not in a is True, since 5 is not in the list.
#use cases
Checking whether a variable is None, which should always use is None rather than == None
Checking whether an item exists in a list, string, or dictionary before acting on it, like if username in registered_users
Understanding subtle bugs caused by confusing is (identity) with == (equality), especially with mutable objects
Filtering or validating data by checking membership in an allowed set of values

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.