(b) Write the output of the code given below:
my_dict = {"name": "Aman", "age": 26}
my_dict['age'] = 27
my_dict['address'] = "Delhi"
print(my_dict.items())
Answer:
Output: dict_items([('name', 'Aman'), ('age', 27), ('address', 'Delhi')])
Explanation:
- my_dict is a dictionary with 2 key-value pairs.
- ‘my_dict['age'] = 27’ changes the age in the dictionary my_dict to 27.
- ‘my_dict['address'] = "Delhi" ‘ adds a new-key value pair to the dictionary my_dict.
- The items() method returns a view object . The view object contains the key-value pairs of the dictionary , as tuples in a list. So ‘print(my_dict.items( ))’ prints all the key-value pairs in the dictionary.