lambda function python

What is Lambda Function? 

A lambda function is an anonymous function in Python that is defined using the lambda keyword. A lambda function can take any number of arguments but can only have one expression, which is executed and returned when the function is called. 

How we use lambda function?

A lambda function is a small anonymous function that is defined using the lambda keyword in Python. The general syntax for a lambda function is:

       >> lambda arguments : expression

The lambda keyword is used to define the function. The arguments part is optional and specifies the inputs to the function. You can have one or more arguments, separated by commas. The expression part specifies what the function does. It's evaluated and returned when the function is called.

A lambda function is a short, one-line function that you can use without giving it a name. It's defined using the lambda keyword, followed by the inputs (if any), a colon, and the expression to be evaluated. When you call the function, it returns the result of the expression.

For Example:

Find Square By Using Lambda Function (Pass Single Arguments)

square = lambda x : x**2
print(square(5)) 
                                                             OR
print(lambda x : x**2

Both are same and same Answer as an output change only on written format as your wish you use.

Answer is : 25

In this example, the lambda function takes one input x and returns its square. The function is assigned to a variable square so it can be reused.

Find sum by using Lambda Function (Pass Multi-Arguments)

sum = lambda a, b, c : a + b + c
print(sum(2,3,4))
                                                             OR
print(lambda a, b, c : a + b + c)

Both are same and same Answer as an output change only on written format as your wish you use.

Answer is : 9

In this example, the lambda function takes three arguments a, b, and c, and returns the sum of their values. When you call the function with sum(2, 3, 4), it returns 9.

Video for better Understanding 👇👇👇

LIKE | SHARE | SUBSCRIBE ðŸ‘ˆðŸ‘ˆðŸ‘ˆ

Lambda functions have some restrictions, including: 

  1. They can only have one expression, which means they can't contain statements or annotations. 
  2. They can't include statements like return or raise. 
  3. They can't reference variables from the containing scope, except for variables that are passed in as arguments