Question:
Define a function that can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are squares of keys. The function should just print the values only.
Hints:
- Use dict[key]=value pattern to put entry into a dictionary.
- Use the ** operator to get the power of a number.
- Use range() for loops.
- Use keys() to iterate keys in the dictionary. Also, we can use item() to get key/value pairs.
Solution:
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for (k,v) in d.items():
print v
printDict()
Code language: Python (python)