Python program to create a dictionary in the given form.

In this python dictionary program, we will create a dictionary in the given form of (n^3).

Steps to solve the program
  1. Take the value of n and create an empty dictionary.
  2. User for loop with range function to iterate over the given number.
  3. Add that number as the key and its cube as its value.
  4. For calculating the cube use n**3.
  5. Print the output.
				
					n = 4
D1 = {}

for i in range(1,4+1):
    D1.update({i:i**3})

print(D1)
				
			

Output :

				
					{1: 1, 2: 8, 3: 27, 4: 64}
				
			

iterate over a dictionary.

insert a key at the beginning of the dictionary.

Leave a Comment