Preparation

Verify how Python reacts if you consecutively execute the following instructions in an interactive Python 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. >>> fruit = d.keys()
    >>> fruit.sort()
    >>> print(fruit)

  7. >>> del d['apples']
    >>> 'apples' in d

Make sure you understand why the different results are generated. 

Assignment

Apply what you have just learned to complete the body of the function below. Your implementation must endure the doctest given.

def add_fruit(basket, fruit, amount=0):

    """
    Add a certain amount of fruit to the basket.

    >>> new_basket = {}
    >>> add_fruit(new_basket, 'strawberries', 10)
    >>> 'strawberries' in new_basket
    True
    >>> new_basket['strawberries']
    10
    >>> add_fruit(new_basket, 'strawberries', 25)
    >>> new_basket['strawberries']
    35
    """