Predict Dict Comprehension
Predict Output Medium
Question
What does this code print?
Code
squares = {x: x**2 for x in range(1, 4)}
print(squares[3])
Hint
The comprehension maps each x to x squared.
Explanation
The dict comprehension creates {1: 1, 2: 4, 3: 9}. squares[3] is 9. Output: 9
Related Resources
Related Lessons
- Dictionaries
Dict comprehensions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.