← CS Unplugged

CS Unplugged activity

Trace it: lists and loops

About 35 minutesSolo, then compare with a partnerPencil

Before you run code, you should be able to say what it will do. Professional programmers do this all the time. It’s how they spot a bug by reading, instead of running the program over and over and hoping.

The tool for this is a trace table: one column for each variable, and a new row every time a variable changes. You play the computer, one line at a time.

How to trace: a worked example

scores = [3, 8, 5]
total = 0
for s in scores:
    total = total + s
print(total)

The loop runs once for each item in scores. Each time around, s is the next item, and total gets s added to it.

Step s total
before the loop (none yet) 0
first time around 3 3
second time around 8 11
third time around 5 16

The loop is out of items, so Python moves on to print(total), which prints 16.

Notice that total is written once before the loop. If it were inside the loop, it would reset to 0 every time around. That is one of the most common list bugs there is.

Your turn

Predict first, in pencil. Don’t peek at the answers until you’ve written something for every problem.

1. Indexes

pets = ["cat", "dog", "fish", "newt"]
print(pets[1])
print(pets[-1])
print(len(pets))

What prints? Line 1: Line 2: Line 3:

2. Building a list

evens = []
for n in range(5):
    if n % 2 == 0:
        evens.append(n)
print(evens)
n n % 2 == 0? evens after this step
     
     
     
     
     

What prints?

3. Finding the biggest

temps = [61, 58, 72, 70]
biggest = temps[0]
for t in temps:
    if t > biggest:
        biggest = t
print(biggest)
t t > biggest? biggest
(before the loop)    
     
     
     
     

What prints?

Why does the code start biggest at temps[0] instead of at 0? (Hint: what if every temperature were below zero?)

4. Two names, one list

a = [1, 2, 3]
b = a
b.append(4)
print(a)

What prints?

Explain in one sentence why.

5. Put it in order

These lines count how many words have more than three letters, but they’re scrambled. Number them 1 to 6 in the order they should run. The indentation is correct, so use it as a clue.

OrderLine
count = count + 1
print(count)
words = ["the", "ocean", "is", "salty", "and", "deep"]
if len(w) > 3:
count = 0
for w in words:

When the lines are in order, what prints?

Check your answers

1. dog, then newt, then 4. Indexes start at 0, so pets[1] is the second item. -1 counts from the end.

2. range(5) gives 0, 1, 2, 3, 4. Only 0, 2 and 4 pass the test, so evens grows [0], then [0, 2], then [0, 2, 4]. It prints [0, 2, 4].

3. biggest starts at 61. 61 is not bigger than 61, 58 is not bigger, 72 is (so biggest becomes 72), 70 is not. It prints 72. Starting at temps[0] works for any list; starting at 0 would give the wrong answer if every temperature were negative.

4. It prints [1, 2, 3, 4]. b = a doesn’t copy the list. It gives the same list a second name, so changing it through b changes what a sees too. (Chapter 9 calls this aliasing.)

5. The order is:

words = ["the", "ocean", "is", "salty", "and", "deep"]
count = 0
for w in words:
    if len(w) > 3:
        count = count + 1
print(count)

(Lines 1 and 2 can swap; either works.) It prints 3: ocean, salty, deep.

CC BY-NC-SA 4.0.