Combining questions: and, or, not with their precedence order, chained range checks, and the truth tables the exam loves to ask.
01
Learning objectives
Build compound conditions with and, or and not and evaluate them to a single bool.
State the precedence: not binds tightest, then and, then or.
Use Python's chained comparison form 0 <= x <= 100 correctly.
Complete two-column truth tables for and and or.
Rewrite a nested condition as a compound one and choose the clearer version.
02
Lesson notes
Real decisions rarely rest on one question. A merit scholarship needs marks and attendance; a warning fires when the temperature is too high or the pressure too low; an alarm sounds when the system is not armed. Python's three logical operators express exactly this. A and B is True only when both sides are True. A or B is True when at least one side is True (including both). not A flips its single operand. Each of A and B is itself a bool — usually a comparison like marks >= 80.
The truth tables are short enough to memorise and appear on papers as explicit questions. For and: True/True → True; True/False, False/True, False/False → False. For or: only False/False → False; the other three combinations → True. For not: not True is False, not False is True. A useful reading: and is strict (one failure fails everything), or is generous (one success saves everything). When in doubt in an exam, rebuild the table for the actual values rather than trusting intuition.
Precedence matters the moment operators mix: not binds tightest, then and, then or — with comparisons binding tighter than all three. So not a == b or c > d parses as (not (a == b)) or (c > d). The professional habit is not to memorise beyond this but to parenthesise: (marks >= 80) and (attendance >= 75) costs nothing and reads instantly. Brackets in logic are like brackets in arithmetic — they trade a little ink for absolute clarity.
Python has one more elegance most languages lack: chained comparisons. The mathematical range check 0 <= marks <= 100 is valid Python and means exactly 0 <= marks and marks <= 100 — the variable is written once and evaluated once. This form kills the classic beginner bug 0 <= marks <= 100 written as 0 <= marks or marks <= 100 (always True — every number satisfies at least one half). Whenever a condition describes a range, the chain form is the right spelling.
Finally, meet short-circuit evaluation: Python evaluates compound conditions left to right and stops as soon as the answer is decided. In A and B, if A is False the whole thing is False — B is never even computed. In A or B, a True A settles it. Programs exploit this for safety: if n != 0 and total / n > 0.5: guards the division — when n is 0, the dangerous right side never runs. Remember the order (guard on the left) and the trick is yours for life.
Boolean truth table
03
Worked code examples
Truth tables, printed
and is strict — one False fails it. or is generous — one True saves it. not flips its operand; not (4 > 10) is not False, i.e. True.
print(True and True, True and False)
print(False or True, False or False)
print(not True, not (4 > 10))
Output
True False
True False
False True
Precedence and the bracket habit
Line 1: both questions needed True, attendance failed. Line 2: not binds first — (not (84 < 80)) = True, so the or is True without reading further. Line 3: marks passes and not (70 >= 75) is also True, so the and prints True.
marks, attendance = 84, 70
print(marks >= 80 and attendance >= 75)
print(not marks < 80 or attendance >= 75)
print(marks >= 80 and not attendance >= 75)
Output
False
True
True
Range chains and short-circuit safety
The chain 0 <= 112 <= 100 fails at the second link. In the guard, n != 0 is False, so and short-circuits: total / n is never evaluated and the ZeroDivisionError never happens.
marks = 112
print(0 <= marks <= 100)
total, n = 180, 0
if n != 0 and total / n > 10:
print("high average")
else:
print("guarded — division never ran")
Output
False
guarded — division never ran
04
Key terminology
compound condition
Several questions joined with and / or / not into one True-or-False expression.
truth table
A table listing every input combination of a logical operator and its output.
short-circuit
Stopping evaluation as soon as the result is decided — the right side of and/or may never run.
chained comparison
The form lo <= x <= hi, equivalent to lo <= x and x <= hi but written and evaluated once.
guard
A safety condition placed first (usually with and) so a risky operation only runs when it is safe.
05
Common misconceptions
Common mistake
A or B means 'either A or B but not both'.
Correct understanding
Python's or is inclusive: True or True is True. (The exclusive version exists but is spelled differently.)
Common mistake
not and or have equal priority, so read left to right.
Correct understanding
not binds tightest, then and, then or. Comparisons bind tighter than all of them — and brackets beat everything.
Common mistake
0 <= x or x <= 100 tests whether x is in range.
Correct understanding
That is always True — every number passes at least one half. Range checks need and (or the chained form).
06
Check your understanding
1. What is (5 > 3) and (2 == 2) or (1 > 9)?
2. Which condition is True only when age is a teenager (13-19)?
3. In if n != 0 and 100 / n > 5:, why is the order important?