Saturday 24 December 2016

What is Python Lambda?

      

Lambda operator or Lambda Function is used to create functions without name.

Syntax: lambda arguments :expression

Example:IN:sum = lambda x, y : x + y   #lambda expression with x,y as arguments, x+y expression
               IN:sum(3,4)   #sum function here x=3 and y=4 
               OUT: 7
                The expression (x+y) is evaluated and returned

               The above is similar to
               IN:def  sum(x,y):  #defining a function sum which takes x and y as arguments
                            return x+y

In Python normal functions are defined using "def" keyword where as anonymous functions are defined using "lambda" keyword.


Important  :

1.Lambda functions can have any number of arguments but only one expression.
2.lambda creates a function and return itself.
3.lambda can be used inside list,dictionary etc.....


Why Use Lambda Functions?

1.Is used when we require nameless function.
2.When we need to pass  functions to other function.
3.lambda is useful when developing GUI .
They are mainly used along with :
1.filter()
2.map()
3.reduce()