Lambda function is one of the miscellaneous topics in python programming. In this article, we will learn what is it and how we can use it in different scenarios.
What is the Lambda function in Python?
As per the definition, It is a single-line anonymous function. It means a function without a name. It is created with lambda
a keyword and may or may not be assigned to a variable. Lambda keyword in the programming languages is taken from Lambda calculus.
First Lambda function:
Let’s check out the first example of the lambda function and how to invoke that as followed.
Create a lambda function that takes two string arguments and returns a combined string with a space.
lambda x,y:f"{x} {y}"
Here lambda
is the keyword not the variable
bound variables are : x,y
The function body is: f”{x} {y}”
The above function is similar to the below standard python function.
>> def combine_str(x,y):
return f"{x} {y}"#-----calling the above functtion
>> combine_str('khush','Love Python')
#-----output
'khush Love Python'
Invoking Lambda functions: