Define a function that can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are squares of keys

Question:

Define a function that can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are squares of keys.

Hints:

  • Use dict[key]=value pattern to put entry into a dictionary.
  • Use the ** operator to get the power of a number.

Solution:

def printDict():
	d=dict()
	d[1]=1
	d[2]=2**2
	d[3]=3**2
	print d
		

printDict()
Code language: Python (python)

Leave a Comment