Lambda function python:

Lambda function python

lambda function python


        Lambda function in python is also known as  anonymous function .Lambda 
function is so called function because is  not declare with def keyword like other 
functions.Rather , they are created using lambda keyword .Lambda functions are 
throw away function .
Syntax of Lambda function can be written on single line .
i.e. :
        lambda arguments :expression 

multiple arguments must be separated by an comma " , " .
Lambda function is assigned to a variable to give name to it .

EXAMPLE :
   
    multi=lambda a, b : a*b
    print("Multiplication =",multi (6,4))

OUTPUT : Multiplication = 24

   In the above code ,the lambda function returns the multiplication of its two 
arguments. In above program ,lambda  a , b: a*b:  is lambda function ,a,b are 
formal parameters and a*b is expression that get evaluated first and then returned 


NOTE
Lambda function has no name .

lambda a, b :a*b

is similar to

def multi (a, b):
    return a*b


So it make  sense  to use lambda function for small function to reduce code 
length , user can use lambda function whenever function objects are required 


NOTE
Lambda functions in python are not similar/equivalent to inline function in C.


KEY Points to Remember :

1.Lambda function has no name .
2.Lambda function can take any number of arguments. 
3.Lambda  function will return only one value >
4.Lambda Function does not have an explicit return statement .
5.Syntax of lambda function is on one line .
6.Lambda function can not access  global variables . 


Users can pass lambda functions in User defined  functions  as well .
EXAMPLE :

def cal(a,b):

       sum = lambda a,b:a+b

      print(sum(a,b))

     diff= lambda a,b:a-b

     print(diff(a,b))

     multi=lambda a,b:a*b      print(multi(a,b)) 

cal(5,2)

OUTPUT : 

7

3

10

User can pass a lambda function without assigning it to a variable 
EXAMPLE:
print((lambda a:a*a*a)(3))     # argument pass through lambda function 
OUTPUT : 27



  NOTE
The time taken by a lambda function to perform a computation is almost similar to time taken by normal function 



I hope this article on lambda function in python is helpful for You .

                                                              Thank u...😊