#10编写一个Python程序来汇总字典中的所有条目
my = {'date':100,'dare':200,'dace':-34}
print(sum(my.values()))
#9编写一个Python程序,使用for循环遍历字典
my = {'date':100,'dare':200,'dace':-34}
for my_key, value in my.items():
print(my_key,my[my_key])
#8编写一个Python脚本合并两个Python字典
d1 = {'a': 100, 'b': 200}
d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)
#7编写一个Python脚本来打印一个字典,其中键是1到15之间的数字(都包括在内),值是键的平方。
d = dict()
for x in range(1,16):
d[x]=x**2
print(d)
#6编写一个Python脚本来生成并打印一个字典,字典的形式为(x, x*x),其中包含一个数字(1到n之间)。
n=int(input("Input a number "))
d = dict()
for x in range(1,n+1):
d[x]=x*x
print(d)
#5 = 9
d = {'x': 10, 'y': 20, 'z': 30}
for d_key, d_value in d.items():
print(d_key,d_value)
#4编写一个Python脚本,检查给定键是否已经存在于字典中
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
def is_key_present(x):
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')
is_key_present(5)
is_key_present(9)
python之字典的习题(一)
最新推荐文章于 2024-04-24 17:34:26 发布
