Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner20 min

Identity operators compare memory addresses, not just values.

Two objects may contain the same data, but they are not necessarily the same object.

Two names can refer to one list object with the same ID

What you'll learn

  • Difference between is and ==
  • Alias (lst2 = lst1) vs copy (lst1.copy())
  • Why None is compared using is
  • 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
python
obj1 is obj2
obj1 is not obj2

Identity means Python compares the object ID (memory address), not the value alone.

is vs ==

These operators answer different questions.

== — value equality

python
a = [1, 2]
b = [1, 2]

print(a == b)
text
True

Same contents.

is — identity

python
a = [1, 2]
b = [1, 2]

print(a is b)
text
False

Different objects.

Rule: == compares values; is compares 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:

python
lst1 = [10, 20, 30]
lst2 = lst1

print(lst1 is lst2)
text
True

None and boolean identity

None is a singleton in Python.

python
a = None
b = None

print(a is b)
text
True

This is why Python programmers write:

python
if value is None:
    print("No value")

Instead of:

python
if value == None:
    ...

Prefer is None — it is clear and idiomatic.

Strings and identity

Some identical strings may share memory through interning.

python
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

python
l1 = [1, 2]
l2 = [1, 2]
  • l1 == l2 → True
  • l1 is l2 → False

Dictionaries

python
d1 = {1: "A"}
d2 = {1: "A"}
  • d1 == d2 → True
  • d1 is d2 → False

Memory model

Alias shares one object; copy creates a new ID

Mental model

  • y = x → same object → is returns True
  • z = x.copy() → new object → is returns 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

python
if value is None:
    print("Missing")

Avoid

python
if value is 10:
    ...

Practice exercises

Exercise 1 — Predict the output:

python
lst1 = [10, 20]
lst2 = lst1

print(lst1 is lst2)

Answer: True

Exercise 2 — Predict:

python
{1: 2} is {1: 2}

Will it be True or False? (False — two separate dict literals.)

Exercise 3 — Compare both operators:

python
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

  1. What does is compare?
    The identity (memory address) of two objects.

  2. Is lst.copy() the same object?
    No. It creates a new object.

  3. Should you use is to compare integers like 256?
    No. Use ==. Reserve is mainly for None and genuine identity checks.

Summary

  • is and is not compare 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…