Python Practice › Dictionary Programs › Program 22
Squares of 1 to 5
Write a Python program to create a dictionary with keys 1 to 5 and their squares as values.
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Approach
- Dict comprehension.
Solution
print({k: k * k for k in range(1, 6)})Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Learn the concept
New to this topic? Read the lesson, then come back.