# sooele
'''字典创建方式'''
'''使用{}字典'''
scores = {'张三':100,'lisi':98,'wangwu':45}
print(scores)
print(type(scores))
'''第二种创建方式()'''
scores2 = dict(name='jack',age=20)
print(scores2)
''''空字典'''
d={}
print(d)
'''获取字典中的元素'''
scores3 = {'张三':100,'lisi':98,'wangwu':45}
'''第一种方式,使用[]'''
print(scores3['张三'])
# print(scores3['chengl']) #KeyError: 'chengl'
'''第2种方式,使用get()方式'''
print(scores3.get('张三'))
print(scores3.get('chengl')) #None
print(scores3.get('maqi',99))
'''key的判断'''
scores4 = {'张三':100,'lisi':98,'wangwu':45}
print('张三'in scores4)
print('张三'not in scores4)
'''del删除'''
del scores4['张三']
print(scores4)
'''clear清空字典'''
scores4.clear()
print(scores4)
'''新增'''
scores4['666']=98
print(scores4)
'''修改'''
scores4['666']=100
print(scores4)
'''获取字典中的key'''
scores5 = {'张三':100,'lisi':98,'wangwu':45}
keys = scores5.keys()
print(keys)
print(type(keys))
print(list(keys)) ##将所有key组成的视图转成列表
'''获取字典中的values'''
values = scores5.values()
print(values)
print(type(values))
print(list(values))
'''获取字典中的items'''
items = scores5.items()
print(items)
print(type(items))
print(list(items))
相关