字典

out = {'name':'Liu','age':22}
print(out['name'])

'''获取所有的键'''
print(out.keys())

''' 获取所有的值'''
print(out.values())

'''获取所有的键值'''
print(out.items())

'''根据K获取值,如果K不存在,可以指定一个默认值'''
t = out.get('name')
print(t)
y = out.get('val',123)
print(y)

'''检查字典中指定Key是否存在'''
ret = 'name' in out.keys()
print(ret)

'''更新'''
wow = {'city':'Beijing','weather':'smog'}
out.update(wow)
print(out)

'''清空字典'''
out.clear()
print(out)

'''添加字典'''
方法(1)
out.update({'k2':123})
print(out)
方法(2)
out['k2']=123

 

posted @ 2016-12-12 14:09  200ML  阅读(151)  评论(0编辑  收藏  举报