Frequently Asked Questions

What is lambda in Python? Why it is Used?

  • The lambda function in python is an anonymous function as it has no name.
  • It uses the lambda keyword instead of the def keyword to create a function.
  • It is used to write simple functions in just one line.
  • It can have multiple parameters but a single expression.
  • It is good for performing short operations and data manipulations.

Lambda Function Syntax

lambda arguments : expression

Let us create a simple function add which takes a parameter a and returns a+10.

 
def add(a):
    return a+10

print(add(5))

15

Now, let us convert the above function into a lambda function using the syntax [lambda arguments: expression]


x= lambda a : a+10

print(x(5))

15


x= lambda a,b,c : a+b+c

print(x(5,6,7))

18

Other Popular Questions