# import some things %pylab --no-import-all inline import numpy as np import pylab as pl from scipy import linalg s = "Hello World!" print s %run my_script.py my_str = 'Hello World!' print my_str print my_str[0] print my_str.replace('World', 'Why N\' How') print my_str.upper() my_list = [1, 2, 3, 'test'] # Notice: [] creates a list print my_list my_list.append('test2') print my_list my_list.insert(1, 0) print my_list print my_list print my_list[0] # first element print my_list[-1] # last element print my_list[-2] # second last element print my_list print my_list[2:5] # Notice: index 5 is not included print my_list[:3] # first 3 elements print my_list[-3:] # last 3 elements print my_list[::2] # every 2nd element print my_list[::-1] # list with order reversed phone = {'joe': 554, 'bob': 308} # using {} creates a dict print phone # Notice: no order print phone['joe'] phone[0] = 101 print phone print phone.keys() # list with the keys print phone.values() # list with the values a = 10 if a == 1: print 1 print 22 elif a == 2: print 2 else: print 'a lot' for word in ['cool', 'powerful', 'readable']: print 'Python is %s !!!' % word def disk_area(radius): area = 3.14 * radius * radius return area print disk_area(1.0) print disk_area(2.0) import copy def foo(a): a.append(1) b = [0] foo(b) print b # a has been modified !!! import numpy as np # import numpy so we can use it a = np.array([0, 1, 2, 3], dtype=np.float) # create array print a print a.ndim # number of dimensions, in Matlab `ndims(a)` print a.shape # shape, in Matlab `size(a)` print a.dtype # the data type of the array # 2-D array b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array print b print b.dtype # Notice: here the data type is int64 print b.shape # 3-D c = np.array([[[1], [2]], [[3], [4]]]) print c.shape # in Matlab `size(c)` a = np.ones((3, 3)) print a b = np.zeros((2, 3)) print b c = np.eye(3) print c a = np.diag(np.arange(3)) print a print a[1, 1] print a[:,1] # takes the entire second row! # slicing a = np.arange(10) print a print a[::2] # every 2nd element print a[-5:] # last 5 elements a = np.arange(10) print a b = a[::2] print b b[0] = 100 print b print a # a was modified as well! a = np.arange(10) b = a[::2].copy() # force a copy b[0] = 100 print b print a a = np.arange(10) np.save('test.npy', a) a = 0 a = np.load('test.npy') print a a = np.triu(np.ones((2, 2)), 1) # see help(np.triu) print 'a:' + str(a) b = np.diag([1, 2]) print 'b:' + str(b) c = np.dot(a, b) # same as a.dot(b) print 'c:' + str(c) print a * b # element-wise multiplication a_t = a.T print a_t print 'a.flags:\n' + str(a.flags) print 'a_t.flags:\n' + str(a_t.flags) from numpy import linalg # OR from scipy import linalg # even better A = np.triu(np.ones((3, 3)), 0) print 'A:\n' + str(A) B = linalg.inv(A) C = np.dot(B, A) print 'C:\n' + str(C) x = linalg.solve(A, [1, 2, 3]) # linear system U, s, V = linalg.svd(A) # SVD vals = linalg.eigvals(A) # Eigenvalues x = np.arange(5) print x print np.sum(x) # or x.sum() x = np.array([[1, 1], [2, 2]]) print np.sum(x, axis=0), # columns (first dimension) print np.sum(x, axis=1) # rows (second dimension) from scipy import stats a = np.random.normal(0, 1, size=10) b = np.random.normal(1, 1, size=10) tval, pval = stats.ttest_ind(a, b) print 'T=%0.4f, p=%0.4f' % (tval, pval) import pylab as pl t = np.linspace(0, 8 * np.pi, 1000) pl.plot(t, np.sin(t)) pl.xlabel('$x$') pl.ylabel('$sin(x)$') pl.ylim([-1.1, 1.1]) pl.savefig('pylab_demo.pdf') # natively save pdf, svg, etc. image = np.random.rand(30, 30) pl.imshow(image) pl.gray() pl.show()