python字典get()方法用法分析

yipeiwu_com5年前Python基础

本文实例讲述了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的Django框架使用的一些实用建议

前言:随着Django1.4第二个候选版的发布,虽然还不支持Python3,但Django团队已经在着手计划中,据官方博客所说,Django1.5将会试验性的支持python3。 Dja...

python3.3实现乘法表示例

python3.3实现乘法表示例

复制代码 代码如下:from StringHelper  import PadLeft  for x in range(1,10):   ...

基于Python的文件类型和字符串详解

基于Python的文件类型和字符串详解

1. Python的文件类型 1. 源代码--直接由Python解析 vi 1.py #!/usr/bin/python print 'hello world' 这里的1.py...

Python 操作 ElasticSearch的完整代码

Python 操作 ElasticSearch的完整代码

官方文档:https://elasticsearch-py.readthedocs.io/en/master/   1、介绍     python提供了操作ElasticSearch 接...

wxpython绘制圆角窗体

本文实例为大家分享了wxpython绘制圆角窗体的具体代码,供大家参考,具体内容如下 # -*- coding:gbk -*- import wx class RCDialo...