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


























