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), 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
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 = 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]
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
A list can contain different types of items with different lengths.
a = [1, 2, 3.14, 'apple', "orange", [1, 2]]
a
[1, 2, 3.14, 'apple', 'orange', [1, 2]]
When you assign a list to another list, only the pointer is copied.
a = [1, 2, 3]
b = a
b[1] = 4
a
[1, 4, 3]
When you want to copy the content, use [:]
a = [1, 2, 3]
b = a[:]
b[1] = 4
a
[1, 2, 3]
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']
9040495
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
1
There is a shorthand notation with the condition in the middle:
x if x>0 else 0
1
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)
0 0
1 1
2 3
3 6
4 10
You can specify start, end and interval.
for i in range(3,9,2):
print(i)
3
5
7
for
loop can also be over a list.
a = [1, 2, 3]
for x in a:
print(x**2)
1
4
9
s = "hello"
for c in s: # characters in a string
print(c)
h
e
l
l
o
s = ["hello", "goodby"]
for c in s: # strings in a list
print(c)
hello
goodby
enumerate()
function gives pairs of index and content of a list.
for i, c in enumerate(s):
print(i, c)
0 hello
1 goodby
You can also apply a for loop for a dictionary.
for k in postal: # get the key
print('%8s:'%k, postal[k])
onna: 9040411
tancha: 9040412
fuchaku: 9040413
oist: 9040495
# get key-value pair
for (k, v) in postal.items():
print('{0:8s}: {1}'.format(k, v))
onna : 9040411
tancha : 9040412
fuchaku : 9040413
oist : 9040495
List ‘comprehension’#
There is a quick way of constructing a list from a for loop.
y = [x**2 for x in range(5)]
y
[0, 1, 4, 9, 16]
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
array([1, 2, 3])
type(b)
numpy.ndarray
type(b[0])
numpy.int64
Like a list, the index starts from zero
b[1]
np.int64(2)
Operators work component-wise.
b + b
array([2, 4, 6])
b * b
array([1, 4, 9])
b + 1 # broadcast
array([2, 3, 4])
arange()
gives an evenly spaced array.
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0, 10, 0.5)
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,
6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
linspace()
gives an array including the last point.
np.linspace(0, 10)
array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,
1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,
2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,
3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,
4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,
5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,
6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,
7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,
8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,
9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
np.linspace(0, 10, num=11)
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Nested array#
You can make a matrix as a nested array.
A = np.array([[1,2],[3,4]])
A
array([[1, 2],
[3, 4]])
Components can be accessed by [ , ]
A[1][1]
np.int64(4)
A[1,0] # this if fine for a numpy ndarray, not for a regular list
np.int64(3)
Take the first row
A[0]
array([1, 2])
A[0,:]
array([1, 2])
Take the second column
A[:,1]
array([2, 4])
Component-wise arithmetics
A + A
array([[2, 4],
[6, 8]])
A * A
array([[ 1, 4],
[ 9, 16]])
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]]
array([[ 7, 10],
[15, 22]])
Common matrices#
np.zeros([2,3])
array([[0., 0., 0.],
[0., 0., 0.]])
np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
np.empty([3,2]) # the contents are not specified
array([[0., 0.],
[0., 0.],
[0., 0.]])
np.empty([3,2], dtype=int) # to specify the data type
array([[0, 0],
[0, 0],
[0, 0]])
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
'/Users/doya/OIST Dropbox/kenji doya/Python/iSciComp'
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
/Users/doya/py/iSciComp
!hostname
starfishM4.local
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
Haisai!
Mensore!
with open('haisai.txt', 'r') as f:
s = f.read()
print(s)
Haisai!
Mensore!
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
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]]
np.savetxt("square.txt", X) # by default, delimited by a space
%cat square.txt
0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00
2.000000000000000000e+00 4.000000000000000000e+00
3.000000000000000000e+00 9.000000000000000000e+00
4.000000000000000000e+00 1.600000000000000000e+01
Another common format is CSV, comma-separated value.
np.savetxt("square.csv", X, delimiter=",", fmt="%1d, %.5f")
%ls s*
square.csv square.txt
%cat square.csv
0, 0.00000
1, 1.00000
2, 4.00000
3, 9.00000
4, 16.00000
Y = np.loadtxt("square.txt")
Y
array([[ 0., 0.],
[ 1., 1.],
[ 2., 4.],
[ 3., 9.],
[ 4., 16.]])
Y = np.loadtxt("square.csv", delimiter=",")
Y
array([[ 0., 0.],
[ 1., 1.],
[ 2., 4.],
[ 3., 9.],
[ 4., 16.]])
Getting help#
Python offers several ways of getting help.
help()
Welcome to Python 3.13's help utility! If this is your first time using
Python, you should definitely check out the tutorial at
https://docs.python.org/3.13/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To get a list of available
modules, keywords, symbols, or topics, enter "modules", "keywords",
"symbols", or "topics".
Each module also comes with a one-line summary of what it does; to list
the modules whose name or summary contain a given string such as "spam",
enter "modules spam".
To quit this help utility and return to the interpreter,
enter "q", "quit" or "exit".
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
Cell In[90], line 1
----> 1 help()
File <frozen _sitebuiltins>:103, in __call__(self, *args, **kwds)
File ~/miniforge3/lib/python3.13/pydoc.py:2006, in Helper.__call__(self, request)
2004 else:
2005 self.intro()
-> 2006 self.interact()
2007 self.output.write('''
2008 You are now leaving help and returning to the Python interpreter.
2009 If you want to ask for help on a particular object directly from the
2010 interpreter, you can type "help(object)". Executing "help('string')"
2011 has the same effect as typing a particular string at the help> prompt.
2012 ''')
File ~/miniforge3/lib/python3.13/pydoc.py:2018, in Helper.interact(self)
2016 while True:
2017 try:
-> 2018 request = self.getline('help> ')
2019 if not request: break
2020 except (KeyboardInterrupt, EOFError):
File ~/miniforge3/lib/python3.13/pydoc.py:2038, in Helper.getline(self, prompt)
2036 """Read one line, using input() when appropriate."""
2037 if self.input is sys.stdin:
-> 2038 return input(prompt)
2039 else:
2040 self.output.write(prompt)
File ~/miniforge3/lib/python3.13/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt)
1279 if not self._allow_stdin:
1280 msg = "raw_input was called, but this frontend does not support input requests."
-> 1281 raise StdinNotImplementedError(msg)
1282 return self._input_request(
1283 str(prompt),
1284 self._parent_ident["shell"],
1285 self.get_parent("shell"),
1286 password=False,
1287 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
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.