Chapter 3 exercise set 2

More 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.

    >>> 'NVCC Rocks!'[5]
    >>> "Strings are sequences of characters."[-2]
    >>> len("What's all this then? Amen!")
    >>> 'Mystery'[:4]
    >>> 'x' in 'Aardvark'
    >>> 'pin' in 'Pinapple'
    >>> 'a' not in 'Pinapple'
    >>> (2, 3, 5, 7, 11, 13, 17)[-3:]
    >>> [("cheese", "red", "sing"), ("queso", "red", "cantar")][0][2][0]
    
  2. You’ll need to become familiar with the different methods of each type to do these.

    >>> 'Python'.upper()
    >>> 'We are all in this together!'.count('e')
    >>> numbers = [11, 7, 42, -3, 0, 18]
    >>> numbers.sort()
    >>> numbers
    >>> (2, 3, 5, 7, 11, 13, 17, 19, 23).index(7)
    >>> "\t   \n     Just the facts, mam!   \n   \n".strip()
    >>> nomystery = "These are the times that try men's souls".split()
    >>> nomystery
    >>> nomystery.sort()
    >>> nomystery
    >>> nomystery.reverse()
    >>> nomystery
    

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

What are you learning?

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

$ python3 learning_what.py

So, tell me one thing you are learning in that course: HTML
Your list of skills now contains: ['HTML']

Name another thing you are learning: CSS
Your list of skills now contains: ['HTML', 'CSS']

And another: Python
Your list of skills now contains: ['HTML', 'CSS', 'Python']

Wow!  I should take that class too.

$