Exercises
1.
{}
2.
{'fow': 42}
3. The dictionary is unordered
4. Error (keyerror)
5. The first is to search both the key and the search value, the second value of the search key is not different, the in operator checks whether a value is a dictionary key.
6. The first type is the search key and the search value, the second value of the first check key
7.
Spam.setdefault ('color'black')
Pprint functions for 8.pprint modules
Practical Projects
List of items 5.6.1 Fun Games
1Stuff = {'Rope': 1,'Torch': 6,'Gold coin': 42,'Dagger': 1,'Arrow': 12}2 3 defDisplayinventory (Inventory):4 Print("Inventory:")5Item_total =06 forKvinchInventory.items ():7 Print(Str (v) +' '+k)8Item_total + =v9 Print("Total number of items:"+str (item_total))Ten OneDisplayinventory (Stuff)
5.6.2 list to dictionary functions, List of items for fun game
1 defDisplayinventory (Inventory):2 Print("Inventory:")3Item_total =04 forKvinchInventory.items ():5 Print(Str (v) +' '+k)6Item_total + =v7 Print("Total number of items:"+str (item_total))8 9 defaddtoinventory (Inventory, addeditems):Ten forIinchAddeditems: One ifIinchInventory: AINVENTORY[STR (i)] = Inventory[str (i)] + 1#if the item was previously present, add 1 to the number of the item - Else: -INVENTORY[STR (i)] = 1#If the item does not exist before, add it to the dictionary. the -INV = {'Gold coin': 42,'Rope': 1} -Dragonloot = ['Gold coin','Dragger','Gold coin','Gold coin','Ruby'] - + addtoinventory (INV, Dragonloot) -Displayinventory (INV)
Python workbooks--5th dictionary and structured data