Iterations and Loops

You need to print a word letter by letter — each on its own line. Without a loop that's six separate print calls: one for "P", one for "y", one for "t", and so on. A longer word means more lines by hand, and if the word only becomes known while the program runs, you can't write them out in advance.

A loop spares you that: you describe the step once, and Python repeats it for each letter on its own. One such repetition is called an iteration.

Python has two loops: for walks through a ready-made set of values, while keeps going as long as a condition holds. Let's start with the first.

The for loop

A string is a sequence of characters, and for can walk straight through it:

Python 3.13
message = "Python"

for char in message:
    print(char)
P
y
t
h
o
n

It reads almost like English: "for each character char in the string message — print it". Python takes the values one by one, binds each to the name char, and runs the indented body. When the values run out, the loop ends — you don't have to track that yourself.

for walks any set of values the same way, not just strings: lists, tuples and dictionaries are waiting in the chapters ahead, and you'll walk them with this very loop.

Iterating over a range of numbers

Often we need to execute a loop a certain number of times. For this, the range() function is used:

Python 3.13
# Using range() to create a sequence of numbers
for i in range(5):  # from 0 to 4
    print(f"Number: {i}")
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4

The range() function creates a sequence of numbers and has several usage options.

The examples below use print(..., end=" "): by default print appends a newline at the end, but the end=" " parameter replaces it with a space so values print on a single line.

Python 3.13
# Range() usage options

# range(stop): from 0 to stop-1
for i in range(3):
    print(i, end=" ")
print()  # For line break
0 1 2
# range(start, stop): from start to stop-1
for i in range(2, 5):
    print(i, end=" ")
print()
2 3 4
# range(start, stop, step): from start to stop-1 with step
for i in range(1, 10, 2):  # Odd numbers from 1 to 9
    print(i, end=" ")
1 3 5 7 9

The while loop

The while loop executes as long as the specified condition remains true. This is useful when we don't know in advance how many times the loop should execute.

While loop syntax

Python 3.13
# Simple example of a while loop
count = 1

while count <= 5:
    print(f"Loop is executing for the {count}th time")
    count += 1  # Increasing the counter
Loop is executing for the 1th time
Loop is executing for the 2th time
Loop is executing for the 3th time
Loop is executing for the 4th time
Loop is executing for the 5th time

The loop runs while the condition count <= 5 is true. Inside, you must change the variable from the condition (here count += 1), otherwise the loop will be infinite.

Infinite loop

If the condition in a while loop is always true, the loop will execute infinitely. This can be useful, but usually requires a way to exit such a loop:

Python 3.13
# Example with explicit breaking of an infinite loop
counter = 0

while True:  # This condition is always true
    print(f"Iteration {counter}")
    counter += 1

    if counter >= 5:  # Condition to exit the loop
        print("Exiting the loop!")
        break  # Interrupts the loop execution
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Exiting the loop!

Loop control

Python provides several tools for controlling loop execution. The two main ones are break and continue.

The break statement

The break statement allows you to immediately exit a loop, regardless of the condition:

The loop walks through numbers 0–9, hits break at 5 and exits the loop; numbers 6–9 are not visited

Python 3.13
# Using break to exit a loop
for i in range(10):
    print(i, end=" ")
    if i == 5:
        print("\nReached number 5, exiting the loop!")
        break
0 1 2 3 4 5
Reached number 5, exiting the loop!

The continue statement

The continue statement skips the current iteration and moves to the next one:

The loop walks through numbers 0–5, hits continue at 3, skips over it, and resumes at 4

Python 3.13
# Using continue to skip iterations
for i in range(10):
    # Skip even numbers
    if i % 2 == 0:
        continue
    print(i, end=" ")
1 3 5 7 9

The else block in loops

for and while loops can have an else block that runs when the loop ends normally (not via break):

Python 3.13
for i in range(5):
    print(i, end=" ")
else:
    print("\nLoop completed without break")
0 1 2 3 4
Loop completed without break

The feature exists, but it's rarely used in real-world code.

Nested loops

Loops can be nested inside each other to process multidimensional data structures or solve more complex problems:

Python 3.13
# Example of nested loops - multiplication table
for i in range(1, 4):  # Rows
    for j in range(1, 4):  # Columns
        print(f"{i} × {j} = {i*j}", end="\t")
    print()  # Moving to a new line after each row of the table
1 × 1 = 1	1 × 2 = 2	1 × 3 = 3
2 × 1 = 2	2 × 2 = 4	2 × 3 = 6
3 × 1 = 3	3 × 2 = 6	3 × 3 = 9

Understanding check

Let's check how well you've understood the topic of loops:

What result will the following code give?

Python 3.13
total = 0
for i in range(1, 5):
    if i % 2 == 0:
        total += i
print(total)