In the previous article, we have discussed Python Program to Check if a String is a keyword or Not
Dictionary in python :
A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.
example:
dict = {‘january’ :1, ‘febrauary’: 2, ‘march’: 3 }
Given a dictionary, and the task is to find the Product of values of elements in a dictionary.
Examples:
Example1:
Input:
Given dictionary = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6}
Output:
The Product of values in a given dictionary = 211200
Example2:
Input:Â
Given dictionary = {'a': 1, 'b': 5, 'c': 2, 'd': 4, 'e': 7, 'f': 2}
Output:
The Product of values in a given dictionary = 560
Program to Find Product of Values of elements in a Dictionary
Below are the ways to Find the Product of Values of elements in a Dictionary.
Method #1: Using For Loop (Static Input)
Approach:
- Give the dictionary by initializing it with some random values and store it in a variable.
- Get all the values of the given dictionary using the dictionary.values() method and store it in another variable.
- Take a variable to say ‘product’ and initialize its value with ‘1’
- Iterate in the above-given dictionary values using For loop.
- Inside the loop, Multiply the above-initialized product variable with the iterator and store it in the same variable.
- Print the product of values for the above-given dictionary.
- The Exit of the program.
Below is the implementation:
# Give the dictionary as static input and store it in a variable. gvn_dict = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6} # Get all the values of given dictionary using dictionary.values() method # and store it in another variable. dict_vlue = gvn_dict.values() # Take a variable say 'product' and initialize it's value with '1' fnl_prod = 1 # Iterate in the above given dictionary values using using For loop. for itrator in dict_vlue: # Inside the loop, Multiply the above initialized product variable with the iterator # and store it in a same variable. fnl_prod = fnl_prod*itrator # Print the product of values for the above given dictionary. print("The Product of values in a given dictionary = ", fnl_prod)
Output:
The Product of values in a given dictionary = 211200
Method #2: Using For Loop (User Input)
Approach:
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Get all the values of the given dictionary using the dictionary.values() method and store it in another variable.
- Take a variable to say ‘product’ and initialize its value with ‘1’
- Iterate in the above-given dictionary values using For loop.
- Inside the loop, Multiply the above-initialized product variable with the iterator and store it in the same variable.
- Print the product of values for the above-given dictionary.
- The Exit of the program.
Below is the implementation:
# Take a dictionary and initialize it with an empty dictionary using dict() or {}. gvn_dict = {} # Give the number of keys as user input using int(input()) and store it in a variable. numb_of_kys = int( input('Enter some random number of keys of the dictionary = ')) # Loop till the given number of keys using for loop. for p in range(numb_of_kys): # Inside the for loop scan the key and value as # user input using input(),split() functions # and store them in two separate variables. keyy, valuee = input( 'Enter key and value separated by spaces = ').split() # Initialize the key with the value of the dictionary. gvn_dict[keyy] = valuee # Get all the values of given dictionary using dictionary.values() method # and store it in another variable. dict_vlue = gvn_dict.values() # Take a variable say 'product' and initialize it's value with '1' fnl_prod = 1 # Iterate in the above given dictionary values using using For loop. for itrator in dict_vlue: # Inside the loop, Multiply the above initialized product variable with the iterator # and store it in a same variable. fnl_prod = fnl_prod*int(itrator) # Print the product of values for the above given dictionary. print("The Product of values in a given dictionary = ", fnl_prod)
Output:
Enter some random number of keys of the dictionary = 5 Enter key and value separated by spaces = hello 4 Enter key and value separated by spaces = this 9 Enter key and value separated by spaces = is 10 Enter key and value separated by spaces = btechgeeks 12 Enter key and value separated by spaces = python 1 The Product of values in a given dictionary = 4320
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Intersection of Two Dictionaries via Keys in Python
- Python Program to Print an Inverted Star Pattern
- Program to Print 1 and 0 in alternative Columns in C, C++ and Python
- Program to Print 1 and 0 in alternative rows in C, C++ and Python