Friday, 17 December 2021

Python-Slip 20-B-Write a Python script to generate and print a dictionary which contains a number (between 1 and n) in the form(x,x*x).Sample Dictionary (n=5) Expected Output: {1:1, 2:4, 3:9, 4:16, 5:25}

 Write a Python script to generate and print a dictionary which contains a number (between 1 and n) in the form(x,x*x).Sample Dictionary (n=5) 

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

 Method 1:-

 n=int(input("Enter a number:"))

d={x:x*x for x in range(1,n+1)}

print(d)

 Method 2:-

n=int(input("Input a number "))

d = dict()

for x in range(1,n+1):

d[x]=x*x

print(d)