- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).
When it is required to generate a dictionary that contains numbers within a given range in a specific form, the input is taken from the user, and a simple ‘for’ loop is used.
Example
Below is a demonstration for the same −
my_num = int(input("Enter a number.. ")) my_dict = dict() for elem in range(1,my_num+1): my_dict[elem] = elem*elem print("The generated elements of the dictionary are : ") print(my_dict)
Output
Enter a number.. 7 The generated elements of the dictionary are : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
Explanation
- The number is taken as user input.
- An empty dictionary is created.
- The number is iterated over.
- The square of the number is stored in the dictionary.
- It is displayed as output on the console.
- Related Articles
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Difference between x++ and x = x+1 in Java
- Difference between x++ and x= x+1 in Java programming
- If ( x=a^{m+n}, y=a^{n+1} ) and ( z=a^{l+m} ), prove that ( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} )
- If ( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} ) and ( c=x^{l+m} y^{n} ), prove that ( a^{m-n} b^{n-1} c^{l-m}=1 . )
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Prove that( frac{1}{1+x^{b-a}+x^{c-a}}+frac{1}{1+x^{a-b}+x^{c-b}}+frac{1}{1+x^{b-c}+x^{a-c}}=1 )
- Find minimum x such that (x % k) * (x / k) == n in C++
- C program to generate the value of x power n using recursive function
- Absolute difference between the first X and last X Digits of N?
- Insert five rational numbers between $x$ and $|x|$, where $x=frac{-17}{20}$.
- Differences between Python 2.x and Python 3.x?
- Use remainder theorem to find remainder when p(x) is divided by q(x) in the following question:nnp(x)=x^9-5x^4+1; q(x)=x+1
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python

Advertisements