Python study NOTE 4: lambda expressions and switch, pythonlambda

Source: Internet
Author: User

Python study NOTE 4: lambda expressions and switch, pythonlambda
I. Definition

lambda arg1,arg2... : returnValue
Ii. Example
#!/usr/bin/pythondef f(x,y):    return x*yprint f(2,3)#6g = lambda x,y:x*yprint g(2,3)#6
3. An implementation scheme of 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)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.