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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers