Control flow · Lesson 9 of 25
Loops: for and while
range, and looping over things.
8 minutes3 games
for loops over anything with items. range(5) gives 0 to 4 and range(1, 6) gives 1 to 5. while repeats while its condition is true.
Python
for i in range(1, 4):
print("Round", i)
for letter in "abc":
print(letter.upper())
count = 3
while count > 0:
print(count)
count -= 1
print("Go!")Step by step1 / 6
- ▶total = 0
- 2for n in range(1, 3):
- 3 total += n
- 4print(total)
Variables
| total | 0 |
Output
…
What's the output?
for i in range(3):
print(i)What will this code print?
Complete the code
Print the numbers 1 to 10.
for n in range(, ): print(n)
Exercise
Print the sum of 1 to 100 (the result is 5050).
Expected output: 5050
Python
total = 0
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.