Lists & Tuples
Lists — Ordered, Mutable Collections
A list is like a train with numbered cars — under the hood, Python lists work like dynamic arrays. Each car holds one item, you can add cars, remove cars, or swap what's inside any car. They're ordered (car 0 is always first), and you can put anything inside — numbers, strings, even other lists.
Lists are created with square brackets [] and items are separated by commas.
Creating & Accessing Lists
Modifying Lists
Unlike strings, lists are mutable — you can change them after creation. You can add items, remove items, sort them, and more. Here are the essential list methods you'll use every day:
.append(x)— Add item to the end.insert(i, x)— Insert item at indexi.pop(i)— Remove and return item at indexi(defaults to last).remove(x)— Remove first occurrence of valuex.sort()— Sort in place.reverse()— Reverse in place.extend(list)— Add all items from another list
List Methods in Action
Tuples — The Immutable Cousin
A tuple is like a list that's been sealed in plastic wrap. Once created, you can't add, remove, or change items. Why would you want that? Because it's a promise: "This data won't change."
Tuples are created with parentheses () — or even without them, just commas. They're perfect for things like coordinates (x, y), RGB colors (255, 0, 128), or returning multiple values from a function.
Tuples & Unpacking
Quick check
Continue reading