Python3 字典 get() 方法

Python 3 教程 · 2019-02-06 15:43:05

描述

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

语法

get()方法语法:

dict.get(key, default=None)

参数

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

返回值

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

实例

以下实例展示了 get()函数的使用方法:

#!/usr/bin/python3 

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

print ("Age 值为 : %s" %  dict.get('Age'))
print ("Sex 值为 : %s" %  dict.get('Sex', "NA"))

以上实例输出结果为:

Age 值为 : 27
Sex 值为 : NA

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Learning PHP, MySQL, and JavaScript

Learning PHP, MySQL, and JavaScript

Robin Nixon / O'Reilly Media / 2009-7-21 / USD 39.99

Learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript - whether or not you know how to program. This simple, streamlined guide explains how the powerful combination of P......一起来看看 《Learning PHP, MySQL, and JavaScript》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具