0% found this document useful (0 votes)
4 views

Python Lecture 16-High Order Function

The document discusses high-order functions in Python, focusing on lambda functions, which are anonymous and consist of a single expression. It explains higher-order functions, such as map, reduce, and filter, which take other functions as parameters, and provides a brief overview of how the map and reduce functions operate. Additionally, it highlights that lambda functions can be used as parameters in these higher-order functions.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Lecture 16-High Order Function

The document discusses high-order functions in Python, focusing on lambda functions, which are anonymous and consist of a single expression. It explains higher-order functions, such as map, reduce, and filter, which take other functions as parameters, and provides a brief overview of how the map and reduce functions operate. Additionally, it highlights that lambda functions can be used as parameters in these higher-order functions.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Python: High Order Functions

Lamda Function
• Function without name (anonymous)
• Lambda functions can only be comprised of
a single expression
– No loops, no calling other methods
– Lambda functions can take any number of
variables
• Syntax:
lambda param1,…,paramn : expression
Exmples

False

56
Higher Order Function
• A higher-order function is a function that takes
another function as a parameter
• They are “higher-order” because it’s a function of a
function
• Examples
– Map
– Reduce
– Filter
• Lambda can work as a parameter to higher-order
functions
Higher Order Function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
Higher Order Function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
Higher Order Function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
map function

map(function, iterable, ...)

• Map applies function to each element of


iterable (sequence/list) and creates a list of the
results
map function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
map function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
map function

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
map function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
map function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
reduce function
Reduce takes a binary function and a list, and
returns a single value, which is obtained by
repeatedly applying the binary function to pairs
of elements in the list.

So, if the list contains elements x1,x2,…xn and


the function is f then the result will be
f(...f(f(x1,x2),x3),...xn)
reduce function

source: https://courses.csail.mit.edu/6.01/spring08/handouts/week2/lecture-handout-week-2.pdf
reduce function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
reduce function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
reduce function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
filter function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf
filter function

source: https://courses.cs.washington.edu/courses/cse143/10wi/python/3/python143_lecture2.pdf

You might also like