zoukankan      html  css  js  c++  java
  • python学习笔记四:lambda表达式和switch

    一、定义

    lambda arg1,arg2... : returnValue

    二、示例

    #!/usr/bin/python
    
    def f(x,y):
        return x*y
    
    print f(2,3)
    #6
    
    g = lambda x,y:x*y
    print g(2,3)
    #6

    三、switch的一种实现方案

    #!/usr/bin/python
    
    from __future__ import division
    
    #a=int(raw_input('please input num1:'))
    #b=int(raw_input("please input num2:"))
    
    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
    
    def 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:
            pass
    
    operatord = {'+':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)
  • 相关阅读:
    生活重心
    做自己才对,想多只会徒增烦恼
    列下计划,一个个实现吧
    公司搬迁
    限制文件的类型
    总结
    mvc mvp mvvm区别
    sessionStorage
    localStorage点击次数存储
    2016.09.01 html5兼容
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/python-note-lambda-switch.html
Copyright © 2011-2022 走看看