Python Identity Operators

In the last lesson you learned Membership Operators. Those check if a value sits inside a list, string, or other collection. Now we look at a different kind of check. Identity operators ask a deeper question. Are these two names pointing to the exact same thing in the computer’s memory? That sounds like asking if two values are equal. But it is not the same. And mixing them up causes a very common bug. It catches new and experienced Python writers alike.

🤔 Why Do We Need Identity Operators?

Here is the pain. You have two variables. You want to know if they are the same object. Not just two separate things that happen to look alike.

Think about real people for a second. Two people can both be named Alex. They can both be 30 years old. On paper they look equal. But they are still two different humans. Same idea in code. Two lists can hold the same numbers and still be two separate objects. They sit in two separate spots in memory.

The fix is the is operator. It tells you whether two names point to one single object. It does not check whether their contents match. This matters most when you check for None. You will do that all the time once you start writing real programs.

🧩 What “is” Actually Checks

Python keeps every object somewhere in memory. Each object has its own little address. Think of it like a house number on a street. When you write a = b, both names point to the same house.

The is operator asks one plain question. Do these two names point to the same address? If yes, you get True. If they point to two different objects, you get False. That stays False even when the objects look identical.

Here is the simplest way to see it. This code makes one list and points two names at it.

a = [1, 2, 3]
b = a # b points to the SAME list as a
print(a is b) # same object?

Output

True

We never made a second list. We just gave the one list a second name. So a and b are the same object. That is why is says True.

Now watch what happens when we build two separate lists that look the same.

a = [1, 2, 3]
b = [1, 2, 3] # a brand new list, built separately
print(a is b) # same object?
print(a == b) # same values?

Output

False
True

See the difference? a == b is True because both lists hold the same numbers. But a is b is False. They are two different objects in two different spots in memory. They are twins, not the same person.

⚖️ is vs == (The Part Everyone Mixes Up)

This is the heart of the whole lesson. So let us make it very clear.

  • == asks: do the values match? It compares contents.
  • is asks: is it the same object? It compares identity, the memory address.

This small table sums it up.

Operator Question it asks Compares
== Are the values equal? Contents
is Is it the same object? Identity (memory address)

Most of the time you actually want ==. When you compare two prices, two names, or two scores, you care about the values. So you reach for ==. The is operator is for the special case. You use it when you genuinely care whether something is one specific object.

Want to peek at the real memory address Python gives an object? You can use the built-in id() function. Same number means same object.

a = [1, 2, 3]
b = [1, 2, 3]
print(id(a) == id(b)) # this is exactly what `is` checks
print(a is b)

Output

False
False

So a is b is really just a friendly way of writing id(a) == id(b).

✅ The Main Job: Checking for None

This is the main place you will actually use is. In Python there is a special value called None. It means “nothing here yet” or “no value”. You will see it everywhere. A function that returns nothing gives back None. An empty setting starts as None. And so on.

There is only ever one None object in the whole program. Every name that holds “nothing” points to that same single object. That makes None a perfect match for is.

The clean, correct way to check for None looks like this.

result = None
if result is None:
print("No value yet")
else:
print("We have a value")

Output

No value yet

And to check the opposite, that something does have a value, you use is not.

name = "Riya"
if name is not None:
print(f"Hello, {name}")

Output

Hello, Riya

Read name is not None out loud. It says exactly what it means. “name is not nothing”. That readability is a big reason Python writers prefer is None and is not None over other ways of checking.

Tip

The official Python style guide says to always compare to None with is or is not. Never use == for this. So write x is None, not x == None. It is clearer. And it avoids surprises with custom objects that redefine what == means.

⚠️ Common Mistakes

A few traps catch people again and again. Here are the ones worth remembering.

Using is to compare values. This is the big one. Never use is to check if two numbers or two strings are equal. It sometimes works by accident, because Python reuses small objects. But it is not reliable and will cause bugs later.

a = 1000
b = 1000
# ❌ Avoid: using `is` to compare values
print(a is b) # the result is not guaranteed for large numbers, do not rely on it
# ✅ Good: use `==` for value comparison
print(a == b) # True, this is what you meant

Writing == None instead of is None. It often gives the same answer. But is None is the standard, clearer way.

value = None
# ❌ Avoid
if value == None:
pass
# ✅ Good
if value is None:
pass

Expecting two equal lists to be is. Building two lists with the same contents makes two separate objects. They are == but not is.

# ❌ Avoid: this is False, which surprises people
print([1, 2] is [1, 2])
# ✅ Good: compare their values
print([1, 2] == [1, 2])

✅ Best Practices

Keep these simple habits and you will rarely go wrong.

  • Reach for == by default. You almost always care about values, not identity.
  • Use is and is not mainly for None. That is their everyday job.
  • Always write is None and is not None, never == None.
  • Do not use is to compare numbers or strings. Use == for those.
  • Not sure whether you want identity or equality? You want equality. So pick ==.

🧩 What You’ve Learned

✅ The is operator checks if two names point to the same object in memory, not just equal values.

== compares values (contents), while is compares identity (the memory address).

✅ Two separate lists with the same items are == but not is, because they are different objects.

✅ The is not operator is the opposite of is, and it reads naturally in conditions.

✅ The main real-world use of is is checking for None: write x is None and x is not None.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does the `is` operator check?

    Why: `is` checks identity, meaning whether both names point to the exact same object in memory, not whether their values match.

  2. 2

    Given `a = [1, 2, 3]` and `b = [1, 2, 3]`, what do `a == b` and `a is b` return?

    Why: The lists hold the same values so `==` is True, but they are two separate objects so `is` is False.

  3. 3

    What is the recommended way to check if a variable holds None?

    Why: Python style guides say to always compare to None using `is`, so `if x is None` is the correct and clearest form.

  4. 4

    Why should you avoid using `is` to compare two numbers like `a is b`?

    Why: `is` checks if they are the same object, not if the values are equal, so for numbers you should use `==` instead.

🚀 What’s Next?

You now know how to tell “same object” apart from “same value”. And you know the safe way to check for None. Next we put these comparisons to work. We let your program make real decisions based on them.

if Statement

Share & Connect