codingstreets
Search
Close this search box.
python-lambda-function

Introduction a unique Insider: Python Lambda Function

In this article you will learn about Python lambda function.

Python lambda function – Before moving ahead, let’s know a little bit about Python def function

lambda function – The python lambda function is an anonymous function and known as the lambda function. A lambda function is a function that supports only one argument but can accept more than one or any number of arguments.

Anonymous function – An anonymous function is a function defined without a name whereas other functions in Python defined with the def keyword. In Python, an anonymous function defined with lambda keyword.

Syntax - lambda arguments : expression

It executed the expression and returned the result.

Example 1- An argument is passed to lambda function and multiplies 5 to it.

x = lambda a : a * 5
print (x(10))
python-lambda-function

Explanation – In the above example, lambda a: a * 5 is the lambda function. Here a is the argument and a * 5 is the expression that multiplied with 5.

Example 2- Pass any number of argument to lambda function.

x = lambda a, b, c : a*b*c
print(x(2, 2, 2))
python-lambda-function

Explanation – In above example, lambda a,b,c : a*b*c is the lambda function. Here a,b,c is the argument and a*b*c is the expression that multiplied with 2 triple times.

A lambda function can take any mathematical operator. It can handle two or more different types of mathematical operator at same time.

Example 3- Pass two types of mathematical operator to lambda function.

x = lambda a, b, c : a*b+c
print(x(6, 6, 1))
python-lambda-function

Explanation – In above example, lambda a, b, c : a*b+c is the lambda function. Here a, b, c is the argument and a*b+c are the expression that multiplied with 6 two times and then added 1.

What is need of using Lambda Functions – A lambda function works as argument passes to it.

For example, a function that takes one argument, and that argument will be multiplied with an unknown number:

def myfunc(i):
     return lambda a : a * i

Example 4- Use of lambda function with def function to double the number passed as argument.

def my_num(n):
   return lambda a : a*n 
x = my_num(10)
print(x(2)) 
python-lambda-function

Explanation – In the above example, lambda a : a*n is the lambda function. Here a is the argument and a*n is the expression. Here lambda function is performed that returns doubling of two numbers by multiplying argument and expression.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on