#定义字典 dic = {'a':'aaaa', 'b':'bbbb'} print(type(dic)) print(dic['b']); # 混合使用 name='myName' sex = '男'; dic2 = {name:'张三', 'sex':sex} print(dic2) #{'myName': '张三', 'sex': '男'} #添加元素 dic['c'] = 'cccc'; #修改 dic['b'] = '123'; # 删除 del dic['a'] # 循环 for k in dic: print(dic[k]) #输出: #cccc #123 # 弹出元素,(删除并且返回键为‘b’的元素) print(dic.pop('b')) #123 # 清空字典 dic.clear() print(dic) #{} # 删除整个字典 del(dic) #print(dic); #变量销毁了,所以会发生报错NameError: name 'dic' is not defined print('################################################') dic3 = {'a':'aaaa', 'b':'bbbb', 'c':'cccc'} print(dic3.fromkeys('b','123')) print(dic3) help(dic3.fromkeys)