LambdaLabTM
Computer Science · Class 11 · Expressions & Operators
ExpressionsBooleans⏱️ 9 min read

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

<
less than
2 < 3
True
>
greater than
12 > 3
True
<=
less than or equal to
12 <= 3
False
>=
greater than or equal to
12 >= 12
True
==
equal to
12 == 12
True
!=
not equal to
12 != 10
True

Read 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.

Python prompt — interactive mode
>>> print(2 < 3)
True
>>> print(12 < 3)
False
>>> print(12 <= 3)
False
>>> print(12 <= 12) # less than OR equal to — the 'equal to' half is enough
True
>>> print(12 == 12)
True
>>> print(12 != 12)
False
Key Takeaway
Every relational operator gives a boolean value — 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 >.

Move the two numbers — every answer is True or False
12 < 3less than
False
12 > 3greater than
True
12 <= 3less than or equal to
False
12 >= 3greater than or equal to
True
12 == 3equal to
False
12 != 3not equal to
True
Each of these six answers is a bool. It is never a number, and never text.
Note
Notice that <, == 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:

Python prompt — interactive mode
>>> print('cat' == 'cat')
True
>>> print('cat' == 'Cat') # capital C — a different character
False
>>> print(42 == '42') # a number is never equal to text
False
Tip
Python is case sensitive. This means 'cat' and 'Cat' are two different strings, so comparing them gives False.

4Try it yourself

Python prompt — interactive mode
# Ask Python a question. The answer is always True or False.
>>>
try

5Recap

Key Takeaway
The six relational operators — <, >, <=, >=, == and != — compare two operands, and always give a boolean value.
Quick Check

What does print(12 <= 12) show?

Quick Check

Which operator checks whether two values are equal?

Quick Check

What type of value does 5 > 3 give?