5. Repeating work with loops
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.
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.
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?
Check your understanding
1/7 · 82 XPWhat does range(2, 8) produce?