Python Lambda Syntax | lambda arguments | def myfunc(n)

Python Lambda

A lambdaĀ operateĀ could be aĀ tinyĀ anonymousĀ operation. A lambdaĀ operateĀ willĀ take anyĀ varietyĀ of arguments,Ā howeverĀ willĀ solelyĀ have one expression.

Syntax:

lambda arguments: expression

The expression isĀ deadĀ and also theĀ result’s returned:

Example:

A lambdaĀ operates that adds 15Ā toĀ the quantityĀ passed in asĀ Associate in Treating argument, and print the result:

y = lambda a: a + 15
print(y(5)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
20

Process finished with exit code 0

Lambda functions willĀ take anyĀ variety of arguments:

Example

A lambdaĀ operates that multiplies argument x with argument y and print the result:

a = lambda x, y : x * y
print(a(5, 6))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
30

Process finished with exit code 0

Example:

A lambda operates that sums argument x, y, and z and prints the result:

a = lambda x, y, z : x + y + z
print(a(5, 6, 2))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
13

Process finished with exit code 0

Why Use Lambda Functions?

The power of lambda is bestĀ shownĀ once you use them as anĀ Associate function inside another function.

Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number.

def myfunc(n):
return lambda a : a * n

Use that operateĀ definitionĀ to createĀ aĀ operateĀ thatĀ foreverĀ doublesĀ the quantity you send in:

Example1

def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)

print(mydoubler(11))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
22

Process finished with exit code 0

Or, use an identicalĀ operateĀ definitionĀ to create anĀ operationĀ thatĀ foreverĀ triplesĀ the quantity you send in.

Example:2

def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
33

Process finished with exit code 0

Or, use identicalĀ operatesĀ definitionĀ to createĀ each function,Ā within the same program:

Example3

def myfunc(n):
return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)

print(mydoubler(11))
print(mytripler(11))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
22
33

Process finished with exit code 0

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpurdham, Nepal. He was born on December 23, 1996, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India.He is director of Appan Bazaar Pvt.Ltd., as teacher in Tridev Eng. School , and an Educational Consultant, and he is currently working as a developer and Digital Marketer.



Leave a Comment