PCC·04 · Chapter record
Working with Lists
Chapter 4 — looping, generating, slicing, copying, and locking lists
Five sessions on doing real work with lists: looping through every item, generating numeric lists with range(), grabbing a slice, copying a list properly, and tuples — lists that refuse to change. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
for Loops
Concept
- A for loop repeats the same action for every item in a list, without repetitive code and without caring how long the list is.
for magician in magicians:pulls one name at a time from the list and assigns it tomagician— every indented line under it runs once per item.- You can write as many indented lines as you like inside the loop — each one runs on every single pass.
- A line after the loop that isn't indented runs only once, after the loop finishes — perfect for a summary message.
- Python uses indentation, not braces, to know what's inside the loop — forget to indent and you get an IndentationError.
Key Terms
- for loop
- repeats a block of code once for each item in a list
- loop variable
- the temporary name (magician, cat, item…) holding the current item
- IndentationError
- raised when Python expects an indented line and doesn't find one
Exercise
Store three foods you like in a list called pizzas, then use a for loop to print a sentence about each one.
Store a few animals in a list, then use a for loop to print one fact about each animal.
Inside a loop over a list of names, print two lines per person — a greeting, then a farewell with a blank line after — then add one summary line after the loop that runs only once.
Making Numerical Lists
Concept
range()generates a series of numbers —range(1, 5)produces 1, 2, 3, 4, stopping one short of the second value, the same off-by-one rule as slicing.- Wrap
list()around arange()call to turn it into an actual list:list(range(1, 6)). - A third argument to range() sets a step size, letting you skip numbers —
range(2, 11, 2)gives the even numbers 2 to 10. min(),max(), andsum()work directly on a list of numbers.- A list comprehension collapses the "empty list + loop + append" pattern into one line:
squares = [value**2 for value in range(1, 11)].
Key Terms
- range(start, stop, step)
- generates numbers from start up to (not including) stop
- min() / max() / sum()
- the smallest, largest, and total of a list of numbers
- list comprehension
- a one-line way to build a list from a loop and an expression
Exercise
Use a for loop with range() to print the numbers 1 through 20, inclusive.
Use the third argument of range() to build a list of the odd numbers from 1 to 20, then print each one with a for loop.
Build a list of the first 10 cubes (value ** 3) two ways: with a loop, then again as a one-line list comprehension.
Slicing a List
Concept
- A slice grabs a subset of a list with
list[start:stop]— like range(), it stops one before the second index. - Omit the start index to begin at the very beginning:
players[:4]. Omit the stop index to go all the way to the end:players[2:]. - Negative indexes work in slices too —
players[-3:]grabs the last three items, however long the list is. - You can loop through just a slice with a for loop:
for player in players[:3]:.
Key Terms
- slice
- a subset of a list, written list[start:stop]
Exercise
Using a list of at least 5 items, print the first three items, three items from the middle, and the last three items — each with its own slice.
Loop through just the first three items of a list with a for loop and a slice, printing each one in title case.
Copying a List
Concept
- To copy a list, slice the whole thing:
friend_foods = my_foods[:]— this makes a genuinely separate list. - Just writing
friend_foods = my_foodsdoes not copy anything — both names now point to the exact same list, so changing one changes the other. - This matters because lists are mutable: appending to a fake "copy" silently changes the original too.
Key Terms
- list[:]
- a slice of the entire list — the correct way to copy one
- mutable
- able to be changed after creation, like a list
Exercise
Make a list of pizzas you like, copy it properly into friend_pizzas, then add a different pizza to each list and print both to prove they're separate.
Repeat the exercise, but this time set friend_pizzas = pizzas directly instead of slicing — add a pizza to one list and see it appear in both.
Tuples
Concept
- A tuple looks like a list but uses parentheses () instead of square brackets — and once created, it can't be changed.
- Python calls this immutable. Access tuple items the same way as list items:
dimensions[0]. - Try to reassign a tuple item and Python raises a TypeError — tuples don't support item assignment.
- You can still loop over a tuple with a for loop, exactly like a list.
Key Terms
- tuple
- an ordered, immutable collection written with ( )
- immutable
- can't be changed after creation
- TypeError
- raised when you try to assign to a tuple's item
Exercise
A rectangle's dimensions are stored as a tuple (200, 50). Try changing dimensions[0] to 250 in the widget below and read the error — then explain in one sentence why Python refuses.