< Back

Activity 24

Dictionaries

Dictionaries are very similar to Lists in Python. The only difference is that the keys (indicies) of a dictionary can be any immutable data type. This means that your indicies can be strings now! This is incredibly useful, but can open a brand new can of worms.


For each of the following, describe what you expect the program to output.

  1.   my_dictionary = {"Smith": "120B", "Bouchard": "120A"}
      print(my_dictionary["Smith"])
    
  2.   my_dictionary = {"Smith": "120B", "Bouchard": "120A"}
      print(my_dictionary["Shende"])
    
  3.   my_dictionary = {"Smith": "120B", "Bouchard": "120A"}
      print(my_dictionary["Bouchard"])
    
  4.   my_dictionary = {"Smith": "120B", "Bouchard": "120A"}
      my_dictionary["Shende"] = "250A"
      print(my_dictionary["Shende"])
    
  5.   my_dictionary = {"Smith": "120B", "Bouchard": "120A", "Shende": "250A"}
      print(my_dictionary.keys())
    
  6.   my_dictionary = {"Smith": "120B", "Bouchard": "120A", "Shende": "250A"}
      print(my_dictionary.values())
    
  7.   my_dictionary = {"Smith": "120B", "Bouchard": "120A", "Shende": "250A"}
      del my_dictionary["Bouchard"]
      print(my_dictionary.keys())
    
  8.   my_dictionary = {"Smith": "120B", "Bouchard": "120A", "Shende": "250A"}
      del my_dictionary["Minton"]
      print(my_dictionary.keys())