05 字典和集合
字典
字典由一些列键-值对构成,键值对包含在一对花括号里。键值用冒号分割,多个键值用逗号分隔。
创建字典
elist={1:"小猫",3:"小狗",5:"黄牛"} print(type(elist)) print(elist[3]) //小狗
字典数据修改
print("=========================") elist={1:"小猫",3:"小狗",5:"黄牛"} print(type(elist)) print(elist[3]) #修改元素 elist[3] = "羊" #添加元素 elist[7] = "水牛" #删除元素 del elist[1] for key,value in elist.items(): print(key,value)
遍历字典
print("=========================") elist={1:"小猫",3:"小狗",5:"黄牛"} print(type(elist)) print(elist[3]) #修改元素 elist[3] = "羊" #添加元素 elist[7] = "水牛" #删除元素 del elist[1] #遍历键值 for key,value in elist.items(): print(key,value) print("=========================") #遍历所有键 for key in elist.keys(): print(key) print("=========================") #遍历所有值 for value in elist.values(): print(value) print("=========================")