Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

(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.
Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.