🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Python Dictionaries: all 15 questions

Quiz: Python Dictionaries · Question 13 of 15

What does this print? (code)

What does this print?

squares = {n: n * n for n in range(1, 4)}
print(squares)
  1. A.{1: 1, 2: 4, 3: 9}
  2. B.{1: 2, 2: 4, 3: 6}
  3. C.[1, 4, 9]
  4. D.{0: 0, 1: 1, 2: 4, 3: 9}
Answer & explanation

Correct answer: {1: 1, 2: 4, 3: 9}

A dictionary comprehension builds key: value pairs; range(1, 4) gives 1, 2, 3.

Can you get the rest right without peeking?

Take the timed Quiz: Python Dictionaries quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →