Dictionary in python :
A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.
Examples:
Example1:
Input:
Given dictionary = {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}
Output:
The above given dictionary is : {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'} The respective value at the given key 200 : this The respective value at the given key 400 : btechgeeks The respective value at the given key 500: None
Example2:
Input:
Given dictionary = {'good': '5', 'morning': '10', 'btechgeeks': '15'}
Output:
The above given dictionary is : {'good': '5', 'morning': '10', 'btechgeeks': '15'} The respective value at the given key btechgeeks = 15 The respective value at the given key hello = None
Program for Dictionary get() Function in Python
 get() method in Dictionary:
The get() method of the Python dictionary returns a value for the given key. If the key is not available, the default value None is returned.
Syntax:
dict.get(key, default = None)
Parameters:
Key: This is the Key that will be looked up in the dictionary.
default: If the key does not exist, this is the value that will be returned.
Return Value:
This method returns the value for the specified key. If the key is not available, the default value None is returned.
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the dictionary by initializing it with some random values and store it in a variable.
- Print the above-given dictionary.
- Apply the get() method to the given dictionary to get the respective value for the given key.
- Store it in another variable.
- Similarly, do the same for the other keys to get the respective value for the given key.
- The Exit of the Program.
Below is the implementation:
# Give the dictionary by initializing it with some random values and store it # in a variable. gvn_dictinry = {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'} # Print the above-given dictionary. print("The above given dictionary is :") print(gvn_dictinry) # Apply get() method to the given dictionary to get the respective value # for the given key. # Store it in another variable. rslt = gvn_dictinry.get(200) # Similarly, do the same for the other keys to get the respective value for the given key. print("The respective value at the given key 200 : ", rslt) rslt_1 = gvn_dictinry.get(400) print("The respective value at the given key 400 : ", rslt_1) rslt_2 = gvn_dictinry.get(500) print("The respective value at the given key 500: ", rslt_2)
Output:
The above given dictionary is : {100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'} The respective value at the given key 200 : this The respective value at the given key 400 : btechgeeks The respective value at the given key 500: None
Method #2: Using Built-in Functions (User Input)
Approach:
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Print the above-given dictionary.
- Give the key as user input using the input() function to get the respective value for the given key.
- Store it in another variable.
- Apply the get() method to the given dictionary to get the respective value for the above-given key.
- Store it in another variable.
- Similarly, do the same for the other keys to get the respective value for the given key.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it with an empty dictionary using dict() or {}. gvn_dictinry = {} # Give the number of keys as user input using int(input()) and store it in a variable. numb_of_kys = int( input('Enter some random number of keys of the dictionary = ')) # Loop till the given number of keys using for loop. for p in range(numb_of_kys): # Inside the for loop scan the key and value as # user input using input(),split() functions # and store them in two separate variables. keyy, valuee = input( 'Enter key and value separated by spaces = ').split() # Initialize the key with the value of the dictionary. gvn_dictinry[keyy] = valuee # Print the above-given dictionary. print("The above given dictionary is :") print(gvn_dictinry) # Give the key as user input using the input() function to get the respective value # for the given key. # Store it in another variable. gvn_key= input("Enter some random key = ") # Apply the get() method to the given dictionary to get the respective value for the above-given key. p = gvn_dictinry.get(gvn_key) # Store it in another variable. print("The respective value at the given key ",gvn_key," = ", p ) # Similarly, do the same for the other keys to get the respective value for the given key. gvn_key2= input("Enter some random key = ") q = gvn_dictinry.get(gvn_key2) print("The respective value at the given key ",gvn_key2," = ", q)
Output:
Enter some random number of keys of the dictionary = 3 Enter key and value separated by spaces = good 5 Enter key and value separated by spaces = morning 10 Enter key and value separated by spaces = btechgeeks 15 The above given dictionary is : {'good': '5', 'morning': '10', 'btechgeeks': '15'} Enter some random key = btechgeeks The respective value at the given key btechgeeks = 15 Enter some random key = hello The respective value at the given key hello = None