1dictionary = {"message": "Hello, World!"}
2
3data = dictionary.get("message", "")
4
5print(data) # Hello, World!
6
1#The get() method in dictionary returns:
2#the value for the specified key if key is in dictionary.
3#None if the key is not found and value is not specified.
4#value if the key is not found and value is specified.
5# value is provided
6print('Salary: ', person.get('salary', 0.0))
1#!/usr/bin/python3
2
3dict = {'Name': 'Zara', 'Age': 27}
4
5print ("Value : %s" % dict.get('Age'))
6print ("Value : %s" % dict.get('Sex', "NA"))
7
8# Value : 27
9# Value : NA
1dict = {'color': 'blue', 'shape': 'square', 'perimeter':20}
2dict.get('shape') #returns square
3
4#You can also set a return value in case key doesn't exist (default is None)
5dict.get('volume', 'The key was not found') #returns 'The key was not found'
1#!/usr/bin/python
2
3dict = {'Name': 'Zabra', 'Age': 7}
4print "Value : %s" % dict.get('Age')
5print "Value : %s" % dict.get('Education', "Never")