Dictionaries Exercises

  1. Discuss with someone else how dictionaries work
  2. Discuss with someone else what is a key-value pair
  3. Compare and contrast using a dictionary and a list
  4. Discuss with someone else at least three scenarios to use a dictionary and why
  5. Dictionaries can be used to store similar items as keys and their respective values. Write Python code to create a dictionary with your cousins' names (as the keys) and ages (as the values).
  6. Dictionaries can also be used to store several items related to a single object. Write Python code to create a dictionary with at least 5 items about your favorite movie (for example, lead actor/actress, year, budget, rating, best quote, genre)
  7. Write a Python script that uses a dictionary to store student's grades. Give the user the following choices:
    1. Add a new student
    2. Update a student's grade
    3. Display a student's grade
    4. Remove a student
    5. Quit
  8. Dictionary Methods
    1. Given the following code:
      bread = {
          'title': "Grandma's Honey Whole Wheat Bread",
          'ingredients': {
              'honey': '8 oz',
              'oil': '3.5 oz',
              'water': '3.1 pounds',
              'whole wheat flour': '4.5 - 4.75 lbs',
              'yeast': '3 Tablespoons',
              'salt': '1.5 Tablespoons'
          },
          'servings': 84,
          'Nutrition Facts': {
              'calories': 105.5,
              'total fat': '1.6 g',
              'sodium': '118.2 mg',
              'potassium': '3.3 mg',
              'carbohydrates': {
      	    'total': '21.1 g',
                  'dietary fiber': '3.2 g',
                  'sugars': '2.2 g'
              },
              'protein': '3.5 g'
          }
      }
      What do each of the following display:
      1. print( '\nbread.keys()')
        for variable in bread.keys():
            print( variable )
      2. print( '\nbread.values()')
        for variable in bread.values():
            print( variable )
      3. print( '\nbread.items()')
        for variable in bread.items():
            print( variable )
      4. print( '\nbread')
        for variable in bread:
            print( variable )
  9. in Operator
    Replace _________________ with appropriate code
    trailMix = { }
    trailMix['title'] = 'Trail Mix'
    ingredientsDictionary = dict(
        peanuts = '2 cups',
        raisins = '2 cups',
        MandMs = '1 1/2 cups'
    )
    trailMix['ingredients'] = ingredientsDictionary
    trailMix['instructions'] = 'Combine all ingredients'
    
    recipes = []
    recipes.append( bread )
    recipes.append( trailMix )
    
    nutritionFactsLabel = 'Nutrition Facts'
    for recipe in recipes:
        title = recipe.get('title', 'No title')
        _________________:
            print( f"{title}'s {nutritionFactsLabel}:" )
            for nfKey in recipe[nutritionFactsLabel]:
                print( nfKey + ': ' + str(recipe[nutritionFactsLabel][nfKey]) )
        else:
            print(f'Note, {title} does not have {nutritionFactsLabel} information')
  10. Aliasing and Copying
    1. What will interpreting the following code display?
      gourmetTrailMix = dict( trailMix )
      gourmetTrailMix['title'] = 'Gourmet Trail Mix'
      print( 'gourmetTrailMix:', gourmetTrailMix )
      print( 'trailMix:', trailMix )
    2. What will interpreting the following code display?
      # assumes that ingredients is a valid key
      gourmetTrailMix['ingredients']['almonds'] = gourmetTrailMix['ingredients']['peanuts']
      del gourmetTrailMix['ingredients']['peanuts']
      print( 'gourmetTrailMix:', gourmetTrailMix )
      print( 'trailMix:', trailMix )
  11. Discuss with someone else which is faster (and why):
    1. Inserting 1,000,000 words into a sorted list (and continually maintaining it in sorted order) or inserting 1,000,000 of words into a dictionary
    2. For each one of 1,000,000 words, determining if it is in a sorted list or a dictionary