Python字典简单示例

#-*-coding:utf-8-*-
import sys

if __name__=="__main__":
	dict={"a":"apple","b":"banana","c":"choice"}
	print dict
	print dict["a"]
	dict ["d"]="dog"#向字典添加键值对
	print dict
	del(dict["c"])#删除
	print dict
	print dict.pop("b")
	print dict
	for k in dict:
		print "dict[%s]="%k,dict[k]
	print dict.keys()
	print dict.values()
	dict ["f"]="find"
	if not dict.has_key("f"):
		print "key 'f' not exist"
	else:
		print dict["f"]

你可能感兴趣的:(Python)