,

Unpacking the Python Lambda Function – Understand How it works.

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
  1. 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 (arguments a and b) 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.
  2. 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 a def keyword like a regular function. Instead, you write lambda followed 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))
  1. def myfunc(n):: This line defines a function named myfunc that takes one argument n. Inside this function, it returns another function (a lambda function) that takes one argument a.
  2. return lambda a : a * n: This inner lambda function takes an argument a and multiplies it by the value of n (the argument passed to myfunc). It essentially creates a function that will double any number you give it.
  3. mydoubler = myfunc(2): Here, we call myfunc with the argument 2, which means n is set to 2. This call returns a lambda function that doubles its input. We assign this lambda function to the variable mydoubler.
  4. print(mydoubler(11)): Finally, we use mydoubler as a function. When we call mydoubler(11), we’re essentially calling the inner lambda function with a set to 11, which then multiplies 11 by 2 (the value of n set earlier), resulting in 22.

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:

  1. def myfunc(n):: This line defines a function named myfunc that takes one argument n.
  2. return lambda a : a * n: Inside myfunc, it returns a lambda function. This lambda function takes one argument a and multiplies it by the value of n (the argument passed to myfunc).
  3. mydoubler = myfunc(2): Here, myfunc(2) is called, which means n is set to 2. This call returns a lambda function that doubles its input. We assign this lambda function to the variable mydoubler.
  4. mytripler = myfunc(3): Similarly, myfunc(3) is called, which means n is set to 3. This call returns a lambda function that triples its input. We assign this lambda function to the variable mytripler.
  5. print(mydoubler(11)): We use mydoubler as a function. When we call mydoubler(11), we’re essentially calling the inner lambda function with a set to 11, which then multiplies 11 by 2 (the value of n set earlier), resulting in 22.
  6. print(mytripler(11)): Similarly, when we call mytripler(11), we’re calling the inner lambda function with a set to 11, which then multiplies 11 by 3 (the value of n set earlier), resulting in 33

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.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>