But what can you really do with all of these different expressions? If you combine them into functions, quite a bit.
As with algebra, a function is something that takes one or more inputs, does something with this input, and returns an output. Let’s start with an example that takes no inputs:
$$ f() = 5 $$
This function always returns $5$. Let’s see how we would implement this in Python:
def f(): return 5
We can use this function over and over:
>>> f() 5 >>> y = f() >>> y 5 >>> y = 6 >>> y 6 >>> f() 5
Notice that assigning
f() will assign it to the result of calling
f(), not the function itself. So, changing
6 doesn’t change the return value of
Before we analyze the anatomy of a function, let’s look at another one.
$$ g(x) = x + 2 $$
This function, in math, takes an input $x$ and returns $x+2$. In Python, this looks like:
def g(x): return x + 2
We can use this as follows:
>>> g(5) 7 >>> z = g(2) >>> z 4 >>> z = 5 >>> z 5 >>> g(2) 4 >>> g(3) 5
Let’s break down this function.
def g(x): # there's a function coming called `g` that takes one input `x` return x + 2 # `g` returns `x+2`
Note that a function can have more than one line, but only one return statement.
def h(x): # there's a function coming called `h` that takes one input `x` y = x + 2 # assign `y` to `x+2` return y # `h` returns `y`
This function does that exact same thing as
g, but it first assigns
x+2 to a new variable before returning that new variable.
Python comes with a number of functions that you may find useful. We’ll discover more later, but here’s a few.
>>> print('Hello!') # print the value inside the parentheses, without quotes. Hello! >>> print(print('Hello!')) # see the next section to understand how this is evaluated. Hello! # the `print` function prints the input... None # ...but note that it returns None, which is a representation of nothing. >>> min(2, 5) # return the minimum of two or more numbers, separated by commas. 2 >>> max(2, 5) # return the maximum of two or more numbers, separated by commas. 5
The way a function is evaluated is key to understanding how it works. Essentially, Python tries to evaluate every operand before evaluating the operator. In
h is the operator and
x is the operand.
We’ll use the functions
mul for this example, so you can see a PEMDAS-style operation. These functions are in the
operator package, which you’ll learn about in a later week. For now, ignore the
import statement in the example below.
>>> from operator import mul, add >>> add(5, mul(add(5, 2), 4)) 33
So what happened here?
mul(add(5, 2), 4).
addfunction executed and returned
mulfunction’s parameters became
4, so the
mulfunction executed and returned
addfunction’s parameters became
28, so the function executed and returned