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




## Exercise 1.1: Basic math

In the lecture we saw how Python code can be used to evaluate mathmatical expressions, for example $x = 42 \cdot 5$:


```
x = 42 * 5
print(x)
```

### Question 1.1.1:
Using Python code (similarly to the example above), what is the result of $x = 3.5^6$?


```
x = 3.5**6 # <-- change this line yourself
print(x)
```

    1838.265625


## Exercise 1.2: For loop

In this exercise we use `for`-loops to carry out summations.

For example the following sum
\begin{equation}
x = \sum_{n=0}^{25} n
\end{equation}

can be be calculated using the code below


```
x = 0

# Remember how range(i) goes from 0 to i-1!
for n in range(26):
    x = x + n
  
print("x is", x)
```


### Question 1.2.1:
In this exercise, write your own code below and to calculate the answer to the following summation:

\begin{equation}
x = \sum_{n=0}^{100} n^2
\end{equation}



```
x = 0

for n in range(101):

  x = x + n**2

print("x is", x)

```

    x is 338350


## Exercise 1.3: Debugging
Someone wrote some bad code for you to print the square root of all numbers from $0$ to $9$.
### Question 1.3.1:
Several things are wrong in the code block below. Pay attention to the error message and fix the errors.


```
import numpy as np

# Print the square root of all numbers up to 9
for i in range(10):
  print(np.sqrt(i))
```

    0.0
    1.0
    1.4142135623730951
    1.7320508075688772
    2.0
    2.23606797749979
    2.449489742783178
    2.6457513110645907
    2.8284271247461903
    3.0


# Exercise 1.4: NumPy functions

In the lecture, we used the `np.exp()` function from NumPy to calculate the exponential function.

For example, we can print the result of $\exp\left(2.0\right)$ using this function:


```
import numpy as np

x = np.exp(2.0)

print(x)
```



NumPy has a most other mathmatical function you can think of. 
You can find the documentation for many of these math routines here:

https://docs.scipy.org/doc/numpy/reference/routines.math.html

Before you answer the next question, take a look at the manual page in the link to see what is there.

### Question 1.4.1:
Most problems in programming are solved by Googling or looking in the manual.

Find a routine from NumPy, and use it to calculate and print the value of $x = \sin(2.0)$.


```
import numpy as np

# Calculate sin of 2.0
x = np.sin(2.0) #<-- change this line yourself

print(x)
```

    0.9092974268256817


# Exercise 1.5:  Double loop? (harder exercise)

In the lecture we had&mdash;not only single `for`-loops&mdash;but also double `for`-loops.

For example a double sum of products
\begin{equation}
n = \sum_{x=0}^{5} \sum_{y=0}^{5} \left(x \cdot y\right)
\end{equation}
Can be calculated using nested `for`-loops as demonstrated below




```
n = 0

for x in range(6):      # <--- NOTE: SHOULD SAY range(6), not range(5)
  for y in range(6):    # <--- NOTE: SHOULD SAY range(6), not range(5)
    n = n + x * y

print("n is", n)
```

    n is 100


### Question 1.5.1:
In the code-box below, evaluate the following double sum and print the answer

\begin{equation}
z = \sum_{x=0}^{3} \sum_{y=0}^{10} y^{x}
\end{equation}


```
# Do-it-yourself!

z = 0

for x in range(3):
  for y in range(10):

    z = z + y**x

print(z)

# Note, technically 0^0 is not defined, so not a real number.
# Python implementes this as 1
print(0**0)
```

    340
    1


ec6d7b70f76d7c9e3b03f81671b198b2f0870918
ipynb
Jupyter Notebook
intro_exercises1_solutions_2020.ipynb
andersx/python-intro
8409c89da7dd9cea21e3702a0f0f47aae816eb58
CC0-1.0
2020-05-03T11:59:01.000Z
2021-11-15T12:33:39.000Z
intro_exercises1_solutions_2020.ipynb
andersx/python-intro
8409c89da7dd9cea21e3702a0f0f47aae816eb58
CC0-1.0
intro_exercises1_solutions_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