# CS8, 10F, H14, due Tue Lecture 11.23—Miller/Ranum, 4.5.3 (pp. 135-139), Python Dictionaries—Total points: ?

 Name: (4 pts) Umail Address: (4 pts) @umail.ucsb.edu
 Lab Section (2 pts)—circle one: 9am 10am 11am noon unknown crashing
 You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here: (He/she should also enter your name on her/his assignment.)

This assignment is due in Lecture on Tuesday, 11.23.
It may ONLY be submitted in Lecture, at 2pm on Tuesday.

You must come IN PERSON to turn it in during your assigned Lecture section.

The reading for this assignment takes us back to Chapter 4, to a concept we skipped over before: the idea of a dictionary. Please read sections 4.5.1,4.5.2 and 4.5.3 (pp. 131-142), then answer these questions.

1. (10 pts) Section 4.5.1 describes computing the mean of a list, and listing 4.4 shows how to do that.
However, if the list is empty, an error will result, like this:

```>>> mean([])
Traceback (most recent call last):
...
ZeroDivisionError: int division or modulo by zero
>>>
```

Rewrite Listing 4.4 so that if the list is empty, the function returns the string "N/A" instead of producing an error.

2. (4 pts) Section 4.5.2 describes the median. What is the median of the following list of numbers?
[10, 100, 200, 1000]

...continued from other side

1. Listing 4.5 (p. 134) contains the following line of code:
`copylist = alist[:]`

The book describes the purpose of this line of code—but to understand it further, please try the commands below in Python. These commands make two lists, `list1` and `list2` and then use assignment statements to make two new lists, `list1copy`, and `list2copy`. Note list1copy is assigned directly from `list1`, while `list2copy` uses a "slice" that copies the entire list `[:]`

```list1=[40,10,30,20]
list2=[45,15,35,25]
list1copy=list1
list2copy=list2[:]

list1copy.sort()
list2copy.sort()
```

Then, type `list1`, `list2`, `list1copy` and `list2copy` one at a time at the Python prompt to see what each of the values are.

1. (3 pts) What does this show about the difference between doing a straight assignment (like `list1copy=list1`) vs. doing an assignment with the slice operator (like `list2copy=list2[:]`)?

2. (3 pts) Now, go back to line 2 of Listing 4.5, and explain what would be different if the line were `copylist = alist`

2. Section 4.5.3 describes how to set up a Dictionary in Python—something that can translate one kind of value into another (e.g. a name into an age). In the example shown on p. 136 of the dictionary ages, the name is the key, and the age is the value.
1. (5 pts) Suppose we want to set up a dictionary called `gauchos` that has as its keys the last names of players on the UCSB Men's Soccer team, and as the values, the Jersey numbers.

Write a line of code that sets up the dictionary with only a mapping from "Garza" to 17, and "Opuku" to 18.

2. (5 pts) Write a line of code that adds Silva with jersey number 7 to the `gauchos` dictionary you defined in the previous problem—similar to how Session 4.13 on p. 136 adds Kelsey with age 19 to the `ages` dictionary.

3. (5 pts) Suppose you turn your back and your pair partner adds an additional number of team members to the dictionary—but you aren't sure how many. What can you write to return exactly the number of team members in the dictionary (as an int?)

4. (5 pts) What can you write that will return True if "Pontius" was added to the dictionary while your back was turned, and False if "Pontius" was not added to the dictionary?

End of H14