Loading…
for loops repeat code for each item in a sequence.for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
| Code | Result |
|---|---|
| range(5) | 0, 1, 2, 3, 4 |
| range(1, 6) | 1, 2, 3, 4, 5 |
| range(0, 10, 2) | 0, 2, 4, 6, 8 |
Print numbers 1 through 5, each on a new line.
Downloading Python… (one-time, ~10 MB)
This runs entirely in your browser and is cached for next time.