Write a Python Program to Create Dictionary of Numbers 1 to n in (x, x*x) form with practical example.

Before you start, please refer Dictionary article to understand everything about Dictionaries.

## Python Program to Create Dictionary of Numbers 1 to n in (x, x*x) form Example 1

In this python program we are using using for loop to iterate from 1 to user specified value. Within the for loop, we are assigning values for dictionary by using * operator.

# Python Program to Create Dictionary of Numbers 1 to n in (x, x*x) form number = int(input("Please enter the Maximum Number : ")) myDict = {} for x in range(1, number + 1): myDict[x] = x * x print("\nDictionary = ", myDict)

**OUTPUT**

**ANALYSIS**

Given number = 5.

**First Iteration x will be 1 :** for 1 in range(1, 6)

myDict[x] = x * x

myDict[1] = 1 * 1 = 1

**Second Iteration x will be 2 :** for 2 in range(1, 6)

myDict[2] = 2 * 2 = 4

Do the Same for the remaining iterations

## Python Program to generate Dictionary of Numbers 1 to n in the form of (x, x*x) Example 2

This is another approach of creating dictionary. Here, we are using single line to generate dictionary of numbers in the form of x, x*x

# Python Program to Create Dictionary of Numbers 1 to n in (x, x*x) form number = int(input("Please enter the Maximum Number : ")) myDict = {x:x * x for x in range(1, number + 1)} print("\nDictionary = ", myDict)

**OUTPUT**