What does the python get function do? Example analysis

乌拉乌拉~
Release: 2018-08-18 17:55:02
Original
11388 people have browsed it

In this article, we will learn about the relevant knowledge about the python get function in the python dictionary. What does the get function mean and what role it plays will be answered in the following article.

Description

Python Dictionary (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 - - The key to look up in the dictionary.

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 Demonstration

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

#!/usr/bin/python

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

print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Sex', "Never")
Copy after login

The output result of the above example is:

Value : 27Value : Never
Copy after login

The above is all the content of this article, the built-in get function of the dictionary in python. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What does the python get function do? Example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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