×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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

By IncludeHelp Last updated : December 21, 2024

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}

Python Dictionary Programs »

Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement!
Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.