In this video, Kathyrn teaches you about the different comparison (also called relational) operators available in Python. These operators compare the values on either sides of them and determine whether the relation is true or false.
- [Instructor] Let's review some comparison operators. And so, a comparison operator, also called a relational operator, determines the equality or difference between values. The whole expression ultimately returns a Boolean value. So some of the stuff you may have heard of before, but basically comparison operators are kind of like greater than, equal to, or less than. Some examples of comparison operators are, as you see here, you know, five is equal to five, or five is less than ten. And this is the stuff we'll be using in our code.
So let's work with some comparison operators in code. And so, we could write a print statement that uses the less than operator, and we'll go ten is less than 75. And this whole thing will either return true or false depending on if ten is actually less than 75. So if we run it here, this is true because ten is less than 75. We could also check if 75 is less than ten. This is false, and so this whole print statement is going to return false. We could also add an if statement in here.
We could say if ten is less than 75, we'll go ahead and print out the bigger number is bigger because that's what's happening here. And so we have the bigger number is bigger. Now let's create some variables. We could have a variable called kitten, have it equal ten, which will represent its weight in pounds, and then we'll have tiger, which equals 75. Then we'll see if the kitten is less than the tiger, we'll go ahead and print out the kitten weighs less than the tiger because this is in fact true if kitten and tiger are representing their weight.
Running it, we see the statement ends up getting printed out because this statement is true. Now we can combine these comparison operators with the logical operators we learned in the last video. So we can create a new variable called mouse, and we'll set it equal to one, representing its weight. And then we could say if the mouse weighs less than the kitten and the mouse weighs less than the tiger, then we're going to go ahead and print out the mouse weighs the least because it is, in fact, you know, less than the weight of the kitten and less than the weight of the tiger.
And so running this, we see the mouse weighs the least because one is less than ten and 75. Again, this is a built-in function, so we can also apply it to other types of input that are not numbers. And so scrolling down here, we could apply it to a Boolean. And so we could say, print false is greater than true, and if we run this, it's going to return false. And that's because false evaluates to zero, true evaluates to one, so it's really like saying is zero greater than one? And that is not true, and so that is why we get false in the output.
We can also do this with strings. Say we write print Jennifer is greater than Jenny and run this, this is actually going to return false although Jennifer has more letters than Jenny, and that's because for this comparison operator it's looking for the first mismatched letter. And so here it's i and y, and then between these two letters, it's looking to see which has a greater value. And so in this case, since we're mapping it to one to 26, y maps to 25 and i maps to nine, and therefore nine is not greater than 25 and so this whole expression is false.
We can also do this with characters. We could say print a is less than or equal to b, and this will return true because a would map to one and b would map to two. And one is less than or equal to two. Comparison operators are very helpful if you need to know if one value is less than, equal to, or greater than another value in a piece of code.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content