COMP 204 Final

0.0(0)
studied byStudied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/136

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:56 PM on 4/22/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

137 Terms

1
New cards
Converting from binary to decimal
(i.e. 11010)
(1*2^4)+(1*2^3)+(0*2^2)+(1*2^1)+(0*2^0) \= 26
2
New cards
Converting from decimal to binary
divide by two and take remainder, then continue until result is 0 and write reminders from bottom (2/2 or 1/2) to top (initial number)
3
New cards
int type
integer without decimal
4
New cards
float type
integer with decimal
5
New cards
modulus (%)
returns remainder
6
New cards
division (/)
gives a float number even if both are integers
7
New cards
floor division (//)
rounds down to nearest whole number, will be a float if at least one number is a float
8
New cards
print("Hello " + "World")
Hello World
Concatenation
9
New cards
print("Hello " * 5)
Hello Hello Hello Hello Hello
10
New cards
How to swap variables
X \= 137
y \= 42
temp \= x
x \= y
y \= temp
11
New cards
input()
pauses program and waits for user to type information and press enter
12
New cards
math.pi
value of pi
need to input math first
13
New cards
math.factorial()
Returns the factorial of a number
14
New cards
when is None returned
when no return statement is in the function
15
New cards
slicing strings
fruit \= "banana"
print(fruit[:3])

ban
16
New cards
variable.upper()
makes string variable upper case (can also do lower)
17
New cards
variable.startswith("something")
if the string variable starts with "something", it will return True (can also do .endswith)
18
New cards
while loop
while CONDITION:
\>>statement_block
(can create a counter and increment it a certain amount of times)
19
New cards
break
stops a loop during its execution
20
New cards
for loop
for VARIABLE in OBJECt:
\>>> STATEMENT_BLOCK
21
New cards
for range loop
for VARIABLE in range(start,stop, step)
22
New cards
recursion
calls a function within itself
23
New cards
string{}.format(x)
replaces {} with X
24
New cards
Specify decimal points
{:.nf} where n is the decimal points
- use with .format to input a value
25
New cards
are strings mutable
no
26
New cards
are lists mutable
yes
can say list[1] \= new_value and it will be replaced
27
New cards
traversing a list
for variable in range(len(list))
28
New cards
Concatenate a list
new_list \= list1 + list2
29
New cards
repeating a list
zeros \= [0] * 5
\[0,0,0,0,0]
30
New cards
equality of lists
only the same if the lists have the same elements in the same order
31
New cards
list slicing
t \= [1,2,3,4,5]
print(t[start:stop:step)
32
New cards
call a function
function_name(arguments)
33
New cards
call a method (a function that belongs to an object)
object.method_name(arguments)
34
New cards
list.append(element)
adds new elements to the end of the list
-does not return a value
35
New cards
list.extend(other_list)
adds all elements of a list to the end of the other without changing the added list
-does not return a value
36
New cards
list.insert(index, value)
inserts an element at a particular index and moves all other elements to the right
-does not return a value
37
New cards
list.sort()
sorts elements in a list
and does not return a value
38
New cards
element.isupper()
True if the element is uppercase
39
New cards
Can you add a method that does not return a value to a variable?
no
40
New cards
del names [start,stop,step]
deletes list elements
41
New cards
remove \= list.pop(index)
removes the element from the list and returns it in the variable
42
New cards
list.remove(element)
Removes the first matching element from a list.
return value is None
43
New cards
list.index(element)
returns the index of the first matching item in a list
44
New cards
new \= list.count(element)
returns the number of occurences of an element in a list
45
New cards
len(list)
Find the length of the list.
46
New cards
min(list)
Get the minimum element in the list.
47
New cards
max(list)
Find the element in list with the largest value.
48
New cards
sum(list)
Find the sum of all elements of a list (numbers only).
49
New cards
splitting \= list.split(delimiter)
will split the list according to the value in the delimiter
50
New cards
joining \= delimiter.join(list)
joins a list of strings and concatenates the elements at the delimiter
51
New cards
new_list \= list[:]
makes a clone of a list
52
New cards
dictionary[key] \= value
-add new key value pair
53
New cards
dictionary.values()
Returns view of all values in dictionary
54
New cards
keys \= list(dictionary)
returns a list of the keys
55
New cards
dictionary.update(other_dictionary)
combines two dictionaries
56
New cards
tuple \= (element,)
needs the comma after if just one element
57
New cards
can tuples be modified?
no, they are immutable and elements can not be added or removed but can be replaced with another tuple (len,min,max, and sum work the same)
58
New cards
new_set \= set([list])
adds all unique items from a list to an unordered set. can also add immutable objects with set \= (tuple)
59
New cards
set.add(element)
adds an element to a set
60
New cards
set.remove(element)
removes element from a set
61
New cards
common \= setA.intersection.setB
returns a new set of the common values of sets A and B
62
New cards
union \= setA.union(setB)
returns a new set that is a union of sets A and B
63
New cards
diff \= setA.difference(setB)
returns a new set that is the set difference of sets A and B
64
New cards
Handling an exception
try:
\>> \#code that isn't trusted

except EXCEPTION TYPE
\>> action

finally:
\>>anything here will always be printed
65
New cards
raise Value Error
proactively cause an error so this doesn't disrupt the rest of the code
66
New cards
fobj \= open(filename."r")
reads file
67
New cards
fobj \= open(filename."w")
writing or overwriting if the file already exists
68
New cards
fobj \= open(filename. "a")
appends to the end of a file
69
New cards
file_content \= fobj.read()
returns a string containing file content
70
New cards
for line in fobj:
\>>words \= line.split(delimiter)
produces a sequence of lines contained in the file separated with delimiter
71
New cards
content \= fobj.readlines()
returns a list containing each line in the file
72
New cards
content \= fobj.write(writing \n)
writes string on new line (\t is new tab)
73
New cards
for line in fobj:
\>>list \= line.strip()
gets rid of all \n in a CSV file
74
New cards
Local scope variable
variable only exists within a function - python assumes local scope
75
New cards
Binary Search Algorithm
def search(seq, item)
\>>low \= 0
\>>high \= len(seq) -1
\>>while low < \= high
\>> mid \= (low+high)//2
\>> if seq[mid] < item:
\>> low \= mid +1
\>> elif seq[mid] \> item:
\>> high \= mid - 1
\>> else:
\>> return mid
return None
76
New cards
Selection Sort Algorithm
def sort(seq):
\>>N \= len(seq)
\>>for i in range(N):
\>> min \= i
\>> for k in range(i+1, N):
\>> if seq[k]
77
New cards
Two List Insert Sort
def insert(sort,unsort):
\>>for item in unsort:
\>> if item\>sort[-1]:
\>> sort +\=[item]
\>> continue
\>> for i in range(len(sort):
\>> if sort[i] < item:
\>> continue
\>> else:
\>> half1 \= sort[0:index]
\>> half2 \= sort[index:]
\>> sort \= half1 +[i] + half2
\>> break
\>>return sort
78
New cards
plt.plot(values)
plots the values
79
New cards
import matplotlib.pyplot as plt
imports Matplotlib to plot data
80
New cards
plt.show()
displays the graph
81
New cards
plt.title("label", fontsize)
plt.ylabel
plt.xlabel
inputs titles, along with x and y label
82
New cards
plt.bar(x axis, y axis, width, color)
Creates a bar graph
83
New cards
plt.scatter(x,y,size, marker, color)
Creates a scatter plot
84
New cards
plt.savefig(file.png)
saves a figure
85
New cards
array \= np.array([list],dtype\=int/np.float)
creates a NumPy array which is different from a regular array as it can not change its size and all values must be the same type
86
New cards
np.shape()
indicates the length in each dimension
87
New cards
np.zeros(number)
creates an array of zeros with shape of the number
88
New cards
np.ones(number)
creates an array of 1s with shape of the number
89
New cards
np.full(shape,value)
creates an array of shape shape with value value
90
New cards
np.arange(start, stop, step)
An array of number starting with start, going up in increments of step, and going up to but excluding step.
91
New cards
np.linespace(start,stop,\# in list)
creates a list of evenly-spaced numbers within a range
92
New cards
np.sin(array)
broadcasts math function to all elements in the array
93
New cards
np.exp(array)
broadcasts exponents to all elements
94
New cards
Vectorized numpy arrays
array1 + array2
does math function on an element-by-element basis
95
New cards
copy \= np.copy(array)
gets a copy of an array
96
New cards
size \= np.reshape(array(r,c))
reshapes an array to one with 2 rows and 3 columns
97
New cards
matrix[rstart: rstop: rstep, cstart: cstop: cstep]
slices a matrix with row slices, colum slices
98
New cards
OOP objects
objects all have data and methods that belong to a single theme
99
New cards
OOP Class
keyword to declare code belonging to an object
100
New cards
OOP Instance
the action of creating an object from a class