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

烟雨青岚
Release: 2020-07-07 11:44:43
forward
24977 people have browsed it

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)
Copy after login

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
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!