SlideShare a Scribd company logo
1 of 6
Python Lambda
Python Lambda
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but can only
have one expression.
• Python Lambda Functions are anonymous function means that the
function is without a name. As we already know that the def keyword
is used to define a normal function in Python. Similarly,
the lambda keyword is used to define an anonymous function
in Python.
Syntax:
• Syntax: lambda arguments : expression
• The expression is executed and the result is returned:
• 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.
Example:
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5)) Output: 15
Lambda functions can take any number of arguments:
Example
Multiply argument a with argument b and return the result:
x = lambda a, b : a * b
print(x(5, 6)) output: 30
x = lambda a, b, c : a + b + c
print(x(5, 6, 2)) o/p:13
Why Use Lambda Functions?
• 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 lam
• Use that function definition to make a function that always doubles the number
you send in:bda a : a * n
• def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11)) o/p:22
• # Define the lambda functions
• double = lambda x: x * 2
• add = lambda x, y: x + y
• # Take input from the user
• num = int(input("Enter a number: "))
• num1 = int(input("Enter first number: "))
• num2 = int(input("Enter second number: "))
• # Apply the lambda functions
• result1 = double(num)
• result2 = add(num1, num2)
• # Display the results
• print("Double of", num, "is", result1)
• print("Sum of", num1, "and", num2, "is", result2)

More Related Content

Similar to Python lambda.pptx

Similar to Python lambda.pptx (20)

Functional programming
Functional programmingFunctional programming
Functional programming
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Python functions
Python functionsPython functions
Python functions
 
Java 8
Java 8Java 8
Java 8
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
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...
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
ch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdfch-3 funtions - 1 class 12.pdf
ch-3 funtions - 1 class 12.pdf
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Scala qq
Scala qqScala qq
Scala qq
 
c.p function
c.p functionc.p function
c.p function
 
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
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 

More from prakashvs7 (15)

Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptx
 
unit 5_Real time Data Analysis vsp.pptx
unit 5_Real time Data Analysis  vsp.pptxunit 5_Real time Data Analysis  vsp.pptx
unit 5_Real time Data Analysis vsp.pptx
 
unit 4-1.pptx
unit 4-1.pptxunit 4-1.pptx
unit 4-1.pptx
 
unit 3.ppt
unit 3.pptunit 3.ppt
unit 3.ppt
 
final Unit 1-1.pdf
final Unit 1-1.pdffinal Unit 1-1.pdf
final Unit 1-1.pdf
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
AI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptxAI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptx
 
AI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxAI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptx
 
AI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxAI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
 
Php unit i
Php unit i Php unit i
Php unit i
 
The process
The processThe process
The process
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 

Python lambda.pptx

  • 2. Python Lambda • A lambda function is a small anonymous function. • A lambda function can take any number of arguments, but can only have one expression. • Python Lambda Functions are anonymous function means that the function is without a name. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python.
  • 3. Syntax: • Syntax: lambda arguments : expression • The expression is executed and the result is returned: • 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.
  • 4. Example: Add 10 to argument a, and return the result: x = lambda a : a + 10 print(x(5)) Output: 15 Lambda functions can take any number of arguments: Example Multiply argument a with argument b and return the result: x = lambda a, b : a * b print(x(5, 6)) output: 30 x = lambda a, b, c : a + b + c print(x(5, 6, 2)) o/p:13
  • 5. Why Use Lambda Functions? • 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 lam • Use that function definition to make a function that always doubles the number you send in:bda a : a * n • def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11)) o/p:22
  • 6. • # Define the lambda functions • double = lambda x: x * 2 • add = lambda x, y: x + y • # Take input from the user • num = int(input("Enter a number: ")) • num1 = int(input("Enter first number: ")) • num2 = int(input("Enter second number: ")) • # Apply the lambda functions • result1 = double(num) • result2 = add(num1, num2) • # Display the results • print("Double of", num, "is", result1) • print("Sum of", num1, "and", num2, "is", result2)