Python CrashCourse
Python CrashCourse
Spring 2020
Why?
• Popular programming language created in 1991 by Guido van Rossum
• Open-source, powerful and versatile
• Lots of online community support
• Lots of packages for all sorts of things
• Easy, readible and short syntax
• Works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc)
• Supports procedural, an object-orientated and functional programming
• Runs on an interpreter
Python 3.x installation (1)
• Note that there are two versions of Python: 2.x and 3.x.
We’ll be using the latter.
• The easiest way to install all you need and more is to use the
python Anaconda distribution from Continuum Analytics. It comes
with lots of installed packages for scientific computing,
engineering and data science.
2. Run the installer and follow all steps. (Don’t select « add to
PATH », nor « preferred install »)
Jupyter Notebook:
code editor
console
Spyder IDE
• https://python.org/
(documentation, tutorials, beginners guide, core distribution, ...)
• https://docs.python.org/3/tutorial/
• https://wiki.python.org/moin/BeginnersGuide/Programmers
• http://scipy.github.io/old-wiki/pages/NumPy_for_Matlab_Users.html
• http://cs231n.github.io/python-numpy-tutorial/
• http://wiki.python.org/moin/PythonBooks
docs.python.org/3/library/stdtypes.html
Sets
• set:
• an unordered collection of unique objects
• E.g. set(‘abc’), {‘a’, ’b’, ’c’}
• frozen set:
• like set, but immutable
• E.g. frozenset(‘abc’)
Python basics: indexing
Indexing:
>>> list1 = [‘a’, ‘b’, ‘c’]
>>> list1[0] [0] is the first item.
‘a’
>>> list1[1] [1] is the second item
‘b’
>>> list1[3] Out of range values
IndexError: list index out of range raise an exception
>>> list1[-1]
Negative values
‘c’
go backwards from
>>> list1[-2] the last element.
‘b’
Indexing applies also to tuples & strings
Python basics: slicing
Slicing:
>>> list1[:0] Slice from first to first element (non-inclusive)
[]
>>> list1.pop(1) Returns and removes second element from list1, which now has the
‘a’ following elements: ['e', 'b', 'c', 'd', 'a', 'b']; (to just remove: del list1[1]).
>>> list1.reverse()
['b', 'a', 'd', 'c', 'b', 'e'] Reverse elements in list1
>>> 'a' in dict1, 'd' in dict1 Test if dict1 has keys ‘a’ and ‘d’. Note that specification on a single line
(True, False) returns answer as tuple.
>>> dict1.get('d', 'unknown') Avoid KeyError and output some default value (e.g. ‘unknown’) by
‘unknown’ using the get() method.
>>> dict1.update({'d':4, 'e':5}) Update dict1 with new keys and values.
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Logical operators:
Python basics: control flow
For loop
• Repeats a set of statements over a group of values.
• Syntax:
• Example:
for a in range(1, 5): Output:
print(a**2) 1 4 9 16
• continue: skips the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next
iteration.
Note both keywords have default values that do not need to be specified when calling the function:
>>> print_msg_student3()
Hello Sarah!
Note that the order of the arguments does not matter. They are called by name:
>>> print_msg_student3(msg = 'Goodbye', name = 'Elly')
Goodbye Elly!
Python basics: functions
• Unknown number of positional arguments and one keyword argument:
def print_msg_student4(*names, msg = 'Hello'):
for name in names:
print(msg + " " + name + "!")
def get_even_numbers(numbers):
even = []
for number in numbers:
if number % 2 == 0:
even.append(number)
return even
• Syntax:
• Example:
>>> pow = lambda x,n: x**n
>>> pow(3,2)
9
Python basics: functions
• Scope:
Global variables are variables declared outside of the function or in the global scope. They can
be accessed inside or outside of the function.
Example:
>>> x = 10
>>> def foo():
print(2*x)
>>> foo()
20
>>> print(3*x)
Note that calling foo() does not have any effect on the
30
global x.
Python basics: functions
• Scope:
Global variables can be changed from inside a function by using the global keyword.
Example:
>>> x = 10
>>> def bar():
global x
x = x*2
print(x)
>>> print(x)
10
>>> bar()
20
>>> print(x)
20 Note that calling bar() does have an effect on the global x.
Python basics: functions
• Scope:
Local variables are variables declared inside the function’s body or in the local scope. They
cannot be accessed from outside of the function (or local scope).
Example:
>>> def foobar():
a = 2
>>> foobar()
>>> print(a)
NameError: name 'a' is not defined
>>> a = 3
>>> foobar()
>>> print(a)
3 Note that calling foobar() did not have any effect on the global a.
Python basics: classes
• Syntax:
class name:
"documentation"
statements
-or-
class name(base1, base2, ...): Class definition with inheritance
... from classes base1, base2, …
• Modules are used to break down large programs into small manageable
and organized files; and allow for code reusability.
def add(a,b):
""" Add a and b. """
return a + b
def subtract(a,b):
""" Subtract a and b. """
return a – b
>>> import example as basicmath Note that you have to add the new name of the
>>> basicmath.add(3,4.0) module in front of the function/method.
7.0
• Useful methods:
• open(filename, mode = ‘r’): open a file. Mode specifies what we want to do with
the file. E.g. ‘r’ (read), ‘w’ (write) or ‘a’ (append).
• read(n): read at most n characters. Read until end of file if n is negative or None.
• readline(n=-1): read and return one line from file. Return at most n bytes if specified.
• readlines(n=-1): read and return a list of lines from file. Return at most n
bytes/characters if specified.
• seek(offset, from = SEEK_SET): change position in file to offset bytes, in
reference to from.
• tell(): returns current file position.
• write(s): write string s to file and return number of characters written.
• writelines(lines): write a list of lines to the file.
Python basics: file I/O
Read lines one by one from input file and write them to an output file:
f_in = open(“in_file.txt")
f_out = open(“out_file.txt", "w")
for line in f_in:
f_out.write(line)
• The methods mentioned above are built-in. However, other specialized methods for
reading / writing images, arrays, dataframes, etc. from / to various file formats (e.g. csv,
txt, xlsx, …) exist as part of a variety of modules (pandas, numpy, scikit-image, …):
1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
5. Broadcasting
Python basics: Numpy (NUMerical PYthon)
Arrays:
• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays:
• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays:
• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays: basic properties
A NumPy array is a homogeneous collection of “items” of the same “data-type” (dtype)
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> np.ones((2,5))
• np.ones, np.zeros array([[1., 1., 1., 1., 1.],
• np.arange [1., 1., 1., 1., 1.]])
• np.concatenate
• np.astype >>> np.zeros((3,5))
array([[0., 0., 0., 0., 0.],
• np.zeros_like,
[0., 0., 0., 0., 0.],
np.ones_like [0., 0., 0., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> np.arange(5)
• np.ones, np.zeros array([0, 1, 2, 3, 4])
• np.arange >>> np.arrange(2,5)
• np.concatenate array([2, 3, 4])
• np.astype >>> np.arange(2,10, 2)
array([2, 4, 6, 8])
• np.zeros_like,
np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> A = np.ones((2,4))
• np.ones, np.zeros >>> B = np.zeros((3,4))
• np.arange >>> C = np.concatenate([A,B])
• np.concatenate array([[1., 1., 1., 1.],
• np.astype [1., 1., 1., 1.],
[0., 0., 0., 0.],
• np.zeros_like, [0., 0., 0., 0.],
np.ones_like [0., 0., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
• np.ones, np.zeros
>>> A = np.ones((4,1))
• np.arange
>>> B = np.zeros((4,2))
• np.concatenate >>> C = np.concatenate([A,B], axis = 1)
• np.astype array([[1., 0., 0.],
• np.zeros_like, [1., 0., 0.],
[1., 0., 0.],
np.ones_like [1., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> A = np.array([[1.2, 1.0, 1.5],
[-1.2, 1.0, 260.6]])
• np.ones, np.zeros >>> B = A.astype(np.int8)
• np.arange >>> B
array([[ 1, 1, 1],
• np.concatenate [-1, 1, 4]], dtype = int8)
• np.astype
• np.zeros_like, Note that np.int8 [-128, 127] repeats:
E.g. try
np.ones_like >>> np.arange(0,261, 1).astype(np.int8)
• np.random.random array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
…
121, 122, 123, 124, 125, 126, 127, -128, -127, -126,
… = 127, 128
-3, -2, -1, 0, 1, 2, 3, 4], dtype=int8)
= 255,256,257 = 260
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> type(A)
• np.ones, np.zeros numpy.ndarray
• np.arange >>> A.dtype
• np.concatenate dtype(‘float64')
• np.astype >>> B.dtype
dtype('int8')
• np.zeros_like,
np.ones_like type gives type of object
• np.random.random dtype gives type of elements of ndarray
Python basics: Numpy (NUMerical PYthon)
Arrays: creation >>> A = np.ones((2,3))
>>> B = np.zeros_like(A)
>>> B.shape
• np.ones, np.zeros
(2,3)
• np.arange >>> B
• np.concatenate array([[0., 0., 0.],
• np.astype [0., 0., 0.]])
>>> B.size
• np.zeros_like, 6
np.ones_like >>> B.ndim
• np.random.random 2
• np.ones, np.zeros
• np.arange >>> np.random.random((4,3))
• np.concatenate array([[0.27466025, 0.61004034, 0.94851734],
• np.astype [0.96894833, 0.00439395, 0.15156152],
[0.70234794, 0.76490032, 0.84585757],
• np.zeros_like,
[0.20728297, 0.50805883, 0.45459315]])
np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: (re-)shaping
>>> a = np.arange(6)
>>> a = a.reshape(3,2)
>>> a = a.reshape(2,-1)
>>> a = a.ravel()
>>> a = np.arange(10).reshape(5,2)
>>> a = a.transpose((1,0))
>>> a.shape
(2,5)
>>> a = a.T
>>> a.shape
(5,2)
column
row
>>> a[0,3:5]
array([3, 4])
0 1 2 3 4 5
10 11 12 13 14 15 >>> a[:,2]
array([2,12,22,32,42,52])
20 21 22 23 24 25
30 31 32 33 34 35 >>> a[4:,4:]
array([[44, 45],
40 41 42 43 44 45 [54, 55]])
50 51 52 53 54 55
>>> a[2::2,::2] #with strides of 2
array([[20, 22, 24],
[40, 42, 44]])
Python basics: Numpy (NUMerical PYthon)
>>> a = np.array([[0,1,2,3,4,5]]).T
Fancy array indexing + 10*np.array([[0,1,2,3,4,5]])
using lists/tuples and boolean masks
>>> a[(0,1,2,3,4),(1,2,3,4,5)]
array([ 1, 12, 23, 34, 45])
0 1 2 3 4 5
10 11 12 13 14 15 >>> a[3:,[0, 2, 5]]
array([[30, 32, 35],
20 21 22 23 24 25 [40, 42, 45]])
30 31 32 33 34 35 [50, 52, 55]])
When operating on multiple arrays, broadcasting rules are used to convert them to the same
shape.
4x3 3
0 0 0 0 1 2 0 0 0 0 1 2
0 1 2
10 10 10 10 10 10 0 1 2
+ = + = 10 11 12
20 20 20 20 20 20 0 1 2
20 21 22
30 30 30 30 30 30 0 1 2
stretch 30 31 32
4x1 3
0 0 1 2 0 0 0 0 1 2
10 10 10 10 0 1 2
+ = + =
20 20 20 20 0 1 2
30 30 30 30 0 1 2
stretch stretch
Python basics: Numpy (NUMerical PYthon)
Array broadcasting:
The trailing axes of both arrays must either be 1 or have the same size for broadcasting to occur.
Otherwise, a “ValueError: operands could not be broadcast together
with shapes (4,3) (4,)” exception is thrown.
mismatch!
4x3 4
0 0 0 0 1 2 3
10 10 10
+ =
20 20 20
30 30 30
Python basics: Numpy (NUMerical PYthon)
Array broadcasting:
>>> a = np.array((0,10,20,30))
>>> a.shape 0 0 1 2 0 1 2
(4,)
10 10 11 12
>>> b = np.array((0,1,2)) + =
20
>>> b.shape 20 21 22
(3,) 30 30 31 32
>>> y = a + b
ValueError: operands could not be
broadcast together with shapes (4,) (2,)
>>> y = a[:, None] + b
>>> a = np.array([1,2,3])
>>> b = np.array([10,100,1000])
>>> a * b
array([ 10, 200, 3000])
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array
>>> a = np.array([[0.28659319, 0.74356839, 0.06362368],
[0.77662682, 0.82634601, 0.33838486]])
>>> b = a < 0.5
>>> b
array([[False, True, False],
[ True, True, False]])
>>> b.dtype
dtype('bool')
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
• Arithmetic operations are element-wise >>> a = np.arange(4).reshape(2,-1)
• Logical operator return a bool array >>> a
array([[0, 1],
• In place operations modify the array [2, 3]])
>>> b = a + 1
>>> b
array([[1, 2],
[3, 4]])
>>> a *= b
>>> a
array([[ 0, 2],
[ 6, 12]])
Python basics: Numpy (NUMerical PYthon)
Math, upcasting:
Just as in Python and Java, the result of >>> a = np.array([0,1,3],dtype=np.uint64)
a math operator is cast to the more >>> b = np.array([0,1,3],dtype=np.uint16)
general or precise datatype.
>>> c = a + b
• uint64 + uint16 => uint64 >>> c.dtype
dtype(‘uint64')
• float32 / int32 => float32
• float64 * int8 => float64 >>> a = np.array([0,1,3],dtype=np.float32)
• … >>> b = np.array([0,1,3],dtype=np.int32)
>>> c = a + b
Warning: upcasting does not prevent >>> c.dtype
overflow/underflow. You must dtype('float32')
manually cast first.
Python basics: Numpy (NUMerical PYthon)
Math, universal functions:
• Also called ufuncs >>> a = np.array([0,1,3],dtype=np.float64)
• Element-wise >>> b = np.sqrt(a)
>>> b
• Examples: array([0. , 1. , 1.73205081])
❑np.exp
❑np.sqrt
❑np.sin
❑np.cos
❑np.isnan
Python basics: Numpy (NUMerical PYthon)
Arrays axes:
>>> np.savez('data.npz', a = a)
>>> data = np.load('data.npz')
>>> a = data['a']
Shape operations:
• a.flat() – An iterator to step through array as if it is 1D.
• a.flatten() – Returns a 1D copy of a multi-dimensional array.
• a.ravel() – Same as flatten(), but returns a ‘view’ if possible.
• a.resize(new_size) – Change the size/shape of an array in-place.
• a.swapaxes(axis1, axis2) – Swap the order of two axes in an array.
a.transpose(*axes) – Swap the order of any number of array axes.
• a.T – Shorthand for a.transpose()
• a.squeeze() – Remove any length=1 dimensions from an array.
Python basics: Numpy (NUMerical PYthon)
Fill and copy:
• a.copy() – Return a copy of the array.
• a.fill(value) – Fill array with a scalar value.
Conversion / coersion:
• a.tolist() – Convert array into nested lists of values.
• a.tostring() – raw copy of array memory into a python string.
• a.astype(dtype) – Return array coerced to given dtype.
Complex numbers:
• a.real – Return the real part of the array.
• a.imag – Return the imaginary part of the array.
• a.conjugate() – Return the complex conjugate of the array.
• a.conj() – Return the complex conjugate of an array.(same as conjugate)
Python basics: Numpy (NUMerical PYthon)
Saving:
• a.dump(file) – Store a binary array data out to the given file.
• a.dumps() – returns the binary pickle of the array as a string.
• a.tofile(fid, sep="", format="%s") Formatted ascii output to file.
Search / sort:
• a.nonzero() – Return indices for all non-zero elements in a.
• a.sort(axis=-1) – Inplace sort of array elements along axis.
• a.argsort(axis=-1) – Return indices for element sort order along axis.
• a.searchsorted(b) – Return index where elements from b would go in a.
Multiple lines:
>>> plt.plot(x,y,x2,y2)
>>> plt.xlabel(‘radians’)
Python basics: Matplotlib (plotting)
• Line plots
Line formatting:
# red, dot-dash, triangles
>>> plt.plot(x,np.sin(x),'r-^')
>>> plt.subplot(212)
>>> plt.plot(t3, cos(2*pi*t3), 'r.')
>>> plt.grid(True)
>>> plt.xlabel('time (s)')
>>> plt.ylabel('Undamped')
>>> plt.show()