python_test_03

# 4.有一个字典a={1:2,2:3,3:5},求字典键的和,键所对应的值的和,请写出代码

a = {1: 2, 2: 3, 3: 5}

# 方法1:

b = c = 0
for key,value in a.items():
    b += key
    c += value
print(b,c)

# 方法2:

b = c = 0
for key,value in a.items():
    b,c = b + key,c+value
print(b,c)

# 方法3:

print(sum(a.keys()))
print(sum(a.values()))

猜你喜欢

转载自blog.csdn.net/weixin_44786482/article/details/88825195