myDict = {
"comment": "A like if you love learning python with grepper!"
}
myDict["comment"] #retrieves comment if found. Otherwise triggers error
#or if not sure if key is in dictionary, avoid exiting code with error.
myDict.get("comment") #retrieves comment or None if not found in dict
#The get() method in dictionary returns:
#the value for the specified key if key is in dictionary.
#None if the key is not found and value is not specified.
#value if the key is not found and value is specified.
# value is provided
print('Salary: ', person.get('salary', 0.0))
# welcome to softhunt.net
# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net', 2: 'By Ranjeet', 'user': 'Greetings to you'}
print("Dictionary", Dictionary)
# accessing a element using get()
# method
print("Accessing a element using get:", Dictionary.get('user'))
# without default
{"name": "Victor"}.get("name")
# returns "Victor"
{"name": "Victor"}.get("nickname")
# returns None
# with default
{"name": "Victor"}.get("nickname", "nickname is not a key")
# returns "nickname is not a key"