• In python programming language lambda function is an anonymous function or a function having no name.
  • It becomes a small and restricted function having no more than one line.
  • A lambda function can have multiple arguments with one expression, like a normal function.
  • In python lambda expressions or lambda forms are utilized to construct anonymous functions.
  • Here we use the lambda keyword just as we use def to define normal functions.
  • In python every anonymous function we define will have 3 essential parts, they are lambda keyword, parameters, function body.
  • This function can have any number of parameters, but it contains only one expression.
  • A lambda function is written in a single line of code and can also be invoked immediately.
  • It is not exactly an inline function, but it simulates inline functions of C and C++.

Sample Code

# a and b are the arguments and a*b is the expression which gets evaluated and eturned.    
x = lambda a,b: a*b
print("mul = ", x(20,10))
mul = 200
#the function table(n) prints the table of n
def table(n):
return lambda a:a*n # a will contain the iteration variable i and a multiple of n is returned at each function call
n = int(input("Enter the number:"))
b = table(n) #the entered number is passed into the function table. b will containa lambda function which is called again and again with the iteration variable i
for i in range(1,11):
print(n,"X",i,"=",b(i)) #the lambda function b is called with the iteration variable i

Output

Categorized in: