The for loop
If you need to print the word “Hello” 100 times, are you going to write 100 print lines? No way! That’s what a loop is for.
for — “repeat this many times”
Section titled “for — “repeat this many times””Inputs (for input())
One value per line — read in order by input().
Read it as: “repeat 5 times: print Hello”. The indentation rule applies here too — the lines inside the loop are 4 spaces in.
i — the counter
Section titled “i — the counter”i is just an ordinary variable, and its value changes on every repeat. Look:
Inputs (for input())
One value per line — read in order by input().
Not what you expected, right? It started at 0 and ended at 4! Programmers love counting from 0. range(5) = “0, 1, 2, 3, 4” — that’s 5 numbers in total, but starting at 0.
The two forms of range
Section titled “The two forms of range”range(5)→ 0, 1, 2, 3, 4range(1, 6)→ 1, 2, 3, 4, 5 — from a start up to an end (the last number is not included!)
Inputs (for input())
One value per line — read in order by input().
Missions
Section titled “Missions”The repeat machine
+10 XP ✓ CompletedPrint the sentence Python is great! 10 times — but write print only once in your code.
Inputs (for input())
One value per line — read in order by input().
1 to 10
+15 XP ✓ CompletedPrint the numbers from 1 to 10 in order. Careful: in range, the last number is not included!
Inputs (for input())
One value per line — read in order by input().
Times table
+15 XP ✓ CompletedSolve it with code before the teacher even finishes writing it on the board: the 5 times table, from 1 to 10, in the format 5 x 3 = 15.
Inputs (for input())
One value per line — read in order by input().
Next lesson: loop + variable = superpower. We’ll add up every number from 1 to 100 in a split second — like a little Gauss. Continue →