# Homework 2

## Contents

# Homework 2¶

Type your name and email in the “Student details” section below.

Develop the code and generate the figures you need to solve the problems using this notebook.

For the answers that require a mathematical proof or derivation you can either:

Type the answer using the built-in latex capabilities. In this case, simply export the notebook as a pdf and upload it on gradescope; or

You can print the notebook (after you are done with all the code), write your answers by hand, scan, turn your response to a single pdf, and upload on gradescope.

The total homework points are 100. Please note that the problems are not weighed equally.

Note

This is due before the beginning of the next lecture.

Please match all the pages corresponding to each of the questions when you submit on gradescope.

## Student details¶

**First Name:****Last Name:****Email:**

## Problem 1 - Lists¶

Consider the following list:

```
data = [1, 4, 3, 10, 4, 3, 4, 4]
```

Add the element 6 at the end of `data`

:

```
# your code here
```

Add the list [0, -1, 3] at the begining of `data`

```
# your code here
```

How many elements are in the `data`

?

```
# your code here
```

Extract all `data`

elements from the second to the second to last (inclusive).

```
# your code here
```

Extract all `data`

elements from the second to the second to last (inclusive) skipping every other element.

```
# your code here
```

Sort the list.

```
# your code here
```

Find the minimum of the list.

```
# your code here
```

Find the average of the elements in the list.

```
# your code here
```

## Problem 2 - Numerical Python¶

Let’s make a random array:

```
import numpy as np
np.random.seed(12345) # We need this so that we get the same numbers
# every time we runt the following code
x = np.random.randn(100)
x
```

Find the minimum of `x`

.

```
# your code here
```

Find the maximum of `x`

.

Find the maximum of `x`

.

```
# your code here
```

Use `x.shape`

to get the number of elements of `x`

as an integer.

```
# your code here
```

Find the sum of all the elements of `x`

divided by the number of elements of `x`

. This is the average.

```
# your code here
```

Compare the average you found above with the result of `x.mean()`

. Are they the same?

```
# your code here
```

Square the elements of `x`

and store them in a new array called `x2`

.

```
# your code here
```

Find the average of `x2`

and subtract from it the square of average of `x`

.

```
# your code here
```

Compare the result you found on the cell above with `x.var()`

. Are they the same? (Remark: You just calculated the empirical variance of the “data” `x`

in two different ways!)

```
# your code here
```