# Given that d refers to a dictionary, change the value mapped to by the key ‘Monty’ to ‘Python’.

Given that d refers to a dictionary, change the value mapped to by the key 'Monty' to 'Python'. Write in Python

The required statement is as follows:

d['Monty'] = 'Python'

Explanation:

The general syntax to set the value mapped by a key in a dictionary is as follows: dictionary_name[key_var] = value_var

where dictionary_name is the name associated with the dictionary, key_var is the key for which the mapped value is to be set and, the value_var is the value to be set.

If there is already a value mapped by the key_var in dictionary_name then, that value is overridden by value_var. It is given that the required dictionary is associated with the name ‘d’ and, the value mapped by the key ‘Monty’ is to be changed to ‘Python’ Hence, the required statement is as follows:

d['Monty'] = 'Python'

Below is the code to test the working of the above given statement.

#Define a dictionary to test the working of the statement.d = {'Name1': 'Language1', 'Monty':'Language2', 'Name3': 'Language3'}#Print the value mapped by the key 'Monty'.print("Value mapped by the key \'Monty\':",d['Monty'])#Change the value mapped#by the key 'Monty' to 'Python'.d['Monty'] = 'Python'#Print the value mapped by the key 'Monty'.print("Value mapped by the key \'Monty\' after the change:",d['Monty'])

Related Posts