Definition:
This method is used to get some values from a dictionary by it’s key, meaning we type the key inside get() method and it returns its associated value. Also, if it does not find a matching key it will return None or whatever we specify.
Syntax:
X = dictionary.get(‘key’)
This method takes 1 argument which is key itself. But we can give another argument after the comma and that would be in string format. That will be used when there is no matching key.
Working:
You can copy code here.
dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Age’: 23,
‘Male’: True,
‘Subjects’: [‘Physics’, ‘Maths’, ‘Computers’]}
x = dictionary.get(‘Name’)
print(x)
Code:
- A dictionary with some values is being taken.
- get() method is being applied to the dictionary.
- Since the get() method returns the output, therefore this output is printed inside x.
Output:
- Value of key Name is being printed
Related Post: