# Python lambda function

Python lambda function
A small anonymous function is lambda function.

A lambda has only one expression but,it can take any number of arguments that the programmer want.

Syntax

lambda arguments : expression

Following is an example that will add 10 to the number passed to it in the form of argument snd after adding 10 it will print the result.

x = lambda a : a + 10

print(x(5))

A lambda function may take any unmber of arguments.
Following is an example that will multiply argument 'a' with the argument 'b' and after performing the multiplication it will print the result:

x = lambda a, b : a * b

print(x(5, 6))

Following is an example that will add arguments 'a, b and c' and after performing the addition it will print the result:

x = lambda a, b, c : a + b + c

print(x(5, 6, 2))

Why Use Lambda Functions?
When you use a lambda function into another function as anonymous function then the power of the lambda function can be best shown.

For e.g. you have a function that takes an argument and that argument will be multiplied with an unknown number:

def myfunc(n):

return lambda a : a * n

Use that function definition to make a function that will always doubles the number you pass to it:

def myfunc(n):

return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))

Or, we can use the same function definition to make a function that will always triples the number you pass to it:

def myfunc(n):

return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))

Or we can use the same function definition to make both of the functions in the same program:

def myfunc(n):

return lambda a : a * n

mydoubler = myfunc(2)

mytripler = myfunc(3)

print(mydoubler(11))

print(mytripler(11))