Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Dictionaries in Python:
In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science).
Each key denotes a value and is separated by a colon (:).
Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.
Examples:
Example1:
Input:
given number = 17
Output:
Enter some random number = 17 printing the resultant dictionary : key = 1 value = 1 key = 2 value = 4 key = 3 value = 9 key = 4 value = 16 key = 5 value = 25 key = 6 value = 36 key = 7 value = 49 key = 8 value = 64 key = 9 value = 81 key = 10 value = 100 key = 11 value = 121 key = 12 value = 144 key = 13 value = 169 key = 14 value = 196 key = 15 value = 225 key = 16 value = 256 key = 17 value = 289
Example2:
Input:
given number=11
Output:
printing the resultant dictionary : key = 1 value = 1 key = 2 value = 4 key = 3 value = 9 key = 4 value = 16 key = 5 value = 25 key = 6 value = 36 key = 7 value = 49 key = 8 value = 64 key = 9 value = 81 key = 10 value = 100 key = 11 value = 121
Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x, x*x) in Python
There are several ways to generate a dictionary which contains numbers from 1 to n in the form ( x, x*x ) some of them are:
- Using Dictionary Comprehension (User Input)
- Using Dictionary Comprehension (Static Input)
- Using for loop (Static Input)
Method #1: Using Dictionary Comprehension (User Input)
Approach:
- Scan the number and store it in a variable
- Create a dictionary and use dictionary comprehension to populate it with values that have a number between 1 and given number as the key and the square of the number as their values.
- Print the resultant dictionary
- Exit of program
Below is the implementation:
# given number numb = int(input("Enter some random number = ")) # using dictionary comprehension resultdict = {k: k*k for k in range(1, numb+1)} # printing the resultant dictionary print('printing the resultant dictionary :') for key,value in resultdict.items(): print("key =",key,"value =",value)
Output:
Enter some random number = 17 printing the resultant dictionary : key = 1 value = 1 key = 2 value = 4 key = 3 value = 9 key = 4 value = 16 key = 5 value = 25 key = 6 value = 36 key = 7 value = 49 key = 8 value = 64 key = 9 value = 81 key = 10 value = 100 key = 11 value = 121 key = 12 value = 144 key = 13 value = 169 key = 14 value = 196 key = 15 value = 225 key = 16 value = 256 key = 17 value = 289
Explanation:
- A number must be entered by the user and saved in a variable.
- Using dictionary comprehension, a dictionary is declared and initialized with values.
- The digits 1 to n are preserved as keys, while the squares of the numbers are used as values.
- The final resultant dictionary is printed
Method #2: Using Dictionary Comprehension (Static Input)
Approach:
- Give the number as static input.
- Create a dictionary and use dictionary comprehension to populate it with values that have a number between 1 and given number as the key and the square of the number as their values.
- Print the resultant dictionary
- Exit of program
Below is the implementation:
# give the number as static numb = 11 # using dictionary comprehension resultdict = {k: k*k for k in range(1, numb+1)} # printing the resultant dictionary print('printing the resultant dictionary :') for key, value in resultdict.items(): print("key =", key, "value =", value)
Output:
printing the resultant dictionary : key = 1 value = 1 key = 2 value = 4 key = 3 value = 9 key = 4 value = 16 key = 5 value = 25 key = 6 value = 36 key = 7 value = 49 key = 8 value = 64 key = 9 value = 81 key = 10 value = 100 key = 11 value = 121
Method #3:Using for loop (Static Input)
Approach:
- Give the number as static input.
- Take a empty dictionary
- Traverse from 1 to given number using for loop.
- For each iterator value initialize the key p with the value p*p where p is iterator value.
- Print the resultant dictionary
- Exit of program
Below is the implementation:
# give the number as static numb = 13 # Take a empty dictionary resultdict = {} # using for to iterate from 1 to numb for p in range(1, numb+1): resultdict[p] = p*p # printing the resultant dictionary print('printing the resultant dictionary :') for key, value in resultdict.items(): print("key =", key, "value =", value)
Output:
printing the resultant dictionary : key = 1 value = 1 key = 2 value = 4 key = 3 value = 9 key = 4 value = 16 key = 5 value = 25 key = 6 value = 36 key = 7 value = 49 key = 8 value = 64 key = 9 value = 81 key = 10 value = 100 key = 11 value = 121 key = 12 value = 144 key = 13 value = 169
Explanation :
Here we assigned square of iterator value to the key of the dictionary instead of using dictionary comprehension