Key Takeaways: Making Decisions — Conditionals and Boolean Logic
One-Sentence Summary
Conditionals let programs choose between paths by evaluating Boolean expressions that resolve to True or False — computers don't "decide," they evaluate.
The Three Conditional Structures
| Structure | When to Use | Example |
|---|---|---|
if |
Do something only when a condition is true | Check for negative input before processing |
if-else |
Choose between exactly two paths | Pass or fail, voting age or not |
if-elif-else |
Choose among multiple paths (first match wins) | Letter grade from numeric score |
Boolean Operators at a Glance
| Operator | Meaning | True When... |
Precedence |
|---|---|---|---|
not |
Negate | The operand is False |
Highest (1st) |
and |
Both | Both operands are True |
Middle (2nd) |
or |
At least one | At least one operand is True |
Lowest (3rd) |
Short-Circuit Evaluation
and: Stops at the firstFalse(result is alreadyFalse)or: Stops at the firstTrue(result is alreadyTrue)- Use for safety guards:
if len(x) > 0 and x[0] == "#"preventsIndexError
match/case vs. if-elif-else
Use if-elif-else for... |
Use match/case for... |
|---|---|
| Ranges and inequalities | Discrete value matching |
| Complex Boolean expressions | Menu selections, commands, status codes |
| All Python versions | Python 3.10+ only |
Top 5 Pitfalls to Avoid
=vs==: Single equals is assignment; double equals is comparison- Wrong condition order: Put the most restrictive condition first in
elifchains - Forgetting
int()oninput(): Comparing a string to a number raisesTypeError if x == "A" or "B": Must writeif x == "A" or x == "B"- Missing colon: Every
if,elif,else, andcaseline must end with:
Threshold Concept
Computers don't decide — they evaluate. Every conditional is a Boolean expression resolving to True or False. There is no ambiguity, no judgment, no subjectivity. Given specific values, the result is always deterministic.
TaskFlow v0.3 Additions
- Task priority: high, medium, low with conditional formatting
- Input validation: reject empty names and invalid priorities
- Pattern: validation gates — each check must pass before proceeding
What's Next
Chapter 5: Loops and Iteration — making the computer repeat tasks, re-prompt on bad input, and process collections of data.