Python contains many kinds of operators, and another type of operator is comparison operators. These are used to compare the values of different numbers or variables against each other. Below is a table of a few simple comparison operators:
English: | Python: |
Greater than | > |
Less than | < |
Equal to | == |
These will come in handy in the next lesson, where we will be introducing conditionals and loops, but for now, we are able to see how they work by using booleans (true or false). Run the below program to see how the first two work (> and <):
The first program prints the word “True”, as 5 is greater than 3, however the second prints “False”, as 3 is not less than 2.
An easy way to remember which sign is “greater than” and which is “less than”, is to remember that the open end of the arrowhead points to the larger number. If it doesn’t, the program will print false, like in the second example.
You may have noticed that the “equal to” operator is two consecutive equals signs (==) instead of the usual single one (=) used so far. The single = is used when assigning values to variables, whereas the == sign is used when comparing two numbers, two variables, or two pieces of text to see if they are the same. Here are some examples of what is outputted when using ==:
Your task is to add comparison operators between the numbers in the following program, so that all the lines the program prints read “True”, without changing the numbers: