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

查看所有标签

具体数学(英文版第2版)

具体数学(英文版第2版)

[美] Ronald L. Graham、Donald E. Knuth、Oren Patashnik / 机械工业出版社 / 2002-8 / 49.00元

This book introduces the mathematics that supports advanced computer Programming and the analysis of algorithms. The primary aim of its well-known authors is to provide a solid and relevant base of ma......一起来看看 《具体数学(英文版第2版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具