Python for beginners - Lambda function in python

Python has some interesting function called “Lambda”. In this function, the user can have any number of arguments in one expression.

The syntax be like,

Lambda arguments: expression

For example, Multiply the variable by 2 and return the result. The code will be given below..

y = lambda b: b * 2

print(y(3))


Next, using 2 arguments in lambda function. It has 2 arguments x, y. we add the values and return it.

a = lambda x, y : x + y

print (a(7,8))

while executing this, you get the below output.

When you use 3 arguments in lambda function, it look like below program.

Let d as the returning value. x,y,z are three variables. The function is add these 3 variables.

d = lambda x, y, z : x + y + z

print(d(3,4,5))

The output is


Lambda function can be used in many ways.

It can be defined once, use it various places.

The function may be called in many times.

But, it can be used in short period of time.

No comments:

Post a Comment