Question 3:

With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.

Suppose the following input is supplied to the program: 8 Then, the output should be:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

Suppose the following input is supplied to the program: 4 Then, the output should be:

{1: 1, 2: 4, 3: 9, 4: 16}

Hints:

1. In case of input data being supplied to the question, it should be assumed to be a console input.

2. Consider use dict()

3. Use range function to generate range of value

4. Use key in the dictionary as number(string data type) for this problem statement

Solution:

``````n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i

print(d)``````

DIscussion:

1. Get input value using the input() function.

2. Convert the input value to integer using the int() function.

3.  Generate range of values using the range function and iterate the values using the for loop. Use the +1 range. Why because in this example, we need the end value to be inclusive. By default range() function omits the end value.

4. Add the i value as key in dictionary and multiplied i value as value in dictionary.

5. Step4 will be repeated until it reaches the end value

6. Print the value