Lecture Materials

Questions & Answers


Q: Are the diagnostics graded with the bucket system as well or letter grades? Also, where will we be able to see our grades and comments?

A1:  The diagnostic is graded out of 60pts not using the bucket system.


Q: will the diagnostic itself be graded on a curve??

A1:  We decide on the details of the curve at the end of the quarter


Q: where can we see the diagnostic once it’s returned?

A1:  We will post solutions to the diagnostic and will send you a link at which you can see your grade and comments on the exam.


Q: for warhol.py, is it ok if the function make_warhol() is quite long? or are there ways to make it more concise through loops/lists?

A1:  I would recommend using helper functions


Q: Will the future diagnostic allow us to run/test our code? It’d be extremely helpful if we could because retyping everything on Pycharm is very time consuming.

A1:  No you will not be able to run your code on future diagnostics.


Q: I imported the SimpleImage library and it doesn't run??

A1:  Post on Ed!


Q: what is “temp”?

A1:  temp is a temporary variable storing alist[index1] we are going to use it as intermediate storage


Q: Can lists be nested? As in can you have a list of lists

A1:  Yes!


Q: can we use return in the previous function to prevent buggy?

A1:  its a bit hard with only one return


Q: For the next diagnostic, it is possible to do an assignment on the topic before directly attempting it on the diagnostic? The sections and assignments are very helpful in understanding concepts.

A1:  I think list was the only one where the assignment wasn’t already due, but the assignment was out


Q: for assignment 3 ghost.py, it says “no such file” when it calls for the image file. I called hoover in terminal and the file is definitely there.

A1:  Post on Ed!


Q: why does my_list get updated if it doesn’t get returned by swap_element_working?

A1:  Because we are mutating my_list inside of the swap_elements_working. Lists are mutuable so the changes will persist if you mutate them inside a function.


Q: shouldn’t alist be called my_list in the swap_elements_working ?

A1:  You can call variables by different names in separate functions.


Q: so we don’t have to return the list?

A1:  correct


Q: in that example, you don’t need to return the list at the end of s_e_w?

A1:  No because lists are mutuable, so when you pass them. to a funciton and mutate them, the changes persist.


Q: to claify, you don’t need to return the list to the main function?

A1:  You do not need to retun the list to the main function.


Q: Can we change a list in a "for each" loop?

A1:  It depends. Check out this new handout http://web.stanford.edu/class/cs106a/handouts/mutation.html


Q: what if we want the last element of the list in our slice. Would we have to append

A1:  You can do something like lst[4:len(lst)] this would give you a slice starting at index 4 and including the last element.


Q: Did anyone mention having a problem with running simpleimage.py?

A1:  Post on Ed if you are having trouble with that and we can help!


Q: doesn't the for loop do the same thing where the first number is included but the last number in the range is not?

A1:  Good point! They do have that in common


Q: What if we want to slice the last number? Would it cause an error since it isn’t included?

A1:  you can put the len(list) as the second position


Q: What would you do if you wanted to make a slice including the last value of the original list?

A1:  use len(list) as your second position


Q: so im a bit confused with mutability. so if we define an integer that can't be changed, whereas a list can be?

A1:  Yes! Integers are immmutable, lists are mutable.


Q: Does slicing alter the original list? Can we keep slicing?

A1:  Slicing does not alter the original list.


Q: how would a slice with a missing start/end look like? would we just do list[ :end] or list[start: ]?

A1:  Yes!


Q: What happens if your start point is after the end point

A1:  you get the empty list


Q: Do you get error or empty list when you slice an empty list?

A1:  empty list >>> x =[] >>> x[2:1] []


Q: why don’t the negative indexes go from -5 to 0?

A1:  zero means the first element, and -1 is the ultimate. if you work backwards to get the full set


Q: can we do something like alist[-2,2]?

A1:  yes!


Q: will we learn to build programs outside of the terminal?

A1:  on monday!


Q: Is alist[b:a:-1] the same as alist[b:a]

A1:  alist[b:a] would give back the empty list because lists don’t automatically wrap around.


Q: how about we use [4:1:1]?

A1:  That would give back the empty list. Try these out in your terminal! It doesn’t automatically wrap around.


Q: So if we want to use a function and don't want it to mess with our list values, should we just send a [:] slice of the list instead?

A1:  You can do that as one option.


Q: So can we slice a slice?

A1:  sure thing!


Q: in general, when do we use for each loops vs for i in range loops?

A1:  You can use for i in range when you need access to i. So if you want to know the index of a value in a list you should use for i in range. If you only need access to the elements, you can use a for each loop.


Q: does it loop by the step or what does the step do in the for each loop?

A1:  it increases the value of the counter by step size each tie


Q: what is the difference between pop and del?

A1:  pop pops a single element. del deletes a portion of an existing list.


Q: how does the for i in range(start, end, step) know which list to refer to?

A1:  it doesn’t refer to a list. you can use len(my_list) to lookup the length of a list. Instead it counts numbers


