Skip to content
Back to course

5. Repeating work with loops

Imagine you work at a TeleBirr office and need to print a receipt for every one of 100 customers. Writing the same print() line 100 times is painful — and what if the number changes to 500 tomorrow? Loops solve this problem. A loop is a Python structure that repeats a block of code automatically, either for every item in a list or until a condition becomes False. In this lesson you will learn Python's two main loops: for and while.

Without a loop, repeating an action means copying code:

print("Good morning, customer 1")

print("Good morning, customer 2")

print("Good morning, customer 3")

# ... 97 more lines!

With a loop, you write the action once and Python repeats it:

for i in range(1, 4):

print("Good morning, customer", i)

Output:

Good morning, customer 1

Good morning, customer 2

Good morning, customer 3

This is the power of loops: less code, fewer mistakes, and easy to change. If you want 1 000 greetings, you just change range(1, 4) to range(1, 1001) — one edit, not a thousand lines.

A for loop goes through every item in a sequence (a list, a range of numbers, or a string) and runs the indented block once for each item.

Basic structure:

for variable in sequence:

# code to run for each item

The variable takes the value of each item in turn. After the last item the loop ends automatically.

Example 1 — loop over a list of names (Sara, Abebe, and Almaz visit the telebirr booth):

customers = ["Sara", "Abebe", "Almaz"]

for name in customers:

print("Serving:", name)

Output:

Serving: Sara

Serving: Abebe

Serving: Almaz

Example 2 — loop over a range of numbers:

for n in range(1, 6):

print(n, "x 5 =", n * 5)

Output:

1 x 5 = 5

2 x 5 = 10

3 x 5 = 15

4 x 5 = 20

5 x 5 = 25

range(1, 6) produces the numbers 1, 2, 3, 4, 5 — it starts at 1 and stops before 6. Remember: the end number is excluded.

range() has three useful forms: range(5) → 0, 1, 2, 3, 4 (start defaults to 0) range(1, 6) → 1, 2, 3, 4, 5 (start=1, stop before 6) range(0, 10, 2) → 0, 2, 4, 6, 8 (start=0, stop before 10, step=2) The third argument is the step — how much to add each time. You can count backwards too: range(5, 0, -1) gives 5, 4, 3, 2, 1.

A while loop repeats a block of code as long as a condition is True. It is useful when you do not know in advance how many times you need to repeat.

Structure:

while condition:

# code to run while condition is True

Python checks the condition before every repetition. When the condition becomes False the loop stops.

Example — a simple shop ATM that keeps allowing withdrawals as long as the balance is enough:

balance = 500 # birr

withdrawal = 150

while balance >= withdrawal:

balance = balance - withdrawal

print("Withdrawn:", withdrawal, "birr | Remaining:", balance, "birr")

print("Balance too low. No more withdrawals.")

Output:

Withdrawn: 150 birr | Remaining: 350 birr

Withdrawn: 150 birr | Remaining: 200 birr

Withdrawn: 150 birr | Remaining: 50 birr

Balance too low. No more withdrawals.

Each time the loop runs it subtracts 150 from balance. Once balance falls below 150 the condition (balance >= withdrawal) becomes False and the loop ends.

Beware of infinite loops — a while loop that never stops because its condition never becomes False. This usually happens when you forget to update the variable inside the loop: # DANGER — this runs forever! count = 1 while count < 5: print(count) # forgot to add: count = count + 1 Always make sure your while loop has a line that eventually makes the condition False. If your program seems to freeze, press Ctrl + C to stop it.

Two special keywords give you extra control inside loops:

break — exits the loop immediately, even if there are more items or the condition is still True.

continue — skips the rest of the current repetition and jumps straight to the next one.

Example using break — stop scanning items once a banned product is found in a shop inventory:

items = ["teff", "injera", "alcohol", "sugar", "oil"]

for item in items:

if item == "alcohol":

print("Banned item found:", item, "— stopping scan.")

break

print("OK:", item)

Output:

OK: teff

OK: injera

Banned item found: alcohol — stopping scan.

Example using continue — print only even numbers from 1 to 10:

for n in range(1, 11):

if n % 2 != 0: # if n is odd

continue # skip the print, go to next n

print(n)

Output: 2 4 6 8 10

Scenario

Abebe sells mobile phone top-up cards at his shop in Merkato. He writes this program to print all top-up amounts in his stock that are 50 birr or more: topups = [10, 50, 20, 100, 5, 200] for amount in topups: if amount < 50: continue print(amount, "birr card") How many lines does the program print?

Lesson recap: • A loop repeats a block of code automatically — saving you from writing the same lines many times. • A for loop goes through every item in a sequence (list, range, string) — use it when you know the items to loop over. • range(start, stop) generates numbers from start up to (but not including) stop. • A while loop repeats as long as a condition is True — use it when you do not know the count in advance. • Always update the variable in a while loop; otherwise you get an infinite loop. • break exits the loop immediately; continue skips to the next repetition. • A common loop pattern: start a total = 0 outside the loop, then add to it inside.

Check your understanding

1/7 · 82 XP

What does range(2, 8) produce?