What Are Identity Operators in Python? Full Guide with Code
Ever compared two things in Python and ended up surprised?
“Wait… a == b is True, but a is b is False?”
You’re not alone. This little twist trips up a lot of Python learners. The key lies in identity operators — and understanding how they work can make a big difference in how you write and debug your code.
Most folks are familiar with ==, which checks if two values are equal. But identity operators — like is — go a step further. They check whether two variables point to the exact same object in memory, not just if they look the same.
In this guide, you’ll explore:
== vs. isBy the end, you’ll spot the difference between is and == like a pro — no more surprises.
In Python, we use identity operators to check whether two variables are actually the same object in memory.
Let’s break that down.
You already know that == checks if values are the same.
But identity operators check if two things are actually the same exact object — not just equal, but pointing to the same spot in your computer’s memory.
Python gives us two identity operators:
is → This returns True if both variables refer to the same object.is not → This returns True if the variables refer to different objects.Here’s a simple example to help make sense of it:
a = [1, 2, 3]
b = a # b points to the same list as a
c = [1, 2, 3] # c is a different list with the same values
print(a == b) # True – same values
print(a is b) # True – same object
print(a == c) # True – values are the same
print(a is c) # False – different objects
So even though a and c look the same on the outside, Python sees them as two separate objects. That’s why a is c is False.
But a and b? They point to the exact same object — so a is b is True.
let me explain identity operators using a real-world example you can easily picture.
Let’s say you and I are sitting at a table. There are two cups of tea.
You take one cup of tea and give it two names:
Now, if we ask:
In Python, this is like:
a = [1, 2, 3]
b = a
print(a == b) # True – same tea (same values)
print(a is b) # True – same cup (same object)
Now imagine you pour the same kind of tea into two separate cups — Cup C and Cup D.
They look the same. They taste the same.
But… they are two different cups.
In Python, this is like:
c = [1, 2, 3]
d = [1, 2, 3]
print(c == d) # True – same tea (same values)
print(c is d) # False – different cups (different objects)
is Really Do?== asks: “Do these cups have the same tea?” (same values)is asks: “Are these the same cup?” (same object)And that’s exactly what identity operators check — whether two things are not just equal, but identical (pointing to the same thing in memory).
is not with Tea Cups So far, we’ve seen:
== checks if the tea tastes the same (same value).is checks if it’s the same cup (same object).Now, let’s talk about is not.
You have Cup A and Cup C.
If we ask:
So in Python, you’d write:
a = [1, 2, 3]
c = [1, 2, 3]
print(a == c) # True – same tea (same values)
print(a is not c) # True – different cups (different objects)
a is not c is True because they are not the same object — even though they look the same.
is → True if both variables point to the same cupis not → True if they are different cups, even if they have the same teaThis helps Python understand whether two variables are literally the same thing, or just look alike.
We already finished explaining is and is not using the tea cup story.
Now, let’s move forward with how identity works with small numbers — a very common thing that surprises beginners.
You may think that when two variables have the same value, Python always treats them as the same thing. But that’s not always true.
Try this:
a = 100
b = 100
print(a == b) # True – values are the same
print(a is b) # True – same object in memory
a == b is True — they both have the value 100a is b is also True — they point to the same memory
That’s because Python saves memory by reusing numbers between -5 and 256. These are used a lot, so Python keeps them ready and shares them across variables.
But Now Look at This:
x = 1000
y = 1000
print(x == y) # True
print(x is y) # False
x == y is True — they both have value 1000x is y is False — because now Python gave them separate memory locations
So even though they look the same, they are not the same object in memory.
| What you’re asking | Use this | What it means |
|---|---|---|
| Do they have same value? | == | Like “Do both cups have same tea?” |
| Are they the same object? | is | Like “Is it the same cup, not just same tea?” |
let’s now talk about how Python treats strings when we use is and ==.
Let’s start with a basic example:
a = "hello"
b = "hello"
print(a == b) # True
print(a is b) # True
Looks good, right? Both a and b have the same value ("hello"), and is is also True.
So does that mean strings always behave like numbers? Not exactly.
Python tries to save memory by reusing some strings — especially short ones made up of letters, numbers, and underscores.
That’s why "hello" and "hello" might point to the same memory location.
But… watch what happens when we build a string in a different way:
x = "py" + "thon"
y = "python"
print(x == y) # True
print(x is y) # It might be True or False
This is tricky. Sometimes Python will optimize this and reuse the same object. Other times, it won’t — especially if the strings are created dynamically (e.g. from user input, or by combining variables).
Another Example with Dynamic Strings:
a = "".join(["h", "e", "l", "l", "o"])
b = "hello"
print(a == b) # True – same value
print(a is b) # False – different memory
So:
== checks the value: same teais checks the object: different cupsEven though they look the same, Python made two separate string objects.
== to compare string content.== and is?Let’s start by understanding what happens when we use == and is with lists. I’ll explain each one clearly, step by step.
== to Compare Lists (Checking if Their Values are the Same)The == operator compares the values inside the lists. If the lists have exactly the same values, it returns True, even if they are different objects in memory.
Let’s look at an example:
a = [1, 2, 3] # Create a list with the values 1, 2, 3
b = [1, 2, 3] # Create another list with the same values
print(a == b) # True – the values are the same
In this case:
a and b both have the same values: [1, 2, 3].==? True, because the values match.is to Compare Lists (Checking if They Are the Same Object in Memory)The is operator checks whether two variables are pointing to the same object in memory. It doesn’t care about their values. It only cares if both variables are pointing to the same list in memory.
Let’s see it in action:
a = [1, 2, 3] # Create a list with the values 1, 2, 3
b = [1, 2, 3] # Create another list with the same values
print(a is b) # False – they are two different objects in memory
Why is this False?
a and b look the same, they are two separate objects in memory.a and b are not the same object, even though they contain the same values.Imagine you have two identical books. They both have the same content, but they are physically different books. Even though they are identical, they are not the same book in your collection. So, in Python:
== compares if the content of the books is the same.is compares if you are looking at the exact same book in your collection (same object in memory).is Important with Lists?You might wonder, “Why does it matter if two lists are the same object?”
It matters because sometimes you want to check if two variables are referring to the exact same object in memory.
For example, if you modify one list, you might want the other list to reflect that change. This would only happen if both variables were pointing to the same object.
Let’s look at this example:
x = [1, 2, 3]
y = x # y points to the same list as x
x.append(4) # Modify x by adding 4 to it
print(x) # [1, 2, 3, 4]
print(y) # [1, 2, 3, 4] – y changes too because it points to the same list as x
Since both x and y point to the same list, modifying x also modifies y.
Now, let’s try this with separate lists:
x = [1, 2, 3]
y = [1, 2, 3] # y is a new list, with the same values but a separate object
x.append(4)
print(x) # [1, 2, 3, 4]
print(y) # [1, 2, 3] – y doesn’t change because it’s a different list
Here, x and y are different objects, so modifying one doesn’t affect the other.
== checks if the values inside the lists are the same.is checks if two variables point to the same list in memory.Let’s explore when Python reuses objects and why this happens. We’ll focus on small integers and strings because Python has special rules for reusing them to save memory and speed up your code.
In Python, some values are reused to make your program run faster and use less memory. But, not all values are reused. Small integers and short strings are special cases. Let’s explore this with a few simple examples.
Python does not create new objects for small integers and short strings every time. Instead, it reuses them. This makes your program faster and saves memory because Python doesn’t have to keep creating the same objects over and over again.
Think of it like this:
Python does something similar with small integers and short strings.
Python reuses small integers between -5 and 256. Let’s see how:
a = 100
b = 100
print(a is b) # True
a and b are both assigned the value 100.100 is a small integer and reuses the same object in memory.a and b point to the same place in memory, and a is b is True.Let’s test a larger number:
x = 1000
y = 1000
print(x is y) # False
Why is False?
1000 is greater than 256, so Python doesn’t reuse it.x and y, and they do not point to the same memory location.x is y is False.Python also reuses short strings. Let’s see an example:
a = "hello"
b = "hello"
print(a is b) # True
"hello" is a short string, and Python reuses it.a and b point to the same memory location for the string "hello".a is b is True.Let’s try a new string built with code:
x = "py" + "thon"
y = "python"
print(x == y) # True
print(x is y) # False
Why is x is y False?
"python" is the same in both x and y, Python does not reuse the dynamically created string "py" + "thon".x and y are separate objects in memory, so x is y is False.-5 to 256) to make your code more efficient."hello") for the same reason.is, you might get True because both variables are pointing to the same object in memory.is will return False.If you compare small integers and short strings like this:
a = 42
b = 42
print(a is b) # True
str1 = "apple"
str2 = "apple"
print(str1 is str2) # True
Python will reuse both the integer 42 and the string "apple", so a is b and str1 is str2 will both be True.
But if you use larger numbers or dynamic strings, you’ll see a different result.
is to Compare Strings or NumbersOne of the most common mistakes is using the is operator to compare values like strings or numbers. Remember, is checks if two variables point to the same memory location, not if the values are equal.
Let’s look at an example:
a = 1000
b = 1000
print(a is b) # This is False!
You might expect this to be True because the values are the same, but it’s actually False. This is because 1000 is not a small integer (it’s larger than 256), so Python does not reuse it. Each variable gets its own memory location, so a and b don’t point to the same memory location, and a is b returns False.
Instead, use == to compare values. The == operator checks if the values are equal, not their memory location:
a = 1000
b = 1000
print(a == b) # This is True! (because the values are equal)
is is used to compare object identity (whether two variables refer to the same object in memory).== is used to compare values (whether the values in the variables are the same).is only when you care about whether two variables point to the same object (e.g., for singletons like None).is to Compare Object Identity CorrectlyNow, here’s an example where using is is perfectly fine.
a = None
b = None
print(a is b) # True
is operator works because None is a singleton in Python, meaning there is only one instance of None in memory. Therefore, both a and b point to the same object in memory.is correctly: None (since None is unique in memory).True and False, in some specific cases.Here’s a dangerous pitfall that many developers might not realize:
status = input("Enter status: ")
if status is "done": # Problem!
print("Task complete!")
status is "done" checks if status is the same object in memory as the string "done"."done" in your program may not be the same object as the string "done" in memory, leading to unexpected results.To compare string values, use == instead:
status = input("Enter status: ")
if status == "done": # Correct
print("Task complete!")
Now, Python will correctly compare the values of status and "done", not whether they are the same object in memory. This is the reliable way to compare strings.
is to compare string or number values. Use == instead to compare the values.is when checking if two variables refer to the same object (for singletons like None).is with strings that come from user input or other dynamic sources — always use ==.is Actually Makes SenseNow that you know using is to compare values like numbers or strings isn’t always safe, let’s talk about when it is the right tool.
NoneHere’s an example:
my_var = None
if my_var is None:
print("Nothing to see here.")
This is exactly how you should use the is operator.
In Python, None is special. It’s what we call a singleton — which means there is only one instance of None that exists in memory. Every time you use None, Python gives you the exact same object.
So when you write:
if my_var is None:
You’re asking:
“Is my_var pointing to the actual None object in memory?”
Which is exactly what you want to know.
== HereTechnically, this would still work:
if my_var == None:
But this isn’t the best practice. Why?
Because:
== checks value, not identity.==, and it could behave unexpectedly.is None makes your intent very clear: you’re checking for the exact None object.Python developers and style guides (like PEP 8) recommend using is when checking for None.
is None or is not NoneIt’s clean, safe, and the way Python is meant to be written.
Examples:
if result is None:
print("No data returned.")
if user_input is not None:
print("User typed something.")
So in short — this is one of the best places to use is in your code!
id() to See the Object’s Memory AddressWhen you’re not sure whether two variables are pointing to the same object in memory, Python gives you a built-in tool: id().
Here’s a short example:
a = [1, 2]
b = a
print(id(a)) # Something like 140412345678888
print(id(b)) # Exactly the same as a
In this case, both a and b are pointing to the same list, so their IDs will be exactly the same.
id() Do?id() gives you the identity of an object — basically, where it lives in memory.ids are different, they’re separate objects — even if they hold the same values.Let’s say you’re debugging your code and you’re not sure if two variables are copies of the same object or just look similar. id() helps you confirm what’s really happening under the hood.
Here’s another example:
x = [3, 4]
y = [3, 4]
print(id(x)) # Different
print(id(y)) # Different
print(x == y) # True (values are equal)
print(x is y) # False (not the same object)
Even though x and y have the same content, they are different objects — and id() proves it.
id() when you want to see the actual memory location of an object.is works behind the scenes.Look at each code snippet below and guess whether the output will be True or False.
Don’t scroll too fast — try to think it through first!
True
Python reuses short strings to save memory. So "hello" is interned, and both a and b point to the same string object.
False
Even though x and y hold the same values, they’re two separate list objects. Their id() values will be different, so x is y is False.
True
This is the perfect use of is. There’s only one None object in Python, so result is None is True.
Understanding the difference between == and is can save you from some really confusing bugs — especially when you’re working with lists, strings, or None.
Here’s what you’ve learned:
== when you care about value equality — whether two things look the same.is when you care about identity — whether two things are actually the same object in memory.is — Python might reuse them sometimes, but you shouldn’t rely on that.None, always use is — it’s the Pythonic way.id() can help you peek under the hood and see what’s really going on.If you’ve followed along, you’re now one big step closer to writing cleaner, more reliable Python code.
Next time you’re debugging, just pause and ask:
“Am I checking if two things are equal… or if they’re the exact same thing?”
That small question can save you hours later.
Thanks for reading — and if this helped you, feel free to share it with a friend or check out more tutorials at emitechlogic.com.
is instead of ==?Use is when you want to check if two variables point to the exact same object (like None). Use == when you want to check if their values are the same.
a == b return True but a is b return False?Because == checks value equality, while is checks object identity. They can look the same but still be different objects.
is to compare strings or numbers?It’s not safe. Python may reuse some strings and numbers, but not always. Always use == for comparing values of strings or numbers.
id() function?id() shows the memory address of an object. It’s useful when you want to check if two variables refer to the same object.
Python Documentation – Boolean Operations
Official Python docs explaining how and, or, and not work in expressions.
Python Documentation – Comparisons and Identity
Details about comparison operators, including is, is not, and how they’re evaluated.
After debugging production systems that process millions of records daily and optimizing research pipelines that…
The landscape of Business Intelligence (BI) is undergoing a fundamental transformation, moving beyond its historical…
The convergence of artificial intelligence and robotics marks a turning point in human history. Machines…
The journey from simple perceptrons to systems that generate images and write code took 70…
In 1973, the British government asked physicist James Lighthill to review progress in artificial intelligence…
Expert systems came before neural networks. They worked by storing knowledge from human experts as…
This website uses cookies.