Q: how does the negative step work with the list [: : -1] because the blank start isnt greater than the blank end

A1:  that does work >>> x [2, 1] >>> x[::-1] [1, 2]


Q: whats the difference between del and pop?

A1:  pop pops a single element. del deletes a portion of an existing list.


Q: we could also use remove.num_list()? why is del better?

A1:  the first command doesn’t work


Q: Chris, I have conflict during your 1:1 hours, how can I setup individual hour for you?

A1:  certainly email — but post surgery im still catching up on extra meetings


Q: Can you use list.sort on lists with strings or mixed lists?

A1:  Yes! With strings it will sort alphabetically.


Q: Can you also do 'list.remove[x : y]' or 'list.pop[x : y]' instead of using 'del' for removing slices from original list?

A1:  pop doesn’t take a slice, use del!


Q: How does passing a list of strings into sort work?

A1:  It will sort alphabetically


Q: How to sort the list in descending order?

A1:  Great question! You can do reversed(sort(list))


Q: does array.sort() work for int/float only?

A1:  you can sort lists with strings as well. It will sort alphabetically.


Q: In this 2-D list, can we find an individual element with grid[0[1]] ?

A1:  you can do grid[0][1]


Q: Wait so technically could you have an infinitely dimentioned list?

A1:  yes… ! very exciting


Q: in mixed lists, would sorting put the numerical elements first or the alphabetically sorted elements first?

A1:  you need to write your own comparison if you want to sort a list of lists (beyond the scope of this class, but cool question)


Q: What it you specified like this: grid[][0]. Would that return all of the first values in each list in the "grid"?

A1:  It would throw an error!


Q: How do we check for how many ‘brackets’ are there to know the dimension of the list?

A1:  you need to keep track of how many dimensions (brackets) for your multi-dimension list yourself!


Q: is there a shorthand for referring multiple dimensions instead of cube[0][1][0]?

A1:  thats the standard notation


Q: Is a grid the name of the list or a seperatr function?

A1:  the name of the “outer” list


Q: can you swap things of different dimension like list[0] and list[1][0]

A1:  Yes you could write a program to do this!


Q: What’s the max dimension list we’ll work with in this class?

A1:  2


Q: what does j represent??

A1:  j is the counter variable inside the inner loop.


Q: have we defined row as a variable yet in the for each loop?

A1:  in the for row in grid statement, row is a variable defined as each list in grid. It is defined the same way that elem is defined in for elem in list


Q: is this a valis 2D list? [[10, 20], 48, 100] i.e. only 0th element is a list rest of them are regular numbers like 1D list?

A1:  that is valid!


Q: is this lecture relevant for assignment 3?

A1:  its for assn 4!


Q: so python recognises rows as “rows”?

A1:  that is our interpretation of elements in a 2D list


Q: what are the x and y in this example, are they used in the same way as i? is row a built in variable?

A1:  x and y are used in the same way as i! Row is not a built in variable.


Q: am finding the lists of lists slightly non-intuitive :3 any suggestions? Thanks!

A1:  You will get practice at section next week! And you will get more practice in assignment 4. You can also trace through some of the functions that Mehran had on the slides


Q: why do you need to return the grid? isn’t it mutable and changes to it are available outside of the scope of the function?

A1:  Good question! Check out the new handout http://web.stanford.edu/class/cs106a/handouts/mutation.html


Q: If there is a value you use many times within a function, is it better style to perform the calculation and assign it a name at the beginning and use that name everywhere, or to recalculate the value upon every use? For example, creating a variable called rgb_average versus writing pixel.red + pixel.blue + pixel.green // 3 in every line where I need the RGB average of a pixel.

A1:  It is better to calculate it once and store it in a variable. Great insight!


Q: This program is SO cool! as if tracking COVID!

A1:  We are glad that you like it!


Q: is this program posted on the cs106a website?

A1:  Yes! It is in the lecture code for today’s lecture


Q: Should doctests be removed before submission for grading?

A1:  No! Keep them in, and add your own if you like :)

A2:  No you can leave them in!


Q: I am extremely lost

A1:  This code is posted on the website. Feel free to come to LaIR or office hours and we can talk through it.


Q: Will we have to do something this extensive for an assignment?

A1:  Yes but will handle the graphical interface part of it.


Q: We are meant to write a short description of what the program does in both the main function comment and the header?

A1:  Yes!


Q: How does the spread function take care of not counting the same person

A1:  live answered


Q: In a list can you have expressions followed by for or and an if statement inside square brackets?

A1:  We will looked into this later in the quarter!


Q: I suppose a better question is what is the differnece between the descriptions we write in the header and in the main function comment?

A1:  live answered


Q: is 2D list covered on the Python reader?

A1:  live answered


Q: are YEAH hours held live, or just recorded?

A1:  Just recorded for now. Would live be something you are interested in?


Q: thank you Mehran, this was very interesting!

A1:  Feel free to go to office hours and ask some questions about it!