✦ 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

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Loops repeat an action for you.

A loop repeats a block of code, so you do not have to write the same instruction again and again. Use for to repeat once for each value, and while to repeat while a condition is True. Each pass through a loop is called an iteration.

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

range(5) produces 0, 1, 2, 3, and 4. First, number gets 0 and Python runs the indented block. Then number gets 1 and the block runs again. This continues through 4; when there are no more values, the loop ends. number is a variable whose value changes on each iteration.

for → once for each value while → repeat while a condition is True

A for loop follows values one by one. A while loop keeps checking a condition. In both cases, Python runs the indented block, then either moves to the next value or checks the condition again.

↳

Indentation still matters: As in Conditions, indentation tells Python which instructions belong to the block. Every indented line in a for or while loop repeats; code outside the indentation runs after the loop finishes.

Why loops exist
print("Hello")
print("Hello")
print("Hello")

# The same action with a loop
for number in range(3):
    print("Hello")

Without a loop, you repeat the same instruction yourself. With a loop, Python repeats the indented block for you. The point is not the word "Hello"; it is that one block can run many times.

The whole indented block repeats
for number in range(3):
    print(number)
    print("Inside the loop")

print("Loop finished")

Both indented print() lines run during every iteration. print("Loop finished") is not indented, so it is not part of the loop and runs once after the loop ends.

range() has start, stop, and step
range(5)        # 0, 1, 2, 3, 4
range(2, 5)     # 2, 3, 4
range(0, 10, 2) # 0, 2, 4, 6, 8

With one argument, range(5) starts at 0 and stops before 5. With start and stop, range(2, 5) includes 2 but excludes 5. A third argument is the step: range(0, 10, 2) moves forward by 2 each time. Think: start included → stop excluded → step controls the jump.

Repeat while a condition is True
count = 1

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

A while loop connects directly to Conditions: Python checks count <= 3. If it is True, Python runs the indented block, changes count, and checks again. When the condition becomes False, the loop stops. count += 1 is shorthand for count = count + 1.

A while loop needs a change
count = 1

while count > 0:
    print(count)

count starts as 1, so count > 0 is True. Nothing inside this loop changes count, so the condition stays True forever. When you write a while loop, ask: what will change so that this condition can eventually become False?

break leaves the loop completely
for number in range(5):
    if number == 3:
        break
    print(number)

break immediately ends the whole loop. The flow is 0 → 1 → 2 → 3 → break → STOP. When number == 3, Python runs break and never continues to 4.

continue skips one iteration
for number in range(5):
    if number == 2:
        continue
    print(number)

continue skips the rest of this iteration and moves to the next value. The flow is 0 → 1 → 2 → continue → 3 → 4. Unlike break, it does not end the loop: break stops the loop, while continue skips one iteration.

Trace a value that changes
total = 0

for number in range(1, 4):
    total = total + number

print(total)

The value of total stays between iterations. Start with total = 0; when number = 1, total becomes 1; then number = 2 makes it 3; and number = 3 makes it 6. The final output is 6.

When to think for or while
for number in range(3):
    print(number)

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

Use for when you have a sequence or range of values to go through. Use while when repetition depends on a condition staying True. A helpful first question is: do I have values to go through, or a condition to keep checking?

Python Playground

Loading Python runtime…

Before running the code, predict the output of the for loop and how many times the while loop will run. Change range(), the starting value, or the condition, then compare the result with your prediction.

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

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.