不同于C语言和SHELL,python中没有switch case语句,关于为什么没有,官方的解释是这样的

使用Python模拟实现的方法:

def switch_if(fun, x, y):
    if fun == 'add':
        return x + y
    elif fun == 'sub':
        return x - y
    elif fun == 'mul':
        return x * y
    elif fun == 'div':
        return x / y
    else:
        return None

def switch_dict(fun, x, y):
    return {
        'add': lambda: x + y,
        'sub': lambda: x - y,
        'mul': lambda: x * y,
        'div': lambda: x / y,
    }.get(fun,None)()

print("switch_if('add',1,2):",switch_if('add',1,2))
print("switch_if('sub',1,2):",switch_if('sub',1,2))
print("switch_if('mul',1,2):",switch_if('mul',1,2))
print("switch_if('div',1,2):",switch_if('div',1,2))

print("switch_dict('add',1,2):",switch_dict('add',1,2))
print("switch_dict('sub',1,2):",switch_dict('sub',1,2))
print("switch_dict('mul',1,2):",switch_dict('mul',1,2))
print("switch_dict('div',1,2):",switch_dict('div',1,2))

switch_if('add',1,2): 3
switch_if('sub',1,2): -1
switch_if('mul',1,2): 2
switch_if('div',1,2): 0.5
switch_dict('add',1,2): 3
switch_dict('sub',1,2): -1
switch_dict('mul',1,2): 2
switch_dict('div',1,2): 0.5

python技巧 switch case语句的更多相关文章

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

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

  2. python中Switch/Case实现

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

  3. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  4. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  5. switch… case 语句的用法

    switch… case 语句的用法   public class Test7 { public static void main(String[] args) { int i=5; switch(i ...

  6. if语句,if...else if语句和switch...case语句的区别和分析

    前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...

  7. 为什么switch...case语句比if...else执行效率高

    在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...

  8. JavaScript基础知识(if、if else、else if、while、switch...case语句)

    13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句     "{ }"将多条语句包裹 u ...

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

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

随机推荐

  1. 微信小程序入門學習資料鏈接

    https://blog.csdn.net/github_38847071/article/details/73250258 https://blog.csdn.net/lily2016n/artic ...

  2. html 佈局

    html常見佈局方式有以下幾種: 1.使用div的html 利用div可以為html實現多列佈局. 2.使用html5的網站佈局, 利用html新增的header.footer.nav.section ...

  3. mybatis There is no getter for property named '*' in 'class java.lang.String

    1.原因 server层     xxxx.get("1234") map <if test="aaa != null and aaa.id != null and ...

  4. MT【29】介绍向量的外积及应用举例

    我们在学校教材里学到的数量积(内积)其实还有一个孪生兄弟向量积(外积),这个对参加自主招生以及竞赛的学生来讲是需要掌握的,这里稍作介绍: 原理: 例题: 应用:

  5. UOJ #314. 【NOI2017】整数 | 线段树 压位

    题目链接 UOJ 134 题解 可爱的电音之王松松松出的题--好妙啊. 首先想一个朴素的做法! 把当前的整数的二进制当作01序列用线段树维护一下(序列的第i位就是整数中位权为\(2^k\)的那一位). ...

  6. WinRM不起作用 Connecting to remote server failed with the following error message : WinRM cannot complete the operation

    当我运行下面的 powershell  脚本时: $FarmAcct = 'domain\user'  $secPassword = ConvertTo-SecureString 'aaa' -AsP ...

  7. 利用NEST2.0 在C#中操作Elasticsearch

    前言:本文主要演示了如何通过c#来操作elasticsearch,分两个方面来演示: 索引数据 搜索数据 Note: 注意我索引数据和搜索数据是两个不同的例子,没有前后依赖关系 准备工作:需要在vis ...

  8. 做一个懒COCOS2D-X程序猿(一)停止手打所有cpp文件到android.mk

    前言:”懒”在这里当然不是贬义词,而是追求高效,拒绝重复劳动的代名词!做一个懒COCOS2D-X程序猿的系列文章将教会大家在工作中如何偷懒,文章篇幅大多较短,有的甚至只是几行代码,争取把懒发挥到极致! ...

  9. A1091. Acute Stroke

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  10. redis前端启动和后台启动的区别

    Part I. 直接启动下载官网下载安装tar zxvf redis-2.8.9.tar.gzcd redis-2.8.9#直接make 编译make#可使用root用户执行`make install ...