Q. 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 :-

x in d1.keys()  So, Values of x are keys of Dictionary

print (x, ':', d1[x], end = ' ') It will print key : values of dictionary 

print (d1[x] * 3) It will print values three times.


7 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. there in the question it is given to repeat the dx 3 times...but why u repeated 4 times??

    ReplyDelete
    Replies
    1. You are wrong. Please run it in your idle python.

      Delete
    2. this is because in the third last line its executing "x" one and then its executing it again 3 times in second last line.So it will be executed 4times totally...

      Delete
  2. Can you explain the code by entering message??

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post