1 h02 CS8 F17
Name:
(as it would appear on official course roster)
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h02: Perkovic 2.3 (Lists, Tuples)

true Tue 10/10 08:00AM Tue 10/17 08:00AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)

1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work.

Also: while we strongly prefer that you submit your homework on a single sheet of paper, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.

DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.

2. Section 2.3 describes lists and tuples in Python. Assuming the following assignment statements have been entered at the Python prompt:

schools = ("UCSB","Stanford","UCLA","UCSD","Cal Tech")
schedules = [ ["CMPSC 8","MATH 3A"], ["CMPSC 8","PSTAT 5A"],["MATH 3B"] ]
units = [8, 8, 4]


What would be the result of entering the following at the Python interactive shell prompt?

(Note: You are encouraged to check your answers at the Python prompt before turning in your work, but try this on paper first, just by reading the text and trying to predict what will happen. Then try typing in the results at the Python prompt. Change your answers if they were mistaken, but even more important, try to figure out why you were incorrect.)

Be very precise. Note that True is not the same in Python as true; upper vs. lower case matters. You will not get full credit for answers that are not precisely correct. And note that "UCSB" and ["UCSB"] are not the same in Python—one is a string, and the other is a list of length one containing a single string.

Points Expression Result Points Expression Result
(4 pts) len(schedules)   (4 pts) sum(units)
(4 pts) len(units)   (4 pts) "CMPSC 8" in schedules[0]
(4 pts) len(schedules[1])   (4 pts) "UCSD" in schools
(4 pts) len(schools[-1])   (4 pts) "U" in schools[1]
(4 pts) min(units)   (4 pts) "MATH 3A" in schedules
3. (5 pts) As described in section 2.3, lists and tuples are similar, but there are two big differences. One is that lists are written with square brackets [] while tuples are written with parentheses (). What is the other big difference between lists and tuples?

4. (5 pts) Write a line of Python code that assigns the variable mySchool to have a value that is a tuple of length 1, containing the single string "UCSB".

5. Assume that the following sequence of statements has been entered at the Python prompt. Note that subsequent statements may change the value of the variables (e.g. colors is altered by the call to the append method)

colors = ["red","red","green"]
colors.append("blue")
colors.reverse()

Points Expression Result Points Expression Result
(4 pts) len(colors)   (4 pts) colors[-1][-1]
(4 pts) colors[0]   (4 pts) colors[2][0]
(4 pts) colors.count("red")   (4 pts) "blue" not in colors
(4 pts) colors.count("blue")   (4 pts) colors.count(colors[-1])
(4 pts) len(colors[1])   (4 pts) "e" in colors[0]