python字典的增删查改

info = {"id":1,"name":"黄同学","age":18}

# '''
# 增
# newID= input("请输入新的学号:")
# info["id"] =newID
# print(info["id"])
# '''


#删
#[del]
#[clear] 清空
# print("删除前: %s"%info["name"])
# del info["name"] #删除指定的键值对,会报错
#print("删除后: %s"%info["name"])
# print("删除前:%s"%info)
#
# del info
# print("删除后:%s"%info)

# print("清空前:%s"%info)
#
# info.clear()
# print("清空后:%s"%info)


#改
#
# info["age"]=20
#
# print(info["age"])

#查 遍历
# print(info.keys()) #得到所有的键
# print(info.values()) #得到所有的值
#
# print(info.items()) #得到所有的项

#遍历所有的值
#
# for key in info.keys():
#   print(key)
# for value in info.values():
#   print(value)

# for key,value in info.items():
#   print("key=%s,value=%s"%(key,value))


#使用枚举函数,同时拿到列表中的下标和元素内容
# mylist = ["a","b","c","d"]
#
#
# for i,x in enumerate(mylist):
#   print(i+1,x)

望关注

你可能感兴趣的:(笔记,python)