Show Menu

COMP204 Final Cheat Sheet by

python     compsci

Binary Numbers

Binary Numbers

8 bits
1 byte
16 bits
32 bits
double word
always at least 1 byte
add one to first number in sequence to make negative, add zeros to get full byte

Machine Learning

-program adjusts itself automa­tically to fit data, end result is a program trained to achieve a given task
Supervised learni­ng-­given examples of input and desired outputs, predict outputs on future unseen inputs­(cl­ass­ifi­cation, regres­sion, time series)
Unsupe­rvised learni­ng-­creates a new repres­ent­ation of the input
Reinfo­rcement learni­ng-­lea­rning action to maximize payoff
Types of supervised learning tasks
1. classi­fic­ation- predict which predefined set of classes and example belongs to
2. regression - predict a real value
2. probab­ility estimation - estimate probab­ility of an event
Sensit­ivity = fraction of positive examples predicted to be positive
Specif­icity= proportion of negative examples predicted negative
False-­pos­itive rate(FPR)= negatives predicted to be positive


stores an unordered set of objects, CANT BE INDEXED, no duplic­ates, only immutable objects contained
myset = set([])
creates a set from a list
length of the set
if x in myset:
evaluates boolean condition
for element in myset:
iterate through set
for set1 = A and set2 = B, A&B = inters­ection
elements in common to A and B
A | B or A.union(B)
union of two sets
difference of sets, elements in A that are not in B
all these rules can be applied to multiple sets

Integer Sequences

range(­start, end, step)
(start default is zero, step default 1)
0 1 2 3 4
3 4 5 6 7
sequence of index of values in seq
2 5 8 11


def functi­onname( argume­nts):
defines a function of given name with given arguments
only returns a certain value or string generated by the function, doesn't print, exits at this value
list = [[0 for i in range(­ncols)] for j in range(­nro­wns)]
creates a two dimens­ional list of nrows and ncols filled with zeros

Operations on Lists

lst = []
creates empty list
adds item to list at end
add sequence of items at end­ser­t(idx, val)
inserts item at index­mov­e(val)
remove first item in the list with value val
remove and return item at index­rt(­)/l­st.r­ev­erse()
sort/r­everse list in place
finds the min/max


.plot(x,y, color)
plots the x values to x values in a certain color
shows the graph
names y axis
names x axis
.savefig( figname)
saves image under figname


defines attrib­ute­s(i­nfo­rmation we want to keep together) and methods( operations want to be able to perform on that data)
class some_c­las­s_name:
defines a class of some_c­las­s_name
def __int_­_(self, other attrib­utes):
defines and initia­lizes attributes­thod()
calls method on an object of that class


Sytax errors
violation of a writing rule
syntax is fine, some other thing is occasi­onally flawed­:Ze­roD­ivi­sio­nEr­ror­(can't divide by zero), NameEr­ror­(can't access the variable), IndexE­rro­r(When the index you are attempting to access does not exist), TypeError( operation on non-co­mpa­tible type)
Logical (semantic) errors
code runs, but doesn't do what its supposed to
does something that may cause an exception
except <So­meE­xce­pti­onT­ype­>:
do something to handle the exception
raise[ exception object ]
raises a certain, defined type of exception
try, except, else
try something, that could cause the exception, if its fine, go to else
adds statement that happens no matter what
for a, b in zip(list 1, list 2):
iterates over elements of two lists in parallel, yields a tuple with both iterations

File Input/­Output

f = open(m­yfile, 'x')
opens the file, x=r for reading only, x=w for writing only, x=a for appending, x=b for file in binary format, x=wr+ reading and writing and so on
reads the entire file, returns a single string, size is optional number of characters
reads all lines and returns them as a list of strings
returns a string with the end-of­-line charac­ter(\n) removed from the end of the string
reads single line from file, returns empty string if end of file
closes file
interface to read/write compressed files, .gz extension

Operations on Dictio­naries

dict = {}
sets up an empty dictionary
dict[key] = value
sets value of said at that key to value
calls the value at that key
del dict[key]
deletes a key from dict
clears the dict of all keys
can update/add associ­ations from another
looks at either all keys, values, or all items in the dict
removes element associated to that key
removes key, value pair and returns it as a tuple
returns value at that key
either returns value of the key or creates the given key if not previously existent

Download the COMP204 Final Cheat Sheet

2 Pages

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!



No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.