学习字典;
- 创建字典,
- 删除某一项,
- 增加某一项,
- 遍历字典并演示;
- 查找字典是否有存在这个值;
# 学习字典;创建字典,删除某一项,增加某一项,遍历字典并演示;查找字典是否有存在这个值;
# 'ab'是地址(Address)簿(book)的缩写
ab = {
'Swaroop' :'swaroop@qq.com',
'Larry' :'larry@163.com',
'Matsumoto':'matsumoto@aliyun.com',
'Spammer' : 'spammer@sina.com'
}
print("Swaroop's address is",ab['Swaroop'] )
#删除一对键值
del ab['Spammer']
print('\n There are {} contacts in the address-book \n'.format(len(ab)))
for name,address in ab.items():
print('Contace {} at {}'.format(name,address))
#添加一组
ab['Guido'] = 'guido@gmail.com'
if 'Guido' in ab:
print("\nGuido's address is ",ab['Guido'])