;

Python Program to Create a Dictionary of Numbers 1 to Nth in (x, x*x) Form


Tutorialsrack 08/05/2020 Python

In this python program, we will earn how to create a dictionary of the numbers 1 to Nth in (x,x*x) form.

Here is the source code of the program to create a dictionary of the numbers 1 to Nth in (x,x*x) form.

Program 1: Python Program to Create a Dictionary of Numbers 1 to n in (x, x*x) form Using For Loop

Python Program to Create a Dictionary of Numbers 1 to n in (x, x*x) form Using For Loop
# Python Program to Create a Dictionary of Numbers
# 1 to n in (x, x*x) form Using For Loop

# Take the Input from the USer
number = int(input("Enter the Maximum Number: "))
myDict = {}

for x in range(1, number + 1):
    myDict[x] = x * x
    
# Print the Output    
print("\nDictionary = ", myDict)
Output

Enter the Maximum Number: 5

 

Dictionary =  {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Program 2: Python Program to Create a Dictionary of Numbers 1 to n in (x, x*x) form using Another Approach

Python Program to Create a Dictionary of Numbers 1 to n in (x, x*x) form using Another Approach

# Python Program to Create a Dictionary of Numbers
# 1 to n in (x, x*x) form using Another Approach

# Take the Input From the User
number = int(input("Enter the Maximum Number : "))

myDict = {x:x ** 2 for x in range(1, number + 1)}
    
# Print the Output       
print("\nDictionary = ", myDict)
Output

Enter the Maximum Number : 7

 

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


Related Posts



Comments

Recent Posts
Tags