7AAVDH10 Chap.3 Booleans and Conditional Branches
Booleans and Conditional Branches
Turn a rule into a test
Boolean expressions translate comparisons, equality and membership into True or False. Conditional statements then use those results to select an indented action. This chapter keeps the test separate from the action, traces branch chains from top to bottom and checks values exactly on every threshold.
Order controls meaning
An if, elif and else chain stops after its first match.
Broad conditions can therefore make later categories unreachable. The examples expose that structural fault and show when truthiness is appropriate for empty sequences and when it would collapse a meaningful zero into a missing-value case.
What this chapter covers
- 01
Boolean values
- 02
Comparison operators
- 03
Membership tests
- 04
Conditional indentation
- 05
Branch order
- 06
Truthiness
Worked example · free
Classify a score by thresholds
- 2State mutually exclusive categories. The marks shown are a study allocation, not an official University marking scheme.
- 3Evaluate conditions from strictest to broadest.
- 2Test every threshold boundary.
Key terms
- Boolean
- A Boolean is either True or False and can control program flow.
- Conditional
- A conditional executes code according to a tested truth value.
- Truthiness
- Truthiness is Python's treatment of values such as empty sequences and zero as false-like.
Booleans and Conditional Branches FAQ
Why does branch order matter?
Python stops at the first matching branch. A broad condition placed before a stricter one can capture values too early, so write categories in plain language and order overlapping thresholds deliberately.
When should I test a boundary value?
Test every value printed in a strict or inclusive comparison, plus its nearest neighbours. Those cases are where less than differs from less than or equal and where category gaps or overlaps appear.
What does a bare list condition test?
A bare list condition tests whether the list is non-empty. It says nothing about accuracy, relevance or the truth of items inside, so inspect those properties separately when the claim requires them.
How do I justify a classification threshold?
State the humanities distinction the threshold is meant to represent, then identify the evidence or convention behind it. Test the exact boundary and neighbouring values so the code matches the stated inclusion rule. If another defensible threshold would reclassify important cases, show that sensitivity and qualify the result.
Python can apply a boundary consistently, but it cannot decide that the boundary is historically or culturally meaningful without an argument and supporting sources.
Exam move
Write categories in plain language before translating them into comparisons. Check whether any category descriptions overlap or leave a gap. For every threshold, prepare the exact boundary value and its nearest neighbours, then predict each Boolean result. Record True or False beside the condition before following the branch. Build a descending threshold chain and explain why the stricter case must precede the broader one.
Then deliberately reverse the order and identify which category becomes unreachable. Compare the chained structure with independent if statements using the same tests. State when multiple actions are appropriate and when a single mutually exclusive label is required.
Separate condition debugging from action debugging: if the truth value is wrong, inspect input type, operator and threshold; if the truth value is right, inspect indentation and statements in the selected block. Include a year stored as text so the exercise exposes a type problem before a logic problem. Add an empty list, a non-empty list, zero and a missing value to study truthiness.
Explain why emptiness is sometimes the desired question and why zero may instead be a meaningful observation. Keep an error ledger under comparison, branch order, indentation and truthiness. For each repair, preserve unaffected categories and rerun previously passing cases. In closed-note practice, trace top to bottom and stop at the first match.
In project planning, connect every threshold to a research justification and identify the evidence that could support another boundary. Finish by writing the selected category as a sentence with its condition and scope. This final sentence proves that the code's control flow and the humanities classification express the same rule.
Add a weekly decision-table exercise with columns for input, first test, second test, selected branch and printed label. Populate it with every boundary and one clearly internal value per category. Then convert the table into code and compare each row with the program. Introduce one deliberate indentation error and one reversed threshold order, diagnose them from symptoms, and repair only the faulty structure.
End with a research reflection: explain who or what would be reclassified if the threshold moved, which evidence could justify that change and whether the resulting groups remain mutually exclusive. This final step makes the conditional an accountable analytical rule rather than an invisible convenience.