(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:

Question 21 (b) -Teachoo.png

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.
Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.