Programming in Python: A concise introduction

Prepared by: Linsheng Zhuang, Junjie Zhang, Research Assistant

Sargent Institute of Quantitative Economics and Finance

Peking Univeristy HSBC Business School

Content:

  • Calculation
  • Variable
  • List
  • Loop
  • Conditional Statements
  • Function
  • Import package
  • Plot

1. Calculation

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 ^)

In [1]:
1 / (2**(1/3) / 3 + 3*5**0.5)
Out[1]:
0.14028831124855431

There are at least two advantages to using a program for scientific computing:

  • The calculation process can be reused. This means that you only need to implement the formula once and you can execute it repeatedly. If you need to modify the formula, you can make changes based on the formula you have already entered, and then re-run it without having to retype the formula.
  • The calculation process can be carried out in steps. That is to say, if at a moment, you want to see the item in the denominator, for example $2^{1/3}/3$, What is the value of it? You just need to assign the result of the operation of that piece of code to a variable. Sounds a bit abstract? Let us give it a try:
In [2]:
a = 2**(1/3) / 3
1 / (a + 3*5**0.5)
Out[2]:
0.14028831124855431

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:

In [3]:
a
Out[3]:
0.41997368329829105

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

2. Variables

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.

  • int integer
  • float float
  • str string

1) Integer (int) and float (float)

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.

In [4]:
a = 1.0
b = 1
a == b  
Out[4]:
True

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:

In [5]:
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
False
False
True
True

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.

2) String (str)

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.

In [6]:
name = 'Peter'
print(name)
Peter

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

In [7]:
name_1 = 'Tom'
name_2 = 'Jerry'

name_1 == name_2
Out[7]:
False

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:

In [8]:
name = 'Tom'
print(name)
Tom

3. List

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.

  • list is a container that can hold any number of data of any type. It can dynamically change the value of the elements in the list. You can delete, modify, or add elements.
  • Separate the data with commas and enclose them in square brackets [ ], and we get a list.

Next, let's generate two list variables, list_1 and list_2:

In [9]:
list_1 = [1, 2, 'abc']
list_2 = [1, 2, 'abc']

print(list_1)
print(list_1 == list_2)
[1, 2, 'abc']
True

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.

1) Some common operations on lists

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:

In [10]:
len(list_1)
Out[10]:
3

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'). :

In [11]:
list_1.append('xyz')
list_1
Out[11]:
[1, 2, 'abc', 'xyz']

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:

In [12]:
list_1.insert(0, 'abc')
list_1
Out[12]:
['abc', 1, 2, 'abc', 'xyz']

Finally, take a look at how many times the string 'abc' appears in the list list_1?

In [13]:
list_1.count('abc')
Out[13]:
2

2) Get the value of the list

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.

In [14]:
list_1[2]
Out[14]:
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:

In [15]:
print( list_1[0] )
print( list_1[1] )
print( list_1[2] )
print( list_1[3] )
print( list_1[4] )
abc
1
2
abc
xyz

4. Loop

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...

1) for loop

  • for loop, the most common type of Python's basic loop structure, loops through all the elements of a container.
In [16]:
for x in list_1:
    print(x)
abc
1
2
abc
xyz

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.

2) range function

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)

In [17]:
for i in range(3, 8, 1):
    print(i)
3
4
5
6
7

Example 2: We can use a loop to generate a list with list elements of 0 10 20 30 40 50 60

In [18]:
new_list = []

for i in range(7):
    new_list.append(i * 10)

new_list
Out[18]:
[0, 10, 20, 30, 40, 50, 60]

There is a simpler way of writing:

In [19]:
[i * 10 for i in range(7)]
Out[19]:
[0, 10, 20, 30, 40, 50, 60]

Or

In [20]:
list(range(0, 70, 10))
Out[20]:
[0, 10, 20, 30, 40, 50, 60]

5. Conditional Statements

  • if, elif, else

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:

In [21]:
a = 3

if a % 2 == 1:
    print(a, 'is odd.')
if a % 2 == 0:
    print(a, 'is even.')
3 is odd.

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:

  • Loop through each element in the list list, in each loop, for the extracted element ʻx, if it satisfies condition 1, the operationf(x)is executed; if not satisfied Condition 1, but satisfying condition 2, theg(x)operation is performed; if condition 2 is not satisfied, but condition 3 is satisfied, theh(x)operation is performed; finally, if condition 3 is not satisfied , the operationk(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.

In [22]:
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)
[0, 1, 2]
[3, 5, 7, 9]
[4, 6, 8]

6. Function

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$.

  • In this example, $x$ is the input value of the function, and for each specific input value $x$, there will be an output value of $y$. In Python, the input value $x$ is called the function's parameter, and the output value $y$ is called the function's return value. The function $f$ in this example has a parameter and a return value.
In [23]:
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:

In [24]:
f(3)
Out[24]:
7

