SlideShare una empresa de Scribd logo
1 de 12
Introduction to Advanced
Python Programming
Jagdish D. Chavan
Data Scientist| Emerging Industry Trainer|
Certified Python, Data Science Trainer| AI,ML,DL Expert
Github | Linkedin
Functional Programming
● Functional programming decomposes a problem into a set of functions.
● Ideally, Functions only take inputs and produce output, and don’t have any internal state that affects
the output produced for a given input.
● Functional techniques common to many languages : such as lambda, map, reduce.
*Note:- for more code & projects follow me on Linkedin | Github
Anonymous Functions or Lambdas
● Anonymous functions are the functions without name.
● Anonymous functions are not defined using ‘def’ keyword.
● They are defined using the keyword lambda hence also called as lambda functions.
*Note:- for more code & projects follow me on Linkedin | Github
Syntax for Lambda function.
Syntax for normal function
def function_name(parameters):
function_block
return expression
Example:- Square of Given value
code
def square(x): #declaring function
return x*x
square(9) #calling a function
Output
81
*Note:- for more code & projects follow me on Linkedin | Github
Syntax for Lambda Function
Lambda argument_list : expression
Example:- Square of Given value
Code
y = lambda x: x*x #declaring
value = y(9) #calling a function
print(value)
Output
81
Syntax for Lambda function.
*Note:- for more code & projects follow me on Linkedin | Github
Syntax :- Lambda <argument_list> : <expression>
f = lambda x: x*x
● Observe the keyword ‘lambda’. This represents that an anonymous function is being created.
● ‘X’ is argument of function followed by colon(:) representing beginning of function containing expression x*x.
● Normally, if a function returns some value, we assign that value to a variable as:
y = square(9)
● but , lambda function returns a function and hence they should to assign to function as:
f = lambda x:x*x
● Here ‘f’ is the function name to which lambda expression is assigned. Now if we call the functions f() as: value =f(5)
Properties of Lambda function.
*Note:- for more code & projects follow me on Linkedin | Github
● We can pass n number of arguments in lambda function, but only one expression is allowed.
● They return the result implicitly, hence we should not write any ‘return’ statements in the
lambda functions.
● Because a lambda functions is always represented by a function, we can pass a lambda function
to another function .
● It means we are passing a function (i.e lambda ) to another function, making processing data
very easy lambda function are generally used with functions like filter(), map(), or reduce().
Exercise no 1.
*Note:- for more code & projects follow me on Linkedin | Github
1) Write a Python Program to create a lambda function that returns a square value of given
number.
2) Write a Python Program to calculate the sum of two numbers using lambda function
3) Write a Python Program to find bigger number in two given number using Lambda Function .
Note:- for more examples and answers please do visit Advance Python Programming
Using Lambda with Filter() Function.
*Note:- for more code & projects follow me on Linkedin | Github
● The filter() function is useful to filter out the elements of a sequence depending on the result of
a function.
● We should supply a function and a sequence to the filter() function as
Syntax :-filter(function, sequence)
● function:- represent the function name that may return either True or False.
● Sequence:-represents a list, string or tuple.
● The ‘function’ is applied to every element of the ‘sequence’ and when function returns True, the
element is extracted otherwise it is ignored.
Using Lambda with Filter() Function.
*Note:- for more code & projects follow me on Linkedin | Github
Example
#write a python Program using lambda function that returns even number from the list.
lst = [10,23,45,46,70,99]
lst1 = list(filter(lambda x:(x%2==0),lst))
print(lst1)
Output
[10, 46, 70]
Using Lambda with map() Function.
*Note:- for more code & projects follow me on Linkedin | Github
● map () function acts on each element of the sequence and perhaps changes the element
● The syntax of map() function is as follow
Syntax:- map(function, sequence)
● The function performs specific operations on all the elements of the sequence and modified
element are returned which can be stored in another sequence.
Example
#write a python program using lambda function that returns squares of elements in a list.
lst = [1,2,3,4,5]
lst1 = list(map(lambda x: x*x, lst))
print(lst1)
Output
[1, 4, 9, 16, 25]
Using Lambda with map() Function.
*Note:- for more code & projects follow me on Linkedin | Github
Exercise
1) Write a Python program to find the products of elements of two different lists using lambda
function.
Note:- for more examples and answers please do visit Advance Python Programming
Using Lambda with reduce() Function.
*Note:- for more code & projects follow me on Linkedin | Github
● reduce () function reduces a sequence of elements to a single value by processing elemets
according to a function supplied.
● Syntax :- reduce(function, sequence)
Example
#write a Python program using lambda function to calculate product of elements of list
from functools import *
lst = [1,2,3,4,5]
result = reduce(lambda x,y: x*y,lst)
print(result)
Output
120

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
C function
C functionC function
C function
 
Functions
FunctionsFunctions
Functions
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Function in c
Function in cFunction in c
Function in c
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Function C++
Function C++ Function C++
Function C++
 
Function in c
Function in cFunction in c
Function in c
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
F# 101
F# 101F# 101
F# 101
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 

Similar a Advance python programming

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptxprakashvs7
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingSvetlin Nakov
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...Anil Sharma
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdfalaparthi
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 

