*modules* - which contain existing functions, classes, and variables which allow you to do complex tasks in only a few lines of code. In addition, there are many other third-party modules (e.g. Numpy, Scipy, Matplotlib) that can be installed, and you can also develop your own modules that include functionalities you commonly use.

*Standard Library*, and you can
find a full list of the available functionality in the Python Documentation.

**import** them. The
following example shows how to import the built-in `math`

module, which
contains a number of useful mathematical functions:

In [ ]:

```
import math
```

You can then access functions and other objects in the module with `math.<function>`

, for example:

In [ ]:

```
math.sin(2.3)
```

In [ ]:

```
math.factorial(20)
```

In [ ]:

```
math.pi
```

`numpy`

module, which we will talk about next week, contains useful functions for finding e.g. the mean, median, and standard deviation of a sequence of numbers:

In [ ]:

```
import numpy as np
```

In [ ]:

```
li = [1,2,7,3,1,3]
np.mean(li)
```

In [ ]:

```
np.median(li)
```

In [ ]:

```
np.std(li)
```

Notice that in the above case, we used:

```
import numpy as np
```

instead of:

```
import numpy
```

which shows that we can rename the module so that it's not as long to type in the program.

Finally, it's also possible to simply import the functions needed directly:

In [ ]:

```
from math import sin, cos
sin(3.4)
cos(3.4)
```

You may find examples on the internet that use e.g.

```
from module import *
```

but this is **not** recommended, because it will make it difficult to debug programs, since common debugging tools that rely on just looking at the programs will not know all the functions that are being imported.

How do you know which modules exist in the first place? The Python documentation contains a list of modules in the Standard Library, but you can also simply search the web. Once you have a module that you think should contain the right kind of function, you can either look at the documentation for that module, or you can use the tab-completion in IPython:

```
In [2]: math.<TAB>
math.acos math.degrees math.fsum math.pi
math.acosh math.e math.gamma math.pow
math.asin math.erf math.hypot math.radians
math.asinh math.erfc math.isinf math.sin
math.atan math.exp math.isnan math.sinh
math.atan2 math.expm1 math.ldexp math.sqrt
math.atanh math.fabs math.lgamma math.tan
math.ceil math.factorial math.log math.tanh
math.copysign math.floor math.log10 math.trunc
math.cos math.fmod math.log1p
math.cosh math.frexp math.modf
```

`math.cos`

funtion take radians or degrees? Are there functions that can convert between radians and degrees? Use these to find the cosine of 60 degrees, and the sine of pi/6 radians.

In [ ]:

```
# enter your solution here
```

In [ ]:

```
def do_something():
a = 1
print(a)
```

The variable `a`

is defined in the **local scope** of the function.

Consider the following example:

In [ ]:

```
a = 1
def show_var():
print(a, b)
b = 2
show_var()
```

**global scope**. This is very useful because it means that modules don't have to be imported inside functions, you can import them at the top level:

In [ ]:

```
import numpy as np
def normalize(x):
return x / np.mean(x)
```

**not** mean that you should ever use:

In [ ]:

```
a = 1
def show_var():
print(a)
```

Consider the following example:

In [2]:

```
a = 1
def show_var():
print(a)
a = 2
show_var()
```

**local scope** of the function. Any variable in the local scope takes precedence over any other variable, **even before it is actually used**:

In [3]:

```
def show_var():
print(a)
a = 2
```

`a`

is defined inside the function and so it doesn't matter if a is used anywhere else in the Python code. The above function will therefore not work because `a`

is used before it is defined.

What will the following code print? (think about it, don't run it!):

```
def double(x):
x = x * 2
print(x)
x = 1
double(x)
print(x)
```

and what about this code?:

```
def append_3(x):
x.append(3)
print(x)
x = [1,2]
append_3(x)
print(x)
```

In [ ]:

```
# Ok, you can try them out now!
```

*really* have constants, so how do we recognize these? We now need to speak about coding style.

In [ ]:

```
pi = 3.1415926
def CalculateValues(x):
return(x*pi)
```

*camel-case* used above is reserved for classes).

This is the correct way to write the code:

In [ ]:

```
PI = 3.1415926
def calculate_values(x):
return(x * PI)
```

Other examples include that indentation should always be 4 spaces, etc. In practice, you can check your code with this script although this does not work in the notebook. Download the script to the folder where you are writing code, and do:

```
python pep8.py my_script.py
```

where `my_script.py`

is the script you want to check. For example, you might see:

```
my_script.py:2:1: W191 indentation contains tabs
```

The errors include the line number.