Python: Multiply all the items in a dictionary
Python dictionary: Exercise-11 with Solution
Write a Python program to multiply all the items in a dictionary.
Sample Solution:
Python Code:
# Create a dictionary 'my_dict' with keys 'data1', 'data2', and 'data3', along with their respective values.
my_dict = {'data1': 100, 'data2': -54, 'data3': 247}
# Initialize a variable 'result' to 1. This variable will store the product of all values in the dictionary.
result = 1
# Iterate through the keys in the 'my_dict' using a for loop.
for key in my_dict:
# Multiply the current 'result' by the value associated with the current key in 'my_dict'.
result = result * my_dict[key]
# Print the final 'result,' which is the product of all values in the dictionary.
print(result)
Sample Output:
-1333800
Python Code Editor:
Previous: Write a Python program to sum all the items in a dictionary.
Next: Write a Python program to remove a key from a dictionary.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics