This article describes how to use the get () method to obtain dictionary keys in Python. it is a basic knowledge in Python. For more information, see get () method returns the value of the given key. If the key is unavailable, the default value None is returned.
Syntax
The following is the syntax of the get () method:
Dict. get (key, default = None)
Parameters
- Key -- this is the key to be searched in the dictionary.
- Default -- this is the default value when the return key does not exist.
Return value
This method returns the value of a given key. If the key is unavailable, the default value is None.
Example
The following example shows how to use the get () method.
#! /Usr/bin/pythondict = {'name': 'Zara ', 'age': 27} print "Value: % s" % dict. get ('age') print "Value: % s" % dict. get ('Sex', "Never ")
When we run the above program, it will produce the following results:
Value: 27 Value: Never