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#
Linux and Mac machines usually have Python pre-installed.
To install and setup a variety of packages, it is easiest to install a curated distribution, such as:
Anaconda: https://www.anaconda.com/download
There is a free version of Anaconda for students and practitioners, but if you belong to an organization of more than 200 employees, a Business or Enterprise license is required (https://www.anaconda.com/pricing).
Anaconda also installs many packages you will never use.
To install python and packages without using Anaconda, refer to the section Installing Python by Miniforge.
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).
There are two variants, the original Jupyter Notebook and a newer interface Jupyter Lab.
Type in the terminal
$ jupyter notebook
or
$ jupyter lab
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.
Jupyter Notebook/Lab may not work well with Safari. You can use a specific brower, for example, after starting Chrome, by
$ jupyter lab --browser=chrome
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 delete a cell by the Cut button on the Toolbar or typing ESC DD.
Code cell#
The basic type of cell is “Code cell.”
You can use the triangle button or “Cell” menu to run the codes in the Code cell.
You can type Shift+Return, or Control+Return to stay in the same cell, which is useful for repeated running.
2*3
6
Markdown cell#
For documentation, you can make a cell as a Markdown cell by ESC M, as Code by ESC Y, or simply by the Toolbar menu.
You can format a Markdown cell by Shift+Return, and go back to Edit mode by Return or double clicking.
Markdown is a simple text formatting syntax. For example:
#, ##, ###,... for headings
Chapter#
Section#
*, +, -,... 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
Two spaces at the end of the line makes a line break
and an empty line for a paragraph.
See adam-p/markdown-here for details.
Integer and floating-point numbers#
In digital computers, numbers are represented by a set of binary bits, usually 64 bits in modern computers.
An integer is represented as binary number, from \(-2^{-63}\) to \(2^{-63}-1\), with the first bit representing the sign.
2**63
9223372036854775808
To represent a real number, “floating point” representation is used, typically 1 bit for the sign \(s\)(+,-), 11 bit for the exponent \(e\) (-1022 to 1023), and 52 bit for the fraction \(f\) (0 to \(1-2^{-52}\)), representing $\( s\times (1+f)\times2^e \)$
You can check the type of a number by type() function.
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 = 3, 4
a
3
b
4
Print function#
You can check the content of a variable or an expression by typing it at the bottom of a cell.
You can use print() function to check the variables anywhere in a cell.
Multiple items listed by , are printed with a space in between.
A text string is represented by ' ' or " ".
print(a, b)
print('a =', a)
print("a =", a, ' b =', b)
3 4
a = 3
a = 3 b = 4
You can use .format(), or simply a string starting with f' or F' to embed numbers in a text.
print('The weight is {0}g and the volume is {1}cc.'.format(a, b))
The weight is 3g and the volume is 4cc.
print(F'The weight is {a}g and the volume is {b}cc.')
The weight is 3g and the volume is 4cc.
You can specify the length by : for text, :d for integers, and :f for floating point numbers.
A newline is started by \n.
print(F'weight {a} g\n{'volume':10}{b:6d} cc\n{'density':10}{a/b:6.3f} g/cc')
weight 3 g
volume 4 cc
density 0.750 g/cc
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]
You can append an item to a list by append() function.
b.append(5)
b
[1, 2, 3, 4, 5]
Or `insert` an item at a specified positin.
Cell In[30], line 1
Or `insert` an item at a specified positin.
^
SyntaxError: invalid syntax
b.insert(2,0) # insert 0 after 2nd position
b
For lists, + means concatenation
b + b
You can create a nested list, like a matrix
A = [[1,2,3],[4,5,6]]
A
An item in a nested list can be picked by [ ][ ], but not [ , ]
A[1]
A[1][2]
#A[1,2] # this causes an error for a list
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 be exit by break statement.
for i in range(5):
j = i*i
if j>10:
print(j, 'is too large!')
break
print(i, j)
Or skip the rest and step to the next loop by continue
for i in range(10):
if i%3 > 0: # non-zero remainder?
continue
print(i) # only multiples of 3
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
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.
Installing a new package#
A python environment is usually set up with multiple packages, such as numpy.
You can check the currently installed packages by pip list, or conda list command if you installed conda.
!pip list
If you wand to install a new package, you can use pip install or conda install command.
For example, if you have not installed scikit-learn, which we will use later, you can install that from your console by
$ pip install scikit-learn
or
$ conda install scikit-learn
from your console.
!pip or !conda may not work if y/n input is required.