Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of get function in python (with code)

Detailed explanation of the usage of get function in python (with code)

烟雨青岚
烟雨青岚forward
2020-07-07 11:41:3225058browse

Detailed explanation of the usage of get function in python (with code)

Description

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

Syntax

get() method syntax:

dict.get(key, default=None)

Parameters

key – required in dictionary The key to look for.

default – If the value of 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, returns the default value None.

Example

The following example shows how to use the get() function:

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

print ("Age 值为 : %s" %  dict.get('Age'))
print ("Sex 值为 : %s" %  dict.get('Sex', "NA"))
以上实例输出结果为:

Age 值为 : 27
Sex 值为 : NA

Thank you for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/u014135752/article/details/79561703

Recommended tutorial: "python tutorial"

The above is the detailed content of Detailed explanation of the usage of get function in python (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete