'''字典对象提供了一个get()方法用来返回指定键对应的值,并且允许指定键不存在时返回特定的值'''
Dict = {'age':18,'sorce':'Zheng','sex':'male'}
#输出键对应的值
print(Dict['age'])
print(Dict['sorce'])
print(Dict['sex'])
print(Dict.get('age'))
#输出No Exists. print(Dict.get('adress','No Exists.'))
#输出None print(Dict.get('adress'))
'''这样子的语句会出错 print(Dict['adress']) '''
print('学号:2023310143008')
代码如下:
Dict = {'age':18,'sorce':'Zheng','sex':'male'}
#输出键对应的值
print(Dict['age'])
print(Dict['sorce'])
print(Dict['sex'])
print(Dict.get('age'))
print(Dict.get('adress','No Exists.'))
print(Dict.get('adress'))
print('学号:2023310143008')
执行结果如下:
标签:get,age,sex,案例,Dict,print,adress,字典 From: https://www.cnblogs.com/fang---/p/18396914