Using Comparison Operators in Python
00:00 Whoever said comparison is the thief of joy probably wasn’t a programmer. Comparing objects in Python is key for logic, control flow, and all sorts of design patterns.
00:09 So what do comparison operators do? They compare Python objects, operate on a variety of types, though the rules for this will differ with each operator, return Boolean values, and work as binary operators.
00:24
Here are the comparison operators you’ll find in Python: the double equals operator performs the equal to operation. The expression a == b will return true if the value of a and b are equal, and false otherwise. Exclamation equal is the not equal operator, the opposite of the previous. a != b returns true if a is not equal to b, and false otherwise. These two operators are a bit special as you can use them on any two Python objects.
00:53 But the next four can only be applied to objects that support comparison. Opening angle bracket, also called the less than symbol, functions as the less than operator.
01:02
a < b returns true if a’s value is less than b’s, false otherwise. Less than or equal to is much the same. a <= b returns true if a is less than or equal to b, and false otherwise. Closing angle bracket, aka the greater than symbol, is the opposite of less than. a greater than b returns True if a’s value is greater than b’s, false otherwise. And by now you can probably guess what greater than or equal does.
01:30
a greater than equals b returns true if a is greater than or equal to b, false otherwise. Okay, it’s example time. Back in the REPL, let’s start with comparing numerics. Define a as 10, b as 20.
01:46
Consider that 10 is smaller than 20 and see what results from using the operators we just looked at. a == b returns false.
01:54
a != b returns true because a is not equal to b.
02:00
a < b returns true as does a <= b. Whereas a > b returns false
02:09
as does a >= b. So pretty straightforward. Now what if you have two identical values? Create the variables x and y and set both to 30. x == y returns true while x != y returns false just as you’d expect given x is equal to y. While x < y returns false as does x > y because the values are the same, they are neither less than nor greater than each other.
02:39
It logically flows from here that the greater than or equal to operator x >= y returns true, which it does. And the same is the case for the less than or equal to operator x <= y returns true. Again, this is probably what you’d expect since comparing numbers is pretty natural.
02:57 So how about comparing strings? Python does allow it. Characters in a string are considered by their Unicode code points as that’s the default encoding in Python.
03:06
Since every character maps to a numeric code point, they can then be compared as any other integer. To view the code point of a character, you can pass it into the built-in ord() function.
03:15
So the code point of capital A, ord("A") is 65. The code point of lowercase a is ord("a"), 97. With those numbers in mind, you can now use the operators.
03:31
A == a returns false and A > a also returns false because 65 is smaller than 97. So A < a should return true.
03:49 And when comparing longer strings, Python will compare values one by one in sequence. So is “Hello” greater than “Hola”?
03:59
No, false. Capital H and H have the same code points, but the code point for o is actually higher than the one for e.
04:07 And this element-wise or lexicographical ordering also applies to comparing other container types like lists.
04:15
The list [5, 6] < [7, 3] returns true because 5 is less than 7.
04:23
And it also works if the two containers have different lengths. For example, the tuple (2, 4) < (2, 5)
04:32 also returns true because four is less than five regardless of how many other elements are in that tuple.
04:38
What won’t work is if you try to compare two different container types with each other like this. The tuple (2, 3) > the list [2, 3] raises a TypeError: > not supported between instances of tuple and list.
04:53
You can still use an equality check, though. The tuple (2, 3) == "2, 3" does not raise an error. But typically, when you compare with a type mismatch, the result will be false anyway.
05:06 So if you want to compare two containers, make sure you convert them to the same type first. So I mentioned before that these comparisons always return Boolean, that is, true/false values.
05:16 It turns out Python also provides operators designed specifically for handling Boolean values. If that piques your interest, meet me in the next lesson to find out more.
Become a Member to join the conversation.
