1 # 创建字典 2 d=dict(name=‘lily‘,age=18,phone=‘188011514000‘) 3 print(d) 4 # {‘name‘: ‘lily‘, ‘age‘: 18, ‘phone‘: ‘188011514000‘} 5 6 # 通过key获取对应vlaue 7 name =d[‘name‘] 8 print(name) 9 # lily 10 11 # in,not in 判断key是否存在 12 if ‘name‘ in d: 13 print(‘name is exist‘) 14 else: 15 print(‘no‘) 16 17 # 获取字典所有的key 18 keys = d.keys() 19 print(keys) 20 # dict_keys([‘name‘, ‘age‘, ‘phone‘]) 21 22 # dict.values() 返回一个包含字典所有value的列表 23 vlaue = d.values() 24 print(vlaue) 25 # dict_values([‘lily‘, 18, ‘188011514000‘]) 26 27 # dict.items()返回一个包含所有(键,值)元组的列表 28 keyandvlaue = d.items() 29 print(keyandvlaue) 30 # dict_items([(‘name‘, ‘lily‘), (‘age‘, 18), (‘phone‘, ‘188011514000‘)]) 31 32 # dict.fromkeys(seq, val=None) 创建并返回一个新字典,以seq中的元素做该字典的键,val做该字典中所有键对应的初始值(默认为None) 33 lk =[‘xiao‘,‘da‘,‘hong‘] 34 dic = dict.fromkeys(lk) 35 print(dic) 36 # {‘xiao‘: None, ‘da‘: None, ‘hong‘: None} 37 38 39 a = lambda x:x+1 40 print (a(3))