Chapter 3 exercise set 1

Syntax and operations

What is the result of each of the following?

  1. Add a line between each line starting with a python prompt with the value that would appear when the given expression is evaluated.

    >>> 'Python'[1]
    >>> "Strings are sequences of characters."[5]
    >>> len("wonderful")
    >>> 'Mystery'[:4]
    >>> 'p' in 'Pineapple'
    >>> 'apple' in 'Pineapple'
    >>> 'pear' not in 'Pineapple'
    >>> (2, 4, 6, 8, 10)[1:4]
    >>> [("cheese", "queso"), ("red", "rojo")][1][0][2]
    
  2. You’ll need to become familiar with the different methods of each type to do these.

    >>> 'Python'.upper()
    >>> 'Mississippi'.count('s')
    >>> letters = ['c', 'z', 'b', 's']
    >>> letters.sort()
    >>> letters
    >>> (3, 9, 8, 42, 17).index(42)
    >>> "\t   \n     Don't pad me!   \n   \n".strip()
    >>> mystery = 'apples pears bananas cheesedoodles'.split()
    >>> mystery
    >>> mystery.sort()
    >>> mystery
    >>> mystery.reverse()
    >>> mystery
    
  3. What value will appear after these two lines are entered at the prompt?

    >>> word = "Pneumonoultramicroscopicsilicovolcanoconiosis"
    >>> (word[6] + word[30] + word[33] + word[15]).upper()
    

Logical opposites

Give the logical opposites of these conditions

  1. a > b

  2. a >= b

  3. a >= 18  and  day == 3

  4. a >= 18  and  day != 3

  5. 3 == 3

  6. 3 != 3

  7. 3 >= 4

  8. not (3 < 4)

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, 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
    """
    

Four friends and a movie

Write a program in a file named movie_friends.py that will produce a session something like this:

$ python3 movie_friends.py

Hmmm... You have 5 tickets to that new movie everyone wants to see.
Whom should you invite to go with you?

Enter the name of friend one: Sean
Your invite list now contains: ['Sean']

Enter the name of friend two: Jonathan
Your invite list now contains: ['Sean', 'Jonathan']

Enter the name of friend three: Margot
Your invite list now contains: ['Sean', 'Jonathan', 'Margot']

Enter the name friend four: Justin
Your invite list now contains: ['Sean', 'Jonathan', 'Margot', 'Justin']

Great!  You are ready to go to the movie...

$

What is is

What will be the output of the following program?

this = ['I', 'am', 'not', 'a', 'crook']
that = ['I', 'am', 'not', 'a', 'crook']
print("Test 1:", this is that)
that = this
print("Test 2:", this is that)

Provide a detailed explaination of the results.