# Python Program to Create Dictionary that Contains Number

This is a Python Program to generate a dictionary that contains numbers (between 1 and n) in the form (x,x*x).

Problem Description

The program takes a number from the user and generates a dictionary that contains numbers (between 1 and n) in the form (x,x*x).

Problem Solution

1. Take a number from the user and store it in a separate variable.
2. Declare a dictionary and using dictionary comprehension initialize it to values keeping the number between 1 to n as the key and the square of the number as their values.
3. Print the final dictionary.
4. Exit.

Program/Source Code

Here is source code of the Python Program to generate a dictionary that contains numbers (between 1 and n) in the form (x,x*x). The program output is also shown below.

```n=int(input("Enter a number:"))
d={x:x*x for x in range(1,n+1)}
print(d)```
Program Explanation

1. User must enter a number and store it in a variable.
2. A dictionary is declared and initialized to values using dictionary comprehension.
3. The numbers between 1 to n are kept as keys while the squares of the numbers are made their values.
4. The final dictionary is printed.

Runtime Test Cases
```
Case 1:
Enter a number:5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Case 2:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361}```

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]