Summary exercises - SOLVED#

Exercise 1#

In the cell below assign your three favourite digits to three variables,

  • fav_num1, fav_num2, fav_num3

fav_num1 = 5
fav_num2 = 42
fav_num3 = 55

In the cell below, display

fav_num1 + fav_num2 - fav_num3

fav_num1 + fav_num2 - fav_num3
-8

Now try: $\(\frac{\mbox{fav_num1} + \mbox{fav_num2}}{\mbox{fav_num2} - \mbox{fav_num3}}\)$ you need to put both numerator and denominator in (separate) round brackets, and use / for the fraction bar. Please try it in the cell below.

temp_num1 = fav_num1 + fav_num2
temp_num2 = fav_num2 - fav_num3
answer = temp_num1 / temp_num2
print(answer)
-3.6153846153846154

Now try $\(\mbox{fav_num1} + \frac{\mbox{fav_num2}}{\mbox{fav_num3}} - \mbox{fav_num2} \)$

fav_num1 + (fav_num2 / fav_num3) - fav_num2
-36.236363636363635

In the cell below, display $\((\mbox{fav_num1})^{ (\mbox{fav_num3})}\)$ that is, fav_num1 to the power of fav_num3.

fav_num1 ** fav_num3
277555756156289135105907917022705078125

Now try some boolean expressions:

In the cells below evaluate the expressions above these empty cells:

\[\mbox{fav_num1} < \mbox{fav_num3}\]
fav_num1 < fav_num3
True
\[\mbox{fav_num2} > \mbox{fav_num3}\]
fav_num2 > fav_num3
False
\[\mbox{fav_num2} <= \mbox{fav_num1}\]
fav_num2 <= fav_num1
False
\[\mbox{fav_num3} >= \mbox{fav_num2}\]
fav_num3 >= fav_num2
True
\[\mbox{fav_num1} == \mbox{fav_num1}\]
fav_num1 == fav_num1
True
\[\mbox{fav_num1} != \mbox{fav_num2}\]
fav_num1 != fav_num2
True

Exercise 2#

What results do the following expressions produce, when typed into the cells below (each one to a new cell. Make new cells as needed)

  • 1+1

  • 1+3-8

  • 2*3

  • 4/2

  • 1/3

  • 1//3

  • 2**2

  • 2**3

  • 2**0.5

  • 5 % 3

1+1
2
1 + 3 - 8
-4
2 * 3
6
4 / 2
2.0
1 / 3
0.3333333333333333
1 // 3
0
2**2
4
2**3
8
2**0.5
1.4142135623730951
5 % 3
2

Exercise 3#

In Mathematical notation, we often omit the multiplication symbol. What happens in a Python expression such as \(2 \times 3\) if you omit the multiplication and use a space instead? (Make a note of what you find.)

2 3
  Input In [22]
    2 3
      ^
SyntaxError: invalid syntax

Exercise 4#

In Maths and Physics, we often deal with expressions involving unknowns. What happens if you type the following into the cell below? $\( 2 + x + x \)$

2 + x + x
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [23], in <module>
----> 1 2 + x + x

NameError: name 'x' is not defined

In general, Python requires all objects involved in an expression to have known values. Later in the year we will discuss symbolic programming, but let’s get the basics first.

Exercise 5#

Try typing the following sentence into the cell below: ”Hello World”. What happens if you omit the quotation marks?

Hello World
  Input In [24]
    Hello World
          ^
SyntaxError: invalid syntax

Exercise 6#

Integers and real numbers are represented in different ways inside the computer. In Python, integers are represented by a type called int. To approximate real numbers in Python we have floating-point numbers, represented by a type called float. Thus the number \(1\) in Python is an int, where as the number \(1.0\) is a float.

What do the following expressions evaluate to, when put into the below cells (each in an individual cell)?

  • type(3)
  • type(-5)
  • type(1.02)
  • type("test")
  • type("Hello World")
type(3)
int
type(-5)
int
type(1.02)
float
type("test")
str
type("Hello World")
str

For each arithmetic operation, there are two versions:

(a) the version between integers, and

(b) the version between floats.

