Python Lambda | How to use lambda in python | Anonymous Function

Python Lambda

python lambda


The Function without a name is the Anonymous function and we also called a Lambda function.
A Lambda function uses one expression to take any number of the argument.

Syntax:

lambda arguments: expression

Example:

Add 5 to the arguments a, And then return the value and print it.

f = lambda a : a + 5
print(f(5))

Output:

10


Example of a Lambda with multiple arguments.

Example:

x = lambda ab : a * b
print(x(156))

Output:

90


Example:

x = lambda abc : a + b + c
print(x(1569))

Output:

30

Why Use Lambda Function? What are the features of the lambda function?

Lambda function is used for nameless function, so we have to use it when we required a function with a short period of time and nameless function. 
And lambda contains multiple arguments in one expression.

Make a function that doubles the number we send:

Example:

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)

print(mydoubler(11))

Output:

22

Example:

def myfunc(n):
  return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(8))
print(mytripler(8))

Output:

16
24


Next Post Previous Post
1 Comments
  • bhanu
    bhanu August 18, 2021 at 5:58 PM

    I have found great and massive information
    Python Online Training Hyderabad
    Best Python Online Training

Add Comment
comment url