Repetition (P2)¶
This page introduces repetition: how Python repeats actions using loops.
Repetition is used when:
- processing many rows
- iterating over collections
- performing repeated tasks
Repetition and Uses¶
Repetition means executing the same block of code multiple times.
- Use
forfor known collections - Use
range()for numeric sequences - Use
whilefor condition-based repetition - Use list comprehensions to transfrom each item in a list to a new list
for Loops (Most Common)¶
Use a for loop when you know what you are iterating over.
range() for Numeric Sequences¶
Use range() when you need a sequence of numbers.
Important:
range(start, stop)does not includestop- This prints 2020, 2021, 2022, 2023
Use cases:
- years
- counts
- indexes
while Loops (Condition-Controlled)¶
Use a while loop when repetition depends on a condition.
Rules:
- The condition is checked before each iteration
- You must update the condition
When NOT to Use Loops¶
Do not use loops to schedule work over time (e.g., “run nightly”).
That belongs to:
- operating system schedulers
- workflow tools
- pipelines
Loops are for repetition inside a running program.
List Comprehensions¶
List comprehensions create a new list by transforming another list.
Use them when:
- the transformation is simple
- readability stays high
Nested Repetition (Briefly)¶
Loops can be nested:
Keep nesting shallow when possible.
Repetition in Data Pipelines¶
Repetition appears everywhere in data work:
- CSV rows
- JSON records
- Excel rows
- text lines
Common Mistakes¶
Off-by-one errors¶
Infinite while loops¶
Forgetting to update the condition variable.