One price in a variable is useful. A whole day's sales needs something that holds many values. That's a list.
Lists
A list goes in square brackets, with items separated by commas:
sales = [45000, 120500, 8700, 310000, 56200]
print(len(sales), sum(sales), max(sales), min(sales))
5 540400 310000 8700
len() counts the items, sum() adds them, max() and min() find the largest and smallest. Lists can hold text too:
branches = ["Ikeja", "Lekki", "Abuja", "Port Harcourt", "Kano"]
branches.append("Ibadan")
branches
['Ikeja', 'Lekki', 'Abuja', 'Port Harcourt', 'Kano', 'Ibadan']
.append() adds an item to the end.
Picking items out by position

Each item has a position, called its index, and counting starts at 0:
print(branches[0]) # the first item
print(branches[-1]) # the last item
print(branches[1:3]) # items 1 and 2 (stops before 3)
Ikeja
Ibadan
['Lekki', 'Abuja']
[1:3] is a slice: it starts at index 1 and stops before index 3. It's the most common surprise for new programmers. You'll get used to it fast.
for loops: repeat for each item
A loop runs the same instructions for every item in a list:
for branch in branches:
print(f"Sending report to {branch} branch")
Sending report to Ikeja branch
Sending report to Lekki branch
Sending report to Abuja branch
Sending report to Port Harcourt branch
Sending report to Kano branch
Sending report to Ibadan branch
Notice the colon at the end of the for line and the indent (four spaces) on the line below. The indented lines are the body of the loop. Everything indented runs once for each item. Colab indents for you when you press Enter after a colon.
Building up a result
A very common pattern: start with an empty total, then add to it inside the loop.
sales = [45000, 120500, 8700, 310000, 56200]
total_with_vat = 0
for amount in sales:
total_with_vat = total_with_vat + amount * 1.075
print(f"₦{total_with_vat:,.2f}")
₦580,930.00
You can write total_with_vat += amount * 1.075 as a shortcut for the same thing.
range(): counting
range(n) gives the numbers 0 up to, but not including, n. It's handy when you need to repeat something a set number of times:
for week in range(1, 5):
print(f"Week {week}")
Week 1
Week 2
Week 3
Week 4
Check yourself
Answer in your head first, then open each question to see the answer and why.
What is branches[0] if branches = ["Ikeja", "Lekki", "Abuja"]?
- A "Ikeja"
- B "Lekki"
- C "Abuja"
- D An error
Show the answer
A) "Ikeja" — Positions start at 0, so index 0 is the first item.
What does branches[-1] give?
- A The last item in the list
- B The first item
- C An error, because positions can't be negative
- D The number of items
Show the answer
A) The last item in the list — Negative positions count from the end; -1 is the last item.
In a for loop, how does Python know which lines belong inside the loop?
- A They are indented under the line that ends with a colon
- B They are in capital letters
- C They end with a full stop
- D They are in a separate file
Show the answer
A) They are indented under the line that ends with a colon — Indentation defines the body of the loop.
More free lessons: What Python can do for your work · Loading data and taking a first look · Fixing text and numbers stored as text · Combining many files into one · Using AI coding assistants wisely