Python程序:将字典中的所有项目相乘-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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://kswsj.cn/article/cdogiid.html

其他资讯