In most weeks there will be an online test in which you will be asked to enter some Python code.
For example, a question might ask you to define a function add_forty_two(x)
that
accepts an argument $x$, adds $42$ to it, and returns the result. You would then enter the
following code:
def add_forty_two(x):
"""Add 42 to x."""
return x+42
import
statements, as discussed below).
AddFortyTwo(n)
instead of add_forty_two(x)
, for example. (You should remember
that precise compliance with a specification is very often necessary for your
code to interact correctly with other code, so this is a very normal
issue in software development.)
"""Add 42 to x."""
is a docstring.
Unless otherwise specified, you should include a docstring for each function
that you define. It needs to come directly after the function definition line.
def add_forty_two(x):
"""Add 42 to x."""
print(f"Adding 42 to {x}")
return x+42
print(add_forty_two(1000))
add_forty_two(x)
with the assumption that $x$ is
an integer. You should then check that your function works correctly
when $x$ is an integer, even if it is zero or negative, for example.
However, you do not need to check that your function works correctly
when $x$ is a string or a list or a dictionary or anything else.
plt.show()
. (If you do things in the way that I
will demonstrate in lectures then you will almost never call
plt.show()
anyway. There are various other ways of
working that involve calling plt.show()
, but that is
not compatible with the online test system.)
import
directives at
the top of your code; for example you might need import numpy as np
to make the np.sqrt()
function available. Each question will have
instructions about this, which you should read carefully.
import
directives, so you
will need to solve the problem without using any external libraries.
import
directives you need to include.
matplotlib
. If the
question requires plotting, then matplotlib
will be available
to you without needing to import it. If the question is not set up for
plotting, then importing matplotlib
will cause an error.