University of Free Knowledge
QA 76.73 · fol. 5

True, False, and the Tests Between

Comparison operators and the connectives and, or, and not reduce values to True or False — the two answers every decision is built on. · 12 min

Every decision a program makes comes down to a yes-or-no question. Is the score high enough? Did the person type quit? Python answers such questions with exactly two values: True and False. They are not words printed on the screen — they are a type of their own, called bool, and they are what comparison operators produce. Ask whether 3 < 5 and Python hands back True. This folio covers the operators that ask the questions, and the three connectives — and, or, not — that fold several questions into one answer.

Guess before you learn

Before reading on, commit to an answer. What does Python give back for the comparison 10 != 10?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

The connectives have a precedence, just as arithmetic does: not binds tightest, then and, then or. So not True or False groups as (not True) or False, which is False or False, giving False. When in doubt, add parentheses — they cost nothing and make the grouping plain.

Two refinements worth knowing. Comparisons can be chained: 1 <= x <= 10 means x sits between 1 and 10 inclusive, and Python reads it exactly that way. And and / or short-circuit: x or y never looks at y once x is true, because the answer is already settled.

boolean

A value that is either True or False. Its type is bool, and comparison operators are the usual way to produce one.

ABA AND BA OR BTrueTrueTrueTrueTrueFalseFalseTrueFalseTrueFalseTrueFalseFalseFalseFalse
PLATE I The truth tables for and and or — read each row as one combination of inputs.
Retrieval Gate — answer before you continue 0 / 4

1.What is the value of 7 >= 7?

2.(2 < 3) and (3 < 2) evaluates to?

3.Match each operator to its meaning.

==
!=
>=
not

4.Why does if x = 5: raise an error where if x == 5: does not?

Comparisons rarely stand alone. A real test often asks several questions at once — is the temperature above freezing and below boiling? The tool for that is the boolean expression: comparisons wired together with and, or, and not into a single True or False. Read one carefully by working from the tightest-binding operator outward, exactly as you would an arithmetic expression.

Ink That Thinks — guess first; the answer draws itself.
The test x < 5 returns True (draw it as 1) or False (draw it as 0). Place its value for x = 0, 1, 2, 3, 4, 5, 6, 7 — pencil your guesses first, then watch exactly where it flips.

0246800.20.40.60.81xvalue (True = 1, False = 0)
Tap to place each point.
PLATE II x < 5 as x climbs — one clean step down at the boundary.

Evaluate `not (4 > 2) or (1 == 0)` step by step — the steps fade as you master them

1
Resolve the tightest comparison first: is 4 greater than 2?
not True or (1 == 0)
2
Resolve the other comparison: does 1 equal 0?
not True or False
3
Apply not — it binds tightest, and not True is False
False or False
4
Apply or — True only if at least one side is True
False
Why is this true?

Why is True and False equal to False, but True or False equal to True?

and demands that both sides hold, so a single False sinks the whole thing. or asks only that at least one side hold, so a single True carries it. The keywords mean exactly what they do in careful English.

Retrieval Gate — answer before you continue 0 / 4

1.not (5 == 5) evaluates to?

2.Put the steps for evaluating (3 > 1) and (2 > 8) in the order Python performs them.

  1. Test 3 > 1, which is True
  2. Test 2 > 8, which is False
  3. Combine with and: True and False
  4. Result: False

3.In one sentence, explain the difference between = and == in Python.

4.Which grouping does Python use for not True and False?

Two values, True and False; six comparisons that produce them; three connectives that combine them under a fixed precedence. That is the whole vocabulary of a decision. In the next folio those decisions finally do something: an if statement reads a boolean and chooses which lines of your program actually run.

Practice — new ink and old, interleaved

1.Without looking back: what is a statement, and in what order do a program's statements run?

2.What is the type of the value False?

3.Put the steps to evaluate not (2 == 3) in order.

  1. Test 2 == 3, which is False
  2. Apply not to False
  3. Result: True

4.(10 >= 10) or (10 < 0) evaluates to?

5.Which sentence best describes a computer program?

6.Using operator precedence, evaluate 2 + 3 * 4.

7.Evaluate 10 % 3 (the remainder).

8.Without looking back: what two values can a comparison return, and what does and require to be True?

9.What is the type of the value True?

The Call Slip — search everything Ctrl·K / ⌘K