字典

1.一个简单的字典

alien_0 ={"color":"green","points" : 5}
print(alien_0["color"])
print(alien_0["points"])

2.添加键-值对
alien_0 ={"color":"green","points" : 5}
print(alien_0["color"])
print(alien_0["points"])

alien_0["x_positon"] = 0
alien_0["y_positon"] = 25
print(alien_0)

3.创建空字典
alien__1 ={}
alien__1["color"] ="green"
alien__1["points"] = 5
print(alien__1)

4.修改字典中的值
aline_2 ={"color":"green"}
print("The alien is "+aline_2["color"]+".")
aline_2["color"] ="yellow"
print("The alien is "+aline_2["color"]+".")

5.删除键-值对
alien_3 ={'color': 'green', 'points': 5}
print(alien_3)
del alien_3["points"]
print(alien_3)

6.在字典中存储列表
favorute_langusges = {
'jen': ['python', 'ruby'], 'sarah': ['c'], 'edward': ['ruby', 'go'], 'phil': ['python', 'haskell'],
}

for name ,langusges in favorute_langusges.items():
print("\n" + name.title() + "'s favorite languages are:")
for langusge in langusges:
print("\t" + langusge.title())


# 存储所点比萨的信息
pizza = { 'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese'],}
# 概述所点的比萨
print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:")
for topping in pizza['toppings']:
print("\t" + topping)
7.在字典中存储字典
 
posted @ 2020-11-02 16:59  Z小訾  阅读(108)  评论(0)    收藏  举报