How to Generate a Special Dictionary in Python


In this example we will show how to generate a dictionary consisting of digital (1 to N) as (X,x*x) of the dictionary.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Please input a number: "))
dict1 = {i: i * i for i in range(1, n+1)}
print("The generated dictionary is: ", dict1)

Output:

Please input a number: 10
The generated dictionary is: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments