Python程序:将字典中的所有项目相乘
创新互联python教程:

创新互联专注于贵德企业网站建设,响应式网站开发,商城网站建设。贵德网站建设公司,为贵德等地区提供建站服务。全流程按需定制,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
写一个 Python 程序,用一个实际例子将字典中的所有条目相乘。
Python 程序将字典中的所有项目相乘示例 1
在这个 python 程序中,我们使用 For 循环来迭代字典中的每个元素。在循环的 Python 中,我们将这些值乘以总变量。
# Python Program to Multiply All Items in a Dictionary
myDict = {'x': 20, 'y':5, 'z':60}
print("Dictionary: ", myDict)
total = 1
# Multiply Items
for key in myDict:
total = total * myDict[key]
print("\nAfter Multiplying Items in this Dictionary: ", total)
在字典中增加项目的 Python 程序示例 2
这个 python 程序使用 For Loop 和值函数来相乘字典中的值。
# Python Program to Multiply All Items in a Dictionary
myDict = {'x': 2, 'y':50, 'z':70}
print("Dictionary: ", myDict)
total = 1
# Multiply Items
for i in myDict.values():
total = total * i
print("\nAfter Multiplying Items in this Dictionary: ", total)倍增 Python 字典项输出
Dictionary: {'x': 2, 'y': 50, 'z': 70}
After Multiplying Items in this Dictionary: 7000 标题名称:Python程序:将字典中的所有项目相乘
文章链接:http://www.jxjierui.cn/article/cdogiid.html


咨询
建站咨询
