1. Create dictionaries and assign values to dictionaries , you can use the factory Method Dict () to create a dictionary, or you can use Fromkeys () to create a dictionary with the same value for an element
>>> dict = {' name ': ' Xiaoming ', ' age ':20}>>> dict{' age ': ' Name ': ' xiaoming '}>>> dict2 = Dict ([' X ', 1],[' Y ', 2])) >>> dict2{' y ': 2, ' x ': 1}>>> dict2 = {}.fromkeys (' x ', ' Y '), 1) >>> dict2{' y ': 1, ' X ': 1
2. Accessing values in the dictionary
>>> dict = {' name ': ' Xiaoming ', ' age ':20}>>> dict{' age ': ' "Name ': ' Xiaoming '}>>> for key in D Ict.keys (): ... print ' key=%s,value=%s '% (Key,dict[key]) ... key=age,value=20key=name,value=xiaoming
>>> for key in Dict: ... print ' key=%s,value=%s '% (Key,dict[key]) ... key=age,value=20key=name,value=xiaoming
>>> dict[' name '] ' xiaoming '
>>> ' name ' in Dicttrue
3. Updating dictionaries
>>> dict[' name '] = ' 123 ' >>> dict[' name '] ' 123 '
>>> del dict[' name ']>>> dict{' age ': 20}
>>> dict{' age ': 20}>>> dict.pop (' age ') 20
Python Learning notes--dictionaries