Prepared by: Linsheng Zhuang, Junjie Zhang, Research Assistant
Sargent Institute of Quantitative Economics and Finance
Peking Univeristy HSBC Business School
Content:
Python makes it easy to do basic calculations.
In daily learning, we always encountered the basic mathematical calculations of addition (+), subtraction (-), multiplication (*), division (/), the remainder (%), and power operation (**).
in this small path called "mathematics", your best friend since childhood has been a little calculator. You can calculate the result by using the calculator. However, for the following question, how do you think it could be figured out by using the calculator?
$$ \frac{1}{\frac{2^{1/3}}{3} + 3\sqrt{5}} $$In Python, all you need to solve this problem is a single line of code: (Note that in the code below, **
stands for power ^
)
1 / (2**(1/3) / 3 + 3*5**0.5)
There are at least two advantages to using a program for scientific computing:
a = 2**(1/3) / 3
1 / (a + 3*5**0.5)
In the above code, we assign the result of $2^{1/3}/3$ to a variable $a$, and then use this variable to replace the corresponding part of the calculation. Is the result the same?
Thus, if we want to see the specific value of the temporary variable $a$, we can print it directly:
a
Is it very flexible? The following table summarizes the arithmetic operators supported in Python:
operator | meaning | example | result |
---|---|---|---|
+ | addition | 1 + 1 | 2 |
- | subtraction | 2 - 1 | 1 |
* | multiplication | 2 * 3 | 6 |
/ | division | 6 / 3 | 2 |
% | remainder | 10 % 3 | 1 |
** | power | 2 ** 3 | 8 |
In the previous example, we have seen the role of variables - the use of variables makes the program quite flexible. In the calculation, if you want to use a variable, give it a name directly, and then assign a value with the equal sign =
. Once the assignment is complete, we can call this variable in subsequent operations, just like the previous example.
In this section, we introduce the three most important variables in Python, which are integers, floats, and strings.
What integers are completely unnecessary to explain, each one should know. The so-called float, which can be roughly understood as "a number with a decimal point". So, does a float mean "small number"?
not quite. For example, the number 1.0
should be understood mathematically as an integer rather than a decimal, but in Python, it is a float because it has a decimal point. In the following example, we assign the variable $a$ a float 1.0 and the variable $b$ an integer 1. The two new variables $a$ and $b$ are generated in the same value, but The type is different.
Correspondingly, the number without a decimal point is an integer. So, the variable type in Python is not exactly the same as the mathematical type, but it basically corresponds.
a = 1.0
b = 1
a == b
In the above code, except for a strange symbol ==
, you should know all the other symbols. This ==
symbol is used to compare the values of the left and right sides of it. If they are equal, the result of the ==
operation is True
(correct). If you don't want to wait, the result of the operation is False
.
We see that when we determine if the size of the floating point $a$ is equal to the size of the integer $b$, Python gives us a reply: True.
In addition to ==
to determine equality, Python also provides some other symbols to compare values:
print(a > b) # is a greater then b
print(a < b) # is a less than b
print(a >= b) # is a greater than or equal to b
print(a <= b) # si a less than or equal to b
Here, we use Python's built-in print
function to print the result of the judgment. The result is in line with our understanding - since $a$ is as large as $b$, it is greater than or equal to >=
and An operation less than or equal to <=
outputs a correct (True) result, and strictly greater than >
and strictly less than <
, the result of the error (False) is output.
The previous examples are all numbers. If I want to use a variable to represent my own name, what should I do? My name cannot be 123.
Don't worry, if your name is "Peter", you can also assign a variable to "Peter". Thus, the newly generated variable is a variable of type string
.
name = 'Peter'
print(name)
The two string type variables are not comparable but can be compared to see if they are the same. In other words, ==
can determine if the two strings are exactly the same. It is better to say that the name of the No. 1 student is "Tom" and the name of the No. 2 student is "Jerry". We assign them to two variables name_1
, name_2
name_1 = 'Tom'
name_2 = 'Jerry'
name_1 == name_2
What if you want to change the value of a variable? For example, if you used to call "Peter", you assigned your name to the variable name
. Later, you think the name is too common, and you decided to change it to "Tom." what can we do about it?
We just need to re-assign the value to the variable name
:
name = 'Tom'
print(name)
Beginning with this section, let's introduce the container in Python. A container is a warehouse that can store data. Python provides us with a lot of container types, but we're only going to cover one of the most basic and important container types: List.
Next, let's generate two list variables, list_1
and list_2
:
list_1 = [1, 2, 'abc']
list_2 = [1, 2, 'abc']
print(list_1)
print(list_1 == list_2)
As you can see, the contents of the two lists `list_1
and list_2
are exactly the same, and their elements are 1
2
'abc'
. Therefore, when using the ==
operator to determine if they are equal, Python outputs the correct (True) result.
Here, we introduce four common list operations: (1) calculate the length of the list, that is, how many elements are in the list; (2) add elements at the end of the list; (3) insert elements in the middle of the list; (4) Calculate the number of occurrences of a element in the list.
Function | Operation |
---|---|
len(list) | Calculating the length of the list |
list.append(obj) | Add an element to the end of the list |
list.insert(i,obj) | Insert an element at the i-th position of the list |
list.count(obj) | Calculate the number of occurrences of the element obj in the list |
First, let's see how long the list list_1
is (ie how many elements it contains). Here, we use the Python built-in function len
:
len(list_1)
As you can see, the list list_1
has a length of 3, which contains 3 elements.
Second, if you want to add a string 'xyz'
to list_1
, we just need to add a dot (.
) after the variable list_1
and write the command `append('xyz'). :
list_1.append('xyz')
list_1
Next, if you want to insert a string 'abc'
at the beginning of the list, use the insert
function, whose first argument is the insertion position and the second argument is the inserted value. Since we are inserting an element at the beginning of the list, the index value of the insertion position is 0:
list_1.insert(0, 'abc')
list_1
Finally, take a look at how many times the string 'abc'
appears in the list list_1
?
list_1.count('abc')
Now, we have got a list of length 5, list_1
, whose elements are: 'abc'
1
2
'abc'
, 'xyz'
.
What should I do if I want to take the third element 2
from the list list_1
? Simply put, the code should be written like this:
List_1[2]
That is, add a square bracket [ ] to the list name list_1
and fill in the element's number 2
in parentheses to call the element numbered 2
in the list.
list_1[2]
We successfully got the element 2
from list_1
. You may have noticed a problem. The element 2
is ranked third in the list, but why is it numbered as 2
?
Because the elements in Python are numbered starting from 0
~ In the following figure, we list the relationship between the element number and the corresponding element. Assuming the list length is $n$, list_1[k]
takes the k+1th
elements.
Number | Corresponding element |
---|---|
0 | 1st element |
1 | 2nd element |
... | ... |
n-1 | last element |
In particular, list_1[0]
takes the first element of the list. That's why, in the previous insert
function, we were inserting an element at the very beginning, and the index position was 0. Let's try it out:
print( list_1[0] )
print( list_1[1] )
print( list_1[2] )
print( list_1[3] )
print( list_1[4] )
Now the question is if I want to use the print
function to print out all the elements in list_1
, like the above code to fetch the elements in list_1
one by one, isn't it too much trouble? If the length of list_1
is relatively small, if you encounter a list with a length of 10000 on a certain day, so one value, you have to go to the monkey year?
We can do this with the for
loop...
for x in list_1:
print(x)
If your English is good enough, you should be able to read the meaning of the above code:
For x in list_1:
Print(x)
For x in list_1 (meaning: for any x in list_1):
Print (x)
The above code means: traverse all the elements in list_1
and print it out.
Let's learn another Python built-in function called range()
.
In the for
loop, if the keyword in
is followed by range(n)
, the value of x
is incremented from 0
to n-1
. If in
is followed by range(m, n, k)
, the value of x
is m
, m+k
, m+2k
... until the last one which must be less than n; if k
is omitted, the default k
is 1.
Example 1: Print all integers between 3-7, namely [3, 7)
for i in range(3, 8, 1):
print(i)
Example 2: We can use a loop to generate a list with list elements of 0
10
20
30
40
50
60
new_list = []
for i in range(7):
new_list.append(i * 10)
new_list
There is a simpler way of writing:
[i * 10 for i in range(7)]
Or
list(range(0, 70, 10))
There is a special statement called a judgment statement. Only when a condition is satisfied, a certain command is executed. If the condition is not satisfied, it is not executed. Here is an example: assign the variable $a$ to 3, and then use the program to determine whether $a$ is odd or even. If it is odd, print the phrase "a is odd"; otherwise, if it is even, print " a is even" sentence:
a = 3
if a % 2 == 1:
print(a, 'is odd.')
if a % 2 == 0:
print(a, 'is even.')
Now consider such a problem; store some data in a list, I just want to do some processing on some of the data, and other processing of other data, how should such problems be done?
Such problems can be summarized as a common program structure, which we can call a "traversal structure" here. Its form is as follows:
For x in list:
If condition 1:
f(x)
Elif condition 2:
g(x)
Elif condition 3:
h(x)
Else:
k(x)
In addition to the keyword if
, the pseudo code above also has the keyword else
, which means "other"
; in addition, elif
means that if the previous "if" is not satisfied, At the same time, the current conditions are met. So, the meaning of the pseudo code above is easy to guess:
list
, in each loop, for the extracted element ʻx, if it satisfies condition 1, the operation
f(x)is executed; if not satisfied Condition 1, but satisfying condition 2, the
g(x)operation is performed; if condition 2 is not satisfied, but condition 3 is satisfied, the
h(x)operation is performed; finally, if condition 3 is not satisfied , the operation
k(x)` is executed.This traversal structure often appears in programming, and it is powerful enough to solve a large number of practical problems, so it should be enough attention for beginners.
Example (traversal structure): In 0-9, put elements s maller than 3 in one list, and odd and even numbers greater than or equal to 3 in the other two lists.
less_3 = []
even_ge_3 = []
odd_ge_3 = []
for x in range(10):
if x < 3:
# If x is less than 3, x is placed in container less_3
less_3.append(x)
elif x % 2 == 0:
#If x is greater than 3, and divide by 2 to 0, that is, an even number greater than 3,
#x is placed in the container even_ge_3
even_ge_3.append(x)
else:
#Others are placed in the container odd_ge_3
odd_ge_3.append(x)
print(less_3)
print(odd_ge_3)
print(even_ge_3)
Like a function in math, given an input value, there will be an output value. In Python, we can define a function and then call it multiple times.
Example 1: Implement a function in Python $y = f(x) = 2x + 1$.
def f(x):
y = 2 * x + 1 # expression of the function
return y # returns the value of y to the caller
As seen in the code above, when the function $f$ returns a variable, the Python keyword used is return
.
Below we can call the function $f(x)$ to calculate the function value $y=f(3)$ when $x=3$. Since $y = 2 \times 3 + 1$, $y = 7$. Let's take a look at the results of Python calculations:
f(3)
Example 2: $z = g(x, y) = x^2 + y^2 + xy$
def g(x, y):
z = x**2 + y**2 + x*y
return z
Let's calculate the function value of $g(2, 3)$:
g(2, 3)
Example 3: Implement a function that calculates the sum of $1+2+...+n$ for any integer input value $n$:
def accumulation(n):
S = n
for i in range(n):
S = S + i
return S
When the parameter value is $n = 5$, we know that $1+2+3+4+5 = 15$.
accumulation(5)
Example: $e$, $\pi$, sin, cos, tan, log function
import math
The math
package has the value of the Euler constant $e$, which we can use directly:
math.e
Similarly, the math
toolbox also contains the pi $\pi$:
math.pi
Example 1: Calculate $$ sin\left(\frac{\pi}{6}\right)$$
The math
package contains common trigonometric functions, here we use the sin function. Since the computer cannot represent infinite decimals, and $\pi$ is an infinite decimal, the computer can only get a approximate value, so the result is approximately equal to 0.5.
math.sin(math.pi/6)
Example 2: Calculate $$ ln(e)$$
The log
function in the math
package represents the logarithmic function with $e$ as the base.
math.log(2.71828)
math.log(math.e)
Example 3: Calculate $$ sin\left(\frac{\pi}{6}\right) + cos\left(\frac{\pi}{3}\right) $$
It's convenient to use the math
package. We learn that $sin(\pi/6) + cos(\pi/3) = 1$ in the middle school, let's take a look at Python's calculations:
math.sin(math.pi/6) + math.cos(math.pi/3)
The following statement means importing a small module pyplot
from a package called matplotlib
, which helps us draw.
from matplotlib import pyplot
%matplotlib inline
Second, we need to generate a series of two-dimensional points $(x, y)$.
Imagine how we draw a function graph on paper. We can draw enough points on the paper and then connect the points. If the points we draw are dense enough, the image will become "continuous". So how do you get enough points? We need to find enough $x$ values first, then calculate all the corresponding function values $y$ according to the function expression, draw the points on the axes, and then join them into a line.
#import sin function and pi constant in math toolbox
from math import sin, pi
x_list = []
y_list = []
for i in range(200):
x_point = 0.01*i
x_list.append(x_point)
y_point = sin(pi*x_point)**2
y_list.append(y_point)
There are a total of 200 values in the x_list
container. Here we print out the 200 numbers and round them to two decimal places:
print([round(x, 2) for x in x_list])
Similarly, y_list
is also a list of length 200.
print([round(y, 2) for y in y_list])
Finally, we use the pyplot
tool we imported earlier to draw all the points. Since the points are very dense, when the points are connected one by one, the displayed lines are smooth curves.
pyplot.plot(x_list, y_list)
pyplot.show()
More about Python: Please visit quantecon