Affiliate disclosure

Book titles on this page link to Amazon. As an Amazon Associate, DataField.Dev earns from qualifying purchases — at no additional cost to you.

Further Reading: Repetition: Loops and Iteration

Python Loop Mechanics

  • Python Documentation: "More Control Flow Tools" — for Statements and range() (Tier 1) The official Python tutorial section on for loops and range(). Concise and authoritative. Start here for the canonical explanation of Python's loop semantics. https://docs.python.org/3/tutorial/controlflow.html#for-statements

  • Python Documentation: "The while statement" (Tier 1) The formal language reference for while loops, including the else clause. More technical than the tutorial, but complete. https://docs.python.org/3/reference/compound_stmts.html#the-while-statement

Iteration as a Concept

  • Downey, A. Think Python, 3rd Edition, Chapter 7: "Iteration." (Tier 1) An alternative explanation of loops with different examples and emphasis. Good for hearing the same concepts explained a second way. Free online. https://allendowney.github.io/ThinkPython/

  • Sweigart, A. Automate the Boring Stuff with Python, Chapter 2: "Flow Control." (Tier 1) Covers loops with a strong emphasis on practical automation examples. If you want to see loops used to automate real tasks (renaming files, processing spreadsheets), this is the place to start. Free online. https://automatetheboringstuff.com/2e/chapter2/

Deeper Dives

  • Lutz, M. Learning Python, 6th Edition, Chapters 13-14. (Tier 1) The most comprehensive treatment of Python's iteration protocol, including how for loops work under the hood with iterators and iterables. More detail than you need right now, but valuable when you're ready for it (around Chapters 14-16 of this book).

  • Beazley, D. & Jones, B. K. Python Cookbook, 3rd Edition, Chapter 4: "Iterators and Generators." (Tier 1) Advanced iteration patterns used in production Python code. Bookmarking for when you've finished this book.

The Accumulator Pattern in Practice

Loop Performance and Optimization

  • "Loop Like a Native" — Ned Batchelder, PyCon 2013 talk. (Tier 2) An excellent conference talk about writing Pythonic loops. Covers when to use enumerate(), zip(), and other tools you'll learn in later chapters. Search for it on YouTube.

For the Curious

  • "The Halting Problem" — Computerphile (YouTube video) (Tier 2) A 10-minute video explaining why it's mathematically impossible to write a program that can determine whether any program will eventually halt (stop running) or loop forever. This is a deep connection between loops and the fundamental limits of computation mentioned in Chapter 1's "Big Questions of CS."

  • Knuth, D. E. "Structured Programming with go to Statements." Computing Surveys, 6(4), 1974, pp. 261-301. (Tier 2) A famous paper by one of computer science's greatest thinkers, arguing (among other things) that structured loops (for, while) should replace the chaotic goto statement. Historical context for why we write loops the way we do.