Introduction to python for linear algebra¶

author: Parin Chaipunya affil: KMUTT

Overview¶

What should we know about python ?

  • python is a high-level programming level used widely nowadays in several applications in science and engineering.
  • It is a general purpose language, whiche means it could be used to do several different things.
  • Due to its large community, there are so many packages available to fulfill your need.
  • Eventhough it could be used to establish almost any task, it is not the fastest computation languange. For this reason, most of the computational packages are written with faster languages like C, C++ or fortran.

Packages¶

  • python on its own does not have so many built-in functions. Without any packages imported, it can do simple computation, handle simple data types and structures, and print outputs. We therefore have to import packages in order to have richer functionalities.
  • In this course, we mainly use numpy for matrix computations and matplotlib for visualizations.

Using python¶

  • The first way to use python is by the prompt mode. This mode is suitable for a one-time computation.
  • The second way is to write a python script, which is a .py file, and then execute it with python. This method is suitable for a more serious project that you may require to come back to review/revise the code.
  • The third way is to use an interactive python notebook, which is a .ipynb file. This method is a mixture between the above two, allowing one to review/revise the code but able to observe the output along the way. We can also mix text, visualization and executable code blocks in one file.

In this course, we shall always use .ipynb approach.

First glances¶

Hello world: Text printing¶

A string is put inbetween quotes "...".

In [1]:
print("Hello world")
Hello world

Do not "tab"¶

python is tab-sensitive. It is important not to use tab unintentionally, because it has a special and reserved meaning.

In [2]:
a = 1
    print(a)
  Cell In[2], line 2
    print(a)
    ^
IndentationError: unexpected indent

Simple calculation¶

The following block, we shall compute $$ 1 - \frac{3 \times 11}{20} + 2^{3}(3^{5} - 1)(1 + 5^{2}). $$

In [3]:
1 - (3*11)/20 + 2**3*(3**5 - 1)*(1 + 5**2)   # The result of the computation is showed immediately in the output.
Out[3]:
50335.35

Storing values in variables and printing them¶

Here, we store the value of the same computation in a variable "a". Notice that the result is not printed into the output because it is redirected into the variable.

In [4]:
a = 1 - (3 * 11) / 20 + 2**3 * (3**5 - 1) * (1 + 5**2)

To print the value, we need an explicit command. There many ways to do this where we show them below.

In [5]:
# First, we use "display".
display(a)
50335.35
In [6]:
# Second, we use "print" which is preferred over "display".
print(a)
50335.35
In [7]:
# We can add more context.
print("a = %f" % a)
a = 50335.350000
In [8]:
# The best and recommended way is to use an "f-string".
print(f"a = {a}")
a = 50335.35