You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Comparing Objects

00:00 I want to talk about the difference between the is operator and the equals equals (==) operator because I know that this is often a point of confusion for Python developers, and I’ve been trying to come up with an example that really boils it down to a minimal example necessary to understand this distinction.

00:22 And I think I got a pretty good shot at this, so bear with me. So, at the end of the video, you’re going to know the difference between is and ==, and you’re going to know when to apply them.

00:31 So, the key thing here is that there’s a difference between two things being identical—two objects being identical—and them being equal.

00:40 Like, there’s a semantic difference; there’s a difference in meaning. For example, when I grew up, our neighbors had twin cats, so there were two cats that looked pretty much exactly the same, but of course, they were two different cats. I mean, you wouldn’t say, like, “Oh, that’s the same cat,” right?

01:00 That would be crazy, because there’s two of them. And I’m going to keep stretching that analogy even thinner, so bear with me. All right, so they’re two different cats and they’re two different entities, but they look the same. Like, you could say, “Oh, they’re equal.” But really what this example shows is that there’s a difference between two things being equal and two things being identical, and that difference is really important if you want to understand the difference between is and the == operator. So, let’s start with the == operator first.

01:37 This guy here, it compares by checking for equality. So, if these cats were Python objects and we compared them with the == operator, we’d get, “Hey, these cats are equal!” as an answer because they have the same properties—they look the same. Now, on the other hand, the is operator compares identities.

01:58 So if we compared our seemingly identical cats, or identical-looking cats, with the is operator, then we’d get the answer that “Well, they’re two different cats,” right?

02:11 “They’re not the same cat. There’s two of them. Are you crazy?” Before I get all tangled up in this cat analogy, let’s actually come up with some Python code here.

02:20 So, what we’re going to do first is we’re going to create a new list object, just create this new list [1, 2, 3]. Then, we’re going to create another variable that points to the same list.

02:32 So now, these two variables, a and b, they’re pointing to the same list, so when we inspect a, we can see here, “Okay, this is the contents we put in earlier.” And if we inspect b, then we get the same result. Now, at this point, we know that they kind of look the same, right?

02:52 So if we compare them with the == operator, we get True as the answer because, well, they look the same. They have seemingly the same content.

03:04 Now what that doesn’t tell us, however, is whether or not a and b are actually the same object. I mean, in this case, we know because we created them and we assigned b to point to the same object that a points to—

03:23 but suppose we didn’t know, right? Like, how could we find out? And this is where the is operator comes in. So, if we went a is b, then in this case, Python would tell us, “Yeah, they’re the same object. They are the same thing.” And that would mean if I go in and change a—by let’s say, putting in a string—if I change a, that would also change b because they’re pointing to the same underlying object, right? And so now, I’m actually going to undo this and just create a new list here and then replay everything I did before. So now, I undid this change here where I added this string.

04:07 So, what we’re going to do now is we’re going to create a new copy of this list that we’re going to call c. And the way we can do that is just by calling the list() function, the built-in list() function on a again. What that’s going to do is it’s going to create a shallow copy that will look exactly the same.

04:29 So, let’s take a look at a again, look at b again, and then look at c. And you can see here, they all look exactly the same, right?

04:37 If you’re just looking at their contents, they look exactly the same.

04:42 Now, this is where it gets interesting, right? Because when I go and compare a to c, then we get the answer, “Okay, they look identical.

04:53 They are considered equal.” Now, if I go and do a is c, then we get a different result, because Python tells us, “Hey, they’re not the same object. They might look identical, but they’re not the same fricking cats.

05:06 They’re two different cats that just look the same.” Right? They both have black fur and the same eye color and whatnot, but they’re two different cats. And really, this is the key distinction between == and the is operator,

05:23 because the is operator, or an is expression—it evaluates to True if two variables point to the same identical object. And the == operator, or an == expression, evaluates to True if both objects are equal, or have the same contents.

05:42 But they’re not necessarily the same object—like, we don’t care about that. All right. So, I hope this clarified this difference to you. Just think of cats.

05:52 Every time you’re going to have to make a decision between == and is just think of twin cats. I mean, think of cats all day! You can also think of dogs—it works the same way with dogs.

06:01 But that’s really the key distinction you need to remember.

You must own this product to join the conversation.