1
h02
CS8 M19-A
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
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 Ch2.3 (Lists and Tuples)

ready? assigned due points
true Tue 06/25 09:30AM Tue 07/02 09:30AM

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,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)


READING ASSIGNMENT

Please read Perkovic Ch2.3 (Lists and Tuples). Then complete these problems and turn in your completed homework in lecture on the due date.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  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","MIT","UCLA","Cal Tech")
    schedules = [ ["CMPSC 8","MATH 3A"], ["CMPSC 8","PSTAT 5A"],["MATH 3B"] ]
    units = [4, 4, 8]
    

    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) "UCI" in schools  
    (4 pts) len(schools[-1])   (4 pts) "U" in schools[1]  
    (4 pts) max(units)   (4 pts) "MATH 3B" 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","green","green"]
    colors.append("orange")
    colors.reverse()
    
    Points Expression Result Points Expression Result
    (4 pts) len(colors)   (4 pts) colors[-1][-2]  
    (4 pts) colors[0]   (4 pts) colors[2][0]  
    (4 pts) colors.count("green")   (4 pts) "blue" not in colors  
    (4 pts) colors.count("blue")   (4 pts) colors.count(colors[-2])  
    (4 pts) len(colors[1])   (4 pts) "an" in colors[0]