Computer Science

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 ( )

Python Dictionaries

7 Likes

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

Answered By

3 Likes


Related Questions