from IPython.core.display import HTML
css_file = './stylesheets/custom.css'
HTML(open(css_file, "r").read())
Parts of this tutorial re-use Scientific Python lectures by Robert Johansson linsensed under Creative Commons Attribution 3.0.
Presented on 26 February, 2017.
Prerequisites: No prerequisites.
import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
A Large community of users makes it easy to find help and documentation.
An extensive ecosystem of scientific libraries and environments
Great performance due to close integration with time-tested and highly optimized codes written in C and Fortran: BLAS, ATLAS BLAS, LAPACK, ARCPACK, ...
Good support for
Readily available and suitable for use on high-performance computing clusters.
No license costs, no unnecessary use of research budget.
Python code is usually stored in text files with the file ending ".py
":
myprogram.py
Every line in a Python program file is assumed to be a Python statement, or part thereof.
#
(optionally preceded by an arbitrary number of white-space characters, i.e., tabs or spaces). Comment lines are usually ignored by the Python interpreter.To run our Python program from the command line we use:
$ python myprogram.py
On UNIX systems it is common to define the path to the interpreter on the first line of the program (note that this is a comment line as far as the Python interpreter is concerned):
#!/usr/bin/env python
If we do, and if we additionally set the file script to be executable, we can run the program like this:
$ myprogram.py
This file - an IPython notebook - does not follow the standard pattern with Python code in a text file. Instead, an IPython notebook is stored as a file in the JSON format. The advantage is that we can mix formatted text, Python code and code output. It requires the IPython notebook server to run it though, and therefore isn't a stand-alone Python program as described above. Other than that, there is no difference between the Python code that goes into a program file or an IPython notebook.
First, let's see some useful shortcuts for IPython notebook:
IPython has a number of so-called magic commands. The following is an example.
%%timeit
x = range(10000)
sum(x)
1000 loops, best of 3: 184 µs per loop
Most of the functionality in Python is provided by modules. The Python Standard Library is a large collection of modules that provides cross-platform implementations of common facilities such as access to the operating system, file I/O, string management, network communication, and much more.
To use a module in a Python program it first has to be imported. A module can be imported using the import
statement. For example, to import the module math
, which contains many standard mathematical functions, we can do:
import math
This includes the whole module and makes it available for use later in the program. For example, we can do:
import math
x = math.cos(2 * math.pi)
print(x)
1.0
Alternatively, we can chose to import all symbols (functions and variables) in a module to the current namespace (so that we don't need to use the prefix "math.
" every time we use something from the math
module:
from math import *
x = cos(2 * pi)
print(x)
1.0
This pattern can be very convenient, but in large programs that include many modules it is often a good idea to keep the symbols from each module in their own namespaces, by using the import math
pattern. This would elminate potentially confusing problems with name space collisions.
As a third alternative, we can chose to import only a few selected symbols from a module by explicitly listing which ones we want to import instead of using the wildcard character *
:
from math import cos, pi
x = cos(2 * pi)
print(x)
1.0
It is also possible to rename the function or the imported module
from math import cos as cos1
import math as ma
x = cos1(2 * ma.pi)
print(x)
1.0
Once a module is imported, we can list the symbols it provides using the dir
function:
import math
print(dir(math))
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
And using the function help
we can get a description of each function (almost .. not all functions have docstrings, as they are technically called, but the vast majority of functions are documented this way).
help(math.log)
Help on built-in function log in module math: log(...) log(x[, base]) Return the logarithm of x to the given base. If the base not specified, returns the natural logarithm (base e) of x.
math.log?
log(10)
2.302585092994046
Some very useful modules form the Python standard library are os
, sys
, math
, shutil
, re
, subprocess
, multiprocessing
, threading
.
A complete lists of standard modules for Python 2 and Python 3 are available at http://docs.python.org/2/library/ and http://docs.python.org/3/library/, respectively.
Printing "Hello World!" is the standard first exercise in learning a new programming language. Use the command 'print' to do this.
# %load solutions/session1/hello-world.py
print("Hello World!")
Hello World!
Variable names in Python can contain alphanumerical characters a-z
, A-Z
, 0-9
and some special characters such as _
. Normal variable names must start with a letter.
By convension, variable names start with a lower-case letter, and Class names start with a capital letter.
In addition, there are a number of Python keywords that cannot be used as variable names. These keywords are:
and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield
Note: Be aware of the keyword lambda
, which could easily be a natural variable name in a scientific program. But being a keyword, it cannot be used as a variable name.
The assignment operator in Python is =
. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.
Assigning a value to a new variable creates the variable:
# variable assignments
x = 1.0
my_variable = 12.2
Although not explicitly specified, a variable do have a type associated with it. The type is derived form the value it was assigned.
type(x)
float
If we assign a new value to a variable, its type can change.
x = 1
type(x)
int
If we try to use a variable that has not yet been defined we get an NameError
:
try:
print(y)
except:
print("y not defined")
y not defined
# integers
x = 1
type(x)
int
# float
x = 1.0
type(x)
float
# boolean
b1 = True
b2 = False
type(b1)
bool
# complex numbers: note the use of `j` to specify the imaginary part
x = 1.0 - 1.0j
type(x)
complex
print(x)
(1-1j)
print(x.real, x.imag)
1.0 -1.0
The module types
contains a number of type name definitions that can be used to test if variables are of certain types:
import types
# print all types defined in the `types` module
print(dir(types))
['AsyncGeneratorType', 'BuiltinFunctionType', 'BuiltinMethodType', 'CodeType', 'CoroutineType', 'DynamicClassAttribute', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'LambdaType', 'MappingProxyType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'SimpleNamespace', 'TracebackType', '_GeneratorWrapper', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_ag', '_calculate_meta', '_collections_abc', '_functools', 'coroutine', 'new_class', 'prepare_class']
x = 1.0
# check if the variable x is a float
type(x) is float
True
# check if the variable x is an int
type(x) is int
False
We can also use the isinstance
method for testing types of variables:
isinstance(x, float)
True
x = 1.5
print(x, type(x))
1.5 <class 'float'>
x = int(x)
print(x, type(x))
1 <class 'int'>
z = complex(x)
print(z, type(z))
(1+0j) <class 'complex'>
try:
x = float(z)
except:
print("can't convert float to complex")
can't convert float to complex
x = abs(z) # but we can find the absolute value (length) of a complex number
print(x)
1.0
Most operators and comparisons in Python work as one would expect:
+
, -
, *
, /
, //
(integer division), '**' power1 + 2, 1 - 2, 1 * 2, 1 / 2, 3 // 2
(3, -1, 2, 0.5, 1)
1.0 + 2.0, 1.0 - 2.0, 1.0 * 2.0, 1.0 / 2.0
(3.0, -1.0, 2.0, 0.5)
# Integer division of float numbers
3.0 // 2.0
1.0
# Note! The power operators in python isn't ^, but **
2 ** 2
4
and
, not
, or
.True and False
False
not False
True
True or False
True
>
, <
, >=
(greater or equal), <=
(less or equal), ==
equality, !=
inequality, is
identical.2 > 1, 2 < 1
(True, False)
2 > 2, 2 < 2
(False, False)
2 >= 2, 2 <= 2
(True, True)
# equality
[1,2] == [1,2]
True
# inequality
1 != 1
False
# objects identical?
l1 = [1,2]
l2 = l1
l1 is l2
True
Use Python to evaluate the following expression, for the given variables' values, to get the correct results and print the output
(5 * (a > b) + 2 * (a < b*4) ) / a * c
Note: you will need to use some variables casting
a = 15
b = 5
c = 2.2
# expression evaluation here
val = 0
print(val)
0
# %load solutions/session1/operators.py
a = 15
b = 5
c = 2.2
# expression evaluation here
val = (5 * (a > b) + 2 * (a < b*4) ) / a * c
print(val)
1.0266666666666668
s = "Hello world"
type(s)
str
# length of the string: the number of characters
len(s)
11
# replace a substring in a string with something else
s2 = s.replace("world", "test")
print(s2)
Hello test
We can index a character in a string using []
:
s[0]
'H'
Heads up MATLAB users: Indexing start at 0!
We can extract a part of a string using the syntax [start:stop]
, which extracts characters between index start
and stop
.
NOTE: the start
index is included, the stop
is excluded.
s[0:5]
'Hello'
If we omit either (or both) of start
or stop
from [start:stop]
, the default is the beginning and the end of the string, respectively:
s[:5]
'Hello'
s[6:]
'world'
s[:]
'Hello world'
Negative indexing from the end of the string:
s[:-2]
'Hello wor'
s[-1], s[-2]
('d', 'l')
We can also define the step size using the syntax [start:end:step]
(the default value for step
is 1, as we saw above):
s[::1]
'Hello world'
s[::2]
'Hlowrd'
These technique is called slicing. Read more about the syntax here: http://docs.python.org/release/2.7.3/library/functions.html?highlight=slice#slice
Some examples of this subsection are brought from http://docs.python.org/2/tutorial/inputoutput.html
Many operations can be performed over the strings in python. The following command shows the available string operations provided by str objects.
print(dir(str))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
The following code shows an example of using the c-style formatting. More formatting specifiers can be found in a c language documentation. A good source: http://www.cplusplus.com/reference/cstdio/printf/.
import math
print('The value of %5s is approximately %5.3f.' % ('PI', math.pi))
The value of PI is approximately 3.142.
The following code shows examples of using the format
function to control the formatting of the input values to the string
print('{0} and {1}'.format('spam', 'eggs'))
print('{1} and {0}'.format('spam', 'eggs'))
spam and eggs eggs and spam
print( 'This {food} is {adjective}.'.format(food='spam', adjective='absolutely horrible') )
This spam is absolutely horrible.
print( 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', other='Georg') )
The story of Bill, Manfred, and Georg.
import math
print( 'The value of PI is approximately {0:.3f}.'.format(math.pi) )
The value of PI is approximately 3.142.
Python has a very rich set of functions for text processing. See for example http://docs.python.org/2/library/string.html for more information.
Use string built-in functions and sliding operation to perform the following over the text of Hello world
o
with a
. (Hint: use the 'replace' built-in function in the string)s = "Hello world!"
# %load solutions/session1/strings.py
s = "Hello World"
s = s.replace('o', 'a')
print(s)
s = s[:5].upper() + s[5:].lower()
print(s)
print(s[4:-1])
Hella Warld HELLA warld A warl
Lists are very similar to strings, except that each element can be of any type.
The syntax for creating lists in Python is [...]
:
l = [1,2,3,4]
print(type(l))
print(l)
<class 'list'> [1, 2, 3, 4]
We can use the same slicing techniques to manipulate lists as we could use on strings:
print(l)
print(l[1:3])
print(l[::2])
[1, 2, 3, 4] [2, 3] [1, 3]
Elements in a list do not all have to be of the same type:
l = [1, 'a', 1.0, 1-1j]
print(l)
[1, 'a', 1.0, (1-1j)]
Python lists can be inhomogeneous and arbitrarily nested:
nested_list = [1, [2, [3, [4, [5]]]]]
print(nested_list)
[1, [2, [3, [4, [5]]]]]
Accessing elements in nested lists
nl = [1, [2, 3, 4], [5, [6, 7, 8]]]
print(nl)
print(nl[0])
print(nl[1][1])
print(nl[2][1][2])
[1, [2, 3, 4], [5, [6, 7, 8]]] 1 3 8
Lists play a very important role in Python, and are for example used in loops and other flow control structures (discussed below). There are number of convenient functions for generating lists of various types, for example the range
function:
start = 10
stop = 30
step = 2
print(list(range(start, stop, step)))
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
# convert a string to a list by type casting:
s2 = list(s)
print(s2)
['H', 'E', 'L', 'L', 'A', ' ', 'w', 'a', 'r', 'l', 'd']
# sorting lists
s2.sort()
print(s2)
[' ', 'A', 'E', 'H', 'L', 'L', 'a', 'd', 'l', 'r', 'w']
# create a new empty list
l = []
# add an elements using `append`
l.append("A")
l.append("d")
l.append("d")
print(l)
['A', 'd', 'd']
l[1:3] = ["d", "d"]
print(l)
['A', 'd', 'd']
Remove an element at a specific location using del
:
del l[0]
print(l)
['d', 'd']
Using operators with lists
l1 = [1, 2, 3] + [4, 5, 6]
print(l1)
l2 = [1, 2, 3] * 2
print(l2)
[1, 2, 3, 4, 5, 6] [1, 2, 3, 1, 2, 3]
See help(list)
for more details, or read the online documentation
Perform the following list operations and print the final output
-1
Hello
after the first element
# %load solutions/session1/lists.py
l = list(range(5, 17, 2))
print(l)
l[-1] = list(range(4, 10, 2))
print(l)
l[-1][-2] = -1
print(l)
del l[1:3]
print(l)
l.insert(1, "Hello")
print(l)
[5, 7, 9, 11, 13, 15] [5, 7, 9, 11, 13, [4, 6, 8]] [5, 7, 9, 11, 13, [4, -1, 8]] [5, 11, 13, [4, -1, 8]] [5, 'Hello', 11, 13, [4, -1, 8]]
Dictionaries are also like lists, except that each element is a key-value pair. The syntax for dictionaries is {key1 : value1, ...}
:
params = {"parameter1" : 1.0,
"parameter2" : 2.0,
"parameter3" : 3.0,
1: 4.0,
(5, 'ho'): 'hi'}
print(type(params))
print(params)
<class 'dict'> {'parameter1': 1.0, 'parameter2': 2.0, 'parameter3': 3.0, 1: 4.0, (5, 'ho'): 'hi'}
print("parameter1 = " + str(params["parameter1"]))
print("parameter2 = " + str(params["parameter2"]))
print("parameter3 = " + str(params["parameter3"]))
parameter1 = 1.0 parameter2 = 2.0 parameter3 = 3.0
params["parameter1"] = "A"
params["parameter2"] = "B"
# add a new entry
params["parameter4"] = "D"
print("parameter1 = " + str(params["parameter1"]))
print("parameter2 = " + str(params["parameter2"]))
print("parameter3 = " + str(params["parameter3"]))
print("parameter4 = " + str(params["parameter4"]))
print("'key 1' = " + str(params[1]))
print("'key (5, 'ho')' = " + str(params[(5, 'ho')]))
parameter1 = A parameter2 = B parameter3 = 3.0 parameter4 = D 'key 1' = 4.0 'key (5, 'ho')' = hi
del params["parameter2"]
print(params)
{'parameter1': 'A', 'parameter3': 3.0, 1: 4.0, (5, 'ho'): 'hi', 'parameter4': 'D'}
Create a dictionary that uses a string "first last" name of the person as a key and his/her corresponding age as a value for the following list of people
Perform the following updates to the dictionary
# %load solutions/session1/dicts.py
d = dict()
d["John Smith"] = 30
d["Ahmad Said"] = 22
d["Sara John"] = 2
print(d)
d["John Smith"] += 1
d["Ahmad Ahmad"] = 19
del d["Sara John"]
print(d)
{'John Smith': 30, 'Ahmad Said': 22, 'Sara John': 2} {'John Smith': 31, 'Ahmad Said': 22, 'Ahmad Ahmad': 19}
The Python syntax for conditional execution of code use the keywords if
, elif
(else if), else
:
statement1 = False
statement2 = False
if statement1:
print("statement1 is True")
elif statement2:
print("statement2 is True")
else:
print("statement1 and statement2 are False")
statement1 and statement2 are False
For the first time, here we encounted a peculiar and unusual aspect of the Python programming language: Program blocks are defined by their indentation level.
Compare to the equivalent C code:
if (statement1)
{
printf("statement1 is True\n");
}
else if (statement2)
{
printf("statement2 is True\n");
}
else
{
printf("statement1 and statement2 are False\n");
}
In C blocks are defined by the enclosing curly brakets {
and }
. And the level of indentation (white space before the code statements) does not matter (completely optional).
But in Python, the extent of a code block is defined by the indentation level (usually a tab or say four white spaces). This means that we have to be careful to indent our code correctly, or else we will get syntax errors.
Examples:
statement1 = False
if statement1:
print("printed if statement1 is True")
print("still inside the if block")
if statement1:
print("printed if statement1 is True")
print("now outside the if block")
now outside the if block
# a compact way for using the if statement
a = 2 if statement1 else 4
print("a =", a)
a = 4
name = 'john'
if name in ['jed', 'john']:
print("We have Jed or John")
num = 1
if num in [1, 2]:
print("We have 1 or 2")
We have Jed or John We have 1 or 2
In Python, loops can be programmed in a number of different ways. The most common is the for
loop, which is used together with iterable objects, such as lists. The basic syntax is:
for
loops:
for x in [1,2,3]:
print(x)
1 2 3
The for
loop iterates over the elements of the supplied list, and executes the containing block once for each element. Any kind of list can be used in the for
loop. For example:
for x in range(4): # by default range start at 0
print(x)
0 1 2 3
Note: range(4)
does not include 4 !
for x in range(-3,3):
print(x)
-3 -2 -1 0 1 2
for word in ["scientific", "computing", "with", "python"]:
print(word)
scientific computing with python
To iterate over key-value pairs of a dictionary:
for key, value in params.items():
print(str(key) + " : " + str(value))
parameter1 : A parameter3 : 3.0 1 : 4.0 (5, 'ho') : hi parameter4 : D
Sometimes it is useful to have access to the indices of the values when iterating over a list. We can use the enumerate
function for this:
for idx, x in enumerate(range(-3,3)):
print(idx, x)
0 -3 1 -2 2 -1 3 0 4 1 5 2
while
loops:
i = 0
while i < 5:
print(i)
i = i + 1
print("done")
0 1 2 3 4 done
Note that the print("done")
statement is not part of the while
loop body because of the difference in indentation.
Loop through the following list of words and create a dictionary according to the following rules:
words = ["Aerial", "Affect", "Agile", "Agriculture", "Animal", "Attract", "Audubon",
"Backyard", "Barrier", "Beak", "Bill", "Birdbath", "Branch", "Breed", "Buzzard",
"The", "On", "Upper", "Not", "What", "Linked", "Up", "In", "A", "lol"]
# %load solutions/session1/control_flow.py
d = dict()
for w in words:
n = len(w)
if n > 2:
if n not in d:
d[n] = []
d[n].append(w)
for i, l in d.items():
print(i, l)
6 ['Aerial', 'Affect', 'Animal', 'Branch', 'Linked'] 5 ['Agile', 'Breed', 'Upper'] 11 ['Agriculture'] 7 ['Attract', 'Audubon', 'Barrier', 'Buzzard'] 8 ['Backyard', 'Birdbath'] 4 ['Beak', 'Bill', 'What'] 3 ['The', 'Not', 'lol']
A function in Python is defined using the keyword def
, followed by a function name, a signature within parentheses ()
, and a colon :
. The following code, with one additional level of indentation, is the function body.
def func0():
print("test")
func0()
test
Optionally, but highly recommended, we can define a so called "docstring", which is a description of the functions purpose and behaivor. The docstring should follow directly after the function definition, before the code in the function body.
def func1(s):
# Print a string 's' and tell how many characters it has
print(s + " has " + str(len(s)) + " characters")
return 1, 2, 3
help(func1)
Help on function func1 in module __main__: func1(s)
func1("test")
test has 4 characters
(1, 2, 3)
We can return multiple values from a function using tuples (see above):
def powers(x):
# Return a few powers of x.
return x ** 2, x ** 3, x ** 4
powers(3)
(9, 27, 81)
x2, x3, _ = powers(3)
print(x3)
27
In a definition of a function, we can give default values to the arguments the function takes:
def myfunc(x, p=2, debug=False):
if debug:
print("evaluating myfunc for x = " + str(x) + " using exponent p = " + str(p))
return x**p
If we don't provide a value of the debug
argument when calling the the function myfunc
it defaults to the value provided in the function definition:
myfunc(5)
25
myfunc(5, debug=True)
evaluating myfunc for x = 5 using exponent p = 2
25
If we explicitly list the name of the arguments in the function calls, they do not need to come in the same order as in the function definition. This is called keyword arguments, and is often very useful in functions that takes a lot of optional arguments.
myfunc(p=3, debug=True, x=7)
evaluating myfunc for x = 7 using exponent p = 3
343
Copyright 2017, ACM/SIAM Student Chapter of King Abdullah University of Science and Technology