We know how to repeat code using a while
loop. Using for
loops, we can write programs with much less code and make it easier for other programmers to understand.
To create a for
loop like this, we add the for
keyword, a variable like i, the word in, and finally range():
In a for
loop we can specify how many times we'd like our loop to run with the range() statement.
Adding a number like 6, inside range() means it'll loop over the code block 6 times, from 0 until 5.
>>> for i in range(6):
... print(i)
...
0
1
2
3
4
5