Class 15 - Variables and loops
Date: October 15, 2026
⚠️Homework
Homework 5 due October 18, 2026
‼️Ending Skills
Last week for skills: 1, 2
Skills: 4
Notes (review before class)
The Pattern
To compute with each element of a list, we use this pattern:
def func(lst: list) -> ...:
result = ... # what to return if the input list is empty
for item in lst:
# combine item with the result so far
result = ... item ... result
return result
Variables That Can Change
Until now, a name in the directory could never be defined again:
x = 10
# ...
x = 20 # PLL reports a reassignment, and doesn't run the file
- Python allows it, but PLL's
beginnerlevel (Class 3) doesn't: it is a common cause of bugs. - At
#level intermediate(from now on, the first line of our files), a variable defined inside a function can be changed (mutated) with=again: its name then points to the new value in the directory. - At the top level of the file, it is still reported (why: Class 17).
How for Works
- A
forloop runs its body (the lines indented under it) once per element of the list, in order, each time naming the element in the directory (item). - The loop produces no value; it computes by changing variables.
- A list of numbers has type
list[float]; of strings,list[str].
def sum_list(lst: list[float]) -> float:
"""adds up the numbers in lst"""
run_total = 0
for item in lst:
run_total = item + run_total
return run_total
def test_sum_list():
assert sum_list([5, 1, 7, 3]) == 16
On [5, 1, 7, 3], the body runs four times, as if sum_list's body were:
run_total = 0
run_total = 5 + run_total
run_total = 1 + run_total
run_total = 7 + run_total
run_total = 3 + run_total
return run_total
The loop writes this repetition for us, for a list of any length.
Try: what should sum_list([]) be, and which line decides it? Add a test.
Recap (15 mins)
A word game scores a list of words by their total number of letters. With
Class 14's built-ins, that is sum(list(map(len, words))); here it
is designed with a for loop. Read through the program:
#level intermediate
def total_length(words: list[str]) -> int:
"""the total number of letters in all of the words in words"""
total = 0
for word in words:
total = total + len(word)
return total
def test_total_length():
assert total_length([]) == 0
assert total_length(["fiddleheads"]) == 11
assert total_length(["to", "be", "or", "not"]) == 9
- Trace
total_length(["to", "be", "or", "not"]): what arewordandtotalafter each trip through the loop? - Which line decides
total_length([])? What would go wrong if that line were moved into the loop body? - If
return totalwere indented one more level, which of theasserts would still hold?
Class Exercises (40 mins)
- Design your own
productfunction that takes a list of numbers and returns their product (multiply all of them together). - Design a function
sum_even_numbersthat takes a list of integers and adds up only the even numbers -- the rest should be ignored (%, which computes the remainder after dividing, may be helpful)! - Design a function
my_lengththat takes a list of any value and returns the number of elements in the list. - Design a function
any_negativethat takes a list of numbers and returnsTrueif there is at least one negative number in the list,Falseotherwise. - Design a function
all_short_wordsthat takes a list of strings and returnsTrueif every word has 4 or fewer characters,Falseotherwise. - Design a function
reverse_listthat takes a list and returns a new list with all the elements in reverse order. You'll need to build up the result by adding each element to the front of your accumulator list. - Design a function
concat_allthat takes a list of strings and concatenates them all together into one string (with no separator between them).
Wrap-up (5 mins)
- At
#level intermediate, variables inside functions can be updated with=. forloops can run code for each element of a list; combined with variables that change, this allows writing custom list operations.