Python anonymous functions

Source: Internet
Author: User

First, anonymous function

An anonymous function is one that does not require explicit specified function names , and the most complex operation supported by anonymous functions is the ternary operation.

anonymous function functions:
1. Save Code Volume
2, looks more advanced

# Example One: def Calc (x, y    ): return x*y#  overwrites the above general function as an anonymous function:lambda x,y:x*Lambda X,y:x*y    #  declares an anonymous function and assigns a value to the Funcprint(func (3,8))       #  output of 24 
#Example Two:#To overwrite a complex function with an anonymous functiondefCalc (x, y):ifX <y:returnx*yElse:        returnx/yPrint(Calc (16,8))#converting the above function into an anonymous function, the most complex operation supported by an anonymous function is the ternary operation. func1=LambdaX,y:x*yifX < yElseX/Y#anonymous function converted to ternary operation formPrint(Func1 (16,8))
Second, anonymous function application
data = List (range (10))Print(data)#method One: forIndex,iinchEnumerate (data): Data[index]= i*IPrint(data)#Method Two:defF2 (n):returnNNPrint(List (map (F2,data )))#Method Three:Print(List (Map (LambdaX:x*x,data)))#map functions and anonymous functions are used once, so no need to define functions

Python anonymous functions

Related Article

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.