Function arguments#
So far we have used function arguments in a basic way; this is the way that is familiar from mathematics:
# Load the Numpy package, and rename to "np"
import numpy as np
np.cos(0)
1.0
Here is another Numpy function, that you have already seen:
np.arange(0, 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Remember, this is a sequence of integers, starting at 0 (the first argument), up to but not including 10 (the second argument).
Now let us look at the help for the np.arange
function. As usual, we do this
by appending ?
to the function name, and pressing Enter in the notebook.
# To see the help for np.arange, remove the # at the beginning
# of the next line, and execute this cell.
np.arange?
You should see the help, preceded by a function signature something like this:
arange([start,] stop[, step,], dtype=None, *, like=None)
(Ignore the *
in the signature for now. Also ignore the square brackets in
the signature, they do not change what arguments we can pass). The signature
tells us function can accept up to five arguments. We have passed two. The
first sets the argument called start
to be 0, and the second sets the
argument called stop
to be 2.
To take another example, in this case we are asking for a sequence of numbers
starting at 1 up to, but not including 11. start
is 1 and stop
is 11.
# All integers from 1 through 10.
np.arange(1, 11)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
If we pass three arguments, we also set the step
argument, to set the step
size between integers. The following asks for all integers from -1 up to, but
not including 20, in steps of 4.
# All integers from -1 up to, but not including 20, in steps of 4.
np.arange(-1, 20, 4)
array([-1, 3, 7, 11, 15, 19])
Notice that this is an array.
Now look again at the help. Notice that the help gives each argument a name:
start
, stop
, step
. We can also use these names when we set these
arguments. For example, the cell below does exactly the same thing as the cell
above.
# All integers from -1 up to, but not including 20, in steps of 4.
np.arange(start=-1, stop=20, step=4)
array([-1, 3, 7, 11, 15, 19])
When we call the function using the arguments with their names like this, the named arguments are called keyword arguments.
Passing the arguments like this, using keywords, can be very useful, to make it clearer what each argument means. For example, it is a common pattern to call a function with one or a few keyword arguments, like this:
# Same as above.
np.arange(-1, 20, step=4)
array([-1, 3, 7, 11, 15, 19])
Writing the call like the cell gives exactly the same result as the cell below, but the cell above can be easier to follow, because the person reading the code does not have to guess what the 4 means — they can see that it means the step of the output array.
Another large benefit of keyword arguments is that it allows us to skip some
arguments we are not interested in. For example, lets say we are happy with
the default step
size of 1, but we do want to pass the dtype
argument, that
sets the data type of the output array.
We could do that by passing the default step size, like this:
# Float dtype, specifying step size.
np.arange(1, 11, 1, dtype=float)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Notice that the array values now have decimal points after them, indicating that they are floating point values.
But we have already seen that the step
size is a keyword argument, with a
default value of 1. That means, if we don’t specify the step
, the assumed
value will be 1. We can therefore specify dtype
without having to specify
step
, like this:
# Same as above. Notice, we didn't specify `step` this time.
np.arange(1, 11, dtype=float)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
To take another example, we have already seen the function round
. Inspect
the help for round
with round?
and Enter in a notebook cell.
round
takes up to two arguments. If we pass one argument, it is just the
value that round
will round to the nearest integer:
round(3.1415)
3
If we pass two arguments, the first argument is the value we will round, and the second is the number of digits to round to, like this:
round(3.1415, 2)
3.14
As you saw in the help, the second argument has the name ndigits
, so we can
also write:
round(3.1415, ndigits=2)
3.14
As before, this makes the code a little bit easier to read and understand,
because it is immediately clear from the name ndigits
that the 2 means the
number of digits to round to.