Predict the output of the following code if

mydict = {‘a’:23,'b': '78',’c’ :89, ‘d’:36}
>>>del mydict[‘c’]
>>>print mydict

(a) {‘a’:23,'b': '78',’c’ :89,‘d’:36}

(b) {‘a’:23,'b': '78',‘d’:36}

(c) {‘a’:23,'b': '78',89, ‘d’:36}

(d) {‘a’:23,'b': '78',’d’ :89, ‘e’:36}

 

Answer:

  • The code uses the del statement to delete a key-value pair from a dictionary object in Python. 
  • del mydict[‘c’] means that the key ‘c’ and its associated value 89 are removed from the dictionary object named mydict.
  • The print statement prints the contents of the dictionary object to the standard output. 

Therefore, the output of the code is {‘a’: 23, ‘b’: ‘78’, ‘d’: 36} , which shows that the key ‘c’ and its value 89 are no longer present in the dictionary object named mydict.

So, the correct answer is (b) {‘a’:23,‘b’: ‘78’,‘d’:36}.

Go Ad-free
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.