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 Janakpur Dham, Nepal. He was born on December 23, 1994, 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. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment