CS8, 10F, H08, due Tue Lecture 10.26—Miller/Ranum 4.1-4.3 (pp. 117-128).—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H08—printable PDF

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, 10.26.
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.


Please note the following typos:

p. 122, Session 4.2:
the last two commands should involve mylist (lowercase l), not myList (uppercase L)

p. 125, session 4.5
output of list(range(10,2,-2)) should be
[10, 8, 6, 4] not [10, 8, 6, 4, 2]
also the third example of the list function should look like this:
>>> list("the quick fox")
Not like this
>>> list(''the quick fox'')
(double quotes " vs. 2 single quotes '')

The characters [ ] can be used to create a list in Python. For example:

Review p. 83-94 from Chapter 3 in your textbook, and then read pages 117-128 from Chapter 4, which explain some basics of Python lists, as well as introducing the ideas of mutable and immutable.

Then answer these questions:

  1. (5 pts) Fill in the blank: In Python, a "sequential collection of characters" is referred to as a ______________.



  2. (5 pts) What kind of sequential collection would allow us to have an integer in each position, instead of a character?

    (In lecture, we actually discussed two kinds of sequential collections that would allow for this—though the reading in Chapter 4 concentrates on one of these. I'll accept either answer as valid.)







    Please turn over for more...

    ...continued from other side

  3.  >>> x[2]=4
    Traceback (most recent call last):
      File "", line 1, in 
        x[2]=4
    TypeError: object does not support item assignment
    >>> x[2]
    5
    >>> y[2]=4
    >>> y[2]
    4
    >>> 
    

    Somewhere in Section 4.3.2, the textbook discusses two technical terms that can be applied to various kinds of collections in Python: mutable and immutable.

    Suppose x and y are variables in a Python program. The variables have been assigned some values—both of which are collections—but those values have already scrolled off the screen, and I'm going to keep them a mystery from you. The only thing I'm going to show you is the result of two operations—which you can see in the box at the right hand side of this question.

    Based on what you see in the output shown in the box:

    1. (5 pts) Is the collection that x refers to a mutable or an immutable collection? Circle one:   mutable    immutable



    2. (5 pts) Is the collection that y refers to a mutable or an immutable collection? Circle one:   mutable    immutable







  4. If the name "Sonia" were stored in a variable name in Python, I could use an index—i.e. a number inside square brackets such as [1], [2], or [-3] to pull out particular letters of the name, such as name[1], name[2], name[-3], etc.

    1. (4 pts) What do I write to give the variable name the value "Sonia"?



    2. (4 pts) Suppose I type the assignment statement in (a) at the >>> prompt.
      What would be the result if I then type name[-2] at the >>> prompt?


    3. (4 pts) (continuing....) What would be the result for name[2] ?



    4. (8 pts) What are two different expressions I could write that involve filling in the [ ] in name[   ] with some number, that would result in returning the final letter in "Sonia", that is the "a"?

      (Hint: one uses a positive index, and the other uses a negative index. Give both to get full credit.)



End of H08