✦ Python Foundations Fifth lesson

Concept 05 · Loops

Loops —
Make Python Repeat Actions

Loops repeat a useful action without making you write the same code again and again. Learn how Python moves through a sequence or repeats while a condition stays true.

01 Learn02 Try03 Predict

Loops repeat an action for you.

A loop lets Python repeat a block of code. This saves you from writing the same instruction many times and makes it easier to work through a group of values.

for number in range(5):
    print(number)

range(5) produces 0, 1, 2, 3, and 4. The indented line runs once for each number; the stop value 5 is not included.

0 1 2 3 4

Use range(start, stop) when you want a different start. The start is included and the stop is not. You can also add a step: range(0, 10, 2) produces every second number from 0 to 8.

↳

Indentation still matters: The code inside a for or while loop must be indented. Python uses that indentation to know which instructions repeat.

Repeat while a condition is true
count = 1

while count <= 3:
    print(count)
    count += 1

A while loop checks its condition before every iteration. Here, count += 1 is shorthand for count = count + 1, so the condition eventually becomes false and the loop ends.

Stop a loop with break
for number in range(5):
    if number == 3:
        break
    print(number)

break immediately ends the loop. This prints 0, 1, and 2, then stops when the number is 3.

Skip one turn with continue
for number in range(5):
    if number == 2:
        continue
    print(number)

continue skips the rest of the current iteration and moves to the next one. This prints 0, 1, 3, and 4.

Watch out for infinite loops
count = 1

while count > 0:
    print(count)
    count += 1

This loop never makes count > 0 false, so it keeps running. Make sure a while loop can eventually reach a false condition.

Python Playground

Loading Python runtime…

Change the numbers and predict how many times each loop will run.

loops.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Follow each value from range(1, 5). What happens when the value is 3?

for number in range(1, 5):
    if number == 3:
        continue
    print(number)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about loops

Describe what feels unclear. Sloby can help you trace repetitions, ranges, and loop control.

Hi! I'm Sloby. Let's explore loops together! Ask me about for, while, range, break, or continue.

What will this program print, and why? Explain which numbers are added and what continue does.

total = 0

for number in range(1, 5):
    if number == 3:
        continue
    total += number

print(total)
Python AI Tutor · Learn by understanding, not just by running code.