- To help you learn some Python essentials and programming skills to enjoy the remainder of this course.

**How it works:**

- Complete the notebook below, as follows. Click on a question, then from the menu select Insert -> Insert Cell Below. From the dropdown box (next to the "stop" symbol) choose whether you wish to insert 'Code' or 'Markdown'. Save regularly!
- Press Shift-Enter to execute the code in a cell.
- Submit the .ipynb file via http://somas-uploads.shef.ac.uk/mas212 by 23.59pm on Sun 8th Oct 2017.
*If you are off-campus, you will need to use a VPN to submit.* - Your lecturer will mark each question as either 2 (good), 1 (needs revision), 0 (not attempted/wrong).
- This is an open-book test, which means you may consult books, notes and internet resources.
**Do not discuss or share your test with anyone**. Copy-and-pasting is**not permitted**.*Please give answers in your own words.*

**Key Resources:**

- Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/

**1. Describe the three numeric data types in Python 3. **

`list`

and `ndarray`

types).

**3. What does 'mutable' mean? Give three examples of data types that are mutable.** (N.B. Common data types include

`int, list, tuple, dict, set, string, float`

).</b>**4. What is a Universal Function? (in the context of the numpy module)**

`numpy`

module).

(b) What is the shape of `A*B`

, if the arrays `A`

and `B`

have shapes `(1,3,4)`

and `(2,5,3,1)`

?

(Please answer the following questions in "code" cells).

**6. Python as a calculator: Calculate $6 - 2$, $(3^2 + 5^2)^{1/3}$, $\tan(\pi/8)$ and $(3 + 5i)^{3}$.**

**8. Print the value of $\ln(2)$ to 9 significant figures. **

**9. Using the formula below, calculate Euler's number $e$ to an accuracy of 10 significant figures.**
$$
e = \sum_{n=0}^{\infty} \frac{1}{n!}
$$

**10. Use the decimal package to display $e$ to 50 decimal places.**

`sumdigits(N)`

to compute the sum of the digits of a number. For example, `sumdigits(1234)`

would return `10`

(as 1+2+3+4=10).

Use the function to compute the sum of the digits in your registration number.

In [1]:

```
s = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way. In short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
print(s)
```

**14. How many words are in this paragraph? How many different words?**

In [25]:

```
import numpy as np
```

`A B`

where
$$
A = \begin{bmatrix} 5 & 6 & 2 \\ 1 & -1 & \frac{3}{4} \end{bmatrix} , \quad \quad
B = \begin{bmatrix} 1 & 2 \\ 3 & -4 \\ 5 & 0 \end{bmatrix}
$$

In [21]:

```
def scramble(s, keyword="aardvark"):
"""Scrambles a string."""
n = len(keyword)
s2 = ""
for k in range(len(s)):
c = s[k]
cnew = c
if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
k = (ord(c) + ord(keyword.upper()[k % n]) - 2*65) % 26
cnew = chr(65 + k)
elif c in 'abcdefghijklmnopqrstuvwxyz':
k = (ord(c) + ord(keyword.lower()[k % n]) - 2*97) % 26
cnew = chr(97 + k)
s2 = s2 + cnew
return s2
scramble("Hello everyone, blah blah blah.")
```

Out[21]:

**19. Describe in your own words what the scramble function does.**

**20. Write a function unscramble to reverse its effect.**

Use your function to decipher the following quote by a famous literary detective. From which novel is it taken?</b>

In [55]:

```
txt = "Hon jfkon ydqe S srly ky yfx tykt nkzn iou kvvv elzpdnrded wce smpfvnisve, zcakovei medkinj, hfgevvu idzrosdwlv, mlvo so tyh tieth?"
```