Review
Key Ideas
- Comparisons produce booleans.
ifruns a block when its condition is true.elifchecks another condition.elsehandles the remaining case.- Indentation defines the block.
forrepeats over a sequence or range.range(stop)starts at0and stops beforestop.whilerepeats while a condition remains true.- A running total accumulates values across loop iterations.
- Off-by-one errors come from boundary mistakes.
Key Syntax
| Syntax | Meaning |
|---|---|
score >= 5 | comparison |
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 + value | update a running total |
Common Mistakes
| Mistake | Fix |
|---|---|
Using = when comparing | use == |
Putting broad if branches before specific ones | check specific cases first |
| Forgetting indentation | indent the branch or loop body |
Expecting range(5) to include 5 | remember the stop value is excluded |
Forgetting to update a while loop condition | update inside the loop |
| Resetting a total inside the loop | initialize before the loop |
Checklist
You are ready to move on if you can:
- predict a comparison result
- trace an
if/elif/elsestatement - write a
forloop over a small list - explain what
range(4)produces - trace a
whileloop 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.