LAMBDA Function
What is Lambda Function in Python?
A lambda function is a small anonymous function.
A lambda function can take any number of arguments,
but can only have one expression.
LAMBDA Function
• Lambda Function, also referred to as ‘Anonymous function’ is same as
a regular python function but can be defined without a name.
• normal functions are defined using the def keyword and
• anonymous functions are defined using the lambda
keyword.
• Lambda function are restricted to single line of expression. They can
take in multiple parameters as in regular functions.
Lambda Function Syntax
• The syntax for lambda function is given by :
• lambda arguments: expression
• Notice, there can be any number of arguments but can contain only a
single expression
• There is no return statement
Need for Lambda Functions
There are at least 3 reasons:
1. Lambda functions reduce the number of lines of code when
compared to normal python function defined using def keyword.
2. They are generally used when a function is needed temporarily for a
short period of time, often to be used inside another function such
as : filter, map and reduce (These are built-in functions in Python)
3. Using lambda function, you can define a function and call it
immediately at the end of definition. This can’t be done with def
function.
How to use lambda functions
Let’s try to define a function for calculating the squares of given values.
# calculate squares using lambda
squares = lambda x: x*x
print('Using lambda: ', squares(5))
How to use lambda functions
Let’s also look at how to do the same function using def keyword, and
compare them.
# calculate squares using def
def squares_def(x):
return x*x
print('Using def: ', squares_def(5))
Lambda functions can have 0 or 1 expression, not more.
1. No expression : contains no expression, will give the same output for all arguments.
Example:
x = lambda : "hello Raj Welcome to Python Programming Language"
print(x())
2. Single expression: They can contain either one expression or no expression. We cannot put more
than one expression in a lambda function.
Example:
single = lambda x : (x%2)
print(single(10))
Lambda functions can be Immediately Invoked
You can implement a lambda function without using a variable name. You can
also directly pass the argument values into the
Lambda function . This cannot be done using def function
In lambda - (lambda x,y : x*y)(5,7)
his doesn’t work with def function. def multiply(x, y): return x*y (5,7)
It is possible to write higher order functions using lambda
Here What is higher order functions ?
Answer: A function is called Higher Order Function if it contains other
functions as a parameter
i.e., the functions that operate with another function are known as
Higher order Functions.
It is possible to write higher order functions using lambda
Example for higher order functions ?
# Define a lambda function that can take another lambda function (func1).
high_order = lambda x, lmbfunc: x*lmbfunc(x)
print(high_order(10, lambda x : x*x))
# The inner lambda function is defined when calling the high_order.
print(high_order(10, lambda x : x*x))
Lambda functions accept all kinds of arguments
like normal def function
Lambda function supports all kinds of arguments just like the normal
Example :
1. Keyword Arguments:
2. Variable list of Arguments
3. Positional arguments:
Lambda functions accept all kinds of arguments
like normal def function
## Named Arguments ##
namedArgs=(lambda x, y=3, z=5: x*y*z)(7)
print(namedArgs)
## Variable Arguments ##
variable=(lambda x, y=3, z=5: x*y*z)(x=7)
print(variable)
Lambda functions accept all kinds of arguments
like normal def function
## Variable keyword arguments ##
variableKey=(lambda *args : sum(args))(3,5,7)
print(variableKey)
## Positional arguments ##
positional=(lambda x,y,z : x*y*z)(3,5,7)
print(positional)
How to use lambda function to manipulate a Data frame
What is the difference between lambda and Def?
• Def can hold multiple expressions while lambda is a Uni-
expression function.
• Lambda forms a function object and returns it. Def can have a
return statement.
• Lambda functions are used along with built-in functions like
filter() , map() etc.
• We use lambda functions when we require a nameless
function for a short period of time
What is the difference between lambda and Def?
• Lambda function can be used whenever function objects are
required.
• Fewer Lines of Code − One of the most benefits of a lambda
expression is to reduce the amount of code
LAMBDA Function in Python
Syntax
lambda arguments : expression
Example : lambda a : a+10
Add 10 to argument a and return the value
x = lambda a : a + 10
print(x(5))
O/P – 15
LAMBDA Function in Python
• Lambda functions can take any number of arguments:
but have only one expressions.
Example - Multiply argument a with argument b
x = lambda a, b: a * b
print(x(5, 6)
LAMBDA Function in Python
• Lambda functions can take any number of arguments
but have only one expressions.
• Example 1- Multiply argument a with argument b
x = lambda a, b: a * b
print(x(5, 6)
O/P - 30
LAMBDA Function in Python
Example -2 - Summarize argument a,b and c and return
the result.
x = lambda a, b, c: a + b + c
print(x(5, 6, 2))
O/P = 13
LAMBDA Function in Python
Example -2 - Summarize argument a,b and c and return
the result.
x = lambda a, b, c: a + b + c
print(x(5, 6, 2))
O/P = 13
LAMBDA Function in Python
Example -2 - Summarize argument a,b and c and return
the result.
x = lambda a, b, c: a + b + c
print(x(5, 6, 2))
O/P = 13