如果使用get(key)的方法取值,不存在的键胡返回空值
d = {"first":1}
print(d.get("first"))
print(d.get("second"))
结果:
1
None
但是如果直接取值,不存在的则会报错
print(d["first"])
print(d["second"])
python 字典的不同取值方法
阅读 52
2022-11-16
如果使用get(key)的方法取值,不存在的键胡返回空值
d = {"first":1}
print(d.get("first"))
print(d.get("second"))
结果:
1
None
但是如果直接取值,不存在的则会报错
print(d["first"])
print(d["second"])
相关推荐
精彩评论(0)