Lists & Tuples
4. Lists & Tuples
4.1Storing Collections of Items (Lists)
Imagine a grocery list written on paper — you can list items in order, add new items at the bottom, or cross things off.
SHOW:
WHAT HAPPENED HERE
[]. We grabbed the first item using fruits[0] and counted the items using len().THE TECHNICAL NAME
TAKEAWAY
["a", "b"] to store ordered items in a list.4.2Modifying Lists (Adding & Removing)
Unlike text strings, lists can be changed after you create them — you can append new items or delete existing ones.
SHOW:
WHAT HAPPENED HERE
.append("eggs") added a new item to the end of the list. .pop(0) removed and returned the item at index 0 ("milk").THE TECHNICAL NAME
TAKEAWAY
.append() and remove with .pop().4.3Slicing & Searching Lists
Just like strings, you can take a slice of a list or check if an item is inside it.
SHOW:
WHAT HAPPENED HERE
[0:2] sliced out the first two scores. The word in checked if the number 72 existed in the list and returned True.THE TECHNICAL NAME
in is a membership operator. Extracting sub-lists uses list slicing.TAKEAWAY
[start:stop] to slice lists and item in list to search.4.4Fixed Collections (Tuples)
Think of your birthdate (month, day, year) — once set, it never changes.
SHOW:
WHAT HAPPENED HERE
(). Then we unpacked the values into two separate variables lat and long.THE TECHNICAL NAME
TAKEAWAY
(), are immutable (cannot be changed), and support unpacking.GOTCHA 1
Trying to append or modify elements in a tuple (tuples are immutable!).
GOTCHA 2
IndexError: trying to access an index larger than len(list) - 1.
GOTCHA 3
Confusing .append(x) (adds x as one item) with .extend(x) (adds all elements of x).
WHERE THIS SHOWS UP IN REAL CODE
Apps store shopping items, task lists, and high scores as ordered lists, while fixed pairs like GPS coordinates (lat, long) use tuples.
TRY THIS YOURSELF
Create a list colors = ['red', 'green'], append 'blue', and print colors[-1].
Notice how append() adds to the back while pop(0) removes from the front.
Demonstrate list mutation (append/pop), list slicing, and tuple immutability.
What syntax creates a Python list?
What is the key difference between a list and a tuple?
What does `items.append('notebook')` do?
What does `items.pop(0)` do?
How do you count the total number of items in a list `data`?
What happens if you try to do `t = (1, 2); t[0] = 9`?
What does `'apple' in fruits` return if 'apple' is in the list?
What is tuple unpacking?
What does `nums[1:3]` return for `nums = [10, 20, 30, 40]`?
# comments · lists-tuples
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.