Docsity

EECS 168 Programming Final Exam With Complete Solutions., Exams of Electrical Engineering

Toggle sidebar
bg1
EECS 168 Programming Final Exam With
Complete Solutions
doc string - ANSWER - first line of function body
- """Example doc string."""
call stack - ANSWER - function calls
- functions
- function scope (locally declared variables and parameters)
heap - ANSWER - object assignments
- what the variables are assigned to
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] = ???
- generates new list of tuples where each element in this list is the index and the element at
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download EECS 168 Programming Final Exam With Complete Solutions. and more Exams Electrical Engineering in PDF only on Docsity!

EECS 168 Programming Final Exam With

Complete Solutions

doc string - ANSWER - first line of function body

  • """Example doc string."""

call stack - ANSWER - function calls

  • functions
  • function scope (locally declared variables and parameters)

heap - ANSWER - object assignments

  • what the variables are assigned to

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] = ???

  • generates new list of tuples where each element in this list is the index and the element at

that index

  • focuses on the list rather than indexes

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="")

  • have to include the end="" so it doesnt count the \n

what is at the end of a line in a file - ANSWER - \n

immutable types - ANSWER - string

  • int
  • bool
  • float
  • tuple
  • range

dictionary - ANSWER - key-value pairs

storing a value in a dictionary - ANSWER my_dict[key] = expression/value

  • if the key is not already in the dictionary then a new entry gets creating (this is different from a list bc if you try to look for a value thats not in a list then it crashes)
  • if the key is already in the dictionary then that value is overridden

keys (dictionary) - ANSWER - immutable

  • must be unique
  • used to look up a value
  • make sure to only try to access keys that are in the dictionary (use in keyword)

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()

  • in form of tuple

how to iterate over a dictionary - ANSWER - keys for key in my_dict.keys(): #do something with key

  • values for value in my_dict.values(): #do something with value
  • both for key, value in my_dict.items(): #do something with key and value

** 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

  • gives access to a bunch of different methods through this random module

random.randint(lower, upper) - ANSWER - gives you a random number between lower and upper

  • includes upper bound

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

  • allows for repeats
  • can use basically everything we've seen that is compatible with for in loops -can make k as big as you want bc it allows for repeats

random.sample(population, k=num) - ANSWER - returns list of size k with unique elements

  • no repeats
  • can use basically everything we've seen that is compatible with for in loops K must be less than or equal to the size of the original population or it produces an error

random.shuffle(list) - ANSWER - shuffles the order of elements in a list (must be a list)

  • changes are permanent

iterables - ANSWER - string

  • range
  • list
  • dictionary
  • tuple
  • set

parameter vs argument - ANSWER - parameter: named variables in the function definition

  • argument: values passed in on the call

positional arguments - ANSWER - the order in which you write the values in the call decides which parameter takes on a certain argument

  • can put the arguments directly in there without assignment bc the order determines which parameter corresponds to which argument

keyword arugments - ANSWER - assign a value to a specific parameter on the call

  • doesn't matter the order in which you assign the parameters
  • can have default values for parameters

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

  • methods are only accessible if you have an object of that specific type available

method scope - ANSWER - parameters

  • locally declared variables
  • class members

call stack for class - ANSWER - main()

  • instances (ex: stu1, stu2)

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

  • only really have to define > and == and you can come up with the rest using those
  • remember to use self and other

str(self) - ANSWER - define the string representation of your object

  • return string should be readable (a person could make sense of it)
  • this method is called automatically when your object is passed directly to print

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

  • if you tried to print a list of instances of a class then you would need to use repr

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