Python Lambda Function with Example

Lambda Functions or Anonymous Functions are throw-away functions. They are just needed where they have been created and they can be used anywhere a function is required. It’s just created by using the lambda keyword. It has the following syntax:

lambda arguments: expression

Properties of Lambda Function:

1. Lambda Functions have no name.
2. Lambda Function can take any number of arguments.
3. Lambda Function can return just one value in the form of an expression.
4. It doesn’t have an explicit return statement but it always contains an expression that is returned.
5. They can’t access variables other than those in their parameter list.
6. Lambda Function can’t even access global variables.

Example:

sum=lambda a, b: a+b
print("The addition is:", sum(15, 10)

Output:
The addition is: 25

Program:

def small(x,y):
if(x<y):
return x
else:
return y
sum=lambda x, y:x+y
diff=lambda x, y:x-y
print("The smaller of two number is=", small(sum(-3,-2), diff(-1,2)))

Output:
The smaller of two number is= -5