SlideShare a Scribd company logo
Artificial Intelligent
Python Lambda Function
Python lambda (Anonymous Functions)
In Python, anonymous function means that a function is without a
name. As we already know that def keyword is used to define the
normal functions and the lambda keyword is used to create
anonymous functions. It has the following syntax:
lambda arguments: expression
• This function can have any number of arguments but only one
expression, which is evaluated and returned.
• One is free to use lambda functions wherever function objects
are required.
• You need to keep in your knowledge that lambda functions are
syntactically restricted to a single expression.
• It has various uses in particular fields of programming besides
other types of expressions in functions.
Python lambda (Anonymous Functions)
The following terms may be used interchangeably depending on the
programming language type and culture:
• Anonymous functions
• Lambda functions
• Lambda expressions
• Lambda abstractions
• Lambda form
• Function literals
Example of Lambda Function in python
Here is an example of lambda function that doubles the input
value.
double = lambda x: x * 2#
Output: 10
print(double(5))
In the above program, lambda x: x * 2 is the lambda function.
Here x is the argument and x * 2 is the expression that gets
evaluated and returned.
We can now call it as a normal function.
double = lambda x: x * 2
is nearly the same as
def double(x):
return x * 2
Use of Lambda Function in python
The power of lambda is better shown when you use them as an
anonymous 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 function definition to make a function that always
doubles the number you send in:
Example
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
Use of Lambda Function in python
Or, use the same function definition to make a function that
always triples the number you send in:
Example
def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))
Use of Lambda Function in python
Or, use the same function definition to make both functions, in
the same program:
Example
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Use lambda functions when an anonymous function is
required for a short period of time.
Example use with filter()
The filter() function in Python takes in a function and a list as
arguments.
The function is called with all the items in the list and a new list is
returned which contains items for which the function evaluats to
True.
Here is an example use of filter() function to filter out only even
numbers from a list.
# Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
# Output: [4, 6, 8, 12]
print(new_list)
Example use with map()
The map() function in Python takes in a function and a list.
The function is called with all the items in the list and a new list is
returned which contains items returned by that function for each
item.
Here is an example use of map() function to double all the items in a
list.
# Program to double each item in a list using map()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
# Output: [2, 10, 8, 12, 16, 22, 6, 24]
print(new_list)
Ad

More Related Content

Similar to lambda engineering students machine learnings.pptx (20)

Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
anaveenkumar4
 
Lambda calculus
Lambda calculusLambda calculus
Lambda calculus
Diego Mendonça
 
2-functions.pptx_20240619_085610_0000.pdf
2-functions.pptx_20240619_085610_0000.pdf2-functions.pptx_20240619_085610_0000.pdf
2-functions.pptx_20240619_085610_0000.pdf
amiyaratan18
 
function ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptxfunction ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
function in python programming languges .pptx
function in python programming languges .pptxfunction in python programming languges .pptx
function in python programming languges .pptx
rundalomary12
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
JNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptxJNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
Philip Schwarz
 
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jggPYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
DeepakRattan3
 
Ninth session
Ninth sessionNinth session
Ninth session
AliMohammad155
 
UNIT – 3.pptx for first year engineering
UNIT – 3.pptx for first year engineeringUNIT – 3.pptx for first year engineering
UNIT – 3.pptx for first year engineering
SabarigiriVason
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
georgejustymirobi1
 
python slides introduction interrupt.ppt
python slides introduction interrupt.pptpython slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
Python Objects
Python ObjectsPython Objects
Python Objects
MuhammadBakri13
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
Maneesh Chaturvedi
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
anaveenkumar4
 
2-functions.pptx_20240619_085610_0000.pdf
2-functions.pptx_20240619_085610_0000.pdf2-functions.pptx_20240619_085610_0000.pdf
2-functions.pptx_20240619_085610_0000.pdf
amiyaratan18
 
function ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptxfunction ppt.pptx function ppt.pptx function ppt.pptx
function ppt.pptx function ppt.pptx function ppt.pptx
trwdcn
 
function in python programming languges .pptx
function in python programming languges .pptxfunction in python programming languges .pptx
function in python programming languges .pptx
rundalomary12
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
Philip Schwarz
 
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jggPYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
PYTHON-PROGRAMMING-UNIT-II.pptx gijtgjjgg jufgiju yrguhft hfgjutt jgg
DeepakRattan3
 
UNIT – 3.pptx for first year engineering
UNIT – 3.pptx for first year engineeringUNIT – 3.pptx for first year engineering
UNIT – 3.pptx for first year engineering
SabarigiriVason
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
georgejustymirobi1
 
python slides introduction interrupt.ppt
python slides introduction interrupt.pptpython slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 

More from mrsam3062 (12)

NLP Introduction for engineering stuedents.pptx
NLP Introduction for engineering stuedents.pptxNLP Introduction for engineering stuedents.pptx
NLP Introduction for engineering stuedents.pptx
mrsam3062
 
Day5 String python language for btech.pptx
Day5 String python language for btech.pptxDay5 String python language for btech.pptx
Day5 String python language for btech.pptx
mrsam3062
 
DEEP_LEARNING_Lecture1 for btech students.pptx
DEEP_LEARNING_Lecture1 for btech students.pptxDEEP_LEARNING_Lecture1 for btech students.pptx
DEEP_LEARNING_Lecture1 for btech students.pptx
mrsam3062
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
intoruction to python for machine learning.pptx
intoruction to python for machine learning.pptxintoruction to python for machine learning.pptx
intoruction to python for machine learning.pptx
mrsam3062
 