Example 2: $z = g(x, y) = x^2 + y^2 + xy$

  • This form of function is called "quadratic" in algebra.
In [25]:
def g(x, y):
    z = x**2 + y**2 + x*y
    return z

Let's calculate the function value of $g(2, 3)$:

In [26]:
g(2, 3)
Out[26]:
19

Example 3: Implement a function that calculates the sum of $1+2+...+n$ for any integer input value $n$:

In [27]:
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$.

In [28]:
accumulation(5)
Out[28]:
15

7. Import Package

  • math package contains a variety of math functions that we can use directly to facilitate our math operations. The math package can be used as a Swiss army knife with various tools. After importing, we can use functions provided by the package

Example: $e$, $\pi$, sin, cos, tan, log function

In [29]:
import math

The math package has the value of the Euler constant $e$, which we can use directly:

In [30]:
math.e
Out[30]:
2.718281828459045

Similarly, the math toolbox also contains the pi $\pi$:

In [31]:
math.pi
Out[31]:
3.141592653589793

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.

In [32]:
math.sin(math.pi/6)
Out[32]:
0.49999999999999994

Example 2: Calculate $$ ln(e)$$

The log function in the math package represents the logarithmic function with $e$ as the base.

In [33]:
math.log(2.71828)
Out[33]:
0.999999327347282
In [34]:
math.log(math.e)
Out[34]:
1.0

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:

In [35]:
math.sin(math.pi/6) + math.cos(math.pi/3)
Out[35]:
1.0

8. Plot

  • First, we first import the drawing toolbox.

The following statement means importing a small module pyplot from a package called matplotlib, which helps us draw.

In [36]:
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.

In [37]:
#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:

In [38]:
print([round(x, 2) for x in x_list])
[0.0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27, 1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38, 1.39, 1.4, 1.41, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49, 1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.59, 1.6, 1.61, 1.62, 1.63, 1.64, 1.65, 1.66, 1.67, 1.68, 1.69, 1.7, 1.71, 1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79, 1.8, 1.81, 1.82, 1.83, 1.84, 1.85, 1.86, 1.87, 1.88, 1.89, 1.9, 1.91, 1.92, 1.93, 1.94, 1.95, 1.96, 1.97, 1.98, 1.99]

Similarly, y_list is also a list of length 200.

In [39]:
print([round(y, 2) for y in y_list])
[0.0, 0.0, 0.0, 0.01, 0.02, 0.02, 0.04, 0.05, 0.06, 0.08, 0.1, 0.11, 0.14, 0.16, 0.18, 0.21, 0.23, 0.26, 0.29, 0.32, 0.35, 0.38, 0.41, 0.44, 0.47, 0.5, 0.53, 0.56, 0.59, 0.62, 0.65, 0.68, 0.71, 0.74, 0.77, 0.79, 0.82, 0.84, 0.86, 0.89, 0.9, 0.92, 0.94, 0.95, 0.96, 0.98, 0.98, 0.99, 1.0, 1.0, 1.0, 1.0, 1.0, 0.99, 0.98, 0.98, 0.96, 0.95, 0.94, 0.92, 0.9, 0.89, 0.86, 0.84, 0.82, 0.79, 0.77, 0.74, 0.71, 0.68, 0.65, 0.62, 0.59, 0.56, 0.53, 0.5, 0.47, 0.44, 0.41, 0.38, 0.35, 0.32, 0.29, 0.26, 0.23, 0.21, 0.18, 0.16, 0.14, 0.11, 0.1, 0.08, 0.06, 0.05, 0.04, 0.02, 0.02, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01, 0.02, 0.02, 0.04, 0.05, 0.06, 0.08, 0.1, 0.11, 0.14, 0.16, 0.18, 0.21, 0.23, 0.26, 0.29, 0.32, 0.35, 0.38, 0.41, 0.44, 0.47, 0.5, 0.53, 0.56, 0.59, 0.62, 0.65, 0.68, 0.71, 0.74, 0.77, 0.79, 0.82, 0.84, 0.86, 0.89, 0.9, 0.92, 0.94, 0.95, 0.96, 0.98, 0.98, 0.99, 1.0, 1.0, 1.0, 1.0, 1.0, 0.99, 0.98, 0.98, 0.96, 0.95, 0.94, 0.92, 0.9, 0.89, 0.86, 0.84, 0.82, 0.79, 0.77, 0.74, 0.71, 0.68, 0.65, 0.62, 0.59, 0.56, 0.53, 0.5, 0.47, 0.44, 0.41, 0.38, 0.35, 0.32, 0.29, 0.26, 0.23, 0.21, 0.18, 0.16, 0.14, 0.11, 0.1, 0.08, 0.06, 0.05, 0.04, 0.02, 0.02, 0.01, 0.0, 0.0]

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.

In [40]:
pyplot.plot(x_list, y_list)
pyplot.show()

More about Python: Please visit quantecon