Dictionary n: n*n, n+1: n+1*n+1

  • A+

Having trouble creating a dictionary that loops in term of n:n*n, n+1: n+1*n+1, and so on. It has to contain numbers between 1 and 10 in this form.

d = dict () for n in range (1,10): dict[n*n] 

I cannot print the expected output. I have tried with {} as well to make it cleaner with no success (n:n*n, n+1: n+1*n+1,...).

I want to then be able to switch it up and learn how to use the different variables.

Thank you!


You can use dict comprehension I guess:

{ n: n*n for n in range(10) } 

It works like list comprehension but the result is a dict.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: