Learning Goals
3 minBy the end of this lesson you can:
- Create a
listwith square brackets and read any item back using its index — starting from0. - Count how many items a list holds using
len(). - Add a new item to the end of a list with
.append()and see it appear when you print the list.
Warm-Up
5 minLast lesson, we used nested for loops to paint shapes — rows of columns. Today we still loop, but over a collection of values instead of a range of numbers.
Quick predict-the-output puzzle. Read it carefully — what does Python print?
foods = ["nasi lemak", "roti canai", "satay"] print(foods[0]) print(foods[2])
Show the answer
nasi lemak satay
The first item sits at index 0, not 1. That zero-counting habit will save you headaches all the way to Level 8 — every list, string and array in Python starts from 0.
A list is one variable that holds many items in order. Square brackets to build it, square brackets to read from it, .append() to grow it, len() to measure it.
New Concept · The List
12 minThe treasure-chest analogy
Imagine a row of small numbered boxes inside one big chest. Each box holds one value. The chest itself has one name — the variable. To find a treasure, you call the chest's name and the box number.
drinks = [ "teh tarik" , "kopi-O" , "milo ais" , "sirap bandung" ] index → 0 1 2 3
The chest is called drinks. Four drinks live inside, in fixed positions. Reading drinks[2] gives you the third one — "milo ais" — because Python counts boxes from 0.
Tool 1 · Creating a list
Open a pair of square brackets, drop items inside separated by commas, save the whole thing in a variable. A list can hold any type — strings, numbers, even other lists later on.
prices = [5.50, 3.00, 8.00, 2.50] names = ["Aisyah", "Wei Jie", "Priya"] empty = []
That last one — [] — is an empty list, zero items, ready to be filled. You'll start one of those whenever the items aren't known in advance.
Tool 2 · Reading by index
The index sits in square brackets right after the variable name. The first item is [0], the second is [1], and so on. Negative indices count from the back — [-1] is the last item.
names = ["Aisyah", "Wei Jie", "Priya"] print(names[0]) # → Aisyah print(names[1]) # → Wei Jie print(names[-1]) # → Priya
If a list has 3 items, the valid indices are 0, 1 and 2 — never 3. Asking for names[3] crashes with IndexError: list index out of range. The chest only has so many boxes.
Tool 3 · len() — how big is the chest?
The built-in len() returns the number of items in a list. Same function you met for strings in PY-L1-05 — it counts characters there, items here.
names = ["Aisyah", "Wei Jie", "Priya"] print(len(names)) # → 3
Tool 4 · .append() — drop a new item in
Lists can grow. .append() adds one new item to the end of the list. The dot syntax is new — it means "ask this list to do something for me".
names = ["Aisyah", "Wei Jie"] names.append("Priya") print(names) # → ['Aisyah', 'Wei Jie', 'Priya']
Notice we don't write names = names.append("Priya"). .append() changes the list in place — it doesn't hand back a new one. Treating its return value as the new list is a classic first-week bug.
Worked Example · The Hawker Order Pad
12 minPhase 1 · The first three orders
Pei Shan runs a tiny stall at Pasar Pagi Taman Connaught. We're going to build her a Python order pad that starts with three dishes, lets her add a new one, and reports how many are on the pad.
Save as order_pad.py:
Code · phase 1
# order_pad.py — Pei Shan's hawker order pad orders = ["nasi lemak", "char kway teow", "mee goreng"] print("First order:", orders[0]) print("Last order :", orders[-1]) print("Total items:", len(orders))
Output
First order: nasi lemak Last order : mee goreng Total items: 3
Six lines. The list comes first, then three print lines — one using a positive index, one using -1 for the last, one using len() for the count. Notice how print happily takes a label and a value, separated by a comma.
Phase 2 · A new customer walks in
A customer asks for satay. We append it to the list and reprint the count — without retyping the whole list.
Code · phase 2 (add to the same file)
orders.append("satay") print("Last order :", orders[-1]) print("Total items:", len(orders))
Output · phase 2
Last order : satay Total items: 4
What changed?
One line of new code (orders.append("satay")) added one new item. orders[-1] still asks for the last item — but the "last" has shifted. len() went from 3 to 4 automatically. The list always knows how big it is; we just ask.
Every app you'll ever write — high-score tables, shopping carts, chat messages, exam questions — leans on this exact shape. A list that grows, and a way to read any item back by position.
Try It Yourself
13 minThree exercises, twenty minutes total. Type the starter, then make the change. Don't open the hint until you've given it a real try — a broken first attempt is still progress.
Create a list called drinks holding three Malaysian drinks. Print the first and the third drink on separate lines, and print how long the list is.
Hint
drinks = ["teh tarik", "kopi-O", "milo ais"] print(drinks[0]) print(drinks[2]) print(len(drinks))
Remember: indexing starts at 0. The third drink lives at index 2, not 3.
Start from the same drinks list. Ask the user for a new drink with input(), append it to the list, then print the whole list and the new total count.
Hint
drinks = ["teh tarik", "kopi-O", "milo ais"] new = input("Add a drink: ") drinks.append(new) print(drinks) print("Total:", len(drinks))
Try it twice with different inputs. The list grows each run — but only inside that single program run. We'll learn to save lists to a file in Level 2.
Create a list of five hawker dishes you'd order on a long weekend. Use a for loop to print each dish on its own line, numbered 1 to 5 at the start of the line.
1. nasi lemak 2. char kway teow 3. roti canai 4. satay 5. cendol
Hint
dishes = ["nasi lemak", "char kway teow", "roti canai", "satay", "cendol"] for i in range(len(dishes)): print(i + 1, ".", dishes[i])
The trick is range(len(dishes)) — it gives you the indices 0, 1, 2, 3, 4. We add 1 to the printed number because humans count from 1, but Python lists count from 0.
Mini-Challenge · Aiman's Pasar Pagi Bill
8 minAiman is helping his mum at the Saturday morning market in Shah Alam. She asks him to write a quick Python script that keeps a running list of prices and tells her the total at the end.
Your file market_bill.py must:
- Start with an empty list called
prices. - Use a
forloop that runs exactly4times. Each pass:- Ask the user for a price using
input()— convert it to a float withfloat(). appendthe price toprices.
- Ask the user for a price using
- After the loop, print the full list of prices.
- Use
sum()(a built-in, just likelen()) on the list to print the total in the formTotal: RM 19.50.
Stretch goal. Print the highest price too, using max() — another built-in that takes a list and gives back the biggest item.
Show one possible solution
# market_bill.py — Aiman's pasar pagi bill prices = [] for i in range(4): p = float(input("Price (RM)? ")) prices.append(p) print("Items:", prices) print("Total: RM", sum(prices)) print("Most expensive: RM", max(prices))
The non-negotiables are: prices = [] to start empty, the for loop with range(4), float(input(...)) for each price, prices.append(p), and sum(prices) for the total. Your prompt wording and decimal formatting can vary — we'll learn to format prices with two decimal places when we meet f-strings in Level 2.
Recap
3 minA list is one variable that holds many items in order. Square brackets build it. An index in brackets reads from it. len() measures it. .append() grows it. That tiny set of moves unlocks every collection-shaped problem in programming — leaderboards, inventories, message logs, shopping carts.
Vocabulary Card
- list
- An ordered collection of items, written with square brackets —
["a", "b", "c"]. Items keep the order you put them in. - index
- The position number of an item in a list, starting from
0.my_list[0]is the first item;my_list[-1]is the last. - len()
- A built-in function that returns the number of items in a list (or characters in a string).
- .append()
- A list method — written with a dot — that adds one new item to the end of the list. Changes the list in place; returns nothing.
Homework
4 minCreate a new file called favourite_dishes.py — a personal favourites list of yours.
Rules:
- Start with a list called
dishesthat already contains three of your favourite Malaysian dishes. - Print the list, the first dish (using index
0), and the count usinglen(). - Ask the user (
input()) for two more dishes — one at a time — andappendeach one to the list. - Print the new list, the last dish (using index
-1), and the new count. - Stretch. After all five dishes are in, use a
forloop to print each dish on its own numbered line — exactly like Try-It-Yourself exercise 3.
Bring favourite_dishes.py next class — we'll loop straight through it (without writing a single [i]) in PY-L1-16.
Sample · favourite_dishes.py
# favourite_dishes.py — a growing list of favourites dishes = ["nasi lemak", "roti canai", "char kway teow"] print("Starting list:", dishes) print("First dish :", dishes[0]) print("Count :", len(dishes)) extra1 = input("Add a dish: ") dishes.append(extra1) extra2 = input("Add another: ") dishes.append(extra2) print("Updated list :", dishes) print("Last dish :", dishes[-1]) print("Count :", len(dishes)) # Stretch — numbered list for i in range(len(dishes)): print(i + 1, ".", dishes[i])
Your three starting dishes will be different — that's the whole point. The non-negotiables are: a starting list with three items, two input + append pairs, and at least one access by index. Your prompt wording can vary freely.