1. Introduction to Python#

Python is a programming language developed in 1990s by Guido van Rossum.
Its major features are:

  • consice – (relatively) easy to read

  • extensible – (so-called) object oriented

  • free! – unlike Matlab

It was originally used for “scripting” sequences of processing.
Now it is widely used for scientific computing as well.

Installing Python#

Most Linux and Mac machines usually have Python pre-installed.
To install and setup a variety of packages, it is the best to install a curated distribution, such as:

Starting Python#

From a terminal, type

$ python  

to start a python interpreter.

Python as a calculator#

At the python prompt >>>, try typing numbers and operators, like

>>> 1+1
1+1
2
2**8
256
# Uncomment (remove #) the line below
# exp(2)

The plain Python does not include math functions. You need to import numpy.

Jupyter Notebook#

For building a program step-by-step with notes and results attached, it is highly recommended to use a notebook interface, such as Jupyter Notebook (https://jupyter.org), which is included in Anaconda and other popular distributions.

To start Jupyter Notebook type in the terminal

$ jupyter notebook

which should open a web page showing your working directory.

You can create a new notebook from the New menu on the upper right corner, or open an existing .ipynb file like this.

Working with the notebook#

A notebook is made of “cells.”
You can make a new cell by “+” button on the Toolbar, or by typing ESC A (above) or ESC B (below).
You can make a cell as Markdown (documentation) by ESC M, as Code by ESC Y, or simply by the Toolbar menu.
You can delete a cell by ESC DD, or the Cut button on the Toolbar.

Markdown cell#

Markdown is a simple text formatting tool, with

#, ##, ###,... for headings

*, +, -,... for bullets

  • item 1

  • item 2

$  $ for Latex equations like \(\sum_{i=1}^n \alpha_i\) in line

$$  $$ for equations centered in a separate line

\[\sum_{i=1}^n \alpha_i\]

and two spaces at the end of the line
for a line break.

See adam-p/markdown-here for details.

You can format a Markdown cell by Shift+Return, and go back to Edit mode by Return

Code cell#

You can type Control+Return to run the cell or Shift+Return to run and move to the next cell.
You can also use the triangle button or “Cell” menu to run cells.

2*3
6

Integer and floating-point numbers#

A number can be an integer or floating-point, which sometimes needs distinction.

type(1)
int
type(1.5)
float

In Python 3, division of integers can produce a float.
In Python 2, it was truncated to an integer.

3 / 2  # 1.5 by Python 3; 1 by Python 2
1.5

You can perform integer division by // and get the remainder by %.

5 // 2
2
5 % 2
1

To make an integer as a floating point number, you can add .

type(1.)
float

Variables#

You can assing a number or result of computation to a variable.

a = 1
a
1
b = a + a
b
2

Multiple variables can be assigned at once.

a, b = 1, 2
print(a, b)
1 2

Lists#

You can create a list by surrounding items by [ ].

b = [1, 2, 3, 4]
b
[1, 2, 3, 4]

An item can be referenced by [ ], with index starting from 0.

b[1]  # 2nd item
2
b[-1]  # last item
4

A colon can be used for indexing a part of list.

b[1:3]  # 2nd to 3rd
[2, 3]
b[:3]  # first to third
[1, 2, 3]
b[1:]  # 2nd to last
[2, 3, 4]
b[1::2]  # from 1st, step by 2
[2, 4]
b[::-1]  # all in reverse order
[4, 3, 2, 1]

For lists, + means concatenation

b + b
[1, 2, 3, 4, 1, 2, 3, 4]

You can create a nested list, like a matrix

A = [[1,2,3],[4,5,6]]
A
[[1, 2, 3], [4, 5, 6]]

An item in a nested list can be picked by [ ][ ], but not [ , ]

A[1]
[4, 5, 6]
A[1][2]
6
A[1,2]  # this causes an error for a list
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 A[1,2]

TypeError: list indices must be integers or slices, not tuple

A list can contain different types of items with different lengths.

a = [1, 2, 3.14, 'apple', "orange", [1, 2]]
a

When you assign a list to another list, only the pointer is copied.

a = [1, 2, 3]
b = a
b[1] = 4
a

When you want to copy the content, use [:]

a = [1, 2, 3]
b = a[:]
b[1] = 4
a

Dictionary#

When you store data as a list, you have to remember what you stored 1st, 2nd, …

A dictionary allows you to access the value by name with key:value pairs.

# Postal codes in our neighborhood
postal = {'onna':9040411, 'tancha':9040412, 'fuchaku':9040413, 'oist':9040495}

You can check the value for a key by [ ].

postal['oist']

if branch#

Branching by if statement looks like this. In Python, indentation specifies where a block of code starts and ends.

x = 1
if x>0:
    y = x
else:
    y = 0
y

There is a shorthand notation with the condition in the middle:

x if x>0 else 0

for loop#

A common way of for loop is by range() function.
Don’t forget a colon and indentation.

j = 0
for i in range(5):
    j = j + i
    print(i, j)

You can specify start, end and interval.

for i in range(3,9,2):
    print(i)

for loop can also be over a list.

a = [1, 2, 3]
for x in a:
    print(x**2)
s = "hello"
for c in s:  # characters in a string
    print(c)
s = ["hello", "goodby"]
for c in s:  # strings in a list
    print(c)

enumerate() function gives pairs of index and content of a list.

for i, c in enumerate(s):
    print(i, c)

You can also apply a for loop for a dictionary.

for k in postal: # get the key
    print('%8s:'%k, postal[k])
# get key-value pair
for (k, v) in postal.items():
    print('{0:8s}: {1}'.format(k, v))

List ‘comprehension’#

There is a quick way of constructing a list from a for loop.

y = [x**2 for x in range(5)]
y

Numpy arrays#

For most computation, you need to import numpy package by the following convention:

import numpy as np

Numpy ndarray is specialized for storing numbers of the same type.

b = np.array([1, 2, 3])
b
type(b)
type(b[0])

Like a list, the index starts from zero

b[1]

Operators work component-wise.

b + b
b * b
b + 1  # broadcast

arange() gives an evenly spaced array.

np.arange(10)
np.arange(0, 10, 0.5)

linspace() gives an array including the last point.

np.linspace(0, 10)
np.linspace(0, 10, num=11)

Nested array#

You can make a matrix as a nested array.

A = np.array([[1,2],[3,4]])
A

Components can be accessed by [ , ]

A[1][1]
A[1,0]  # this if fine for a numpy ndarray, not for a regular list

Take the first row

A[0]
A[0,:]

Take the second column

A[:,1]

Component-wise arithmetics

A + A
A * A

A matrix product is inner products of rows and columns, such as

\[\newcommand{\mat}[1]{\begin{pmatrix} #1 \end{pmatrix}}\]
\[\begin{split} \mat{ a & b\\ c & d}\mat{ v & x\\ w & y} = \mat{ av+bw & ax+by\\ cv+dw & cx+dy}. \end{split}\]

From Python 3.5, @ symbol does the matrix product.

# matrix product
A @ A  # it should give [[1*1+2*3, 1*2+2*4], [3*1+4*3, 3*2+4*4]]

Common matrices#

np.zeros([2,3])
np.eye(4)
np.empty([3,2])  # the contents are not specified
np.empty([3,2], dtype=int)   # to specify the data type

Magic functions#

In Jupyter notebook (or ipython), many magic functions preceded by % are available for working with the file system, etc.

# present working directory
%pwd

You can use %quickref to see the list of magic functions

%quickref

Or %magic for the full documentation.

%magic

You can also use ! to run an OS command or a program.

!pwd
!hostname

Saving and loading data#

You can work with files by open(), write() and read() functions.

with open('haisai.txt', 'w') as f:
    f.write('Haisai!\n')
    f.write('Mensore!\n')
# f is closed when the `with` block is finished
%cat haisai.txt
with open('haisai.txt', 'r') as f:
    s = f.read()
print(s)

A common way of writing/reading a data file is to use savetxt() and loadtxt() functions of numpy.

X = [ [i, i**2] for i in range(5)]
X
np.savetxt("square.txt", X)  # by default, delimited by a space
%cat square.txt

Another common format is CSV, comma-separated value.

np.savetxt("square.csv", X, delimiter=",", fmt="%1d, %.5f")
%ls s*
%cat square.csv
Y = np.loadtxt("square.txt")
Y
Y = np.loadtxt("square.csv", delimiter=",")
Y

Getting help#

Python offers several ways of getting help.

help()
help(np.savetxt)

In Jupyter notebook, you can use ? for quick help.

np.*txt?
np.loadtxt?
np.loadtxt??

You can use ‘Help’ menu to jump to a variety of documentations.