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?
!= reads 'not equal to'. Since 10 does equal 10, the answer to 'are they different?' is False. If you expected True, you may have read != as 'equals' — a very common first slip, and the next section pins down each operator so the symbols stop blurring.
9–12
3–5
A comparison is a question about two values. 3 > 5 asks 'is 3 greater than 5?' — the answer is False. 4 == 4 asks 'are these equal?' — the answer is True. Two equals signs mean 'is it equal', not 'make it equal'.
You can join two questions. and is true only when both parts are true. or is true when at least one part is true. not flips a true into a false, and a false into a true.
6–8
Python has six comparison operators: == (equal), != (not equal), <, >, <=, and >=. Each takes two values and returns a bool — either True or False. Note that == is a question, while a single = is assignment; confusing the two is one of the most common beginner errors.
Three connectives combine booleans. not x is True when x is False. x and y is True only when both are True. x or y is True when at least one is True. So (3 < 5) and (5 < 4) is True and False, which comes out False.
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.
K–2
Some questions have only two answers: yes or no. Is a cat bigger than a mouse? Yes. Is 2 bigger than 9? No. The computer keeps just two words for these answers: True and False.
True means yes. False means no. There is nothing in between — the computer always picks one of the two.
Undergrad
In Python bool is a subtype of int: True equals 1 and False equals 0, so True + True is 2. Every object also carries a truthiness — empty collections, 0, and None are falsy; most other values are truthy — and that is what if and while actually test.
and and or do not return a bool; they return one of their operands. x and y yields x when x is falsy, otherwise y; x or y yields the first truthy operand. This is why name or 'guest' is a standard idiom for supplying a default value.
Postgrad
The comparison operators and connectives form a Boolean algebra, and De Morgan's laws govern their interaction: not (a and b) equals (not a) or (not b), and dually for or. Rewriting a condition with De Morgan is a routine move when simplifying or negating a guard.
Short-circuit evaluation makes and and or control-flow operators, not merely functions: the second operand stays unevaluated until it is needed. This is what lets x != 0 and 100 / x > 1 guard against division by zero — the order of the operands is load-bearing, not cosmetic.
boolean
A value that is either True or False. Its type is bool, and comparison operators are the usual way to produce one.
and and or — read each row as one combination of inputs.
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.
Evaluate `not (4 > 2) or (1 == 0)` step by step — the steps fade as you master them
not True or (1 == 0)
not True or False
not — it binds tightest, and not True is FalseFalse or False
or — True only if at least one side is TrueFalse
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.
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?
A statement is one complete instruction; a program's statements run in order from the top of the file to the bottom, each after the one before it.
How close were you? Grade yourself honestly — it sets your review date.
2.What is the type of the value False?
3.Put the steps to evaluate not (2 == 3) in order.
- Test 2 == 3, which is False
- Apply not to False
- 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?
A comparison returns True or False; and is True only when both of its sides are True.
How close were you? Grade yourself honestly — it sets your review date.
9.What is the type of the value True?