Similar a Advance python programming (20)

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptx
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Scala qq
Scala qqScala qq
Scala qq
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Java 8
Java 8Java 8
Java 8
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Ninth session
Ninth sessionNinth session
Ninth session
 
Python functions
Python functionsPython functions
Python functions
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Lecture1
Lecture1Lecture1
Lecture1
 

Último

➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...amitlee9823
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachBoston Institute of Analytics
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...only4webmaster01
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...amitlee9823
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...amitlee9823
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsJoseMangaJr1
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
 

Último (20)

➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning Approach
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 

Advance python programming

  • 1. Introduction to Advanced Python Programming Jagdish D. Chavan Data Scientist| Emerging Industry Trainer| Certified Python, Data Science Trainer| AI,ML,DL Expert Github | Linkedin
  • 2. Functional Programming ● Functional programming decomposes a problem into a set of functions. ● Ideally, Functions only take inputs and produce output, and don’t have any internal state that affects the output produced for a given input. ● Functional techniques common to many languages : such as lambda, map, reduce. *Note:- for more code & projects follow me on Linkedin | Github
  • 3. Anonymous Functions or Lambdas ● Anonymous functions are the functions without name. ● Anonymous functions are not defined using ‘def’ keyword. ● They are defined using the keyword lambda hence also called as lambda functions. *Note:- for more code & projects follow me on Linkedin | Github
  • 4. Syntax for Lambda function. Syntax for normal function def function_name(parameters): function_block return expression Example:- Square of Given value code def square(x): #declaring function return x*x square(9) #calling a function Output 81 *Note:- for more code & projects follow me on Linkedin | Github Syntax for Lambda Function Lambda argument_list : expression Example:- Square of Given value Code y = lambda x: x*x #declaring value = y(9) #calling a function print(value) Output 81
  • 5. Syntax for Lambda function. *Note:- for more code & projects follow me on Linkedin | Github Syntax :- Lambda <argument_list> : <expression> f = lambda x: x*x ● Observe the keyword ‘lambda’. This represents that an anonymous function is being created. ● ‘X’ is argument of function followed by colon(:) representing beginning of function containing expression x*x. ● Normally, if a function returns some value, we assign that value to a variable as: y = square(9) ● but , lambda function returns a function and hence they should to assign to function as: f = lambda x:x*x ● Here ‘f’ is the function name to which lambda expression is assigned. Now if we call the functions f() as: value =f(5)
  • 6. Properties of Lambda function. *Note:- for more code & projects follow me on Linkedin | Github ● We can pass n number of arguments in lambda function, but only one expression is allowed. ● They return the result implicitly, hence we should not write any ‘return’ statements in the lambda functions. ● Because a lambda functions is always represented by a function, we can pass a lambda function to another function . ● It means we are passing a function (i.e lambda ) to another function, making processing data very easy lambda function are generally used with functions like filter(), map(), or reduce().
  • 7. Exercise no 1. *Note:- for more code & projects follow me on Linkedin | Github 1) Write a Python Program to create a lambda function that returns a square value of given number. 2) Write a Python Program to calculate the sum of two numbers using lambda function 3) Write a Python Program to find bigger number in two given number using Lambda Function . Note:- for more examples and answers please do visit Advance Python Programming
  • 8. Using Lambda with Filter() Function. *Note:- for more code & projects follow me on Linkedin | Github ● The filter() function is useful to filter out the elements of a sequence depending on the result of a function. ● We should supply a function and a sequence to the filter() function as Syntax :-filter(function, sequence) ● function:- represent the function name that may return either True or False. ● Sequence:-represents a list, string or tuple. ● The ‘function’ is applied to every element of the ‘sequence’ and when function returns True, the element is extracted otherwise it is ignored.
  • 9. Using Lambda with Filter() Function. *Note:- for more code & projects follow me on Linkedin | Github Example #write a python Program using lambda function that returns even number from the list. lst = [10,23,45,46,70,99] lst1 = list(filter(lambda x:(x%2==0),lst)) print(lst1) Output [10, 46, 70]
  • 10. Using Lambda with map() Function. *Note:- for more code & projects follow me on Linkedin | Github ● map () function acts on each element of the sequence and perhaps changes the element ● The syntax of map() function is as follow Syntax:- map(function, sequence) ● The function performs specific operations on all the elements of the sequence and modified element are returned which can be stored in another sequence. Example #write a python program using lambda function that returns squares of elements in a list. lst = [1,2,3,4,5] lst1 = list(map(lambda x: x*x, lst)) print(lst1) Output [1, 4, 9, 16, 25]
  • 11. Using Lambda with map() Function. *Note:- for more code & projects follow me on Linkedin | Github Exercise 1) Write a Python program to find the products of elements of two different lists using lambda function. Note:- for more examples and answers please do visit Advance Python Programming
  • 12. Using Lambda with reduce() Function. *Note:- for more code & projects follow me on Linkedin | Github ● reduce () function reduces a sequence of elements to a single value by processing elemets according to a function supplied. ● Syntax :- reduce(function, sequence) Example #write a Python program using lambda function to calculate product of elements of list from functools import * lst = [1,2,3,4,5] result = reduce(lambda x,y: x*y,lst) print(result) Output 120