Informatics Practices
Write the output of the following code:
d = { (1,2):1, (2,3):21 }
print (d[1,2] )
Answer
1
Working
In the given code, d is a dictionary that has tuples (1,2) and (2,3) as keys mapped to their respective values 1 and 21. When we access d[1, 2], Python interprets (1,2) as a tuple and looks for this tuple as a key in dictionary d. Therefore, it retrieves the value associated with the key (1,2), which is 1.
Related Questions
The following code is giving some error. Find out the error and correct it.
d1 = {"a" : 1, 1 : "a", [1, "a"] : "two"}What is the output produced by the following code :
d1 = {5 : [6, 7, 8], "a" : (1, 2, 3)} print(d1.keys()) print(d1.values())Write the output of the following code:
d = {'a':1, 'b':2, 'c':3} print (d['a'])Write a program to input your friends' names and their phone numbers and store them in a dictionary as the key-value pair. Perform the following operations on the dictionary:
(a) Display the Name and Phone numbers of all your friends.
(b) Add a new key-value pair in this dictionary and display the modified dictionary.
(c) Delete a particular friend from the dictionary.
(d) Modify the phone number of an existing friend.
(e) Check if a friend is present in the dictionary or not.
(f) Display the dictionary in sorted order of names.