Squares of 1 to 5

EasyDictionary Programs · 22 of 94

Write a Python program to create a dictionary with keys 1 to 5 and their squares as values.

Advertisement
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Approach

  1. Dict comprehension.

Solution

Python
print({k: k * k for k in range(1, 6)})
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs