Beginner Chapter 27 · 20 min read · Module 2: Operators and Expressions
Identity Operators
- After this chapter you can explain what object identity means in Python.
- You can use is and is not to compare identity.
- You can explain the difference between == and is with a clear example.
- You can check for None in the correct way.
- You can use id() to see that two names share one object.
Every value in Python is an object, and every object lives at one place in memory. Sometimes two names point to the same object, and sometimes they point to two separate objects that only look alike. The identity operators let you ask exactly that question.
There are two identity operators: is and is not. They do not compare what the objects contain. They compare whether the objects are the same one. This is a small idea, but it explains many surprising results that beginners meet when they copy lists or compare values.
In this chapter you will learn how identity differs from equality, how id() helps you see identity, and why the standard way to test for None uses is. You will also learn which results you should never depend on.
What is object identity?
When you write a = [1, 2, 3], Python creates a list object and the name a points to it. If you then write b = a, no new list is made. The name b simply points to the same list. Both names are two labels on one object.
Identity means being the very same object. Two objects can be equal in content and still be different objects, just like two printed copies of one book have the same words but are two separate books.
The is and is not operators
The expression x is y gives True when x and y refer to the same object, and False otherwise. The expression x is not y gives the opposite answer. Both return a boolean value.
| Operator | Meaning | Example |
|---|---|---|
| is | same object | a is b |
| is not | different objects | a is not b |
| == | equal values | a == b |
| != | different values | a != b |
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)
print(a is c)
print(a == c)
print(a is not c)In the example above, b is another name for the list that a points to, so a is b is True. The list c was created separately, so a is c is False, even though a == c is True because the contents match.
Seeing identity with id()
The built-in function id() returns a number that identifies an object while it exists. Two names that refer to the same object always give the same id(). The exact number changes from run to run, so you should compare ids and never print them in programs that need a fixed output.
a = [10, 20]
b = a
c = list(a)
print(id(a) == id(b))
print(id(a) == id(c))
b.append(30)
print(a)
print(c)Because a and b are one object, adding an item through b also changes what a shows. The list c is a separate copy, so it stays the same. This is the reason identity matters: changes to a shared mutable object are visible through every name.
Checking for None
None is a special value that means no value. There is only one None object in a running program. Because of that, the recommended way to test for it is is None or is not None. This is clearer than == None, and it cannot be fooled by a class that changes how == works.
def find_index(items, target):
for i, item in enumerate(items):
if item == target:
return i
return None
result = find_index(['a', 'b', 'c'], 'z')
if result is None:
print('not found')
else:
print('found at', result)
print(find_index(['a', 'b', 'c'], 'a') is not None)Notice that the function can return the number 0, which is a valid position but also counts as false in a condition. If you wrote if not result, position 0 would be treated as missing. Using is None avoids that bug.
Mutable and immutable objects
Identity is most important for mutable objects such as lists, dictionaries and sets, because they can change after creation. For immutable objects such as numbers, strings and tuples, sharing one object is harmless because nobody can change it.
Python may reuse the same object for some small numbers and some short strings to save memory. This is an implementation detail. It can differ between Python versions and between ways of creating the value. For that reason, never use is to compare numbers or strings. Use == for values.
== to ask whether two values are equal, and use is only for None, True, False and when you truly care that two names share one object.Why identity matters when copying
Assigning a list to a new name does not copy it. To make an independent copy you can use list(a), a.copy() or slicing with a[:]. After a copy, the new list has a different identity but equal contents. Nested lists need extra care, because a shallow copy still shares the inner lists, which you will study in later chapters.
Walking through the checks
When Python evaluates a is b it does not look inside the objects at all. It only asks whether both names lead to the same object, so the check is very fast and does not depend on how large the objects are. By contrast, a == b on two big lists must compare items one by one. That speed is one reason is is used for the single object None.
You can also combine identity checks with other operators. For example, x is not None and x > 0 first makes sure the value exists and only then compares it. The and operator stops early, so the second part never runs when x is None, and you avoid an error.
Python syntax
x is yx is not yid(obj)if value is None:
...Real-world applications
- Checking whether a function returned None to signal that nothing was found.
- Detecting whether two variables share one mutable list before changing it.
- Using sentinel objects as default markers in function arguments.
- Debugging unexpected changes caused by shared references.
Common mistakes
is to compare numbers or strings, such as if name is 'Asha'.Fix: Use
== for values. Identity of numbers and strings depends on the implementation and can give surprising results.if x == None instead of if x is None.Fix: Use
is None and is not None, which is the standard and safest style.b = a makes a copy of a list.Fix: It only creates a second name for the same list. Use
a.copy() or list(a) for a copy.id() and expecting it to be the same next time.Fix: The id changes between runs. Only compare ids inside one run.
Important tips
- Read
isin your head as 'is the very same object as'. - Use
is Nonefor missing values, but==for everything else. - When a list changes unexpectedly, check whether two names share it with
is. - Remember
is notis one operator;not x is yworks but reads worse.
Key points summary
isandis notcompare identity, not content.==compares values; two equal objects may still be different objects.- Assignment copies the reference, not the object.
Noneshould be tested withis None.id()gives an identity number that is valid only during one run.
Exam and interview points
- Identity operators return a boolean.
a is bis True whenid(a) == id(b).- Equal objects are not always identical, but identical objects are always equal for ordinary types.
- Compare with None using
is.
Quick revision
- is: same object; is not: different object.
- == compares values, is compares identity.
- b = a shares the object; a.copy() makes a new one.
- Use
is Noneto test for None. - Never rely on
isfor numbers and strings.
Frequently asked questions
What is the difference between == and is?
The operator == asks whether two objects have equal values. The operator is asks whether both names refer to the same object. Two separate lists with the same items are equal but not identical.
Why does my small-number comparison with is sometimes give True?
Python may reuse some small immutable objects to save memory. This is an implementation detail and you must not rely on it. Use == to compare numbers.
Can I use is with True and False?
Yes. There is only one True and one False object, so `x is True` works. However, in most code a plain `if x:` is simpler.
Does is not mean the same as not is?
`a is not b` is a single operator and is the normal style. Writing `not a is b` gives the same result but is harder to read.
Practise this chapter
21 practice questions Take the chapter quiz