Check out a free preview of the full Python Fundamentals course

The "Comparisons" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces equality and identity.


Transcript from the "Comparisons" Lesson

>> Nina Zakharenko: In Python, you can do comparisons. You can compare items of the same type. Someone in chat asked about comparing items of different types for the built-ins. That doesn't necessarily make sense. I'm not sure I can think of a case where I would find that useful. Once you kind of graduate and move on to writing your own advanced Python programs, you can create your own objects and define how you want them to be compared with each other.

But you can't compare different types in Python 3, it just doesn't make sense. Okay, so for our comparisons, we have a less than operator, a greater than operator, so just kinda like your simple math. We have a less than or equals and a greater than or equals. And so these comparisons themselves, they return a true or a false value.

>> Nina Zakharenko: There's a little bit of weirdness when you try to compare strings. The string comparison, they're compared lexicographically. That means by the ASCII value, the underlying ASCII value of the character.
>> Nina Zakharenko: You don't really need to know much about ASCII besides for capital letters come before lower case ones.

So you'll just have to remember that in your sort order. When you try to compare two strings, each character is checked one by one until a difference is found. And then, that difference is returned. And that kind of determines the order under the hood. So just remember that uppercase letters are lower-valued in ASCII.

Okay, so is "t" < "T", it's not. Just something to be aware of, but then once you get past the lowercase uppercase, things kind of work as expected
>> Nina Zakharenko: Okay, so equality. Equality is do the values and these two, do these two values have the same values within them?

So for an integer, that's pretty simple, right, is one equal to one? Yes, it is. Is this string equal to this one? Yes, it is. It gets a little bit more complicated when you're looking at more advanced container types, right, so these things are not equal to each other.

>> Nina Zakharenko: But these are. If you have two variables that you kinda created independently. You can check and see if they're equal to each other. And then, not equal is just the inverse of that. And you do that with a !=. So this will be false because a and b do in fact contain equal values.

>> Nina Zakharenko: Okay, someone mentioned earlier, they saw that I used the is keyword to check if something was none. And so is means identity. And it kind of frequently trips up Python beginners, so you wanna make sure that equality, is double equals, and not equals. And then identity is the is keyword.

>> Nina Zakharenko: As a beginner, besides for comparing non-true or false, kind of those built in singletons. You're not gonna be using the is keyword very often. Because that QR test if the two compared objects are stored at the same memory location, if they point to the same thing. I'm not gonna go into too much detail why about this.

Just remember to use.
>> Nina Zakharenko: Let's say x = None. Checking for the is keyword when trying to find out is this variable True, False, None. If we look at our list again, I created them in two separate instances. If I ask, a is b. I know a is equal to b, there is equality.

If I ask, a is b, what do you think I'll get?
>> Class: False.
>> Nina Zakharenko: False, they're not stored at the same memory location, even though they are equal to each other. So just an important distinction to remember. And there's also the not modifier. So if I'm checking if, let's see.

Let's say, a = None. If I want to know if a = None, I can check that. If I want the opposite of this, remember with equality it was the exclamation point and equals, so the not of is, is not. a is not None, well, that's False because it's None, so just remember this order.

The good thing I like about Python is it's really readable and almost sounds like English, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now