Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lecture 01: Python primer

University of Lausanne

Course: Deep Learning for Solving and Estimating Dynamic Models in Economics and Finance
Script reference: Front matter and Appendix~E — Python as a calculator (arithmetic, expressions)
Notebook role: primer (pre-course self-study; skip if you write Python every day)
Author: Simon Scheidegger

Python Basics 1: Python as a Calculator

Let’s see how Python can do basic mathematics.

Addition, multiplication, exponentiation, division, etc.

Here are some examples. Try to guess the result before your run them. You can run the code either by clicking on the “Run” button or by pressing “Shift+Enter.”

1 + 2
3
1 + 2 + 3
6
1 + (2 + 3)
6
2 * 3
6
2 * (3 + 4)
14

That was easy... The only thing to remember here is that whatever you enclose in parentheses is evaluated first. Just like regular mathematics.

The addition + and the multiplication * are called binary operators. Let’s continue see some more binary operators. Just like before, try to guess the result before you run the code.

2 ** 3
8
5 ** 2
25
(1 + 2) ** 2
9

So, the binary operator ** exponentiates. Remember this. It is different than Matlab.

Here is another binary operator:

2 / 3
0.6666666666666666
1 / 2
0.5
5 / (2 + 3) ** 2
0.2

Okay, let’s now divide with zero:

1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/tmp/ipykernel_20447/1455669704.py in <module>
----> 1 1 / 0

ZeroDivisionError: division by zero

Oops... This is how error messages look in Python. You need to get used to reading them. Look what it says: ZeroDivisionErro. Well, it’s obvious what it means. Note also this ----> 1 1 / 0. The first number is the line number of the code block in which the error occured. Let’s put the error a little bit further into the code, to see if that changes:

# A comment line
# Another comment line
1 / 0 # the error line (by the way, this is another way to write comments)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/tmp/ipykernel_20447/4138437632.py in <module>
      1 # A comment line
      2 # Another comment line
----> 3 1 / 0 # the error line (by the way, this is another way to write comments)

ZeroDivisionError: division by zero

Notice that the error now appears in the third line. Read the errors.

The binary operator / is the division operator. What if we wanted integer division? Then you need to use the // operator:

3 // 2
1
9 // 2
4
6 // 4
1

Integer division by zero?

1 // 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/tmp/ipykernel_20447/914108395.py in <module>
----> 1 1 // 0

ZeroDivisionError: integer division or modulo by zero

What about the remainder of the division. This is the so-called modulo operator %. Guess the result of the following:

3 % 2
1
9 % 2
1
6 % 4
2
6 % 2
0

Let’s divide by zero again:

10 % 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/tmp/ipykernel_20447/1190083839.py in <module>
----> 1 10 % 0

ZeroDivisionError: integer division or modulo by zero

What about negative numbers? Sure:

-4
-4
-4 + 5
1
-5 + 5
0

Questions

  • In the code block provided below evaluate the dot product of the vectors:

    r1=4i^+3.5j^+2.5k^,\vec{r}_1 = 4\hat{i} + 3.5\hat{j} + 2.5\hat{k},

    and

    r2=1.5i^+2.5j^.\vec{r}_2 = 1.5\hat{i} + 2.5\hat{j}.

    Remembet that the dot product of two vectors is:

    r1r2=x1x2+y1y2+z1z2.\vec{r}_1\cdot \vec{r}_2 = x_1x_2 + y_1y_2 + z_1z_2.
# Your code here (by the way, this is how Python comments look like!)

Scientific notation

You can use scientific notation to define numbers in Python. For example:

1e-1
0.1
2.5e-3
0.0025

Rounding numbers

Very often we want to round numbers. Here is how:

round(2.24345)
2
round(2.233535, 3)
2.234

How does round work? You can use the help to figure it out:

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

round() is our first example of a Python function. As a matter of fact, it is a built-in Python function. We will learn quite a few of them (but not all) during these hands-on activities. By the way help() is another Python function.

Standard mathematical functions

Python has some built in functions. They are organized in a python module called math. Here is how you can import the functionality of the math module. You just do:

import math

Now you can use math. Here is how:

math.pi
3.141592653589793
math.sin(0.0)
0.0
math.sin(math.pi / 2)
1.0
math.cos(math.pi / 3)
0.5000000000000001
math.cos(math.pi / 3) ** 2 + math.sin(math.pi / 3) ** 2
1.0
math.sqrt(2)
1.4142135623730951
math.sqrt(2) ** 2
2.0000000000000004

Note that some of the results are not coming out exactly right. There is a bit of error. This are called floating point errors or numerical errors. Get used to them...

math.tan(math.pi / 3)
math.sin(math.pi / 3) / math.cos(math.pi / 3)

We saw how we can get π\pi. How about ee?

math.e

What about e2e^2? You should use the math.exp function in this case:

math.exp(2.0)
math.exp(-2.0)
1 / math.exp(-2.0)

What happes to exe^x when xx is too big?

math.exp(10)
math.exp(100)
math.exp(1000)

Oops... It doesn’t handle this very well... An OverflowError.

Anyway, if you want to learn more about what is in math go here (or just Google “Python math”). Alterantively, you could use the help command:

help(math)

The interesting thing to notice in the help is at the very end. There are several special numbers defined. What is inf?

math.inf
1 / math.inf
math.exp(math.inf)
math.exp(-math.inf)
math.inf + math.inf
- math.inf

Can you guess what it is? Let’s try something that will break it:

math.inf - math.inf

What is nan? This is another number defined in math. nan means “not a number”. Here are some other examples of how you can get it.

math.inf / math.inf

Basically, if you see a nan, there is something wrong with your code.

Questions

  • In the code block provided below to evaluate the magnitude of the vector:

    r1=4i^+3.5j^+2.5k^.\vec{r}_1 = 4\hat{i} + 3.5\hat{j} + 2.5\hat{k}.
# Your code here