liveBook · Manning

Loop

As explained in The Quick Python Book, Fourth Edition

A for loop is a control-flow construct that iterates over the items supplied by an iterable.

A Python `for` loop repeats a block once for each value supplied by an iterable. It takes the next value, assigns it to the loop variable, and runs the indented body. Unlike a counter-based loop, it works directly with items from a list, tuple, string, or other iterable. section 8.4

Control flow inside the loop:

  • `continue` skips the rest of the current body and moves to the next item. section 8.4
  • `break` ends the loop immediately. An optional `else` block runs only if the loop finishes without a `break`. section 8.4

Find the first qualifying item

Given `[3, "string1", 23, 14.0, "string2", 49, 64, 70]`, a loop skips values that are not integers, tests the remaining values for divisibility by seven, and breaks after printing `49`. The loop variable receives each list item in turn; it is not an index. section 3.4.5

x = [1.0, 2.0, 3.0, 0.0, -1.0]
for n in x:
    if n == 0.0:
      print("skipping 0.0")
      continue                         #1
    elif n < 0.0:
      print("no negative numbers!")
      break                            #2
    print(1 / n)
else:
  print("Loop completed normally")     #3
 
1.0
0.5
0.3333333333333333
skipping 0.0
no negative numbers!
#1 The loop skips back to the top of the loop with the next item n.
#2 The loop exits immediately; the else block is skipped.
#3 Only executed if no break is executed

This runnable loop prints reciprocals, uses `continue` to avoid division by zero, and uses `break` on a negative number. Because it breaks, its `else` message does not print. section 8.4

Loops are useful for work such as counting words and characters across file lines. Keep repeated work necessary: create a punctuation-translation table once rather than on every line, and consider `join` instead of concatenating strings in another loop. Some loop patterns can also be expressed as comprehensions. section 8.12 section 6.10.2 section 8.14

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines item-by-item execution and explains `break`, `continue`, and optional `else`, without developing iterable internals.
Examples
Shows a divisible-by-seven list search, reciprocal calculations with early exit, and file-based word and character counts.
What it assumes
Assumes general programming familiarity while defining Python’s iterable-based loop behavior.
What you can do after
Practice/perform iteration over Python values while skipping items, stopping early, and avoiding unnecessary repeated work.
How it connects
Connects iteration to iterables, `while` loops, generators, comprehensions, and indentation.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.