ExercisesΒΆ

  1. Write a program that allows the user to enter a string. It then prints a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times each letter occurs. Case should be ignored. A sample run of the program might look this this:

    Please enter a sentence: ThiS is String with Upper and lower case Letters.
    a  2
    c  1
    d  1
    e  5
    g  1
    h  2
    i  4
    l  2
    n  2
    o  1
    p  2
    r  4
    s  5
    t  5
    u  1
    w  2
    $
    

    
    
    

    (ex_11_01)


    
    
    

    (q1_answer)

  2. Give the Python interpreter’s response to each of the following from a continuous interpreter session:

    1. >>> d = {'apples': 15, 'bananas': 35, 'grapes': 12}
      >>> d['banana']
      
    2. >>> d['oranges'] = 20
      >>> len(d)
      
    3. >>> 'grapes' in d
      
    4. >>> d['pears']
      
    5. >>> d.get('pears', 0)
      
    6. >>> fruits = d.keys()
      >>> fruits.sort()
      >>> print(fruits)
      
    7. >>> del d['apples']
      >>> 'apples' in d
      

    Be sure you understand why you get each result. Then apply what you have learned to fill in the body of the function below:


    
    
    

    (q2_dict_answer)

  3. Here’s a table of English to Pirate translations

    English Pirate
    sir matey
    hotel fleabag inn
    student swabbie
    boy matey
    madam proud beauty
    professor foul blaggart
    restaurant galley
    your yer
    excuse arr
    students swabbies
    are be
    lawyer foul blaggart
    the th’
    restroom head
    my me
    hello avast
    is be
    man matey

    Write a program that asks the user for a sentence in English and then translates that sentence to Pirate.


    
    
    

    (ex_11_04)


    
    
    

    (ch11_q5_answer)

Next Section - Working with Data Files