lambda function usage python lambda functions in use in python

lambda function usage in python

 

Example 1: the plurality of parameters passed lambda functions

def sum(x,y):
      return x+y

Lambda is achieved by:

 

p = lambda x,y:x+y
print(p(4,6))

Example 2: passing one parameter, lambda functions

 

the lambda X = A: X X * 
print (a (3)) # Note: This direct a (3) may be performed, but not the output, in front of the print can not be less

Example 3: lambda form of a plurality of parameters:

 

a = lambda x,y,z:(x+8)*y-z
print(a(5,6,8))

Anonymous function lambda: No definition refers to a class identifier (function name) of the function or subroutine.
lambda function may receive any of a plurality of parameters (optional parameters) and returns the value of a single expression.

Important:
. 1, the lambda functions can not contain commands,

2, the expression can not contain more than one.

Description: lambda necessarily have to use the function; any place where they can be used, can define a separate normal function to replace. I use them in require special packaging, non-reusable code without my code flooded with one-way function.

Format anonymous lambda function: before the colon is the parameter can have multiple, separated by commas, colons to the right of expression. In fact, the return value is the address of a lambda function, which is the function object.

Copy the code
a=lambda x:x*x
print(a)
print(a(3))
---->
<function <lambda> at 0x0000000002093E18>
9
Copy the code

Example 1: the plurality of parameters passed lambda functions

def sum(x,y):
      return x+y

Lambda is achieved by:

 

p = lambda x,y:x+y
print(p(4,6))

Example 2: passing one parameter, lambda functions

 

the lambda X = A: X X * 
print (a (3)) # Note: This direct a (3) may be performed, but not the output, in front of the print can not be less

Example 3: lambda form of a plurality of parameters:

 

a = lambda x,y,z:(x+8)*y-z
print(a(5,6,8))

Anonymous function lambda: No definition refers to a class identifier (function name) of the function or subroutine.
lambda function may receive any of a plurality of parameters (optional parameters) and returns the value of a single expression.

Important:
. 1, the lambda functions can not contain commands,

2, the expression can not contain more than one.

Description: lambda necessarily have to use the function; any place where they can be used, can define a separate normal function to replace. I use them in require special packaging, non-reusable code without my code flooded with one-way function.

Format anonymous lambda function: before the colon is the parameter can have multiple, separated by commas, colons to the right of expression. In fact, the return value is the address of a lambda function, which is the function object.

Copy the code
a=lambda x:x*x
print(a)
print(a(3))
---->
<function <lambda> at 0x0000000002093E18>
9
Copy the code

Guess you like

Origin www.cnblogs.com/ceo-python/p/11566844.html