# 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
# lambda functions creates small anonymous function
# create an add functions with that takes two numbers and returns the sum
add = lambda num1, num2: num1 + num2
print(add(9, 5))
# output 14
# double the numbers in a list
print(list(map(lambda num1: num1 * 2, [1, 2, 3, 4])))
# output [2, 4, 6, 8]
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.
'''
# The lambda keyword in Python provides a
# shortcut for declaring small and
# anonymous functions:
add = lambda x, y: x + y
print(add(5, 3))
# Output
# 8
# You could declare the same add()
# function with the def keyword:
def add(x, y):
return x + y
print(add(5, 3))
# Output
# 8
# So what's the big fuss about?
# Lambdas are *function expressions*:
print((lambda x, y: x + y)(5, 3))
# Output
# 8
# • Lambda functions are single-expression
# functions that are not necessarily bound
# to a name (they can be anonymous).
# • Lambda functions can't use regular
# Python statements and always include an
# implicit `return` statement.
# 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.
# Python code to illustrate cube of a number
# showing difference between def() and lambda().
def cube(y):
return y*y*y
lambda_cube = lambda y: y*y*y
# using the normally
# defined function
print(cube(5))
# using the lambda function
print(lambda_cube(5))