python / python-control-flow
14 mins
Python Level 2: Branching & Iteration
Why This Matters: Python uses indentation blocks to structure control flow cleanly.
## Branching & Range Loops
Python uses `if`, `elif`, and `else` for logic branching, and `for` / `while` loops.
```python
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
else:
print("Grade C")
# Range iteration
for i in range(1, 6):
print(f"Count: {i}")
```
MENTAL MODEL & MEMORY LAYOUT
range(1, 6) Sequence Generator: 1 ──► 2 ──► 3 ──► 4 ──► 5 (Excludes upper bound 6)
COMMON PITFALLS TO AVOID
- Mixing tabs and spaces in indentation.
- Expecting `range(1, 5)` to include 5 (range excludes the end index).
Range Loop Summation
total = 0
for n in range(1, 6):
total += n
print(f"Sum 1..5: {total}")Iterates through generated range 1 to 5 accumulating total sum.
CONCEPT MASTERY CHECKPOINT
What numbers are generated by `range(1, 4)`?
NEXT RECOMMENDED LESSON
Python Level 3: Core Collections
Challenge: Write a function `sum_range(n)` returning sum of numbers 1 to n.