Python Lamba/Anonymous Function [ simplest Example ]

Python Lambda function is also known as anonymous function as these type of functions does not have any function name. Nameless functions are defined using lambda keywords thus also known as Lambda functions.

Lambda function is also known as an anonymous function. These types of functions are normally defined to solve very simple expressions.

The syntax of the lambda function is as follows

lambda parameters: expression

Lambda functions can have any number of function arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

So with a lambda function, you can not use any if-else, while, for loop or return the value.

Python Lambda function Example

#program to define lambda function in python 

f = lambda x : x**2

#lambda function with map() function 
result = f(20)
print('The result is :',result)

The result is : 400

Landa function with map( ) function

f = lambda x : x**2

# lambda function with map() function

l1 = [1, 2, 4, 5, 6, 7, 78, 7, 4, 34, 4, 14, 44, 4, 4]

result = list(map(f, l1))

print(result)

output of lambda function with map() function

[1, 4, 16, 25, 36, 49, 6084, 49, 16, 1156, 16, 196, 1936, 16, 16]

Now we think you have the idea of lambda function in Python. Most of the time lambda functions are used when we do not want to assign a function name to very small functions otherwise a simple function is advisable.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.