Two Kinds of Repetitive Tasks
- Batch execution across independent items: Sending a sports meet notice to over 20,000 students, repeating the same independent step each time.
- Step-by-step accumulation: Computing semester GPA via
GPA = Σ(Credits × Grade Points) / Σ(Credits), where each step updates the running total from the step before it.
Two Limits of Sequential Code
- Code grows with data size: Notifying 20,000 students requires 20,000 lines; changing the message template means editing every line by hand.
- Cannot adapt to variable item counts: Students take 5 or 8 courses in different semesters, so a fixed-length formula cannot fit every transcript.
Two Loop Statements in Python: (1) for loop: steps through a sequence whose range or item count is known in advance (such as rosters or course lists); (2) while loop: repeats as long as a condition holds when the iteration count is unknown beforehand (such as saving monthly until reaching a target).