Menu

Relational Operators

Evaluating comparisons

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:

  1. Substitute the value: 5 * 8 >= 43
  2. Work out the arithmetic: 40 >= 43
  3. Apply the operator: is 40 greater than or equal to 43? No, so the result is False.

The 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

Worked examples

ComparisonAfter substituting and working outResult
4 * 6 > 2024 > 20True
c - 9 >= 12 when c is 2112 >= 12True
18 // 4 == 54 == 5False
d * 2 != 30 when d is 1530 != 30False

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.

Practise this