Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In Python, and especially in Python 3, everything is an object, and an object has identity. The identity of an object is a unique ID, a unique number, which is specific to that particular object and no other object. This can be an important consideration when we are considering things like immutable objects. And so Python has an ID function, a built-in function, for finding the identity of an object. This is the Python Shell.
It's Idle, which is the graphical interface to the Python Shell in graphical environments, like Windows and Mac, and some forms of Linux, they have a graphical shell. You will have this graphical shell for a Python included with your Python distribution. In other environments that don't have a graphical shell, you will have something the Python command-line interpreter, which if you just type Python by itself, you will get this same interface. So we are going to start here by creating an object. I am going to call it 'x', and I am going to give it a value, 42.
Now when I type x by itself, I'll get the value 42, and if I type ID of x, I get that object's ID. So this is the ID of the immutable object, which is the integer 42. If I type, type of x, I see that its class is int., and so the x itself is simply a reference to the object, which is of type int and has the value 42, it has the ID that is this 505409528.
In fact, if I just type ID of 42, I get exactly that same ID. And if I type, type of 42, I get class int. So the number 42 is an object, and that object has ID. It has type. It has value, just like any other object. So if I create another variable and call it 'y', and assign it the value 42, the ID of y is going to be exactly the same.
Of course, when I test for quality x==y, I'll get true, but I can also test them for being exactly the same object. And that's what that 'is' operator, I can say x is y and they are exactly the same object, because they have the same ID. The 'is' operator simply compares the IDs, rather than comparing the values. The double equal sign, which is this one here, compares the values to see if the values are the same. The 'is' operator compares the IDs to see if they refer to exactly the same object.
So let's say that I have a mutable dictionary, and I say x = dict, and I'll just give it (x = 42) and, so I now have a dictionary object, and I say it type of x and it's a dictionary object, I type x, and I get that which is the dictionary object and I type ID of x, and it is this. Now, if I create another dictionary object, and I say y = dict, and I say (x = 42), I give it exactly the same contents, and I say ID of y, I have a different ID, and this is because a dictionary object is a mutable object, and so there is no reason - in fact it would make life more difficult if by simply assigning the same content to a dictionary Python were to give me exactly the same object.
X and Y are still references that point to objects, but these two objects, even though they have exactly the same value, if I say x, I get that, if I say y get that. Those are exactly the same value. These are different objects. So I can test for x == y and I get True. If test for x is y, I get False, because it's pointing to two different objects. So these can become important distinctions, and we will see some examples as we go into our examples of working code in Python.
We will see some examples of where you can use something like this. But at this point, what's important for you to understand is what ID is. That ID is a unique ID that refers to a specific object, and how to test for the quality of ID with 'is' operator to see if two different variables - and remember variables are referenced objects - to see if two different variables refer to exactly the same object.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104500 Viewers
56 Video lessons · 116419 Viewers
71 Video lessons · 85628 Viewers
131 Video lessons · 40988 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.