What-is-LAMBDA?

def function_name(): pass

Since Python already has a function why do need to use lambda?

Why-use-LAMBDA?

LAMBDA?”>

To create a lambda is not as difficult as it may seems. It is quite similar in structure like function except lambda is in single line expression compared to function which is more than a line of expressions.

lambda syntax in Pyton

lambda arguments: expression

Mechanism in lambda

  • lambda can only have single expression
  • it can have multiple arguments
print("using function")
def square_x(x):
    return x * x

print(square_x(3))

print("using lambda")

# method 1- to enable, use bracket outside lambda
print(">>>")
foo = (lambda x: x * x)(3)
print(foo)

# method 2
print(">>>")
bar = lambda x: x * x
print(bar(3))

Above example is create a lambda with a single argument. What if we have more than one argument to pass into lambda?

foo = (lambda x, y, z: x * x + y * y + z * z)(2, 3, 4)
print(foo)

or we can construct to make it more readable like this inside it’s body of lambda:

bar = (lambda x, y, z: (x * x) + (y * y) + (z * z))(2, 3, 4)
print(bar)

Now let create a function that produce the same result as above lambda:

def sum_square(x, y, z):
    return (x * x) + (y * y) + (z * z)

print(sum_square(3, 4, 5))

Difference-between-LAMBDA-and-function

Notes: Make comparison lambda vs function by break or split for each compose block using table:

Part Function Lambda
Header def square_x (x) lambda x
Split by : and move to next line : stay in-line as single line expression
Body return x * x x * x
Run square_x(3) only put arguments end of lambda expression like this -> (3)

Next we try to do more examples using a table to split each of the compartment that tell the difference between function and lambda

Part Function Lambda
Header def sum_square (x, y, z) lambda x, y, z
Split by : and move to next line : and stay in-line as single line expression
Body return (x x) + (y y) + (z * z) (x x) + (y y) + (z * z)
Run sum_square(3, 4, 5) only put arguments end of lambda expression like this -> (3, 4, 5)

Can you tell the diference between function and lambda using these two examples?

Use-lambda-inside-a-function

def multiply(x, n): return x ** n # output for multiply(2, 2) # answer is 4 multiply(2, 3) # answer is 8

We got our function for power of exponential. However what if we want it to be declarative instead of calling current function multiply(x, n)

Here is where it comes the useful of having lambda. It can be used inside a function as a single expression. lambda expression can represented in a declarative ways like we would like to.

def multiplier(n):
    return lambda a : a ** n

exponent_two = multiplier(2)
exponent_three = multiplier(3)

print(exponent_two(2))
# answer is 4
print(exponent_three(2))
# answer is 8

Here is the catch using lambda inside a function.

  • It can be used in a declarative way
  • lambda is accessible inside a function in Python
  • assign lambda to another function like variable inside an existing function like

    exponent_two = multiplier(2)

  • compare a declarative calling between having a function or lambda

    multiply(2, 2) vs exponent_two(2)
    or …
    multiply(2, 3) vs exponent_three(2)
    and so on….