To create a for
loop like this, we add the for
keyword, a variable like i
, the word in
, and finally range():
for i in range():
In a for
loop we can specify how many times we'd like our loop to run with the range()
statement.
for i in range(5):
print("Happy birthday to you!")
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)
The variable before in
, in this case, i
, is the counter variable. It counts what repetition of the loop we're currently on.
for i in range(3):
print(i)
print("For loops are great!")