dict = {‘Name‘: ‘sara‘, ‘Age‘: 7, ‘Sex‘: ‘F‘}
1.添加key,value值
dict.setdefault(‘Adderss‘,‘悦榕湾‘)
注意:如果使用
dict[‘Address‘] = ‘悦榕湾‘,会提示This dictionary creation could be rewritten as a dictionary literal
因为系统检测在程序之前有定义dict,所以不需要分行写,可以直接写到一行
2.访问字典里面的值
print(dict[‘Name‘]) 如果写了不存在的值会报错
print(dict.get(‘Name‘)) 如果写了不存在的值,返回None
print(dict.get(‘Name‘,‘not find ‘)) 如果找不到存在的值,返回 “not find”
3.
原文:https://www.cnblogs.com/qiupiao/p/14361970.html