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)

Python Quirks Worth Knowing

  • Truthiness: if accepts any value. 0, 0.0, "", and None count as false; non-zero numbers and non-empty strings count as true.
  • and/or return an operand, not necessarily True/False: "" or "default" is "default".
  • Chained comparisons: 13 <= age <= 19 means 13 <= age and age <= 19.

Short-Circuit Evaluation

  • and: Stops at the first False (result is already False)
  • or: Stops at the first True (result is already True)
  • Use for safety guards: if len(x) > 0 and x[0] == "#" prevents IndexError

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

In a case, a bare name like case limit: captures the value instead of comparing against it. Match against literal values.

Prefer a flat if-elif-else chain to deep nesting; nest only when the inner question makes sense only after the outer one is answered.

Top 5 Pitfalls to Avoid

  1. = vs ==: Single equals is assignment; double equals is comparison
  2. Wrong condition order: Put the most restrictive condition first in elif chains
  3. Forgetting int() on input(): Comparing a string to a number raises TypeError
  4. if x == "A" or "B": Must write if x == "A" or x == "B"
  5. Missing colon: Every if, elif, else, match, and case line 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.