Informatics Practices
Suppose
>>> d1 = { 1 : 'one' , 2: 'two' , 3: 'three' , 4: 'four'}
>>> d2 = { 5 :'five', 6:'six' }
Write the output of the following code:
>>> del d1[3]
>>> print(d1)
>>> d1.pop(4)
>>> print(d1)
>>> d1 [8] =' eight'
>>> print(d1)
>>> d1.clear()
>>> print(d1)
Answer
{1: 'one', 2: 'two', 4: 'four'}
'four'
{1: 'one', 2: 'two'}
{1: 'one', 2: 'two', 8: ' eight'}
{}
Working
del d1[3]: Removes the key 3 and its corresponding value 'three' fromd1. After deletion,d1contains {1: 'one', 2: 'two', 4: 'four'}.d1.pop(4): Removes the key 4 and its corresponding value 'four' fromd1. After popping,d1is {1: 'one', 2: 'two'}.d1[8] = 'eight': Adds a new key-value pair 8: 'eight' tod1. Nowd1becomes {1: 'one', 2: 'two', 8: 'eight'}.d1.clear(): Clears all key-value pairs fromd1, resulting in an empty dictionary{}.
Related Questions
Find errors and rewrite the same after correcting the following code:
d1.clears()Suppose
>>> d1 = { 1 : 'one' , 2: 'two' , 3: 'three' , 4: 'four'} >>> d2 = { 5 :'five', 6:'six' }Write the output of the following code:
>>> d1.items() >>> d1.keys() >>> d1.values() >>> d1.update(d2) >>> len(d1)Write a Python program to find the highest 2 values in a dictionary.
Write a Python program to input 'n' classes and names of class teachers to store them in a dictionary and display the same. Also accept a particular class from the user and display the name of the class teacher of that class.