字典
一个简单的字典
dict={‘a’:1,‘b’:2}
字典是一系列的键-值对,
通过键来访问对应的值(但是不可以通过值来访问键!)
值可以是数字、字符串、列表、字典
>>> alien_0={'color':'green','points':5}
>>> alien_0['color']
'green'
>>> alien_0['points']
5
>>> alien_0['green']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'green'
>>> alien_0[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
注意访问字典的方式
使用字典
访问字典中的值
>>> alien_0={'color':'green','points':5}
>>> print('you just eared '+str(alien_0['points']))
you just eared 5
添加键-值对
不需要通过append这些,直接指定【】就行
>>> alien_0={'color':'green'}
>>> alien_0['x']=0
>>> alien_0['y']=25
>>> alien_0
{'color': 'green', 'x': 0, 'y': 25}
先创建一个空字典
>>> alien_0={}
>>> alien_0['color']='