Python Learning note 6-dictionaries

Source: Internet
Author: User

Defined

Using key-value pairs,

>>> person = {"name":"Keven"," Age": 15,"Gender":"male"}>>> person['name']'Keven'>>>type (person)<type'Dict'>

A dictionary can be modified in place, that is, it is mutable.

>>> dict1={}>>> ID (dict1)139736380198256>>> dict1["name  "keven">>> ID (dict1)139736380198256>>>  dict1{'name' 'keven'}

Using tuples to construct dictionaries, the method is as follows:

>>> name = ([" First","Google"],["Second","Yahoo"])>>> website =dict (name)>>>website{'Second':'Yahoo',' First':'Google'}

Or in such a way:

>>> ad = dict (name="keven", age=42)>>> ad{'  Age ' ' name ' ' Keven '

Basic operations

Len (d), returns the number of key-value pairs in the dictionary (d)

>>> City_code = {'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315'}>>>Len (city_code)4


D[key], returns the value of the key (key) in the dictionary (d)

>>> city_code["Suzhou"]'0512'

D[key]=value, assigns the value to the key (key) in the dictionary (d)

>>> City_code = {'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315'}>>> city_code["Shenzhen"]="1212">>>city_code{'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315','Shenzhen':'1212'}

• Del D[key], delete the key (key) entry for dictionary (d) (delete the key value pair)

>>>delcity_code["Shanghai"]>>>city_code{'Suzhou':'0512','Beijing':'011','Tangshan':'0315','Shenzhen':'1212'}

key in D, check if the dictionary (d) contains keys key

" Shenzhen " inch city_codetrue " sz " inch City_codefalse

Dictionaries can also implement formatted characters

>>> City_code = {"Suzhou":"0512","Tangshan":"0315","Hangzhou":"0571"}>>>"Suzhou is a beautiful city, it area code is% (Suzhou) s"%City_code'Suzhou is a beautiful city, it area code is 0512

Python Learning note 6-dictionaries

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.