Write a Python script to print a dictionary where the keys are numbers between 1 and n (both included) and the values are square of keys


The program prompts the user to enter a limit l using the input function, which returns the user's input as a string. The string is then cast to an integer using the int function.

A dictionary d is created, and a for loop is used to iterate over the range of numbers from 1 to l + 1. For each number in the range, the loop adds a key-value pair to the dictionary, where the key is the number and the value is the square of the number (calculated using the ** operator, which represents exponentiation).

Finally, the dictionary is printed to show the result. In summary, the program demonstrates how to create a dictionary with keys and values based on a calculation, and how to use a for loop to add key-value pairs to a dictionary in Python.

Source Code

l=int(input("Enter the Limit : "))
d = dict()
for x in range(1,l+1):
    d[x]=x**2
print(d)

Output

Enter the Limit : 5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}