Home » Python

Python | Generate dictionary of numbers and their squares (i, i*i) from 1 to N

Python dictionary Example: Here, we will learn how to Generate dictionary of numbers and their squares (i, i*i) from 1 to N?
Submitted by IncludeHelp, on September 05, 2018

Given a number N, and we have to generate a dictionary that contains numbers and their squares (i, i*i) using Python.

Example:

    Input: 
    n = 10 
    Output:
    {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

Program:

# Python program to generate and print 
# dictionary of numbers and square (i, i*i)

# declare and assign n
n = 10

# declare dictionary 
numbers = {}

# run loop from 1 to n 
for i in range(1, n+1):
	numbers[i] = i * i

# print dictionary 
print numbers

Output

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.