字典是一对映射数据类型,或者理解为关联数组或哈希表,由键-值成对构成,key-value。字典元素用{}括起来。
示例:
>>> dict={'host':'earth'}
>>> dict['port']=80 #增加元素到字典
>>> dict
{'host': 'earth', 'port': 80}
>>> dict['host']
'earth'
>>> dict.keys()
['host', 'port']
>>> dict.values()
['earth', 80]
>>> for key in dict:
print key,dict[key]
host earth
port 80