Lesson 08
Identity Operators (`is` / `is not`)
Compare object identity with is and is not — memory addresses, not just values. Learn alias vs copy and when to use is with None.
Identity operators compare memory addresses, not just values.
Two objects may contain the same data, but they are not necessarily the same object.
What you'll learn
- Difference between
isand== - Alias (
lst2 = lst1) vs copy (lst1.copy()) - Why
Noneis compared usingis - Why equal values do not always share identity
Identity operators
| Operator | Meaning |
|---|---|
is |
Both names refer to the same object |
is not |
They refer to different objects |
obj1 is obj2
obj1 is not obj2Identity means Python compares the object ID (memory address), not the value alone.
is vs ==
These operators answer different questions.
== — value equality
a = [1, 2]
b = [1, 2]
print(a == b)TrueSame contents.
is — identity
a = [1, 2]
b = [1, 2]
print(a is b)FalseDifferent objects.
Rule:
==compares values;iscompares identity.
Alias vs copy (interactive)
The biggest identity concept is understanding sharing vs copying.
Alias vs copy
The biggest identity idea is sharing one object versus creating a new one.
Case 1: Alias
lst1 = [10, 20, 30] lst2 = lst1
print(lst1 is lst2)
Result
true✓ same object
Both variables point to one list.
Small integer interning
CPython often reuses small integer objects. This is an implementation detail — not something to rely on in production code.
Choose the example
a = 256 b = 256 print(a is b)
Typical CPython behavior
Often True
CPython usually reuses small integers from -5 to 256.
Important: never write production code like x is 256. Use x == 256 instead.
After lst2 = lst1, both names refer to one list:
lst1 = [10, 20, 30]
lst2 = lst1
print(lst1 is lst2)TrueNone and boolean identity
None is a singleton in Python.
a = None
b = None
print(a is b)TrueThis is why Python programmers write:
if value is None:
print("No value")Instead of:
if value == None:
...Prefer is None — it is clear and idiomatic.
Strings and identity
Some identical strings may share memory through interning.
s1 = "PYTHON"
s2 = "PYTHON"
print(s1 is s2)In CPython this is often True, but do not depend on interning. Use == when comparing text.
Dictionaries and lists
Equal collections are usually different objects.
Lists
l1 = [1, 2]
l2 = [1, 2]l1 == l2→ Truel1 is l2→ False
Dictionaries
d1 = {1: "A"}
d2 = {1: "A"}d1 == d2→ Trued1 is d2→ False
Memory model
Mental model
y = x→ same object →isreturns Truez = x.copy()→ new object →isreturns False
When to use is
| Situation | Use |
|---|---|
Compare with None |
✅ is |
| Compare two numbers | ❌ == |
| Compare two strings | ❌ == |
| Check same object | ✅ is |
| Compare list contents | ❌ == |
Best practice
if value is None:
print("Missing")Avoid
if value is 10:
...Practice exercises
Exercise 1 — Predict the output:
lst1 = [10, 20]
lst2 = lst1
print(lst1 is lst2)Answer: True
Exercise 2 — Predict:
{1: 2} is {1: 2}Will it be True or False? (False — two separate dict literals.)
Exercise 3 — Compare both operators:
a = [1, 2]
b = [1, 2]
print(a == b)
print(a is b)Explain why the results differ: same values, different objects.
Quick knowledge check
-
What does
iscompare?
The identity (memory address) of two objects. -
Is
lst.copy()the same object?
No. It creates a new object. -
Should you use
isto compare integers like256?
No. Use==. Reserveismainly forNoneand genuine identity checks.
Summary
isandis notcompare object identity, not value.==compares contents or values.- Aliasing shares one object; copying creates a new one.
- Interning can make some integers and strings appear identical, but you should never rely on it.
- The most common production use is
value is None.
Next lesson: Ternary Operator (if ... else) — choose one of two expressions with Python’s built-in ternary syntax.
Loading playground…