







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








doc string - ANSWER - first line of function body
call stack - ANSWER - function calls
heap - ANSWER - object assignments
when you assign a variable/list new values does this replace the original ones - ANSWER not really, its basically telling the function to stop looking at one value and to starting looking at another, they don't replace one another, and then when nothing is looking or being assigned to an object then it disappears in memory but we didn't edit the int
enumerate - ANSWER - iterating through a list for index, value in enumerate(my_list): my_list[index] = ???
that index
syntax for opening a file to read or write - ANSWER with open ("file.txt", "r") as my_file: my_file = open("file.txt", "r")
do filenames need a path or no - ANSWER - yes when trying to open a file there needs to be a filepath or python thinks the file is in your current directory
read a whole file - ANSWER with open ("file.txt", "r") as my_file: print(my_file.read())
read the first line of file - ANSWER with open ("file.txt", "r") as my_file: print(my_file.readline())
read each line from file - ANSWER with open ("file.txt", "r") as my_file: for line in my_file: print(line, end="")
what is at the end of a line in a file - ANSWER - \n
immutable types - ANSWER - string
dictionary - ANSWER - key-value pairs
storing a value in a dictionary - ANSWER my_dict[key] = expression/value
keys (dictionary) - ANSWER - immutable
syntax for pulling out only keys (dictionary) - ANSWER my_dict.keys()
syntax for pulling out only values/expressions (dictionary) - ANSWER my_dict.values()
syntax for pulling out both keys and values (dictionary) - ANSWER my_dict.items()
how to iterate over a dictionary - ANSWER - keys for key in my_dict.keys(): #do something with key
** you cannot edit a value at a key during a for in look (cant change the size)
can you change the size of an iterable during a for in loop - ANSWER no you cant add or remove items during a for in loop
empty versions of data structures - ANSWER [] - list
random.randint(lower, upper) - ANSWER - gives you a random number between lower and upper
random.choice(seq) - ANSWER - returns a random element from a sequence (basically everything weve seen thats compatible with for in loops)
random.choices(population, k=num) - ANSWER - returns list of size k with random elements
random.sample(population, k=num) - ANSWER - returns list of size k with unique elements
random.shuffle(list) - ANSWER - shuffles the order of elements in a list (must be a list)
iterables - ANSWER - string
parameter vs argument - ANSWER - parameter: named variables in the function definition
positional arguments - ANSWER - the order in which you write the values in the call decides which parameter takes on a certain argument
keyword arugments - ANSWER - assign a value to a specific parameter on the call
special characters in parameter lists - ANSWER - forcing positional arguments: /
member variable - ANSWER a variable that belongs to class
instance variable - ANSWER considered to also be a member, but its value can vary from instance to instance
instance - ANSWER a single instantiation of a Class (e.g. word1 = 'doggy' is an instance of the string class)
object - ANSWER in python, everything is an object, but more generally we use the word to describe a variable that is more complex than just a number or just a bool. Objects typically have several variables and/or methods built in
instance vs object - ANSWER this is just a matter of semantics. If word1 = 'doggy' then we could say "word1 is an instance of the str class" and we could also say that "word1 is an object of type str"
method - ANSWER - a method is just a function that belongs to a class. It has normal function scope (parameters and locally declared variables) but also has scope on other members
method scope - ANSWER - parameters
call stack for class - ANSWER - main()
heap for a class - ANSWER - instance variables
operator overloading (magic methods) - ANSWER - can provide (additional, not overriding) definitions for these so we can use operators on our class type
str(self) - ANSWER - define the string representation of your object
directly called would be passing a class object straight into print indirectly called would be like printing a list of objects which would not work
repr(self) - ANSWER - also returns string representation of an object but it is meant to be a valid python expression that could be used to recreate that object
set1.intersection(set2) - ANSWER returns a set of elements that occur in both sets
set1.difference(set2) - ANSWER returns a set with values that occur in set 1 that do not occur in set 2