Python Lambda Function
Python Lambda Function
Python Lambda function is a single line function whose body is just an expression and can accept multiple arguments.
In this tutorial, you shall first learn the basics of lambda function and secondly some interesting hacks that we can do with these lambda functions.
Syntax of Lambda Function
The syntax of a Lambda Function is as shown in the following.
lambda arguments : expression
You can assign this lambda function to a variable. Something like in below code snippet.
x = lambda arguments : expression
And you can call the lambda function with the variable name x. Following is an example code snippet to call the lambda function.
result = x(arguments)
The arguments are passed just like a regular function's arguments, but the function name being the variable we assigned this lambda function to.
When you call a lambda function, the expression is evaluated and the evaluated value is returned. So, you have the arguments, body and return statement, everything in just a line. So very concise.
Examples
1. Find square of a number using Lambda function
Following is a simple lambda function, that takes a number and returns the square of it.
Python Program
#lambda function
square = lambda a: a*a
#call lambda function
result = square(6)
print(result)
Output
36
2. How to pass multiple arguments to lambda function
We already mentioned that a Python Lambda function can accept multiple arguments. In the following example, we define a lambda function with two arguments, say a and b, and we shall multiplication of a and b.
Python Program
#lambda function
mul = lambda a,b: a*b
#call lambda function
result = mul(5,3)
print(result)
Output
15
3. Lambda function with no arguments
Yeah! You can define a lambda function with no arguments at all. If there were to be requirement that you create a lambda function with no arguments, you can.
In the following example, we define a lambda function with no arguments, and returns 6
when called.
Python Program
#lambda function
six = lambda : 6
#call lambda function
result = six()
print(result)
Output
6
4. Recursive lambda function
You can design a recursion function using lambda function. According to the syntax of Python Lambda function, all that you need is an expression that evaluates to a single value to return. So, if you can build a recursive function satisfying this criteria, you have a recursive lambda function.
In the following example, we defined a lambda function that can calculate the factorial of a given number, using recursion.
Python Program
#recursive lambda function
factorial = lambda a: a*factorial(a-1) if (a>1) else 1
#call lambda function
result = factorial(5)
print(result)
Output
120
5. Return lambda function
Lambda Function is just like a variable. So, you can return a lambda function from another function.
Python Program
import math
#function returning lambda function
def myfunc(n):
return lambda a : math.pow(a, n)
#lambda functions
square = myfunc(2) # square = lambda a : math.pow(a, 2)
cube = myfunc(3) # cube = lambda a : math.pow(a, 3)
squareroot = myfunc(0.5) # squareroot = lambda a : math.pow(a, 0.5)
print(square(3))
print(cube(3))
print(squareroot(3))
Output
9.0
27.0
1.7320508075688772
Tutorials on Lambda Function
The following tutorials cover some use cases with lambda function, to understand them better in different scenarios.
- Python - Lambda function with two arguments
- Python - Sum of two numbers using Lambda Function
- Python - Lambda Function with if else
- Python - Recursive lambda function
- Python - Lambda Function without arguments
- Python - Lambda function with list comprehension
- Python - Lambda Function with filter()
- Python - Lambda Function that returns multiple values
- Python - Lambda Function that returns None
- Python - Lambda Function that returns True or False
Summary
Concluding this tutorial of Python Examples, we learned how to define a lambda function, and use it in your program constructively, with the help of example programs.