This paper compares and analyzes the usage of lambda and def in Python. Share to everyone for your reference. The specific analysis is as follows:
1. Lambda is used to create anonymous functions, unlike Def (the function created by Def has a name).
2. Lambda does not assign the result to an identifier, and DEF assigns the result of the function to an identifier.
3, Lambda is an expression, and DEF is a statement
Sample program:
>>> f1 = Lambda X,y,z:x*2+y+z # lambda with multiple parameters >>> print F1 (3,2,1) 9>>> F3 = Lambda i:i*2 # lambda With a parameter >>> print F3 (7) 14>>> def fun1 (n): ... Return Lambda M:m**n # m n ...>>> def fun2 (M, n): ... return m+n...>>> F2 = fun1 (2) # dynamically generate a function >>> print F2 (4) 16>>> print fun2 (3, (Lambda x:x+1) (2)) # Lambda is used as a function parameter 6>>>
Hopefully this article will help you with Python programming.