下面是字典的访问和遍历方法:
d = {10:"hello",20:"python",30:"world"}
print(d[10],"--",d[20],"--",d[30])
print(d.get(10))
print("以上两种访问方式的区别是,d[key]若键是空值,则报错;而d.get(key)若键为空值,可以指定输出默认值")
print(d.get(10,"键值不存在"))
print(d.get(40,"键值不存在"))
print("\n字典的遍历")
for item in d.items():
print(item)
for key,value in d.items():
print(key,value)
for i,j in d.items():
print(i,j)