Theory

Loops repeat code multiple times.

FOR loop - for a specific number of times:

for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4

range(5) gives numbers from 0 to 4.
range(1, 6) gives numbers from 1 to 5.

WHILE loop - while condition is true:

count = 0
while count < 3:
print(count)
count = count + 1 # or count += 1

Exercises in this lesson

3

Sum Numbers

Calculate the sum of numbers from 1 to 10 using a loop. Store result in 'total' and print it. Expected result: 55

Medium Points: 20
Solve in the playground
Next lesson: Lists →