Programming Fundamentals
Comparison operators, also called relational operators, compare two values and answer with a Boolean value: either True or False. They never change the values they look at — they just report how the two values relate to each other.
| Operator | Meaning | Question it asks |
|---|---|---|
== | equal to | are the two values the same? |
!= | not equal to | are the two values different? |
< | less than | is the left value smaller? |
<= | less than or equal to | is the left value smaller, or the same? |
> | greater than | is the left value larger? |
>= | greater than or equal to | is the left value larger, or the same? |
Every comparison reduces to a single True or False — never to the values themselves. A cinema hall has booked 87 of its 120 seats, so seats_booked < hall_capacity is True, seats_booked == hall_capacity is False and seats_booked != hall_capacity is True. The numbers themselves are untouched; the comparison only reports on them.
That result can be printed straight away, or stored in a variable. A variable holding a comparison is a Boolean variable: it stores True or False rather than a number or a string, ready to be used later in the program.
seats_booked = 87
hall_capacity = 120
# print a comparison to see its Boolean answer straight away
print(seats_booked < hall_capacity)
print(seats_booked == hall_capacity)
print(seats_booked != hall_capacity)
# or save the answer in a Boolean variable and use it later
full = seats_booked >= hall_capacity
print("Hall full?", full)Output:
True
False
True
Hall full? False
Either side of an operator can be a variable or a fixed value, and both sides can be variables. seats_booked < hall_capacity compares two variables, while seats_booked < 100 compares a variable with a value, and 87 != 120 compares two values directly. The operator sits between the two things being compared, with one on the left and one on the right.
The equals sign always comes second, so the inclusive tests are written <= and >=; typing =< or => is not valid and the program will not run. A single = is not one of the six operators either, because it assigns a value to a variable rather than comparing, so == is the one that asks whether two values are equal.
To evaluate a comparison, replace each variable with the value it holds, work out any arithmetic, and only then apply the comparison operator. The answer is a single Boolean value: either True or False.
Imagine a bakery bakes 5 trays with 8 buns on each tray, and needs at least 43 buns. To evaluate trays * 8 >= 43:
5 * 8 >= 4340 >= 43The arithmetic always comes first, because the comparison needs two finished values: the total number of buns and the target of 43. Comparing first would mean multiplying the True or False answer by 5, which the expression never asks you to do.
weeks = 5
days = weeks * 7
print(days >= 30)
print(days != 35)Output:
True
False
| Comparison | After substituting and working out | Result |
|---|---|---|
4 * 6 > 20 | 24 > 20 | True |
c - 9 >= 12 when c is 21 | 12 >= 12 | True |
18 // 4 == 5 | 4 == 5 | False |
d * 2 != 30 when d is 15 | 30 != 30 | False |
In every row the middle step leaves one operator sitting between two values, such as 24 and 20, and that final question is what produces the True or False. The result is that single Boolean value, not the numbers you compared.
A boundary is the value where a rule changes, such as the smallest number of tokens needed for a reward. The operators > and < exclude that boundary value, while >= and <= include it.
Choose the operator by checking whether the boundary value should be accepted or rejected. If a club gives a free kit at 12 tokens or more, 12 must be accepted, so use tokens >= 12. Writing tokens > 12 would turn away somebody with exactly 12 tokens, which breaks the rule. If the rule were “more than 12 tokens”, then tokens > 12 would be correct and 12 would be rejected.
| Rule in words | Comparison | Boundary 12 |
|---|---|---|
| fewer than 12 | tokens < 12 | 12 rejected |
| 12 or fewer | tokens <= 12 | 12 accepted |
| more than 12 | tokens > 12 | 12 rejected |
| 12 or more | tokens >= 12 | 12 accepted |
Before choosing, test values either side of the boundary. For a rule accepting 12 or more, 11 should fail and 12 should pass. Try a value just above too: 13 should pass. Comparing tokens > 12 with tokens >= 12, only the second passes 12 and 13 while still failing 11.
When the values are whole numbers, a one-step adjustment can give exactly the same result. “More than 12” can be written tokens > 12 or tokens >= 13, because both accept the same whole numbers. In the same way, “12 or more” can be written tokens >= 12 or tokens > 11.
This program uses the 12-token boundary to decide whether a free kit is earned.
tokens = int(input("How many tokens? "))
if tokens >= 12:
print("Free kit")
else:
print("Not yet")Output when the user types 12:
How many tokens? 12
Free kitYou can compare two strings in Python with == (equal to) and != (not equal to). Each comparison gives a Boolean result: True or False.
A string comparison with == is exact and case sensitive. Capital and lower-case letters are different characters, and a space counts as a character too, so the comparison checks the text exactly as it is written.
| Comparison | Result |
|---|---|
"Platform 3" == "Platform 3" | True |
"Platform 3" == "platform 3" | False |
"Platform 3" != "Platform3" | True |
In the second row the lower-case p makes the two strings different. In the third row the missing space makes the strings different, so they are not equal and != gives True. This program compares what you type with a stored string using ==:
planet = input("Name the largest planet: ")
if planet == "Jupiter":
print("That is right")
else:
print("Not an exact match")Output when the user types jupiter:
Name the largest planet: jupiter
Not an exact match
A string is a different data type from an integer or a float. "7.50" == 7.50 is False: the characters look like a number, but one value is text and the other is a number. When you use input(), the result is always a string, so typed digits are still text. To compare that input with a number, convert it first with int() or float(); for example, float("7.50") == 7.50 is True, because both values are now the number 7.5.
You only need == and != for comparing strings. Comparing strings with <, >, <= or >= is not required.
A condition is the test that a selection statement or a loop uses to decide what happens next. You write it as a comparison, so it produces a Boolean result of True or False — slices >= friends asks whether there is a slice of pizza for everyone at the table.
In Python, the comparison goes after the keyword that opens the statement, such as if, and it is answered at the moment the program reaches that line. The True or False is not printed and does not change any variable: it is used straight away to decide whether the code underneath runs.
soil_moisture = 28
if soil_moisture < 40:
print("Water the plants")
if soil_moisture > 70:
print("Drain the soil")Output:
Water the plants
The first condition is worked out when its line is reached: soil_moisture < 40 is True for 28, so the watering message is printed. The second condition is then asked separately, and soil_moisture > 70 is False, so its message is skipped. The comparison is written once but can give a different answer on another run, because the variable may hold a different value.
Loops use a condition in the same way: the same True or False comparison, checked as the loop runs. The loop structures themselves are covered in other subtopics, but the comparison inside them works exactly as it does in an if statement.