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