Thursday , September 19 2024

What is the output of the following snippet? dct = {} dct[‘1’] = (1, 2) dct[‘2’] = (2, 1) for x in dct.keys(): print(dct[x][1], end=””)

Questions BankCategory: Python Essentials 1What is the output of the following snippet? dct = {} dct[‘1’] = (1, 2) dct[‘2’] = (2, 1) for x in dct.keys(): print(dct[x][1], end=””)
What is the output of the following snippet?

dct = {}
dct['1'] = (1, 2)
dct['2'] = (2, 1)
 
for x in dct.keys():
    print(dct[x][1], end="")
  • 12
  • 21
  • (2,1)
  • (1,2)

Explanation: Let’s analyze the code:

  • Line 1: an empty dictionary is created
  • Line 2-3: the key:value pairs are created, and added to the dct dictionary:
    dct = {‘1’: (1, 2), ‘2’: (2, 1)}
  • Line 5-6: perform two for loop iterations (iterate through the dct keys) and print the dictionary values (tuple elements) indexed in the print() function: x = 1 2, x = 2 1
  • the end=”” parameter joins the two printed lines together.

More Questions: Python Essentials 1 (PE1) Course Final Test

ProgrammingAnswers Staff replied 2 years ago

123