For loops

Do you want to repeat code blocks?

To repeat code, the for keyword can be used.

Sometimes you need to execute a block of code more than once, for loops solve that problem. We specify the start and end of the loop using the function range(min,max).

Related Course:
Complete Python Bootcamp: Go from zero to hero in Python


To execute a line of code 10 times we can do:

The last number (11) is not included. This will output the numbers 1 to 10. Python itself starts counting from 0, so this code will also work:

but will output 0 to 9.

Illustrated in this graphic:

for loop

The code is repeated while the condition is True. In this case the condition is: i < 10. Every iteration (round), the variable i is updated.

Nested loops

Loops can be combined:

In this case we have a multidimensional loops. It will iterate over the entire coordinate range (0,0) to (9,9)


Previous Post
Next Post


Leave a Reply