Review

Key Ideas

  • Comparisons produce booleans.
  • if runs a block when its condition is true.
  • elif checks another condition.
  • else handles the remaining case.
  • Indentation defines the block.
  • for repeats over a sequence or range.
  • range(stop) starts at 0 and stops before stop.
  • while repeats while a condition remains true.
  • A running total accumulates values across loop iterations.
  • Off-by-one errors come from boundary mistakes.

Key Syntax

SyntaxMeaning
score >= 5comparison
if condition:run a block when true
elif condition:check another condition
else:fallback branch
for item in items:repeat once per item
range(5)0, 1, 2, 3, 4
while condition:repeat while true
total = total + valueupdate a running total

Common Mistakes

MistakeFix
Using = when comparinguse ==
Putting broad if branches before specific onescheck specific cases first
Forgetting indentationindent the branch or loop body
Expecting range(5) to include 5remember the stop value is excluded
Forgetting to update a while loop conditionupdate inside the loop
Resetting a total inside the loopinitialize before the loop

Checklist

You are ready to move on if you can:

  • predict a comparison result
  • trace an if / elif / else statement
  • write a for loop over a small list
  • explain what range(4) produces
  • trace a while loop with a changing counter
  • compute a running total
  • identify an off-by-one boundary

If You Feel Lost

Trace by hand. Make a small table with the loop variable, condition, and any changing values. Programs become easier when their state is visible.