Loops: for and while
Repeat work without copy-pasting code.
Lesson 5 of 6 10 min
You will learn
- Loop with for and range()
- Loop with while
- Add up numbers and print tables
for loops
range(1, 6) gives 1, 2, 3, 4, 5 — it stops before the second number. A for loop runs its block once for each value.
Python · try it
Output appears here · Ctrl + Enter to run
while loops
A while loop repeats as long as its condition stays True — useful when you do not know in advance how many rounds you need.
Python · try it
Output appears here · Ctrl + Enter to run
Checkpoint
1. How many times does for i in range(2, 8): run?
2. What does the doubling program print first?