Python lambda function basic usage Example Analysis, pythonlambda
This article describes the basic usage of Python lambda functions. We will share this with you for your reference. The details are as follows:
Here we will briefly learn about the python lambda function.
First, let's take a look at the syntax of the python lambda function as follows:
F = lambda [parameter1, parameter2,…] : Expression
In a lambda statement, there are 0 or more parameters before the colon, which are separated by commas. the return value is displayed on the right side of the colon. A lambda statement is actually a function object.
1 No Parameters
F = lambda: 'python lambda! '>>> F <function <lambda> at 0x06BBFF30 >>> f ()' python lambda! '
2. There are parameters and no default values
F = lambda x, y: x + y >>> f (3, 4) 7
3. There are parameters and default values.
F = lambda x = 2, y = 8: x + y >>>> f <function <lambda> at 0x06C51030 >>>> f () # x takes the default value 2, y takes the default value 810> f (1) # x takes 1, y takes the default value 89> f () # x, y all values are 36
4. The function returned by lambda can also be used as a parameter of another function.
Sumxy = lambda x, y: x + ydef test (f, m, n): print f (m, n) >>> sumxy (4, 5) 9 >>> test (sumxy, 4,5) 9