Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

Python Anonymous/Lambda Function



In Python anonymous functions are also called as Lambda functions.

Python Anonymous function is a function which is without a name.

We define functions in python is using the keyword def. Now if we define the anonymous function or lambda function we are not going to use the def keyboard. We will use the lambda keyword.

So the initial difference that we can easily detect is that lambda is the keyboard which will be used to define anonymous/lambda function.

Remember the school days of programming you might have used an inline function. Let's refresh it in contrast to python language.

Lambda Function is having a syntax as:
lambda argument: expression

  • Arguments can be multiple but the expression can be a single
  • It returns the result value to the caller.

We can use this wherever we need to use function objects required.

Example:

# Program to show the use of lambda functions
double = lambda x: x * 5
# Output: 5
print(double(1))

We can use lambda/anonymous function wherever we require the nameless function for a shorter period of time or which is not going to be reusable.

Please let us know about the article in comments or contact us.

Top #3 Articles

Most Read