字典实现swith

from __future__ import division


#if else形式

def operator(x,o,y):

if o == "+":

print x+y

if o == "-":

print x-y

if o == "*":

print x*y

if o == "/":

print x/y


#swith形式

def dicForSwith(x,o,y):

dic = {'+':x+y,'-':x-y,'*':x*y,'/':x/y}

print dic.get(o)


if __name__=='__main__':

operator(2,'/',4)

dicForSwith(1,'/',2)



使用if需要先判断在进行计算,影响效率


你可能感兴趣的:(python,笔记,swith)