SC-CER96940: "Formando Líderes para La Construcción de Un Nuevo País en Paz"
SC-CER96940: "Formando Líderes para La Construcción de Un Nuevo País en Paz"
First-class functions
Python functions are said to be first-class functions, which means they can be assigned to
variables, copied, used as arguments to other functions, etc., just like any other object.
def f(x):
return x*x
g=f
print('f(3) =', f(3), 'g(3) =', g(3), sep = '\n')
f(3) = 9
g(3) = 9
def f(x):
return x**2
def g(x):
return x**3
funcs = [f, g]
print(funcs[0](5), funcs[1](5), sep='\n')
25
125
Here is another example. Say you have a program with ten different functions and the
program has to decide at runtime which function to use. One solution is to use ten if
statements. A shorter solution is to use a list of functions. The example below assumes that
we have already created functions f1, f2, . . . , f10, that each take two arguments.
Functions as arguments to functions. Say we have a list of 2-tuples. If we sort the list,
the sorting is done based off of the first entry as below:
Suppose we want the sorting to be done based off the second entry. The sort method takes
an optional argument called key, which is a function that specifies how the sorting should be
done. Here is how to sort based off the second entry:
def comp(x):
return x[1]
L = [(5,4), (3,2), (1,7), (8,1)]
L.sort(key=comp)
Here is another example, where we sort a list of strings by length, rather than alphabetically.
Recursion
Recursion is the process where a function calls itself. One of the standard examples of
recursion is the factorial function. The factorial, n!, is the product of all the numbers from 1
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
We must specify the n = 0 case or else the function would keep calling itself forever (or at
least until Python generates an error about too many levels of recursion).
Note that the math module has a function called factorial, so this version here is just for
demon- stration. Note also that there is a non-recursive way to do the factorial, using a for
loop. It is about as straightforward as the recursive way, but faster. However, for some
problems the recursive so- lution is the more straightforward solution. Here, for example, is
a program that factors a number into prime factors.
The factor function takes two arguments: a number to factor, and a list of previously found
factors. It checks for a factor, and if it finds one, it appends it to the list. The recursive part
is that it divides the number by the factor that was found and then appends to the list all the
factors of that value. On the other hand, if the function doesn’t find any factors, it appends
the number to the list, as it must be a prime, and returns the new list.
map and filter Python has a built-in functions called map and filter that are used to apply
functions to the contents of a list. They date back to before list comprehensions were a part
The map function takes two arguments—a function and an iterable—and it applies the
function to each element of the iterable, generating a new iterable. Here is an example that
takes a list of strings a returns a list of the lengths of the strings. The first line accomplishes
this with map, while the second line uses list comprehensions:
The function filter takes a function and an iterable and returns an iterable of all the elements
of the list for which the function is true. Here is an example that returns all the words in a list
that have length greater than 2. The first line uses filter to do this, and the second line does
it with a list comprehension:
Here is one approach to finding the number of items in a list L that are greater than 60:
count=0
for i in L:
if i>60:
count = count + 1
Here is a second way using a list comprehension similar to the filter function:
reduce. There is another function, reduce, that applies a function to the contents of a list. It
used to be a built-in function. This function cannot be easily replaced with list
comprehensions. To understand it, first consider a simple example that adds up the numbers
from 1 to 100.
In general, reduce takes a function and an iterable, and applies the function to the elements
from left to right, accumulating the result. As another simple example, the factorial function
could be implemented using reduce:
def fact(n):
return reduce(lambda x,y:x*y, range(1,n+1))