These two sets of operations work in different ways. Python decides which operation to use, based on the types of the operands. So, if we write 1+2, then integer addition is used, because both operands are integers. If we write 1.0 + 2.0, then float addition is used, because both operands are floats. For addition, subtraction, and multiplication, this doesn’t matter very much, because performing these operations between two integers always produces another integer in everyday mathematics.

Integer division: In a division operation involving only integers, many programming languages will perform pure integer division, producing only the integer part of the result. This is not the case in Python 3, the integer division can be executed by using \(//\).

Exercise 7 - The importance of correct bracketing.#

The following is an attempt to calculate \(\frac{2+4}{2}\). What does 2+4/2 produce in Python? Try it in the cell below.

2 + 4/2
4.0

Can you explain this behaviour? What needs to be done to fix the calculation? Try fixing it in the cell below.

(2 + 4)/2
3.0

Exercise 8#

How should the following expression be entered in the below cell, in order to be sure to come up with the correct answer? $\(\frac{1 + \sqrt{5}}{2} - 2^3 \)$

((1 + 5**0.5) / 2) - 2**3
-6.381966011250105
tmpval = (1 + 5**0.5)
print(tmpval/2 - 2**3)
-6.381966011250105

HINT: You can get the square root by using 5 to the power of 0.5?

Exercise 9#

Now we assign the numbers to the following variables, \begin{align*} a = 1+ \sqrt{5},\ b = 2, \ c = b^3. \end{align*} Please do it in the cell below.

a = (1 + 5**0.5)
b = 2
c = b**3

Now in the cell below write the following $\( \frac{a}{b} - c\)$ Compare the answer with the one from Exercise 8.

a/b - c
-6.381966011250105

Exercise 10 - CHALLENGING(!)#

The famous Fibonacci Numbers are given by the sequence \begin{equation} f_0 = 0,\quad f_1 = 1,\quad f_n = f_{n-1}+f_{n-2}\ \mbox{for \(n\ge 2\)}, \end{equation} This means that we begin with the numbers \(0\) and \(1\) and then each successive number is the sum of the previous two. Thus we have \begin{equation} 0, 1, 1, 2, 3, 5, 8, 13,21,34, \ldots \end{equation} It is possible to find a closed form solution for any given Fibonacci number (e.g. if I want to get the 234th number, I don’t have to count up all 233 preceding it). This is given by: \begin{equation} f_n = \frac{a^n - b^n}{a-b}, \end{equation}

Here n represents the nth Fibonacci number and

\begin{equation} a = \frac{1+\sqrt{5}}{2}, \quad\mbox{and}\quad b = \frac{1-\sqrt{5}}{2}. \end{equation}

  • How would you write down expressions for $a$ and $b$ in Python? Do it in a cell below.
  • In the another cell, do `type(a)` and `type(b)`. Are you happy with the results?
  • How would you put those expressions together to make one large expression to calculate $f_4$ (we would call this the fourth number, but as we count from 0, this is actually the fifth number, which is 3)? Set the variable $n=4$ in one cell. Then in another cell write your $f_4$ by using the variables that you have already defined.
  • Check that you obtain the correct answer.
a = ((1 + 5**0.5) / 2)
b = ((1 - 5**0.5) / 2)
type(a), type(b)
(float, float)
n = 50
fn = (a**n - b**n) / (a - b)
fn
12586269025.00002

HINTS:

  • First ensure that you are able to calculate the expressions for $a$ and $b$, by typing their right-hand sides into the cell and checking the result (approximate values are $a\simeq 1.618$ and $b\simeq -0.618$).
  • Using pen and paper, \textbf{substitute the algebraic expressions} for $a$ and $b$ into $f_n$ with $n=4$. Carefully introduce pairs of brackets around any sub-expressions that need to be bracketed in order to override the usual BODMAS/BIDMAS operator precedence.
  • Check that your brackets balance properly! Check that the corresponding pairs of brackets really do surround the correct sub-expressions. Try to use brackets only where needed.
  • Once you have planned how to type in the complicated expression for $f_4$, try it out in the interpreter window and check that you get the correct result (compare with the Fibonacci sequence above, which gives $f_0, f_1,f_2,\ldots$).
  • Was the result exactly correct? If not, subtract your answer from the correct answer, to see how large the error was (it should be rather small).