Comparison operators

Keyboard Shortcuts

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 calleda relational operator, determines the equalityor 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 likegreater 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 usesthe less than operator, and we'll goten is less than 75.And this whole thing will either return true or falsedepending on if ten is actually less than 75.So if we run it here, this is true becauseten is less than 75.We could also check if 75 is less than ten.This is false, and so this whole print statementis 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 aheadand print out the bigger number is biggerbecause 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 representits 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 outthe kitten weighs less than the tigerbecause this is in fact true if kitten and tigerare representing their weight.

Running it, we see the statement ends up gettingprinted out because this statement is true.Now we can combine these comparison operatorswith 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 lessthan the kitten and the mouse weighs less than the tiger,then we're going to go ahead and print outthe mouse weighs the leastbecause it is, in fact, you know, less than the weightof the kitten and less than the weight of the tiger.

And so running this, we see the mouse weighs the leastbecause one is less than ten and 75.Again, this is a built-in function, so we can also apply itto 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 sayingis zero greater than one?And that is not true, and so that is why we getfalse in the output.

We can also do this with strings.Say we write print Jennifer is greater than Jennyand run this, this is actually going to return falsealthough Jennifer has more letters than Jenny,and that's because for this comparison operatorit's looking for the first mismatched letter.And so here it's i and y, and then between thesetwo 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 25and 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 oneand b would map to two.And one is less than or equal to two.Comparison operators are very helpful if you need to knowif one value is less than, equal to, or greater thananother value in a piece of code.

Resume Transcript Auto-Scroll

Author

Released

2/15/2017

Python libraries can cut your development time and reduce your frustration with coding. In this Python 3 course, follow Kathryn Hodge as she walks through the Python standard library. Discover how familiarizing yourself with its facilities can make writing code in this concise, elegant language a little easier. Familiarize yourself with the library's built-in functions, math modules, and input and output modules. Learn how to get information from the Internet with the HTTP module, and more.