Execute Program

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:
    TruePass Icon
  • 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:
    TruePass Icon
  • 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 is operator 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_2
    Result:
    TruePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    list_1 is list_2
    Result:
    FalsePass Icon
  • If we modify one list, the other list is unaffected.

  • >
    list_1 = [1, 2]
    list_2 = [1, 2]
    list_1.append(3)
    list_2
    Result:
    [1, 2]Pass Icon
  • 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_1
    Result:
    TruePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    list_1 is list_1
    Result:
    TruePass Icon
  • 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_2
    Result:
    TruePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    list_1 is list_2
    Result:
    TruePass Icon
  • If we modify list_1, the changes show up in list_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_1
    list_1.append(3)
    list_2
    Result:
    [1, 2, 3]Pass Icon
  • You might wonder whether there should be a fourth case: two variables that are identical (via is) but not equal via (==). But if x is y, then we know that both variables refer to the same value at the same location in memory. Values are equal to themselves, so x is y normally means that x == 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 in to decide whether a value is in a list, and we also saw its inverse not in. A similar pattern applies here: x is not y is equivalent to not (x is y).

  • >
    x = [1, 2]
    y = [1, 2]
    x is not y
    Result:
    TruePass Icon
  • >
    x = [1, 2]
    y = x
    x is not y
    Result:
    FalsePass Icon
  • There's one important caveat for is: its behavior with strings and integers can be strange. Sometimes two equal strings are identical via is, 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 use is on strings or numbers.