Python function-dictionary get() method

get()Description##

The Python dictionary get() function returns the value of the specified key, or the default value if the value is not in the dictionary.
The setdefault() in the Python dictionary can implement the operation of the dictionary default value. The related blog posts are as follows
python function-dictionary setting default value setdefault()python function-dictionary setting get() and setdefault() difference

get() usage##

dict_name.get(key,default= None)
# 	key:Key to set default value
# 	default:To return the value of key, it can be any value, such as integer, string, list, dictionary, etc.
# 	return:If the key in the dictionary originally has a value, then the value corresponding to the key in the dictionary is returned, if not, the value in "default" is returned.

Note: get() is just a value operation without assigning a value to the dictionary. See the following example for details:

get()example##

>>> dict_name ={}>>> dict_name.get("name")>>> dict_name
{}
# Set "name" to get "wangcongying",But print dict_When name, there is no value in the dictionary
>>> dict_name.get("name","wangcongying")'wangcongying'>>> dict_name
{}
>>> dict_name["name"]="wangcongying">>> dict_name
{' name':'wangcongying'}>>> dict_name["gender"]= None
>>> dict_name
{' name':'wangcongying','gender': None}>>> dict_name.get("gender","male")>>> dict_name
{' name':'wangcongying','gender': None}>>> dict_name.get("name","julia")'wangcongying'

to sum up##

What get() does is the default value setting of value operation.

Recommended Posts

Python function-dictionary get() method
Python magic method topic
Python object-oriented magic method
Python3.7 debugging example method
Python error handling method
Python defines a function method
Python TCP packet injection method
Python tracking except information method
Python implements gradient descent method
Python right alignment example method
Python method of parameter passing
Python arithmetic sequence calculation method
Method steps to increase python font
Summary of logarithm method in Python
Python and js interactive call method
Python implements the steepest descent method
End the method of running python
Python crawler example to get anime screenshots
Method of installing django module in python
python implements the gradient method python the fastest descent method
The specific method of python instantiation object
Python Romberg method to find integral examples
Example method of reading sql from python
Python example method to open music files
How to get started quickly with Python
The specific method of python import library
Python replacement pip source method process analysis