手机
当前位置:查字典教程网 >脚本专栏 >python >python字典get()方法用法分析
python字典get()方法用法分析
摘要:本文实例讲述了python字典get()方法用法。分享给大家供大家参考。具体分析如下:如果我们需要获取字典值的话,我们有两种方法,一个是通过...

本文实例讲述了python字典get()方法用法。分享给大家供大家参考。具体分析如下:

如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法。

这里给大家分享的就是字典的get()方法。

这里我们可以用字典做一个小游戏,假设用户在终端输入字符串:"1"或者是"2"或者是"3",返回对应的内容,如果是输入其他的,则返回"error"

>>> info = {'1':'first','2':'second','3':'third'} >>> number = raw_input('input type you number:') input type you number:3 >>> print info.get(number,'error') third >>> number = raw_input('input type you number:') input type you number:4 >>> print info.get(number,'error') error

希望本文所述对大家的Python程序设计有所帮助。

【python字典get()方法用法分析】相关文章:

python del()函数用法

python中的列表推导浅析

python回调函数的使用方法

python 字符串split的用法分享

python 切片和range()用法说明

python 七种邮件内容发送方法实例

Python完全新手教程

python解析模块(ConfigParser)使用方法

Python实现的几个常用排序算法实例

Python help()函数用法详解

精品推荐
分类导航