December 2017
Beginner
412 pages
10h 8m
English
The block of statements inside a loop can contain another loop. In this code, the inner loop is executed once for each item of list outer:
| | >>> outer = ['Li', 'Na', 'K'] |
| | >>> inner = ['F', 'Cl', 'Br'] |
| | >>> for metal in outer: |
| | ... for halogen in inner: |
| | ... print(metal + halogen) |
| | ... |
| | ... |
| | LiF |
| | LiCl |
| | LiBr |
| | NaF |
| | NaCl |
| | NaBr |
| | KF |
| | KCl |
| | KBr |
The number of times that function print is called is len(outer) * len(inner). In Table 13 we show that for each iteration of the outer loop (that is, for each item in outer), the inner loop executes three times (once per item in inner).
Read now
Unlock full access