Branching Logic & Decisions

Python conditionals (`if / elif / else`) and upgrading the console calculator to run the user-selected operation.

Learning Objectives

  • Write boolean expressions and use comparison operators (==, !=, <, <=, >, >=).
  • Control program flow with if, elif, and else.
  • Combine conditions with logical operators (and, or, not) and understand truthiness.
  • Apply guard clauses (e.g., prevent division by zero) and provide simple user feedback.

Session Plan (theory)

  1. Recap (Week 1–2) Printing & formatting; input() and basic casting.
  2. Conditionals in Python
    • if / elif / else structure and execution order.
    • Comparison & logical operators; grouping with parentheses.
    • Truthy/falsey values and common patterns (guard clauses, early returns with exit() for scripts).
  3. Pattern examples
    • Range checks, multi-branch decisions, simple validation messages.

Hands-on

  • Console Calculator v2 (conditionals)
    • Read two numbers (float) and an operator from the user.
    • Use an if/elif/else chain to perform +, -, *, /.
    • Guard against division by zero and unknown operators.
    • Print a clear, formatted result line.