liveBook · Manning

Loop

As explained in Get Programming: Learn to code with Python

A loop is a control-flow construct that repeatedly executes a code block while iterating over values or satisfying a continuation rule.

A loop repeats a block of code without making you write the same statements again and again. The key question is when repetition should stop: after a known set of values, or when a condition changes? section 16.1 section 18.0

Choose a loop to match the task:

  • Use a `for` loop to visit each value in a sequence. Its loop variable takes the next value, runs the indented block, and stops when no values remain. section 16.2
  • Use `range(N)` to repeat N times, with values from 0 through N − 1. Use `range(start, end, step)` to change where counting begins or to skip values. A `for` loop can also visit the characters of a string directly. section 16.3 section 17.1
  • Use a `while` loop when you do not know the number of repetitions in advance. Its block runs while its condition is true; check that something can change the condition, or the loop may never end. section 18.1

Watch a loop run

`for v in range(3):` followed by an indented `print(v)` prints 0, then 1, then 2. You can trace it by imagining three assignments—`v = 0`, `v = 1`, and `v = 2`—each followed by the print statement. Quick check: what would `range(5)` make the loop variable take on? section 16.3

Listing 18.1. A while loop example of a guessing game
secret = "code"
guess = input("Guess a word: ")
tries = 1
while guess != secret:                                   #1
    print("You tried to guess", tries, "times")
    guess = input("Guess again: ")                       #2
    tries += 1
print("You got it!")                                     #3

In the guessing game, each wrong guess leads to another prompt. The new guess changes the value checked by the `while` condition; a correct guess ends the loop. section 18.1

A `for` loop can be rewritten as a `while` loop, but then you must set up and update the counting variable yourself. The reverse is not always possible when there is no known set of repetitions. Within either kind of loop, `break` exits early and `continue` skips to the next repetition. section 18.2 section 18.4

Try using a loop to greet each name in an input, then put loops and conditionals together to find words that can be made from letter tiles. section 17.3 section 19.0

How Get Programming: Learn to code with Python covers it

How deep it goes
Traces loop-variable values, custom ranges, condition checks, and infinite-loop risks; stops at introductory Python examples.
Examples
Uses repeated printing, dice rolls, a word-guessing game, and letter tiles to show repetition in action.
What it assumes
Builds on earlier conditional statements while introducing repetition and loop variables through small examples.
What you can do after
Build it from scratch: write Python `for` and `while` loops, choose between them, and avoid a condition that never changes.
How it connects
Connects loops to sequences, strings, conditionals, loop variables, and `break` and `continue` statements.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.