Homework Solution

Using Python's Built-in Types

Problems to be Submitted (20 points)

  1. (5 points)
    groceries.insert(len(groceries), 'milk')
    
  2. (5 points)
    stringA == stringB.capitalize()
    
  3. (5 points)
    temp = list(fruit)
    temp.sort()
    diet = ' + '.join(temp)
    
  4. (5 points)
    1. ['Do', 'Re', 'Mi'].join('-')
      

      An AttributeError occurs. The join method cannot be invoked upon a list instance; it is a method of the str class. A proper example of its usage is '-'.join(['Do', 'Re', 'Mi']).

    2. 'High' + 5
      

      A TypeError occurs. There is no definition for adding a string and an integer.

    3. ['Do', 'Re', 'Mi'].insert('Fa')
      

      A TypeError occurs. The insert method expects two parameters. The first must be an index into the list at which a new element is to be placed, and the second must be that new element. In this case, we have only sent the new element.

    4. 'hello'.remove('a')
      

      An AttributeError occurs. A string does not support a method named remove.

    5. list('hello').remove('a')
      

      A ValueError occurs. Although the list supports the remove method, the particular value of the parameter was not found in that list.


Extra Credit

  1. (2 points)

    firstIndex = groceries.index('milk')
    secondIndex = groceries.index('milk', firstIndex + 1)
    groceries.pop(secondIndex)
    

Last modified: Wednesday, 03 February 2010