CS8, 09M, UCSB

H04: (Based on Miller/Ranum, Chapter 4 (up to p. 128) Total points: ? (printable PDF)

Accepted: on paper, in lab (either at 11am, or 12:30pm, in Cooper Lab) on Thursday, August 13th
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Lab Section (2 pts) Circle one:         Thu 11am                 Thu 12:30pm                     Unknown

Circle the one you plan to attend, which may or may not be the one you are registered for.


Review p. 83-94 from Chapter 3 in your textbook, and then pages 117-128 from Chapter 4 p. 128.

Please note that there is a typo on p. 125: the third example of the list function should look like this, i.e. double quotes:

>>> list("the quick fox")

Not like this (two single quotes together):

>>> list(''the quick fox'')


Then answer these questions:

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


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


  3. 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 pull out particular letters of the name, such as name[1], name[2], name[-3], etc.
    1. (3 pts) What do I write to assign the variable name to the value "Sonia"?


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

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


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

      (Hint: one uses a positive index, and the other uses a negative index)

  4. (5 pts) As explained in Chapter 4, the characters [ ] can also be used to indicate a list in Python. For example: Chapter 4 introduces the terms "mutable" and "immutable". What do we mean when we say that a list is mutable, but a string is mutable?

    Please turn over for more problems

    Continued from other side

  5. (10 pts) If we assign a variable x, y or z to be equal to some list, we can determine the length of that list with the len function, just like we do with strings (see the example in the left column below.)

    Given that, please write a function hasLengthZero(x) that returns True if x has length zero, and False if it does not. Assume that x is either a string or a list.

    Some test cases appear in the right column below.

    Example of the len() function operating on lists:
    Test cases for the function you are supposed to write
    >>> x = ["Go", "Gauchos"]
    >>> y = [2,3,5,7,11,13,17]
    >>> z = ["Phill", "Conrad", 3,79, 5551212]
    >>> len(x)
    2
    >>> len(y)
    7
    >>> len(z)
    5
    >>> 
    hasLengthZero("") returns True
    hasLengthZero([]) returns True
    hasLengthZero([3]) returns False
    hasLengthZero("foo") returns False








  6. (10 pts) Write a function isLong(s) that takes a string s as a parameter, and returns True if the length of that string has more than 5 characters, and returns False if it has 5 characters or fewer.|

    Test cases:

Hints:















End of H04