今天为大家介绍的是Python中内置数据结构之一,字典,以下为学习参考代码。
# 机构:家里蹲
# 作者:青灬火
# 时间:2021/7/18 9:14
#字典是Python内置的数据结构之一,与列表一样是一个可变序列
#以键值对的方式存储数据,字典是一个无序的序列
#字典的创建
#最常用方式:使用花括号
scores = {'张三': 100, '李四': 98, '王五': 66}
print(scores) #{'张三': 100, '李四': 98, '王五': 66}
print(type(scores)) #<class 'dict'>
#使用内置函数dict()
stu = dict(name='jact', age=20)
print(stu) #{'name': 'jact', 'age': 20}
#空字典
d = {}
print(d) #{}
#字典中元素的获取
scores = {'张三': 100, '李四': 98, '王五': 66}
print(scores['张三']) #100
#print(scores['陈六']) # KeyError: '陈六'
print(scores.get('张三')) #100
print(scores.get('陈六')) # None
print(scores.get('马奇', 99)) # 99 在查找key不存在时,返回默认值 99
#key的判断 in / not in
scores = {'张三': 100, '李四': 98, '王五&