CBSE Class 11 Computer Science Question 86 of 112

Dictionaries — Question 8

Back to all questions
8
Question

Question 8

What will be the output produced by following code ?

d1 = { 5 : "number", "a" : "string", (1, 2): "tuple" }   
print("Dictionary contents")
for x in d1.keys():	# Iterate on a key list
  print (x, ':' , d1[x], end = ' ')
  print (d1[x] * 3)
  print ( )
Answer
Output
Dictionary contents
5 : number numbernumbernumber

a : string stringstringstring

(1, 2) : tuple tupletupletuple
Explanation

d1 is a dictionary containing three key-value pairs.
x in d1.keys() represents that x will iterate on the keys of d1.
The iterations are summarized below:

xd1[x]d1[x] * 3
5numbernumbernumbernumber
astringstringstringstring
(1,2)tupletupletupletuple