python7、字典: dic={‘Python’: 86,'Math':90,'English': 85} 请编写程序解决以下问题,并输出相应结果。 (1)求字典的长度 (2)将‘Python’的value值改为96 (3)删除'English'的键和value值 (4)增加‘chemistry’,其value值是92 (5)将字典中所有的key值存到列表a中 (6)判断 Math 是否在字典中 (7)求字典里value值的和 (8)求字典里最大的value值
时间: 2024-02-17 16:02:34 浏览: 33
好的,以下是你的问题的解答:
```python
# 定义字典
dic = {'Python': 86, 'Math': 90, 'English': 85}
# (1) 求字典的长度
print(len(dic)) # 输出 3
# (2) 将'Python'的value值改为96
dic['Python'] = 96
print(dic) # 输出 {'Python': 96, 'Math': 90, 'English': 85}
# (3) 删除'English'的键和value值
del dic['English']
print(dic) # 输出 {'Python': 96, 'Math': 90}
# (4) 增加'chemistry',其value值是92
dic['chemistry'] = 92
print(dic) # 输出 {'Python': 96, 'Math': 90, 'chemistry': 92}
# (5) 将字典中所有的key值存到列表a中
a = list(dic.keys())
print(a) # 输出 ['Python', 'Math', 'chemistry']
# (6) 判断 Math 是否在字典中
if 'Math' in dic:
print('Math在字典中')
else:
print('Math不在字典中') # 输出 Math在字典中
# (7) 求字典里value值的和
sum_value = sum(dic.values())
print(sum_value) # 输出 278
# (8) 求字典里最大的value值
max_value = max(dic.values())
print(max_value) # 输出 96
```
希望这个解答对你有所帮助!