mathematics for machine learning matrices.pptx
mathematics for machine learning matrices.pptxmathematics for machine learning matrices.pptx
mathematics for machine learning matrices.pptx
mrsam3062
 
day 4-5 python for data science .pptx
day 4-5 python for data science    .pptxday 4-5 python for data science    .pptx
day 4-5 python for data science .pptx
mrsam3062
 
day 1-3 of python for data science .pptx
day 1-3 of python for data science .pptxday 1-3 of python for data science .pptx
day 1-3 of python for data science .pptx
mrsam3062
 
data mining lecture notes for btech students+
data mining lecture notes for btech students+data mining lecture notes for btech students+
data mining lecture notes for btech students+
mrsam3062
 
Python Day 3 .pptx
Python Day 3                                        .pptxPython Day 3                                        .pptx
Python Day 3 .pptx
mrsam3062
 
Machine_Learning_40_Day_Course .pptx
Machine_Learning_40_Day_Course       .pptxMachine_Learning_40_Day_Course       .pptx
Machine_Learning_40_Day_Course .pptx
mrsam3062
 
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
ALLIED MATHEMATICS -I UNIT III MATRICES.pptALLIED MATHEMATICS -I UNIT III MATRICES.ppt
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
mrsam3062
 
NLP Introduction for engineering stuedents.pptx
NLP Introduction for engineering stuedents.pptxNLP Introduction for engineering stuedents.pptx
NLP Introduction for engineering stuedents.pptx
mrsam3062
 
Day5 String python language for btech.pptx
Day5 String python language for btech.pptxDay5 String python language for btech.pptx
Day5 String python language for btech.pptx
mrsam3062
 
DEEP_LEARNING_Lecture1 for btech students.pptx
DEEP_LEARNING_Lecture1 for btech students.pptxDEEP_LEARNING_Lecture1 for btech students.pptx
DEEP_LEARNING_Lecture1 for btech students.pptx
mrsam3062
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
intoruction to python for machine learning.pptx
intoruction to python for machine learning.pptxintoruction to python for machine learning.pptx
intoruction to python for machine learning.pptx
mrsam3062
 
mathematics for machine learning matrices.pptx
mathematics for machine learning matrices.pptxmathematics for machine learning matrices.pptx
mathematics for machine learning matrices.pptx
mrsam3062
 
day 4-5 python for data science .pptx
day 4-5 python for data science    .pptxday 4-5 python for data science    .pptx
day 4-5 python for data science .pptx
mrsam3062
 
day 1-3 of python for data science .pptx
day 1-3 of python for data science .pptxday 1-3 of python for data science .pptx
day 1-3 of python for data science .pptx
mrsam3062
 
data mining lecture notes for btech students+
data mining lecture notes for btech students+data mining lecture notes for btech students+
data mining lecture notes for btech students+
mrsam3062
 
Python Day 3 .pptx
Python Day 3                                        .pptxPython Day 3                                        .pptx
Python Day 3 .pptx
mrsam3062
 
Machine_Learning_40_Day_Course .pptx
Machine_Learning_40_Day_Course       .pptxMachine_Learning_40_Day_Course       .pptx
Machine_Learning_40_Day_Course .pptx
mrsam3062
 
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
ALLIED MATHEMATICS -I UNIT III MATRICES.pptALLIED MATHEMATICS -I UNIT III MATRICES.ppt
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
mrsam3062
 
Ad

Recently uploaded (20)

Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Ad

lambda engineering students machine learnings.pptx

  • 2. Python lambda (Anonymous Functions) In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. It has the following syntax: lambda arguments: expression • This function can have any number of arguments but only one expression, which is evaluated and returned. • One is free to use lambda functions wherever function objects are required. • You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression. • It has various uses in particular fields of programming besides other types of expressions in functions.
  • 3. Python lambda (Anonymous Functions) The following terms may be used interchangeably depending on the programming language type and culture: • Anonymous functions • Lambda functions • Lambda expressions • Lambda abstractions • Lambda form • Function literals
  • 4. Example of Lambda Function in python Here is an example of lambda function that doubles the input value. double = lambda x: x * 2# Output: 10 print(double(5)) In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned. We can now call it as a normal function. double = lambda x: x * 2 is nearly the same as def double(x): return x * 2
  • 5. Use of Lambda Function in python The power of lambda is better shown when you use them as an anonymous 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 function definition to make a function that always doubles the number you send in: Example def myfunc(n): return lambda a : a * n mydoubler = myfunc(2)
  • 6. Use of Lambda Function in python Or, use the same function definition to make a function that always triples the number you send in: Example def myfunc(n): return lambda a : a * n mytripler = myfunc(3) print(mytripler(11))
  • 7. Use of Lambda Function in python Or, use the same function definition to make both functions, in the same program: Example def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) mytripler = myfunc(3) print(mydoubler(11)) print(mytripler(11)) Use lambda functions when an anonymous function is required for a short period of time.
  • 8. Example use with filter() The filter() function in Python takes in a function and a list as arguments. The function is called with all the items in the list and a new list is returned which contains items for which the function evaluats to True. Here is an example use of filter() function to filter out only even numbers from a list. # Program to filter out only the even items from a list my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x%2 == 0) , my_list)) # Output: [4, 6, 8, 12] print(new_list)
  • 9. Example use with map() The map() function in Python takes in a function and a list. The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item. Here is an example use of map() function to double all the items in a list. # Program to double each item in a list using map() my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(map(lambda x: x * 2 , my_list)) # Output: [2, 10, 8, 12, 16, 22, 6, 24] print(new_list)