Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x)


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.

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*x
print(d) 
 

Output

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