We can access the value of the dictionary by passing the key name inside the square brackets with the dictionary.
Syntax:
dictionary_name[key]
Program:
# Python program to access elements
# from a Dictionary using key name
# Dictionary
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21}
# accessing the elements
print("Dictionary \'Record\' elements...")
print(Record['id'])
print(Record['name'])
print(Record['age'])
Output:
Dictionary 'Record' elements...
101
Amit Kumar
21
2) Access elements by using get() method
The get() method can be used to access an item from the dictionary.
Syntax:
dictionary_name.get(key)
Program:
# Python program to access elements
# from a Dictionary using get() method
# Dictionary
Record = {'id' : 101, 'name' : 'Amit Kumar', 'age' : 21}
# accessing the elements
print("Dictionary \'Record\' elements...")
print(Record.get('id'))
print(Record.get('name'))
print(Record.get('age'))
1) Access elements by using key name
We can access the value of the dictionary by passing the key name inside the square brackets with the dictionary.
Syntax:
Program:
Output:
2) Access elements by using get() method
The get() method can be used to access an item from the dictionary.
Syntax:
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer