python基础===使用switch方法,减少使用if语句
生活随笔
收集整理的這篇文章主要介紹了
python基础===使用switch方法,减少使用if语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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):#單獨的看operator.get(o),其實就等于,jia,jian,cheng,chu 中的一種print(operator.get(o)(x,y))f(4,"+",9)#switch 實現 加減乘除的方法!
?
轉載于:https://www.cnblogs.com/botoo/p/7367916.html
總結
以上是生活随笔為你收集整理的python基础===使用switch方法,减少使用if语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# Parallel.For 并行编程
- 下一篇: MySQL explain