PYTHON LAMBDA FUNCTION

Python Lambda Function

Python Lambda Function in python is a small function that is capable of taking any number of arguments just with one expression. The syntax of the lambda function is rather concise than regular python functions. These are anonymous functions, which means functions without a name. Lambda is to define the function without a name instead of the def keyword.

Lambda functions in any programming language are the root in lambda calculus.

As a quick example, let’s take an identity function that returns the value equal to the argument. We can write the standard python definition using the def keyword.

>> def identity(x):
>> return x

The same if constructed using lambda can be expressed as:

>> lambda x:x

Lambda Function Structure

The lambda function mainly consists of :

1. Keyword

2. Bound variable

3. Body

The keyword which is lambda is to initiate a lambda function. The bound variable is the argument provided to it. And the last, which is a free variable, the user can treat it as the body of the expression. It can be the constant or the variable.

A bit more elaborated example can be a lambda function that multiplies 3 to an argument.

>> lambda x: x*3

The above function can also be an argument by surrounding the function and its argument with parentheses.

>> (lambda x: x*3) (2)

It will compute the value of x such that x = 2*3.

So, the output will be 6.

The lambda function can be assigned a name or it can be inside a variable because it is an expression and object.

>> myltiply_three = lambda x: x*3
>> multiply_three(2)

The above code is equivalent to:

def multiply_three(x):

​return x+1

One thing that you must have noticed is that the definition of lambdas and the arguments are not separated from parentheses. Lambda functions that take more than one argument are listed and separated with commas and without surrounding them with parentheses.

>> full = lambda first_one, last_one : f β€˜ Name: {first_one.title()} {last.title()}’
>> full (β€˜Megha’, β€˜Garg’)

Output :

β€˜Name : Megha Garg’

In the above example, the lambda function takes two arguments and returns a string according to the written code, by intermixing two parameters under the “f” string. It can be seen that the listing lambda arguments require no parentheses while calling the function is just like the normal function calling in python, with parentheses surrounding the arguments.

Calling For Anonymous Function

In the above example, we have assigned a name to the lambda function defined, and then we have called that function with the name assigned, passing the arguments into it. 

Lambda function can also be defined without name lossly. Suppose an anonymous function taking two arguments but is not assigned a name.

>> lambda x,y : x+y

The above function defined takes two arguments and returns the sum of them. The case is we have not assigned any name to it. 

To call the above function, the special python interpreter provides in python comes into being.

>> lambda x,y : x+y

Output :

3

The underscore interpreter does the work.

However, In most cases, you will be assigning names to them.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!