Lambda function in Python

05 min read

To create an anonymous function in the form of an expression, we use the Lambda statement. The syntax of lambda statement is as follows:

lambda args : expression

args is a comma-separated list of arguments, and expression is a formula involving those arguments. To understand better, consider the example,

Task: Write a program to add two numbers using lambda.

a= lambda x,y : x+y
print a(2,3)

Output:
5

The code defined with lambda must be a valid expression. Multiple statements and other non- expression statement, such as print, for, and while cannot appear in a lambda statement. Lambda expressions follow the same scoping rules as functions.

the lambda function is also known as a function with no name. Consider an other example,

answer = lambda x: x*7
print(answer(5))

Output:
35

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error