Nested Loops
The code inside a for
loop can be any code that can run outside of the loop, which means you can use one loop inside another loop.
Here is an example to illustrate the concept (pay attention to indention):
for i in range(3): # numbers in [0,1,2]
for j in 'abc': # letters in 'abc'
print(i, j)
Results:
0 a
0 b
0 c
1 a
1 b
1 c
2 a
2 b
2 c
Note:
for loop
header lines always end in colon :- Indent for loop body by 4 spaces relative to the for loop line.