# Exercises to Lecture 3: Introduction to Python 3.6 
By Dr. Anders S. Christensen
`anders.christensen @ unibas.ch`


## Exercise 3.1: If/Else statments
Yesterday, we used if and else statments to check conditions.

For example, we can check if a variable `a` is greater 10:


```
a = 100

if a > 10:
  print("a is greater than 10")
```

### Question 3.1.1:

In this exercise, write a function named for example `check_number(n)` which determines whether a number is negative, zero, or positive, and prints the answer.

Verify with a couple of cases, that the code yields the correct 






```
def check_number(n):

  # Write the rest of the function



# Check the output of the tests below to ensure that your code works
check_number(-1000.0)   # Code should print 'negative'
check_number(-69.0)     # Code should print 'negative'
check_number(0.0)       # Code should print 'zero'
check_number(1.0)       # Code should print 'positive'
check_number(420.0)     # Code should print 'positive'
```

## Exercise 3.2: When to stop a loop?

Sometimes `for`-loops can be terminated before they run to the end of `range()`.

In this example we wish to print all numbers where the square is smaller than 42.

However, we might not know when to stop the `for` loop beforehand. In this case, we can use `if`/`else` to check when the condition is met, and when it is we can use the `break` statement to stop the `for` loop.


```
# Print all integers that where the square is smaller than 42:

# Try all numbers up to 100
for i in range(100):

  # Check if the square is small enough
  if i**2 < 42:
    print(i, i**2)

  # Stop the loop if the test is False
  else:
    break
```

The so-called "Basel problem" in mathematics shows that the reciprocal of the square numbers form a convergent series:

\begin{equation}
x=\sum_{n=1}^\infty \frac{1}{n^2}  = \frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} \dots = \frac{\pi^2}{6}
\end{equation}

The problem is named after Leonhard Euler who lived here in in Basel and worked on the problem.


The code below uses 10,000 iterations of a `for` loop to calculate the result.


```
x = 0

# Do 10000 iterations
for i in range(1, 10000):

  # Calculate 1/n^2 and add it to x
  x = x + 1/i**2

print(x)
```

### Question 3.2.1:



As part of his proof, Euler calculated many terms of this series by hand in order to verify his work.

The sum of the series is approximately $1.645$. To achieve this accuracy, the numerical calculation of the sum can be stopped when $\frac{1}{n^2} < 0.001$, long before 10000 terms as in the above example.

*    In this question, rewrite the above code for the Basel problem, such that the calculation stops when the $\frac{1}{n^2}$ terms are smaller than $0.001$. 

*     **Hint:** Use a `break` statement to stop the for loop.


```
x = 0

for i in range(1, 10000):

  # Print the current iteration number
  print(i)

  ## Insert your own code here to calculate the sum from the Basel probelm


print(x)

  
```

## Exercise 3.3: Matrices and Vectors with Numpy

In this exercises you'll write some code contatining matrix and vector operations with numpy.

### Question 3.3.1: Matrix-Matrix Product

In the code block below, you are give two matrices A ($3\times4$ matrix), and B ($4\times 4$ matrix).


```
import numpy as np

np.random.seed(2089)

A = np.random.random(size=(3,4))
B = np.random.random(size=(4,4))

print(A)
print(B)
```

Use numpy's `np.matmul()` function to calculate the matrix product `C`:

\begin{equation}
\mathbf{C} = \mathbf{A}\mathbf{B}
\end{equation}

**Hint:** The matrix product is not the *element-wise* product!


```
# Calculate the matrix product

C = ??? # <--

print(C)
```

Also answer: what are the dimensions of `C`?

### Question 3.3.2: Dot products
Here you are give two vectors (i.e. 1D Numpy arrays), `p` and `q`:




```
np.random.seed(3001)

p = np.random.random(size=(10))
q = np.random.random(size=(10))

print(p)
print(q)
```

Use a Numpy function to calculate the dot product `k`:

\begin{equation}
k = \mathbf{p} \cdot \mathbf{q}
\end{equation}

**Hint:** The dot product is not the *element-wise* product!


```
# Calculate the dot product, k

k = ??? # <--

print(k)
```

Also answer: Is `k` a scalar (a single number), a vector, or a matrix?

## Exercise 3.4: Plotting a histogram
The code block below makes some "fake" data for the height of 100 people in units of [cm].

For now, you will use this to plot a histogram of the data. Before you start Question 3.4.1, make sure to run the code block below and inspect the output (which is a list named `heights` with the heights of 100 people).


```
import numpy as np
np.random.seed(2021)

heights = np.random.normal(loc=170, scale=10, size=100)
print(heights)
```

### Questions 3.4.1:

In order to plot a histogram, you can use the function `plt.hist(list)` to make a histogram with matplotlib.pyplot.

It works similarly to the `plt.plot()` (lineplots) and `plt.scatter()` (scatterplots), which you have already encountered in these exercises.

If you are unsure, use Google or consult the manual for `plt.hist()` here:
https://matplotlib.org/3.1.1/gallery/statistics/hist.html



```
import matplotlib.pyplot as plt

# Insert the code here to plot a histogram of the list 'heights'

plt.show()
```

### Question 3.4.2: Making a better plot

Similarly to Question 2.4.2 in the exercises from yesterday, add proper labels to the x- and y- axis to the histogram.

***Go back to the code in Questions 3.4.1 and add labels to the plot***

### Question 3.4.3: Changing colors

Your supervisor/professor only wants red histograms!

In this question, use Google, or other resources, to find out how to change the histogram from the default blue color to red color.

***Go back to the code in Questions 3.4.1 and change the color to red***

## Exercise 3.5: Matrix multiplication (harder)

After doing this exercise, I hope you will appreciate NumPy and numpy arrays.

In the example below, there are two random matrices defined, `A` and `B`.

The matrix product 
\begin{equation}
\mathbf{C} = \mathbf{A}\mathbf{B}
\end{equation}
is calculated using the `np.matmul()` function:



```
import numpy as np
np.random.seed(2021)

A = np.random.random(size=(3,5))
B = np.random.random(size=(5,4))

C = np.matmul(A,B)

print(C)
```

### Question 3.5.1:
Without NumPy, we would have to write our own code to calculate the matrix multiplication of `A` and `B`.

For this, the formula is:

\begin{equation}
C_{ik} = \sum_j A_{ij}B_{jk}
\end{equation}

In this question, you have to implement the above equation. That is, calculate the elements of `C` using this summation. 

Some things to consider:

*   Use a triple loop over the indices $i$, $j$, and $k$
*   What are the ranges for the indices?
*   Use `np.zeros()` to initialize the `C` matrix

Print the result and compare to the result in the previous code box to verify that you did it correctly.


```
# Initialize the array C of the correct size
C = np.zeros((3,4))


# Code the matrix multiplication below yourself









# Finally, print C
print(C)
```

8646c9783ae85d9fb89d11948886d605c48ea788
ipynb
Jupyter Notebook
intro_exercises3_2020.ipynb
andersx/python-intro
8409c89da7dd9cea21e3702a0f0f47aae816eb58
CC0-1.0
2020-05-03T11:59:01.000Z
2021-11-15T12:33:39.000Z
intro_exercises3_2020.ipynb
andersx/python-intro
8409c89da7dd9cea21e3702a0f0f47aae816eb58
CC0-1.0
intro_exercises3_2020.ipynb
andersx/python-intro
8409c89da7dd9cea21e3702a0f0f47aae816eb58
CC0-1.0
2020-05-10T21:15:15.000Z
2021-12-05T15:13:54.000Z
Qwen/Qwen-72B
1. YES
2. YES