f = open(filename, "r"), f.close()f.read() reads the entire file as a stringf.read(n) reads the next n charactersfor line in f: reads a line at a timef.readline() reads one line\n (newline)s.strip() gets rid of newlines and whitespaces.split() splits strings into a list (by spaces, by default)s.lower(), s.upper() to convert to lower/uppercases.replace(val1,val2) replaces val1 with val2 in s (e.g. cleaning punctuation)for i in S:, len(), in{"a","b","c"}; empty set is set(), NOT {} (which is a dict) …set(["a","b","c"])S.add("d"). remove with remove().intersection, .union.issubset, comparison operators (<, <= etc.){"A":1, "B":2} or dict([["A",1],["B",2]]) or dict(A=1,B=2)for i in d: iterates over keys; in searches in keysd[k] = vdel d[k]d[k] extracts the value associated with kd.keys() returns keys (set-like); d.values() returns values (list-like); .items() returns a list-like object holding (key,value) tuplesfor k in d: or for k, v in d.items():)random and numpy.random modules (similar)random.seed(102): initialize random-number generator (RNG) to a known point (for reproducibility)random.randrange(): pick one value from a rangerandom.choice(): pick one value from a list/tuplerandom.random(): random float uniformly from \([0,1)\)random.uniform(a,b): random float uniformly from \([a,b)\)np.array(): from list, tuple, nested lists or tuplesdtype= argument specifies data type (“float”, “int32”, “int8”, “uint8” etc.)a.shape returns a tuple giving dimensionslen(a) gives length of dimension 0np.ones(), np.zeros(), np.arange()shape= argument: tuple specifying dimensions; np.ones(4) is the same as np.ones((4,)); np.ones((4,4)) returns a 4 \(\times\) 4 matrixa.fill(v) fills array a with value va[i] or a[i,j] or a[i,j,k] (depending on dimensions)a[m:n] or a[m:n,:] or …; : by itself means “all rows/columns/slices”a.copy() to make a copya.reshape((r,c)) specifies number of columns (total number of elements must match)a[:,np.newaxis] adds a new length-1 dimensiona.flatten() converts to 1-Dnp.identity, np.eye for identity matricesnp.linalg.det, np.linalg.dot, np.linalg.eig, np.linalg.inv)+, -, *, etc.) operates elementwise on arraysnp.sin(), np.cos(), etc.np.sum(), np.mean(), np.prod() etc. operate on all elements by defaultaxis=i argument collapses dimension i (e.g. np.mean(a,axis=0) on a 2D array computes mean of each column, collapsing rows)>, == etc.) work elementwise, producing a bool arraynp.logical_and(), np.logical_or(), np.logical_not()a[b] selects the elements of a for which bool array b is Truea[a>0] selects positive elementsnumpy integers: for an \(n\)-bit signed integer (the default, one bit is used as the sign bit, so the maximum positive value is \(2^{n-1}-1\); maximum negative is \(-2^{n-1}\)uint32), the range is from \(-2^n\) to \(2^n-1\)numpy) integers are special, won’t overflownp.isclose() or math.isclose() to test near-equalityinf-infinf-inf, inf/inf) become nan (not a number)This appears on the test:
Some helpful numbers: \(2^7=128\); \(2^8=256\); \(2^{2^{10}} \approx 10^{308}\); \(2^{-53} \approx 10^{-16}\).
Maybe useful for thinking about integers: