Python for Programmers: Identity and Equality
Welcome to the Identity and Equality lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Python goes to great lengths to give us clear, intuitive equality behavior. For example, two lists are equal to each other if the values at each index are equal.
>
[1] + [2] == [1, 2]Result:
True
Dictionaries are equal when their keys and values match. We can compare them with
==, rather than iterating through their items and comparing them manually.>
{"name": "Amir"} == {"name": "Am" + "ir"}Result:
True
Those examples show the most common kind of equality: value equality. But values can also be equal in a second sense: they can have the same identity. "Identical" means "two variables refer to the same value, located at the same place in memory". We use the
isoperator to decide whether two values are identical.Here are three different cases to explore how identity works.
Case 1: we build two different lists that happen to hold equal values. The lists are equal to each other, but they're not identical! They're two distinct lists.
>
list_1 = [1, 2]list_2 = [1, 2]- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 == list_2Result:
True
- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 is list_2Result:
False
If we modify one list, the other list is unaffected.
>
list_1 = [1, 2]list_2 = [1, 2]list_1.append(3)list_2Result:
[1, 2]
Case 2: we build a single list,
list_1. It's equal to itself. It's also identical to itself.>
list_1 = [1, 2]- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 == list_1Result:
True
- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 is list_1Result:
True
Case 3: we build a single list again. Then we create a second variable that refers to the list, by doing
list_2 = list_1. That assignment doesn't create another list. It only creates a second variable referring to the existing list. Now,list_1 is list_2.>
list_1 = [1, 2]list_2 = list_1- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 == list_2Result:
True
- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 is list_2Result:
True
If we modify
list_1, the changes show up inlist_2, since they're actually the same list.- Note: this code example reuses elements (variables, etc.) defined in earlier examples.
>
list_1 = [1, 2]list_2 = list_1list_1.append(3)list_2Result:
[1, 2, 3]
You might wonder whether there should be a fourth case: two variables that are identical (via
is) but not equal via (==). But ifx is y, then we know that both variables refer to the same value at the same location in memory. Values are equal to themselves, sox is ynormally means thatx == y. (There are some rare exceptions where a value isn't equal to itself, but that's out of scope here.)In an earlier lesson, we used
into decide whether a value is in a list, and we also saw its inversenot in. A similar pattern applies here:x is not yis equivalent tonot (x is y).>
x = [1, 2]y = [1, 2]x is not yResult:
True
>
x = [1, 2]y = xx is not yResult:
False
There's one important caveat for
is: its behavior with strings and integers can be strange. Sometimes two equal strings are identical viais, but sometimes they're not. This happens due to implementation details of the Python runtime, which can change over time and are out of scope for this course. Fortunately, the workaround is easy: always compare strings and numbers with==. There's never a need to useison strings or numbers.