python中的switch

python中的switch

作者:LAMP小白  点击:1792  发布日期:2013-10-20 14:14:07  返回列表

用于编写多分支结构的程序,类似与if elif else

但是python没有提供switch语句


python可以通过字典实现switch语句的功能,有两个步骤


定义一个字典

通过get去获取建立的映射关系


加载运算模块

from __futrue__ import division


#!/usr/bin/python
#coding:utf8
from __future__ import division
def op(x, o, y):
    dic={'+':x+y, '-':x-y, '*':x*y, '/':x/y}
    return dic.get(o)
print op(3,'+',445)




上一篇:匿名函数Lambda表达式 下一篇:快递查询API
0