A lambda function in Python is a small, anonymous function defined using the lambda keyword. Lambda functions can have any number of arguments, but, unlike regular functions, they only have a single expression, which is evaluated and returned.
Example: The following lambda function adds 10 to the argument a and returns the result:
add_nums = lambda x, y: x + y
print(add_nums(4, 28))
32
The strength of lambda functions becomes apparent when used as anonymous functions inside other functions, making your code more concise and readable. For example, you can use a lambda function to create a function that multiplies its input by a given number:
def mult_num(m):
return lambda x: x * m
multiplier = mult_num(10)
print(multiplier(28))
280
In this case, function mult_num
returns a lambda function that multiplies its argument by m
. When calling multiplier(10)
, the value 10 is multiplied by 28, returning 280.
Use the Search Bar to find content on MarketingMind.
Contact | Privacy Statement | Disclaimer: Opinions and views expressed on www.ashokcharan.com are the author’s personal views, and do not represent the official views of the National University of Singapore (NUS) or the NUS Business School | © Copyright 2013-2025 www.ashokcharan.com. All Rights Reserved.