Python语言基础
Python语言进阶
Python数据结构

Python dictionary get() 方法

搞懂Python dictionary get() 方法的用法

描述

Python 字典 get() 函数返回指定键的值,如果键不在字典中返回默认值。

语法

get()方法语法:
dict.get(key, default=None)

参数

key -- 字典中要查找的键。 default -- 如果指定的键不存在时,返回该默认值值。

返回值

返回指定键的值,如果键不在字典中返回默认值 None。

实例

以下实例展示了 get() 函数的使用方法:
dict = { 'Name': 'Lidihuo' , 'Age': 2 }
print ( "Age 值为 : %s" % dict. get ( 'Age' ) )
print ( "Url 值为 : %s" % dict. get ( 'Url' , "NA" ) )
以上实例输出结果为:
Age 值为 : 2
Url 值为 : NA
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4