Locked lesson.
About this lesson
We'll look at the identity operators "Is", and "Is Not".
Exercise files
Download this lesson’s related exercise files.
Identity Operators .docx59 KB Identity Operators - Solution.docx
59 KB
Quick reference
Identity Operators
Use Is and Is Not to compare identities.
When to use
When you need to compare identities, use Is and Is Not.
Instructions
Is and Is Not allow us to compare identities. It is similar to == and !=
variable is variable #returns true if the identities are the same, false if not
variable is not variable #returns true if the identities are not the same, false if they are
Hints & tips
- is and is not compare identities
- 00:04 In this video I want to look at identity operators, specifically is and is not.
- 00:09 So we've already looked at the comparison operator to do equal to which is double
- 00:13 equal to.
- 00:14 So for instance if we have "John" equal to "John"
- 00:19 if we print this whole thing out we run this,
- 00:23 it's going to be true because John equals John.
- 00:28 So we've already sort of established this.
- 00:30 And we can also do not equal to as well.
- 00:33 But there's another way to do this as well, and that's the is and
- 00:36 is not identity operators.
- 00:38 So let's just walk through those here, I'll show you.
- 00:40 Let's create a variable called name and we'll call it John and
- 00:43 then let's do name_2, let's say that's also John.
- 00:47 So behind the scenes, Python will convert these strings into a unique identifier,
- 00:54 a unique identity number, right, a big long integer.
- 00:59 And we can see what that identity number is,
- 01:02 what that identifier is, by typing in id and then the variable name.
- 01:07 Now if we want to print this to the screen like everything else,
- 01:10 we just wrap it in a print function here.
- 01:12 And we get this whole big number.
- 01:14 And every time you run this you're going to get a different number,
- 01:17 because every time it's saving it to a different space in memory.
- 01:20 But we can take this statement and do another one with our second variable.
- 01:26 And you can see, if you compare these,
- 01:28 they are exactly the same because the strings are exactly the same.
- 01:32 If we run it again we'll get two different ones but they are still the same.
- 01:36 Well if we change our variable, our second variable, let's say Elder,
- 01:40 my name is John Elder.
- 01:41 If we run this it's mostly the same up until about right here in the last
- 01:46 three digits, 760, last three digits 856.
- 01:49 So we have a different identity number, a different memory space where
- 01:52 these are being stored because they're different things.
- 01:55 So we can actually compare these two identity numbers using identity operators,
- 02:00 specifically is and is not.
- 02:02 Let's go ahead and do that, first let's print it and
- 02:08 let's go name is, name_2.
- 02:11 And this is just like saying equal to, with our comparison operator, but
- 02:15 instead of saying equal to, we're saying is, because we're comparing identities.
- 02:20 So if we save this and run it, we see false, because John and
- 02:24 Elder are not the same identity numbers.
- 02:27 We can also do the opposite, is not, we save this, true.
- 02:31 So if we change this back to John, save it.
- 02:35 Now it's going to be false because name is not, this is false because they are true.
- 02:40 We can test for trueness, is name equal to name_2?
- 02:44 Name is name_2, put that question mark at the end, name is name_2,
- 02:48 like you're asking a question.
- 02:50 Save this, run it, true.
- 02:53 So we can do the same thing with an if statement.
- 02:56 We can say if (name is name_2): print ("They're the same!").
- 03:03 Save this and run it, we're going to see they're the same.
- 03:07 So those are identity operators.
- 03:08 Sometimes you're going to want to use just regular double equal to or not equal to.
- 03:14 Other times you're going to want to test for that long integer,
- 03:17 unique identity number in that case you use is or is not.
- 03:21 So that's identity operators.
- 03:22 In the next video we're going to look at while loops.
Lesson notes are only available for subscribers.