Q. Assume that price_dict is a Python variable to which a dictionary literal has already been assigned, and that it currently contains at least 10 key-value pairs.


(a) write an expression whose value would be the number of key-value pairs in price_dict
(b) write an expression that would be the value corresponding to the key 'avocados' in price_dict
(c) write an expression that would be a list of all of the keys within price_dict
(d) write an expression that would be a list of all of the keys values within price_dict
(e) write a statement that would replace the current value of the key apples' in price_dict with 1.99


Answer :-

(a)
price_dict = eval (input ("Enter the Dictionary :- "))
dic = price_dict.items()
values = ()
for i in dic:
    values += (i)
print (values)
(b)
price_dict = eval (input ("Enter the Dictionary :- "))
key = eval (input ("Enter key :- "))
values = price_dict.get(key, "Not found")
print (values)
(c)
price_dict = eval (input ("Enter the Dictionary :- "))
values = price_dict.keys()
print (values)
(d)
price_dict = eval (input ("Enter the Dictionary :- "))
values = price_dict.values()
print (values)
(e)
price_dict = eval (input ("Enter the Dictionary :- "))
key = eval (input ("Enter key which would you want to update :- "))
val = eval (input ("Enter New value :- "))
values = price_dict[key] = val
print ("New price_dict :-", price_dict)

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post