Python 程序可对字典中的所有项目进行乘法运算
当需要对字典中的所有元素进行乘法运算时,将对字典中的键值进行迭代。键乘以前一个键,并确定输出。
字典是一组键值对。
示例
以下是相同的演示 -
my_dict = {'Jane':99,'Will':54,'Mark':-3} my_result = 2 for key in my_dict: my_result = my_result * my_dict[key] print("The reuslt of multiplying keys in a dictionary is : ") print(my_result)
输出
The reuslt of multiplying keys in a dictionary is : -32076
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
说明
- 定义了一个字典。
- 将一个变量指定为某个值。
- 对字典中的“键”进行迭代。
- 在每一步,此键将乘以先前指定的变量。
- 此值被赋予相同的变量本身。
- 它作为输出显示在控制台上。
广告