Post

dictationary

p83_dictationary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
dic = {'points': 5}  # 创建一个字典.(一对空大括号则创建空字典)
print(dic)

dic["color"] = "green"  # 如果没有对应的key值则添加,有则修改
print(dic)

# 1.遍历
for k, v in dic.items():
    print(k+"="+str(v))

# 2.遍历所有key
for k in dic.keys():
    print(k)

# 3.遍历所有value
for v in dic.values():
    print(v)

# 4.判断key是否在字典里
f = 'points' in dic
print(f)

del dic['points']  # 删除
print(dic)
This post is licensed under CC BY 4.0 by the author.