In this talk we use Python 2.7
more details in https://docs.python.org/2/library/stdtypes.html
t, f = True, False
print type(t)
print t and f # Logical AND;
print t or f # Logical OR;
print not t # Logical NOT;
print t != f # Logical XOR;
<type 'bool'> False True False True
x = 3
print x, type(x)
print x + 1 # Addition;
print x - 1 # Subtraction;
print x * 2 # Multiplication;
print x ** 2 # Exponentiation;
3 <type 'int'> 4 2 6 9
x += 1
print x # Prints "4"
x *= 2
print x # Prints "8"
4 8
y = 2.5
print type(y) # Prints "<type 'float'>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
<type 'float'> 2.5 3.5 5.0 6.25
hello = 'hello' # String literals can use single quotes
world = "world" # or double quotes; it does not matter.
print hello, len(hello)
hello 5
hw = hello + ' ' + world # String concatenation
print hw # prints "hello world"
hello world
hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
print hw12 # prints "hello world 12"
hello world 12
s = "hello"
print s.capitalize() # Capitalize a string; prints "Hello"
print s.upper() # Convert a string to uppercase; prints "HELLO"
print s.rjust(7) # Right-justify a string, padding with spaces; prints " hello"
print s.center(7) # Center a string, padding with spaces; prints " hello "
print s.replace('l', '(ell)') # Replace all instances of one substring with another;
# prints "he(ell)(ell)o"
print ' world '.strip() # Strip leading and trailing whitespace; prints "world"
Hello HELLO hello hello he(ell)(ell)o world
xs = [3, 1, 2] # Create a list
print xs, xs[2]
print xs[-1] # Negative indices count from the end of the list; prints "2"
[3, 1, 2] 2 2
xs[2] = 'foo' # Lists can contain elements of different types
print xs
[3, 1, 'foo']
xs.append('bar') # Add a new element to the end of the list
print xs
[3, 1, 'foo', 'bar']
x = xs.pop() # Remove and return the last element of the list
print x, xs
bar [3, 1, 'foo']
# Slicing - concise syntax to access sublists
nums = range(5) # range is a built-in function that creates a list of integers
print nums # Prints "[0, 1, 2, 3, 4]"
print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print nums # Prints "[0, 1, 8, 8, 4]"
[0, 1, 2, 3, 4] [2, 3] [2, 3, 4] [0, 1] [0, 1, 2, 3, 4] [0, 1, 2, 3] [0, 1, 8, 9, 4]
#Loop through List
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
#1: cat #2: dog #3: monkey
# List comprehensions
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x ** 2)
print squares
[0, 1, 4, 9, 16]
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print squares
[0, 1, 4, 9, 16]
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares
[0, 4, 16]
# Set
animals = {'cat', 'dog'}
print 'cat' in animals # Check if an element is in a set; prints "True"
print 'fish' in animals # prints "False"
animals.add('fish') # Add an element to a set
print 'fish' in animals # Prints "True"
print len(animals) # Number of elements in a set; prints "3"
animals.add('cat') # Adding an element that is already in the set does nothing
print len(animals) # Prints "3"
animals.remove('cat') # Remove an element from a set
print len(animals) # Prints "2"
True False True 3 3 2
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
# Prints "#1: fish", "#2: dog", "#3: cat"
#1: fish #2: dog #3: cat
# Dictionary
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print d['cat'] # Get an entry from a dictionary; prints "cute"
print 'cat' in d # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet' # Set an entry in a dictionary
print d['fish'] # Prints "wet"
print d['monkey'] # KeyError: 'monkey' not a key of d
cute True wet
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-45-4d04a79d0baa> in <module>() 6 d['fish'] = 'wet' # Set an entry in a dictionary 7 print d['fish'] # Prints "wet" ----> 8 print d['monkey'] # KeyError: 'monkey' not a key of d KeyError: 'monkey'
print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
del d['fish'] # Remove an element from a dictionary
print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"
N/A wet N/A
# Loop through dictionary
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print 'A %s has %d legs' % (animal, legs)
print '------'
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.iteritems():
print 'A %s has %d legs' % (animal, legs)
A person has 2 legs A spider has 8 legs A cat has 4 legs ------ A person has 2 legs A spider has 8 legs A cat has 4 legs
# Dictionary comprehensions
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print even_num_to_square
{0: 0, 2: 4, 4: 16}
# Tuple
d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys
t = (5, 6) # Create a tuple
print type(t)
print d[t]
print d[(1, 2)]
<type 'tuple'> 5 1
# Tuple cannot be mutated
# Thus Error Expected Here
t[0] = 1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-70-9e8806da6510> in <module>() 2 # Thus Error Expected Here 3 ----> 4 t[0] = 1 TypeError: 'tuple' object does not support item assignment
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
Installing: https://docs.scipy.org/doc/numpy/user/install.html
Details see https://docs.scipy.org/doc/numpy-dev/user/index.html Tutorial for Matlab users: http://wiki.scipy.org/NumPy_for_Matlab_Users
import numpy as np
a = np.array([1, 2, 3]) # Create a rank 1 array
print type(a) # Prints "<type 'numpy.ndarray'>"
print a.shape # Prints "(3,)"
print a[0], a[1], a[2] # Prints "1 2 3"
a[0] = 5 # Change an element of the array
print a # Prints "[5, 2, 3]"
b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
print b.shape # Prints "(2, 3)"
print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"
<type 'numpy.ndarray'> (3,) 1 2 3 [5 2 3] (2, 3) 1 2 4
a = np.zeros((2,2)) # Create an array of all zeros
print a # Prints "[[ 0. 0.]
# [ 0. 0.]]"
b = np.ones((1,2)) # Create an array of all ones
print b # Prints "[[ 1. 1.]]"
c = np.full((2,2), 7.) # Create a constant array
print c # Prints "[[ 7. 7.]
# [ 7. 7.]]"
d = np.eye(2) # Create a 2x2 identity matrix
print d # Prints "[[ 1. 0.]
# [ 0. 1.]]"
e = np.random.random((2,2)) # Create an array filled with random values
print e # Might print "[[ 0.91940167 0.08143941]
# [ 0.68744134 0.87236687]]"
[[ 0. 0.] [ 0. 0.]] [[ 1. 1.]] [[ 7. 7.] [ 7. 7.]] [[ 1. 0.] [ 0. 1.]] [[ 0.39743312 0.11084661] [ 0.43026082 0.90327968]]
# Numpy array indexing
import numpy as np
# Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]
# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print a[0, 1] # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print a[0, 1] # Prints "77"
2 77
#Integer array indexing
import numpy as np
a = np.array([[1,2], [3, 4], [5, 6]])
# An example of integer array indexing.
# The returned array will have shape (3,) and
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
# The above example of integer array indexing is equivalent to this:
print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
# When using integer array indexing, you can reuse the same
# element from the source array:
print a[[0, 0], [1, 1]] # Prints "[2 2]"
# Equivalent to the previous integer array indexing example
print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]"
[1 4 5] [1 4 5] [2 2] [2 2]
import numpy as np
# Create a new array from which we will select elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print a # prints "array([[ 1, 2, 3],
# [ 4, 5, 6],
# [ 7, 8, 9],
# [10, 11, 12]])"
# Create an array of indices
b = np.array([0, 2, 0, 1])
# Select one element from each row of a using the indices in b
print a[np.arange(4), b] # Prints "[ 1 6 7 11]"
# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10
print a # prints "array([[11, 2, 3],
# [ 4, 5, 16],
# [17, 8, 9],
# [10, 21, 12]])
[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] [ 1 6 7 11] [[11 2 3] [ 4 5 16] [17 8 9] [10 21 12]]
# Boolean array indexing
import numpy as np
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
# this returns a numpy array of Booleans of the same
# shape as a, where each slot of bool_idx tells
# whether that element of a is > 2.
print bool_idx # Prints "[[False False]
# [ True True]
# [ True True]]"
# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print a[bool_idx] # Prints "[3 4 5 6]"
# We can do all of the above in a single concise statement:
print a[a > 2] # Prints "[3 4 5 6]"
[[False False] [ True True] [ True True]] [3 4 5 6] [3 4 5 6]
import numpy as np
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
# Elementwise sum;
print x + y
print np.add(x, y)
print '-----'
# Elementwise difference;
print x - y
print np.subtract(x, y)
[[ 6. 8.] [ 10. 12.]] [[ 6. 8.] [ 10. 12.]] ----- [[-4. -4.] [-4. -4.]] [[-4. -4.] [-4. -4.]]
import numpy as np
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
# Elementwise product;
print x * y
print np.multiply(x, y)
print '-----'
# Elementwise division;
print x / y
print np.divide(x, y)
print '-----'
# Elementwise square root;
print np.sqrt(x)
[[ 5. 12.] [ 21. 32.]] [[ 5. 12.] [ 21. 32.]] ----- [[ 0.2 0.33333333] [ 0.42857143 0.5 ]] [[ 0.2 0.33333333] [ 0.42857143 0.5 ]] ----- [[ 1. 1.41421356] [ 1.73205081 2. ]]
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
# Inner product of vectors;
print v.dot(w)
print np.dot(v, w)
print '-----'
# Matrix / vector product;
print x.dot(v)
print np.dot(x, v)
print '-----'
# Matrix / matrix product;
print x.dot(y)
print np.dot(x, y)
219 219 ----- [29 67] [29 67] ----- [[19 22] [43 50]] [[19 22] [43 50]]
import numpy as np
x = np.array([[1,2],[3,4]])
print np.sum(x) # Compute sum of all elements; prints "10"
print np.sum(x, axis=0) # Compute sum of each column; prints "[4 6]"
print np.sum(x, axis=1) # Compute sum of each row; prints "[3 7]"
print '-----'
# Transposing
print x.T
print '-----'
# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print v # Prints "[1 2 3]"
print v.T # Prints "[1 2 3]"
10 [4 6] [3 7] ----- [[1 3] [2 4]] ----- [1 2 3] [1 2 3]