Python Lambda Function with Two Arguments
Lambda Function with Two Arguments in Python
To define a Lambda Function with two argument in Python, specify the two parameters after lambda keyword.
For example, the following is a lambda function that takes two arguments and returns their product.
lambda x,y: x*y
Please note that the parameters are separated by comma , symbol. Also, the parameters and the function body are separated by colon : symbol.
Examples
1. Find product of two numbers using lambda function
In this example, we shall define a lambda function that takes two arguments: x
, y
; and returns their product.
Python Program
product = lambda x,y: x*y
num1 = 5
num2 = 3
result = product(num1,num2)
print(f"Product of {num1} and {num2} is {result}.")
Output
Product of 5 and 3 is 15.
2. Lambda function to check if two given numbers are even
In this example, we shall define a lambda function that takes two arguments: x
, y
; and returns true only if both the arguments are even numbers, else it returns false.
Python Program
bothEven = lambda x,y: x % 2 == 0 and y % 2 == 0
num1 = 8
num2 = 14
if bothEven(num1,num2):
print("Both the numbers are even.")
else:
print("Both the numbers are not even.")
Output
Both the numbers are even.
Summary
In this tutorial of Python Lambda Function, we learned how to define a lambda function with two arguments, with the help of example programs.