Python workbooks--5th dictionary and structured data

Source: Internet
Author: User
Tags pprint

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.