DEV Community

Mahidhar Kakumani
Mahidhar Kakumani

Posted on

Lambda functions in Python:

In Python, an anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.

Hence, anonymous functions are also called lambda functions.

Syntax of Lambda Function in python:

lambda arguments: expression

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

EX:

double = lambda x: x * 2

double(4)

output: —> 8

Best Examples of Lambda Functions:

Factorial of a Number:

fact=lambda n:1 if n<=1 else n*fact(n-1)
fact(5)

n th fibonacci Number:

fib=lambda n:n if n<=1 else fib(n-1)+fib(n-2)
fib(8)

Top comments (0)