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
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