Relational Operators
Arithmetic operators give you a number. These six operators give you only True or False. They compare two operands with each other, so they are also called comparison operators.
1The six comparisons
2 < 312 > 312 <= 312 >= 1212 == 1212 != 10Read each one as a question. 2 < 3 asks "is 2 less than 3?" The answer is yes, so Python gives True. 12 < 3 asks "is 12 less than 3?" The answer is no, so Python gives False.
True or False. It never gives a number, and it never gives text.2Move the numbers, watch the answers
Move the sliders for a and b below. All six comparisons are answered again for the new values. Now make the two numbers equal and see which answers change. That is the difference between <= and <, and between >= and >.
12 < 3less than12 > 3greater than12 <= 3less than or equal to12 >= 3greater than or equal to12 == 3equal to12 != 3not equal tobool. It is never a number, and never text.<, == and > can never be True at the same time. Of any two numbers, one is either smaller than, equal to, or greater than the other. Only one of these three can be true.3Comparing things that are not numbers
== and != work on text also. Here you can see why the number 42 and the string '42' are not the same thing:
'cat' and 'Cat' are two different strings, so comparing them gives False.4Try it yourself
5Recap
<, >, <=, >=, == and != — compare two operands, and always give a boolean value.What does print(12 <= 12) show?
Which operator checks whether two values are equal?
What type of value does 5 > 3 give?