Informatics Practices
Consider the following dictionary Prod_Price.
Prod_Price = {'LCD' : 25000,
'Laptop' : 35000,
'Home Theatre' : 80000,
'Microwave Oven' : 18000,
'Electric Iron' : 2800,
'Speaker' : 55000}
Find the output of the following statements:
(a) print(Prod_Price.get('Laptop'))
(b) print(Prod_Price.keys())
(c) print(Prod_Price.values())
(d) print(Prod_Price.items())
(e) print(len(Prod_Price))
(f) print('Speaker' in Prod_Price)
(g) print(Prod_Price.get('LCD'))
(h) del ProdPrice['Home Theatre'] print (ProdPrice)
Python Dictionaries
1 Like
Answer
(a) 35000
(b) dict_keys(['LCD', 'Laptop', 'Home Theatre', 'Microwave Oven', 'Electric Iron', 'Speaker'])
(c) dict_values([25000, 35000, 80000, 18000, 2800, 55000])
(d) dict_items([('LCD', 25000), ('Laptop', 35000), ('Home Theatre', 80000), ('Microwave Oven', 18000), ('Electric Iron', 2800), ('Speaker', 55000)])
(e) 6
(f) True
(g) 25000
(h) {'LCD': 25000, 'Laptop': 35000, 'Microwave Oven': 18000, 'Electric Iron': 2800, 'Speaker': 55000}
Explanation
(a) The get() method retrieves the value associated with the key 'Laptop'. The value (35000) is returned.
(b) The keys() method returns all the keys of the dictionary as a list.
(c) The values() method returns a list of values from key-value pairs in a dictionary.
(d) The items() method returns all the key-value pairs of the dictionary as a list of tuples.
(e) The len() function returns the number of key-value pairs (items) in the dictionary. In this case, there are 6 items in the dictionary.
(f) The in keyword is used to check if the key 'Speaker' is present in the dictionary. If the key exists, it returns True; otherwise, it returns False.
(g) The get() method retrieves the value associated with the key 'LCD'. The value (25000) is returned.
(h) The del statement removes the key 'Home Theatre' and its associated value from the dictionary. After deletion, the dictionary is printed, showing the remaining items.
Answered By
1 Like
Related Questions
Write the output of the following code:
d = {'a':1, 'b':2, 'c':3} print (d['a'])Write a program to input your friends' names and their phone numbers and store them in a dictionary as the key-value pair. Perform the following operations on the dictionary:
(a) Display the Name and Phone numbers of all your friends.
(b) Add a new key-value pair in this dictionary and display the modified dictionary.
(c) Delete a particular friend from the dictionary.
(d) Modify the phone number of an existing friend.
(e) Check if a friend is present in the dictionary or not.
(f) Display the dictionary in sorted order of names.
Write a program that prompts the user for product names and prices, and store these key-value pairs in a dictionary where names are the keys and prices are the values. Also, write a code to search for a product in a dictionary and display its price. If the product is not there in the dictionary, then display the relevant message.
Write a Python program that accepts a value and checks whether the inputted value is part of the given dictionary or not. If it is present, check for the frequency of its occurrence and print the corresponding key, otherwise print an error message.