Latest web development tutorials

Python3 Dictionary get () method

Python3 dictionary Python3 dictionary


description

Python dictionary get () function returns the value of the specified key, if the value is not in the dictionary or a default value.

grammar

get () method syntax:

dict.get(key, default=None)

parameter

  • key - the dictionary to find the key.
  • default - if the specified key does not exist, return the default value.

return value

Returns the value of the specified key, if the value is not in the dictionary or a default value None.

Examples

The following example shows the get () method using the function:

#!/usr/bin/python3 

dict = {'Name': 'w3big', 'Age': 27}

print ("Age 值为 : %s" %  dict.get('Age'))
print ("Sex 值为 : %s" %  dict.get('Sex', "NA"))

The above example output is:

Age 值为 : 27
Sex 值为 : NA

Python3 dictionary Python3 dictionary