A lambda function can take any number of arguments but can only have one expression.
Now,
What are arguments and expression?
Arguments are like pieces of information that you give to a function or a command, telling it what to do or how to do it. For example, if you have a function called add_numbers, you might give it two numbers as arguments, like add_numbers(5, 3), telling the function to add 5 and 3 together.
Expressions, on the other hand, are like small pieces of code that produce a value. They can be as simple as a single number or as complex as a formula. For example, 5 + 3 is an expression that adds 5 and 3 to produce the value 8.
Hope now its clear, lets move forward in this article and check out the syntax of the lambda function. In simple terms how to use a lamba function in Python
Syntax of Lambda Function:
lambda arguments : expression
Example:
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
Output: 15
Question can come up in your mind that, can lambda function take multiple arguments ?
Yes, it can ! let us see how
Example (Multiple Arguments)
Multiply argument a with argument b and return the result:
x = lambda a, b : a * b
print(x(5, 6))
Output: 30
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
in the above code it takes 3 arguments , tell me in the comment section below what is the output ?
Moving on to the most important question
Why To Use Lambda Function ?
Lambda functions in Python are used for creating small, anonymous functions. They are handy when you need a quick function for a short period of time. Unlike regular functions defined with the def keyword, lambda functions are written in a single line and can take any number of arguments, but can only have one expression.
# Regular function
def add(a, b):
return a + b
# Equivalent lambda function
add_lambda = lambda a, b: a + b
# Using both functions
result1 = add(5, 3)
result2 = add_lambda(5, 3)
print(result1) # Output: 8
print(result2) # Output: 8
- Regular function (
def add(a, b): ...): This is like a traditional recipe you might follow to cook a meal. You define the steps (function body) with a name (add) and specify what ingredients (argumentsaandb) to use. When you want to add two numbers, you call this function by name (add) and provide the numbers as inputs, and it gives you the result. - Lambda function (
lambda a, b: a + b): This is like a quick and simple recipe you might jot down on a sticky note. It doesn’t have a name, and it’s designed for a specific, short task—in this case, adding two numbers. You don’t have to define it with adefkeyword like a regular function. Instead, you writelambdafollowed by the arguments and a colon, and then the expression to be evaluated (a + b). This creates a small, anonymous function that can be used wherever you need it.
Lambda functions are often used in situations where you need a function for a short calculation or when you want to pass a function as an argument to another function, like in sorting or mapping operations.
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
def myfunc(n):: This line defines a function namedmyfuncthat takes one argumentn. Inside this function, it returns another function (a lambda function) that takes one argumenta.return lambda a : a * n: This inner lambda function takes an argumentaand multiplies it by the value ofn(the argument passed tomyfunc). It essentially creates a function that will double any number you give it.mydoubler = myfunc(2): Here, we callmyfuncwith the argument2, which meansnis set to2. This call returns a lambda function that doubles its input. We assign this lambda function to the variablemydoubler.print(mydoubler(11)): Finally, we usemydoubleras a function. When we callmydoubler(11), we’re essentially calling the inner lambda function withaset to11, which then multiplies11by2(the value ofnset earlier), resulting in22.
So, the output of this code will be 22, as mydoubler(11) effectively doubles 11 to 22.
Or, use the same function definition to make a function that always triples the number you send in:
def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))
use the same function definition to make both functions, in the same program:
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Explanation of the above code:
def myfunc(n):: This line defines a function namedmyfuncthat takes one argumentn.return lambda a : a * n: Insidemyfunc, it returns a lambda function. This lambda function takes one argumentaand multiplies it by the value ofn(the argument passed tomyfunc).mydoubler = myfunc(2): Here,myfunc(2)is called, which meansnis set to2. This call returns a lambda function that doubles its input. We assign this lambda function to the variablemydoubler.mytripler = myfunc(3): Similarly,myfunc(3)is called, which meansnis set to3. This call returns a lambda function that triples its input. We assign this lambda function to the variablemytripler.print(mydoubler(11)): We usemydoubleras a function. When we callmydoubler(11), we’re essentially calling the inner lambda function withaset to11, which then multiplies11by2(the value ofnset earlier), resulting in22.print(mytripler(11)): Similarly, when we callmytripler(11), we’re calling the inner lambda function withaset to11, which then multiplies11by3(the value ofnset earlier), resulting in33
In conclusion, unpacking the Python lambda function provides a deeper understanding of its functionality and usage. The lambda function is a concise way to create anonymous functions, often used for small tasks or as arguments to higher-order functions. By using lambda functions, you can write more readable and expressive code in situations where a full function definition is not necessary or desirable.
Understanding how lambda functions work, including their syntax and behavior, allows you to leverage their power effectively in your code. It’s important to remember that lambda functions are limited in their capabilities compared to regular functions, as they can only contain a single expression. Despite this limitation, lambda functions are a valuable tool in Python programming, offering a way to write more concise and functional-style code.





Leave a Reply