# Practice Exercises for Lists#

## 1#

Create a list that contains first 6 prime numbers in ascending order. (This list should be created manually.) Print out the 2nd, 4th, and 6th numbers in this list. Prime list templatePrime list solutionPrime list (Checker)

```# Prime number lists

###################################################
# Student should enter code below

prime_numbers = [2, 3, 5, 7, 11, 13]
# 1 is neither a prime or composite number
print(prime_numbers, prime_numbers, prime_numbers)

###################################################
# Expected output

#3 7 13
```

## 2#

Given the list `a` in the template, make a new reference `b` to `a`. Update the first entry in `b` to be zero. What happened to the first entry in `a`? Explain your answer (in a comment). List reference templateList reference solutionList reference (Checker)

```# List reference problem

###################################################
# Student should enter code below

a = [5, 3, 1, -1, -3, 5]
b = a
b = 0
print(a)
print(b)

###################################################
# Explanation
# The names `a` and `b` point to the same list. So we mutated the same list.
```

## 3#

Given the list `a` in the template, make a new copy `b` of the list `a` using the function `list`. Update the first entry in `b` to be zero. What happened to the first entry in `a`? Explain your answer (in a comment). List copy templateList copy solutionList copy (Checker)

```# List reference problem

###################################################
# Student should enter code below

a = [5, 3, 1, -1, -3, 5]
b = list(a)
b = 0
# or
# b = a.copy()
print(a)
print(b)

###################################################
# Explanation
# the list that `a` points to did not change, because we created a copy of this
# list and assigned to the name `b`.
```

## 4#

Write a function `add_vector(v, w)` that takes two 2D vectors `v` and `w` (represented as lists) and returns a new 2D vector (represented as a list) that is the sum of the two vectors. Remember that vector addition is performed independently on each corresponding element of the lists. Hint: returning `v + w` does not work. Vector addition templateVector addition solutionVector addition (Checker)

```# Vector addition function

###################################################
# Student should enter code below
return [a + b, a + b]

###################################################
# Test

###################################################
# Output

#[4, 3]
#[4, 6]
#[-4, 0]
```

## 5#

Challenge: The program template below is a program designed to run two independent timers with their own start and stop buttons. In particular, each timer should be controlled by its own buttons independent of the other timer’s buttons. The current version has an error that causes both timers to work in unison. Find and fix this error. Debugging test templateDebugging test solutionDebugging test (Checker)

```# Mystery bug

# This program should implement two independent timers
# each having their own start and stop buttons.
# Find and correct the error in the code below.

import simplegui

# Initialize two counters.
counter1 = [0, 0]
counter2 = counter1  # <============
# assigning a list to another variable does not create a new list but uses the
# same list which hinders independent timers

# Define event handlers.
def start1():
timer1.start()

def stop1():
timer1.stop()

def start2():
timer2.start()

def stop2():
timer2.stop()

def tick1():
global counter
if counter1 == 9:
counter1 += 1
counter1 = 0
else:
counter1 += 1

def tick2():
global counter
if counter2 == 9:
counter2 += 1
counter2 = 0
else:
counter2 += 1

# Define draw handler.
def draw(canvas):
canvas.draw_text("Timer 1:     " + str(counter1 % 10) + "." + str(counter1), [50, 100], 24, "White")
canvas.draw_text("Timer 2:     " + str(counter2 % 10) + "." + str(counter2), [50, 200], 24, "White")

# Register event handlers.
frame = simplegui.create_frame("Mystery bug", 300, 300)
Compare this with `c` and `d` in this example