#key-value 键值对 集合
l = [3,2,34,23,55,10]
stu1 = {'name':'catcubs','age':18,'gender':'男'}
print(stu1)
stu1['hobby']='Likes programming and Likes electronic games'
print(stu1['hobby'])
del(stu1['hobby'])
print(stu1)
#key-value 键值对 集合
l = [3,2,34,23,55,10]
stu1 = {'name':'catcubs','age':18,'gender':'男'}
print(stu1)
stu1['hobby']='Likes programming and Likes electronic games'
print(stu1['hobby'])
del(stu1['hobby'])
print(stu1)
增加 键值对
stu1={"name":"siki","age":"17","gender":"男"}
stu1["rank"]=3 #结尾增加"rank"=3
print(stu1)
#修改键值对
stu1={"name":"siki","age":"17","gender":"男"}
stu1["age"]=18 #修改age=18
print(stu1)
#删除age里的键值对
stu1={"name":"siki","age":"17","gender":"男"}
del(stu1["age"]) #删除age里的键值对
print(stu1)
#删除age里的键值对
stu1={"name":"siki","age":"17","gender":"男"}
del stu1["age"] #删除age里的键值对
print(stu1)
删除:del(x["x1"])
36.什么是字典(key - value)?
字典是键值对的集合,字典也是python中的数据类型。例如:stu1 = { "name":“siki”,“age”:“17”,“gender”:“男”};其中key 可以是数字、字符串、也可以是布尔类型;value 可以是任意类型数据。
37.访问字典中的数据:
字典名[键名]例如:stu1["name"]
1. 添加键值对:
stu1['rank'] = 3
2. 修改键值对(同上,直接覆盖):
stu1['rank'] = 3
3. 删除:
del(stu1['age']) 或 del stu['age']
字典和列表的相同点:
字典和列表的不同点:
dict={}
dict['a']='a'
dict['a']='b'
del(dict['a'])