Skip to content

Lambda function (lambda)

Single line of expression of a function.


A function can be define in 2 way:

  • def keywords, which can define a function that has their own name.
  • lambda keywords, which is use to define a anonymous function.
  • A define function that can use many time as they have their own name.
  • While anonymouse function only can use once / temporaily

Syntax

py
lambda params1, params2 : single_line_of_function_expression

Info

  • lambda function only suitable for the scenario that only use the function once. If he function that required to use many time, then define function would be the best choice.

Usage

lambda.py

py
## Function as a params (lambda)

def addition_operation(compute):
    results = compute(1, 2)
    print(type(compute))
    print(results)

addition_operation(lambda x, y: x + y)

Output:

<class 'function'>
3