# This is a normal function:
def Function(Parameter):
return Parameter
# And this is a lambda function
Function = lambda Parameter : Parameter
"""
They are both equivalent and do the exact same job (which is
to take in a parameter and output it, in this scenario) the reason
lambda functions exist is to make code shorter and readable since
a lambda function only takes up one line.
Lambda functions are mostly used for simple things
Whereas defining functions are used for complex things.
You do not have to use lambda functions, it's all about preference.
An example of where it would be used is in basic arithmetics, im only
going to show addition, I think you can work out the rest:
"""
Add = lambda a, b: a + b
print(Add(3,4))
# Output:
# >>> 7
# Its equivalent:
def Add(a ,b):
return a + b
print(Add(3,4))
# Output:
# >>> 7
Lamda is just one line anonymous function
Useful when writing function inside function
it can take multiple arguments but computes only one expression
Syntax:
x = lambda arguments : expression
Lamda is just one line anonymous function
Useful when writing function inside function
it can take multiple arguments but computes only one expression
Syntax:
x = lambda arguments : expression
# Normal function, usually called function with name
def add(a,b):
return a + b
print(add(1,2)) # 3
# Lambda function, usually called anonymous function (without name)
add2 = lambda a,b: a+b
print(add2(1,2)) # 3
'''
Note:
If you want to use lambda with return value make a variable before : then
lambda's expression
= lambda x, y: x * y
If you want to use lambda with no return, you don't need to write
variable
= lambda: print("My name Lux")
The way lambda program work is same as a function but without name.
'''
# Lambda is what is known as an anonymous function. Basically it's like a function but a variable
# This is the "python" way of writing a lambda function
multiply = lambda x,y: x * y
# The x,y are the arguments used for the function and "x * y" is the expression
multiply(10, 10) #100
# Lambda are useful sometimes for a number of python projects.