SigmaAnalytics & Tech
A13PythonBeginnerAbout 120 min to complete

Loops

Repetition done right — and a clear-eyed understanding of when a loop is the wrong tool entirely.

Notebook

About 120 minutes to complete

Enrol to read the notebook and download it to run.

What this module covers

Repetition done right — and a clear-eyed understanding of when a loop is the wrong tool entirely.

By the end of it

  • Write for loops over any iterable, and while loops with correct termination
  • Use range() fluently, including its start/stop/step forms
  • Control flow with break, continue, and the little-known loop else clause
  • Recognise and write the four core loop patterns: accumulate, search, filter, transform
  • Avoid infinite loops and off-by-one errors
  • Apply itertools tools — chain, product, combinations, accumulate, groupby
  • Measure and explain the cost of row-wise iteration in pandas versus vectorisation
  • Choose correctly between iterrows(), itertuples(), .apply(), and vectorised code