1.字典的定义
d = {key:value,key2:value2}
d["key"]
2.字典的增删改查
增加:如果key不存在,就添加,否则就是修改
dic = {"name":"admin","age":20} dic["gender"]="male" print(dic) #{'gender': 'male', 'age': 20, 'name': 'admin'}
删除:del dic[key] :如果不存在key,则抛异常
查询:
dic[key],如果不存在,抛异常
dic.get("age"),如果不存在,返回空
3.for遍历
for tmp in dic:
print(tmp) #tmp是key
else:
print('end')
只有是通过break 跳出时,不会执行else
dic.keys() ->>[] (python2) -->对象(python3)
dic.values() ->>[]
dic.items() --> [('name','admin'),('age',12)]
for ...else:
for s in arr:
if s=="":
print('find')
break;
else:
print('not find')
d = {'a':10, 'b':20} for key in d: print(key) for key, value in d.items(): print(key, value)