Chapter 3 Exercise Set 1: Doctest Exercises

Strings, lists, and tuples doctest exercises

Download test_scaffolding.py, a file containing the test scaffolding you were introduced to in Chapter 2 Exercise Set 1: Introducing Doctest, or copy it from here:

"""
Put your tests here:

  >>>

"""
# Put your solutions here:


if __name__ == "__main__":
    import doctest
    doctest.testmod()

Copy it to a new file for each doctest exercise.

  1. """
      >>> type(thing1)
      <class 'list'>
      >>> type(thing2)
      <class 'tuple'>
      >>> type(thing3)
      <class 'str'>
    """
    
  2. """
      >>> thing[3]
      7
      >>> len(thing)
      5
    """
    
  3. """
      >>> seq[2:5]
      [13, 11, 9]
    """
    
  4. """
      >>> whatsthis[2]
      42
      >>> type(whatsthis[4])
      <class 'list'>
      >>> whatsthis[6:8]
      [11, 'what is this?']
      >>> len(whatsthis)
      10
    """
    
  5. """
      >>> thing[2] = 17
      >>> thing[2]
      17
    """
    
  6. """
      >>> tricky[4]
      'this'
      >>> tricky[8]
      42
      >>> type(tricky[7])
      <class 'tuple'>
      >>> type(tricky[0])
      <class 'list'>
      >>> len(tricky)
      12
    """
    
  7. """
      >>> a_list[3]
      42
      >>> a_list[6]
      'Ni!'
      >>> len(a_list)
      8
    """
    
  8. """
      >>> b_list[1:]
      ['Stills', 'Nash']
      >>> group = b_list + c_list
      >>> group[-1]
      'Young'
    """
    
  9. """
      >>> 'war' in mystery_list
      False
      >>> 'peace' in mystery_list
      True
      >>> 'justice' in mystery_list
      True
      >>> 'oppression' in mystery_list
      False
      >>> 'equality' in mystery_list
      True
    """
    

More strings, lists and tuples doctest exercises

  1. """
      >>> type(thing)
      <class 'list'>
      >>> type(thing[3])
      <class 'tuple'>
      >>> type(thing[0])
      <class 'str'>
      >>> type(thing[2])
      <class 'list'>
      >>> len(thing)
      4
      >>> 8 in thing
      True
    """
    
  2. """
      >>> another_thing[1]
      'happiness'
      >>> len(another_thing)
      5
      >>> 42 in another_thing
      True
      >>> type(another_thing) == type([])
      False
    """
    
  3. """
      >>> seq[2:5]
      [13, 11, 9]
    """