# More mathematical commands via Numpy

## Contents

# More mathematical commands via Numpy#

The basic Python language doesn’t contain many more commands for doing maths than the basic arithmetical operations. If we want more complex things like trigonometric functions, we have to `import`

a * package* or a

*which offers those commands. For our purposes, packages and modules are essentially the same thing. There are a number of packages that provide mathematical functions. The one you will most commonly use is*

**module**`numpy`

(which stands for numerical Python). There are several ways of importing packages. For now, we’ll import `numpy`

in the following conventional way:```
import numpy as np
```

```
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import numpy as np
ModuleNotFoundError: No module named 'numpy'
```

The previous cell told Python to make the `numpy`

package available, and call it `np`

. We could call it anything other than `np`

, but `np`

is a conventional name for `numpy`

.
We can now use the functions provided by `numpy`

like so:

```
np.cos(0)
```

```
1.0
```

Note that `numpy`

works in radians. You can get the value of pi using `np.pi`

, so we can compute:

```
np.sin(np.pi / 6)
```

```
0.49999999999999994
```

The output of the last cell looks a bit funny - it should be 0.5 exactly. When dealing with floating point numbers, there will always be some rounding errors in the computations, and you shouldn’t rely on getting exact values. You will see more on this in further notebooks.

When we write `np.cos(0)`

, we are “*calling the function np.cos with argument 0*”. We will later see how to write and call our own functions.

`Numpy`

contains many other useful mathematical functions, listed here. It’s not worth trying to remember all of them; just remember that this list exists!

We could have also imported `numpy`

using `import numpy`

. We would then use the functions in it by writing, for example, `numpy.cos(0)`

.

## A Warning#

You will occasionally see material import `numpy`

or other modules using the following format:

```
from numpy import *
# now we can use the functions from numpy without putting np. before them
cos(pi)
```

```
-1.0
```

* Do not do this.* While it might seem innocuous and indeed useful at first, it is actually considered dangerous and bad practice. If you do want to be able to use things from

`numpy`

without putting `np.`

before them, you should import them like this:```
from numpy import cos, pi
cos(pi)
```

```
-1.0
```

# Other modules#

There are many other packages available in Python - far too many to list - but you can import any of them in the same way as shown above provided they are installed on your computer.

One example that we have already seen is `mobilechelonian`

. We import this using the final syntax shown above:

```
from mobilechelonian import Turtle
donna = Turtle()
donna.forward(50)
donna.left(720)
```

Another module that you will likely make use of is `math`

. This has some overlap with `numpy`

.