- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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)

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}

- 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 Questions & Answers
- Difference between x++ and x = x+1 in Java
- Difference between x++ and x= x+1 in Java programming
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Differences between Python 2.x and Python 3.x?
- Absolute difference between the first X and last X Digits of N?
- Count Elements x and x+1 Present in List in Python
- Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- What are the key differences between Python 2.7.x and Python 3.x?
- Pow(x, n) in Python
- What are the differences in between python 2.x and python 3.x versions?
- Important differences between Python 2.x and Python 3.x with examples
- Count of values of x <= n for which (n XOR x) = (n – x) in C++

Advertisements