Summary exercises
Contents
Summary exercises#
Exercise 1#
In the cell below assign your three favourite digits to three variables,
fav_num1,fav_num2,fav_num3
In the cell below, display
fav_num1 + fav_num2 - fav_num3
Now try:
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.
Now try
In the cell below, display
that is, fav_num1 to the power of fav_num3.
Now try some boolean expressions:
In the cells below evaluate the expressions above these empty cells:
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+11+3-82*34/21/31//32**22**32**0.55 % 3
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.)
Exercise 4#
In Maths and Physics, we often deal with expressions involving unknowns. What happens if you type the following into the cell below?
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?
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")
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.#
Let’s assume we are trying to compute \(\frac{2+4}{2}\). What does 2+4/2
produce in Python? Try it in the cell below.
Can you explain this behaviour? What needs to be done to fix the calculation? Try fixing it in the cell below.
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?
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,
Please do it in the cell below.
Now in the cell below write the following
Compare the answer with the one from Exercise 8.
Exercise 10 - CHALLENGING(!)#
The famous Fibonacci Numbers are given by the sequence
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
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:
Here n represents the nth Fibonacci number and
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)andtype(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.
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, 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).