首先声明,Python没有switch!!!

通过函数与字典的结合实现

 #!/usr/bin/python
#coding:utf-8 from __future__ import division def jia(x,y):
return x+y def jian(x,y):
return x-y def cheng(x,y):
return x*y def chu(x,y):
return x/y operator = {"+":jia, "-":jian, "*":cheng, "/":chu} def f(x,o,y):
print operator.get(o)(x,y) f(3,'/',9)

Python之switch的更多相关文章

  1. Why Doesn't Python Have Switch/Case?

    Why Doesn't Python Have Switch/Case? Tuesday, June 09, 2015 (permalink) Unlike every other programmi ...

  2. python没有switch,可以用字典来替代

    python没有switch,是因为可以用字典来替代,具体方法如下: def add(x,y): print(x+y)def subtraction(x,y): print(x-y)def multi ...

  3. python & dict & switch

    python & dict & switch python 中是没用switch语句的,这应该是体现python大道至简的思想,python中一般多用字典来代替switch来实现. # ...

  4. Python实现switch效果

    Java中有switch这个东东有的地方使用switch感觉还挺好使,但是Python没有提供switch这个东东,下面我们想办法来完成类似Java和C里面的那种switch效果. Java示例代码: ...

  5. python中Switch/Case实现

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...

  6. python技巧 switch case语句

    不同于C语言和SHELL,python中没有switch case语句,关于为什么没有,官方的解释是这样的 使用Python模拟实现的方法: def switch_if(fun, x, y):    ...

  7. Python | 基础系列 · Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  8. Python里如何实现C中switch...case的功能

    python没有switch case 不过可以通过建立字典实现类似的功能 例子:根据输入的年月日,判断是该年中的第几天 y = int(input('请输入年:')) m = int(input(' ...

  9. python 中的"switch"用法

    转载:http://python.jobbole.com/82008/ 为什么Python中没有Switch/Case语句? 不同于我用过的其它编程语言,Python 没有 switch / case ...

随机推荐

  1. matplotlib系列——饼图

    import matplotlib.pyplot as plt import numpy as np import matplotlib import sys 1.主体函数 #饼图 def die(l ...

  2. java树的遍历

    java二叉树的遍历算法: http://blog.sina.com.cn/s/blog_70600f720100ujnp.html

  3. Linux面试基础(二)

    Linux常用目录——存放 /bin  所有用户可以使用的可执行文件 /sbin  新管理员使用的执行文件 /boot  Linux内核映像文件和与引导加载有关的文件 /dev   设备文件 /etc ...

  4. ThreadLocal的使用和理解

    ThreadLocal是个threadlocalvariable(线程局部变量),其实就是为每一个使用该变量的线程都提供一个变量值的副本,从线程的角度看,每个线程都保持一个对其线程局部变量副本的隐式引 ...

  5. java命令-jps

    jps命令,查看当前用户所有java进程pid 可进入/tmp/hsperfdata_xxx(登录用户名)路径下,可查看当前用户下所有的Java进程.jps.jconsole.jvisualvm等工具 ...

  6. 【记录】Idea "Cannot resolve symbol 'SpringBootApplication'" 错误&“找不到主类”错误

    初学,有一个Spring Boot的demo需要用Idea打开.我选择导入(Import Project选项)文件,出现如题错误,且yml文件格式也不正确显示(叶子). 后面细看目录,发现一个main ...

  7. pycharm配色

    setting-appearanse-theme-IntelliJ setting-Editer-color scheme-Default setting-Editer-color scheme-py ...

  8. react app相关知识

    1.快速新建名为hello-world项目的应用命令 npx create-react-app hello-world 2.使用serve来mock数据 ①先安装serve        npm i ...

  9. C/C++ C++ 11 std::move()

    { 0. C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意 是想把参数push_back进去就行 ...

  10. 根据mysql数据库 定义solr Schema.xml中配置业务域

    <!--product--> <field name="product_name" type="text_ik" indexed=" ...