Data made simple: variables, lists and tables
Lesson 2 of 4
2Data made simple: variables, lists and tables
The big pictureA program works on data. Arrange it in a shape that fits the question and the solution is two lines; arrange it at random and it is a page.
What the picture shows: Five shapes cover most programs you will write in your first year. Each answers a different question quickly.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
A variable is like a kitchen jar labelled "sugar". A list is a shelf of jars numbered from 0. A dictionary is a phone book: look up a name, get the number. A queue is the bakery line, and a stack is a pile of plates: the last plate you put on is the first you take.
In memory a variable is a name bound to a value. A list (dynamic array) gives you an item by index in O(1) time, but searching it for a value is O(n). A dictionary (hash table) gives the value by key in O(1) on average. A queue (FIFO) and a stack (LIFO) are constraints on a list that guarantee the exit order.
Before You Start
🧩Algorithms without code
You know sequence, decision and repetition from the previous lesson.
The Core Idea
1A variable: a named box
You write price = 25 and the program keeps a place named price holding 25. You can read and change it: price = price + 5. A good name explains itself: price beats x.
2A list: a numbered row starting at zero
A list keeps things in order. The first item is number 0, not 1; it confuses every beginner once and then becomes habit.
groceries = ["milk", "bread", "dates"]
print(groceries[0]) # first item
groceries.append("tea") # add at the end
print(len(groceries), groceries)3A dictionary: look up by name, not number
When you ask "what does milk cost?" you do not want to walk the whole shelf. A dictionary links a key to a value, so you reach the value directly.
prices = {"milk": 6, "bread": 3, "dates": 25}
print(prices["dates"])
prices["tea"] = 12
total = 0
for item in ["milk", "dates"]:
total += prices[item]
print("total:", total)4Queue and stack: who leaves first?
A queue (FIFO): first in, first out, like customer service tickets. A stack (LIFO): last in, first out, like the editor's "undo" button that cancels the latest change first.
from collections import deque
line = deque(["Ali", "Sara"])
line.append("Noura") # joins the back
print("served:", line.popleft()) # the first one leaves
undo = ["typed A", "typed B", "deleted line"]
print("undo:", undo.pop()) # the last action goes firstWorked example
Problem: a shop wants how many times each product sold from a sales log. Solution: a list for the log (it is in time order) and a dictionary for the counts (we look up by name).
sales = ["tea", "milk", "tea", "dates", "tea", "milk"]
counts = {}
for item in sales:
counts[item] = counts.get(item, 0) + 1
print(counts)Myth vs Reality
| Myth | Reality |
|---|---|
| Lists start at 1 like normal counting. | In most languages they start at 0, because the index means "how many steps from the start". |
| All data lives in lists. | Lists are for order, dictionaries for lookup, queues for fair turns. The wrong choice slows the program. |
Test your understanding
You want an employee's phone number from their name as fast as possible. What do you use?
A dictionary reaches the value by key directly; a list needs a walk through the items.
fruits = ["apple", "fig", "date"] what is fruits[1]?
Indexing starts at 0, so item 1 is the second one.
An editor's "undo" cancels the latest change first. Which shape fits?
Last in, first out: the definition of a stack.
Quick review cards
Check Your Understanding
Why is counting sales with a dictionary better than two parallel lists?
Because the dictionary finds a product's counter directly by name, while two lists need a search for the name every time and manual syncing.
Give an example of a queue in a real system.
A print queue, support tickets, or messages waiting to be sent: all served in arrival order.
What goes wrong with index 3 in a list of 3 items?
The last valid index is 2, so index 3 is out of range and raises an error. The classic off-by-one.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Official docs1
Done with this lesson?
Sign in to save your progress.