Lists: The Workhorse
If variables are boxes, lists are shelves of boxes — one name holding many values, in order. You'll use lists in literally every project from here to the end of the course.
Creating and reading
languages = ["Python", "Go", "Rust"]
marks = [85, 92, 78, 90]
mixed = ["Aarav", 16, True] # allowed, but rare in practice
print(languages[0]) # Python ← index starts at 0
print(languages[2]) # Rust
print(languages[-1]) # Rust ← negative = from the end
print(len(languages)) # 3Slicing — grabbing sections
Same slice syntax as strings: start:stop, stop excluded:
marks = [85, 92, 78, 90, 66, 88]
print(marks[1:3]) # [92, 78] positions 1 and 2
print(marks[:2]) # [85, 92] first two
print(marks[-2:]) # [66, 88] last two
print(marks[::-1]) # reversed! the famous trickModifying lists — the five essential moves
todo = ["homework", "gym"]
todo.append("sleep") # add to the END
todo.insert(0, "wake up") # insert AT a position
todo.remove("gym") # remove by VALUE (first match)
last = todo.pop() # remove & return the LAST item
todo[0] = "meditate" # overwrite by index
print(todo)Useful built-ins
marks = [85, 92, 78, 90]
print(sum(marks)) # 345
print(max(marks)) # 92
print(min(marks)) # 78
print(len(marks)) # 4
print(sorted(marks)) # [78, 85, 90, 92] — NEW sorted list
print(92 in marks) # True — membershipNote sorted(marks) returns a new list; marks.sort() sorts in place (changes the original, returns None). Different tools, different habits:
marks.sort() # modifies marks itself
marks.reverse() # also in placeLists + loops: the power couple
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(f"I like {fruit}")
# With index (when you need the position too):
for i, fruit in enumerate(fruits):
print(f"{i + 1}. {fruit}")Output:
I like apple
I like banana
I like mango
1. apple
2. banana
3. mangoenumerate gives you both the position and the value — the professional way to number things (no manual counters).
Lists are mutable — and that has consequences
Unlike strings, lists change in place. This creates the most surprising behavior in beginner Python:
a = [1, 2, 3]
b = a # b points at the SAME list — no copy!
b.append(4)
print(a) # [1, 2, 3, 4] ← a changed too!To make a real copy: b = a.copy() or b = a[:]. You met this in the operators practice — now you know the full story.
Common Errors & Fixes
.remove("x") on something absent. Check first: if "x" in todo:.sorted(lst) returns a new list; lst.sort() changes in place and returns None. Don't write lst = lst.sort()!✅ Checkpoint
marks[-1] of [85, 92, 78]? *(78)*append and insert? *(End vs specific position)*sorted(lst) vs lst.sort()? *(New list vs in-place)*b change a? *(b = a copies the reference, not the list)*Next: tuples and sets — two specialized containers that solve specific problems.