Show Menu
Cheatography

COMP 202 Cheat Sheet (DRAFT) by

for programming class

This is a draft cheat sheet. It is a work in progress and is not finished yet.

binary numbers

tree branching out
jar with balls
clock

changing a variables value

x = 5
x = x +1 

output: x = 6

assigning values to multiple variables

x, y, z = 1, 2, 3
print("x is", x)
print ("y is", y)
print ("z is", z)

x = y = x = 5
print("x is", x)
print ("y is", y)
print ("z is", z)
this is a shortcut

modulus

27%10
returns 7, remainder of clock arithmetic

properly evaluating equality of floating points

x = 1.1+2.2
epsilon = 0.001
print(abs(x-3.3)<epsilon)
 

algorithm for base conversion

procedure baseexpansion(n,b)
q := n
k := 0

while q =! 0 
ak := q mod b 
q := q/b
k = k+1

return (ak-1....a1,a0)
in general, given a base b and a decimal number n, repeat the following until the number is 0

divide n by b and prepend the remainder of the division

let the new number be n divided by b, rounded down

the life of an object

 
creation
manipu­lation (while it exists)
stops existing when there are no more references to it

swapping values

x = 137
y = 42
temp_var = x 
x = y
y = temp_var
temp_var stores x = 137
then you take x = y, moves both to 42
then define y as temp_var, 137
y = 137
prevents loss of inform­ation

input function always returns

 
a string!
 

type error

print(int('5')*int('3'))
can't multiply between two strings
must cast string as int

object references

look up slides...
if a = 5
then a = "­cat­"
value five is garbage collected

= Is not equality, not commut­ative

 
x = 7
7 = x
illegal in pyton