Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Python also has a complete set of comparison operators. For example, if I say 5 < 6, I'll get True. If I say 6 < 5, I'll get False. Likewise, if I say 5 <= 6, I'll get True, or 5 <= 5, I'll also get True. It also has greater than or equal to, 6 >= 5 or 6 >= 6.
6 is not of course >= 7. And equality is tested with the double equal signs. So 5 == 5 is True. And 5 == 6 is not True. Then it has a not equal operator. So 6 != 7 is True. And 6 != 6 is False. It also has is and is not, and these are for testing id.
So for example, if I have two variables x and y, and we'll assign them values of 5 and 6. So the id(x) is that, and the id(y) is that. So they're not the same. So if I say x is y, I get False. If I say x is not y, I get True. Likewise, if I assign 5 to y, now their ids will be the same.
Id(y) is the same as id(x). Now x is y. For immutables like integers, the ids are always going to be the same if the value is the same. So testing equality and testing id is almost the same thing, but for immutables, like for example, if I have a list, I say x, y = a list with 5 and another list with 5, now they have different ids, id(x) is that and id(y), you can see that they have two different ids now, even know their values are the same.
So if I test x = y, it's True, but if I test x is y, it is now not True, because they have different ids. They're actually different objects. So those are the comparison operators in Python. You can compare values, and you can also compare ids with is and is not. So when would I want to compare ids? Because Python is fundamental an object-oriented language, everything in Python is an object. And because Python, of course, supports polymorphism there's going to be times when you're going to be using objects.
You may need to know is this exactly the same object as this other object over there? Which object is it that I'm dealing with? And you can't know which is by testing value. So this is a time when you might want to use is and is not to test for the identity of the object rather than just testing the value of the object. So those are Python's comparison operators. Of course, you can compare value, as well as comparing identity.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98821 Viewers
61 Video lessons · 86136 Viewers
71 Video lessons · 69994 Viewers
56 Video lessons · 102238 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.
Your file was successfully uploaded.