November 2018
Beginner
304 pages
7h 35m
English
We've seen for loops in previous examples; they are the go-to sequence iterator for Python. The for loops work on nearly anything: strings, lists, tuples, and so on. The main format is shown next. Note how for loops have the same basic look as while loops:
for <target> in <object>: # assign object items to target
<statements>
if <test>:
break # exit loop now, skip else
if <test>:
continue # go to top of loop now
else:
<statements> # if we didn't hit a 'break'
When the for loop starts, it looks at the first item in the sequence. This item is given a value of 0 (many programming languages start counting at 0, rather than 1). Once the code block has finished doing its processing, the for loop looks at the second value and gives it ...
Read now
Unlock full access