1、添加键值对
#!/usr/bin/env python i1 = {'k1':'cai' , 'k2':123} print(i1) i1['k3'] = 0 i1['k4'] = "rui" print(i1) ===================================== {'k1': 'cai', 'k2': 123} {'k1': 'cai', 'k2': 123, 'k3': 0, 'k4': 'rui'}
2、修改字典中的值
#!/usr/bin/env python i1 = {'k1':'cai' , 'k2':123} print(i1) i1['k1'] = 'rui' print(i1) ================================ {'k1': 'cai', 'k2': 123} {'k1': 'rui', 'k2': 123}
3、删除键值对
i1 = {'k1':'cai' , 'k2':123} print(i1) del i1['k1'] print(i1) ===================================== {'k1': 'cai', 'k2': 123} {'k2': 123}
#!/usr/bin/env python i1 = {'k1':'cai' , 'k2':123} for key,value in i1.items(): print(key,value) ======================================= k1 cai k2 123