CIS 325: Programming for Business Analytics
Dept. of Information Systems
Functions: Lambda, Currying, Iterators and Generators
2• The cornerstone of Python is that everything is an object – Functions are objects.
• Since objects have a common foundation (classes, attributes, methods), developers
can build flexible scripts that leverage (almost) any other Python object with flexible
Functions are Objects
• A way of writing (throwaway) functions consisting of a single line statement.
• Also called anonymous functions, as they don’t have a name associated with them.
• Useful in allowing quick calculations or processing input to other functions.
# function code script
lambda arguments: expression
4Lambda Function Examples
Can assign to a variable
• Multiple return values:
• Default argument value:• Multiple arguments:
• The map(F, L) function applies function F on every item in L and returns a list.
• The filter(F, L) function creates a list where the items in L are filtered through a
function F to test if the item is accepted or not.
Lambda Function Examples
6• In mathematics and computer science, currying is the technique
of translating the evaluation of a function that takes multiple
arguments into evaluating a sequence of functions, each with a
• Easily get specialized functions from more general functions.
• Simplifying code.
Currying: Partial Argument
7Currying Components in Python
8• Iterators are objects that represent a stream of data.
• Iterator objects return other objects upon request.
• When an iterator returns objects, traditionally this takes place in the context of a
script call, such as container structure (list, set, tuple, etc) or a loop call. Within this
context, the iterator object returns other objects in their stream of data.
When you write:
This is what actually happens:
Each time you ask for "the next" value, it knows how
to compute it because it holds an internal state.
• In Python, generators provide a convenient way to implement the iterator protocol.
• Essentially, generators succinctly build iterator objects that return a sequence
of objects to their callers.
• The important point here is that the return of the sequence of objects, happens
not at all at once, but one at a time.
• This can be useful when working with huge datasets.
• Generator is built by calling a function with one or more yield statement.
• Iterator is a more general concept: every generator is an iterator, but not vice versa.
• When you hit the yield statement, the function is suspended and returns the
yielded value to the caller.
• In contrast, the return statement stops function execution completely.
• When a function is suspended, the state of that function is saved.
• This allows you to resume function execution right after yield.
• itertools is a standard Python library that has functions for creating iterators
used for efficient looping through objects and container structures.
• itertools helps in creating fast, memory efficient and more understandable code,
making it ideal for processing large streams of data.
• Create a function that has one positional argument.
• Function name: myfunc
• Argument name: x
• The function computes and returns the following: (x2 + x3) × x4
• QA Checks
• If you pass argument 1, function returns 2.
• If you pass argument 2, function returns 192.
• If you pass argument 3, function returns 2916.
• If you pass argument 4, function returns 20480.
• If you pass argument 5, function returns 93750.
In Class Exercise 1
• Accomplish the same outcome as Exercise 1 (previous slide) but using a lambda function.
• Assign the lambda function to the name: myfunc2
• Use a single for loop statement with a range that iterates from 1 to 5 (including the end points).
• At each iteration, print the outcome of the lambda function myfunc2 with an argument equal to
the number generated by your range statement.
• QA Checks: running your solution will output the following to the Python console
In Class Exercise 2
• Given a Python list L = [1, 2, 3, 4, 5, 6], write code that creates a new list named S whose
elements are all numbers in L raised to the power of 2 (squared).
1. Accomplish this using a for loop and the list append() method.
2. Accomplish this using Python list comprehension.
3. Accomplish this using the map() function with a lambda function.
• In all cases, the Python list S should contain[1, 4, 9, 16, 25, 36].
• Next use the filter() function with a lambda function to filter out odd numbers and retain only
even numbers in S from the previous step. Assign the result to a variable named F.
• What is the value of F ?
• What is the type of F?
• When converted to a Python list, F should contain[4, 16, 36].
In Class Exercise 3
• Create a generator function that has one positional argument.
• Function name: mysequence
• Argument name: x
• The function generates a sequence of numbers (using a for loop) from zero to (and including) x in
increments of 1.
• QA Checks
• If you pass argument 4 the generator function will cerate, upon request, the numbers: 0, 1, 2, 3, 4.
• Upon request means, for example, when using a for loop, each number is generated upon demand by the
for loop, and not all numbers at once.
• If you assign g = mysequence(4), and then
• Print out the type of g, you should get:
• Print out the value of g, you should get:
• If you convert g to a Python list, you should get: [0, 1, 2, 3, 4]
In Class Exercise 4