首页 > 学院 > 开发设计 > 正文

python学习笔记四:lambda表达式和switch

2019-11-14 17:26:47
字体:
来源:转载
供稿:网友

一、定义

lambda arg1,arg2... : returnValue

二、示例

#!/usr/bin/pythondef f(x,y):    return x*yPRint f(2,3)#6g = lambda x,y:x*yprint g(2,3)#6

三、switch的一种实现方案

#!/usr/bin/pythonfrom __future__ import division#a=int(raw_input('please input num1:'))#b=int(raw_input("please input num2:"))def jia(x,y):    return x+ydef jian(x,y):    return x-ydef cheng(x,y):    return x*ydef chu(x,y):    return x/ydef Operator(x,o,y):    if o == '+':        print jia(x,y)    elif o == '-':        print jian(x,y)    elif o == '*':        print cheng(x,y)    elif o == '/':        print chu(x,y)    else:        passoperatord = {'+':jia,'-':jian,'*':cheng,'/':chu}def switchoperator(x,o,y):    print operatord.get(o)(x,y)operator(2,'+', 4)operator(2,'-', 4)operator(2,'*', 4)operator(2,'/', 4)switchoperator(2,'+', 4)switchoperator(2,'-', 4)switchoperator(2,'*', 4)switchoperator(2,'/', 4)

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表