HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python is and is not Operators

9 min to complete · By Martin Breuss

Python offers the operators is and is not to compare the memory location of two values. These operators are called the identity operators and both evaluate, yet again, to Boolean values.

Now, what does it mean to compare the memory location of two values? To better understand this, you'll take a peek below the comfortable cover of abstraction that Python lays over a computer's internal operations.

How do Identity Operators Work

You've heard about different data types in Python and you got to know int, float, str, and bool already. You learned that these are different from each other in the sense that you can do different things with and to each of the data types. However, by the nature of how computers operate on the basis of 0 and 1, they are all the same at some level deeper down. You just need to dig deep enough.

Now, Python has another level of abstraction where these data types are conceptually the same. All of them are objects. You will keep hearing this sentence over again, and it might take a while until it clicks:

In Python, everything is an object.

That's right, every int is an object, and every str is an object as well. And if you go one step further, you'll see that every new variable you define, even if it has the same value as another variable, is a new object.

Such an object is a pretty abstract thing, but for now, you can think of it as an item in a particular memory location. It is the digital representation of what takes up some space on your physical computer memory. Pretty cool, huh?

Comparison Operator Example

Different objects can have the same values. You've learned how to compare two values with each other in the lesson on comparison operators. Here, you can recap and dive deeper:

num_1 = 123456
num_2 = 123456

num_1 == num_2  # True

In this example, you see two integers, num_1 and num_2, that both have the same value 123456. When you use the comparison operator == to compare their values, Python confirms that these are the same.

Identity Operator Example

The identity operator, on the other hand, doesn't compare the values of two objects but looks at whether they are both found in the same memory location. That means you're asking Python whether these two variables are the exact same object:

num_1 = 123456
num_2 = 123456

num_1 is num_2  # False

Even though num_1 and num_2 are both integers with the exact same values, they are different objects in memory. This is what you can check with Python's identity operator.

Types of Identity Operators

There are two versions of the identity operator:

  • is: Returns True if the variables on the left and right side of is point to the same object
  • is not: Returns True if the variables on the left and right side of is not do not point to the same object

There are only these two versions of the identity operator in Python. As you can see, one of them is a negation of the other one. Two objects either do point to the exact same memory location, or they do not.

Python Is Operator Practice

  • Open up your Python REPL and try this out. Use higher numbers and longer strings for the expected results. Don't use the online playground for this exercise.
Colorful illustration of a light bulb

Remember: The comparison operator == checks for the same values, and the identity operator is checks for whether they are the same object in memory.

Identity Confusion: Python Caches Small Immutable Objects

There's a little catch here that might trip you up when playing around with this in your Python interpreter. For performance reasons, Python caches small numbers and strings. This might run you into a head-scratch-situation if you've never heard about it before:

num_1 = 5
num_2 = 5

num_1 == num_2  # As expected, this returns True
num_1 is num_2  # Surprisingly this is True too!!

You'd expect the second check to evaluate to False as it did when using the larger numbers further up in the example. However, due to caching, it doesn't.

In the case of small immutable objects, Python cuts some corners and saves time by not creating a new object but only referencing one that already exists. That's unintuitive when you just thought you understood Python's object identity. But equipped with this little nudge, you'll be fine!

You don't have to dive deeper than this, but if you want to, you can read more about it in the resources below.

Colorful illustration of a light bulb

Additional Resources

This final operator, paired with a surprising behavior of Python, wraps up the list of Python operators described in this course section.

But there is one more thing to discuss, which is operator precedence, or how to know what gets evaluated first when you have a lot of them in one line of Python code.

Summary: Python is Operator

  • Identity operators look at whether objects are both found in the same memory location
  • Objects found in the same memory location are the exact same Python object
  • is returns False when comparing two different objects
  • is returns True when comparing two objects that are the same
  • Python does not create new objects for small immutable objects but only references one that already exists