Lecture Materials

Questions & Answers

Q: how we doing today friends

A1:  Living the dream!

Q: When is our next diagnostic?

A1:  That was the only one! No more formal assessments :)

Q: can we do a return to fix this?

A1:  You don’t need to fix this one - this one works

Q: I meant the buggy one

A1:  No - if you don’t have the list, you can’t return it

Q: do you need to include index 1 and 2 in your parameters if you are already giving the list?

A1:  it makes the function generalizable… so it could work for other indices

Q: So is it not necessary to return the function that modifies the list? Would putting a return statement do anything?

A1:  not necessary (because of the URL representation) return would be useful if you tried to change the URL (in other words, if you made a “new” list in the function

Q: if you specifically state index 1 and index 2 tho wont it only work for those two indices in future calling functions?

A1:  Nope - those are just variable names. You can give them any value you want

Q: Will there be a lists reference handout?

A1:  It’s in the Python Reader

Q: can we think of slices as zero-index as well, given it doesn't include the last item number in the slice?

A1:  yes!

Q: Is there a reference on the topic of 'Animation' we did last week? (like moving graphics)

A1:  The slides are your best bet, as are the examples we posted in the examples dropdown.

Q: Will the slice affect the list?

A1:  No, it makes a copy of that portion of that part of the list

A2:  no. good question

Q: in the previous slide, wouldnt it be aslice instead of alist?

A1:  alist was the list created on that slide

Q: wait is len(list) the entire length

A1:  Yep, the number of elements

Q: where wouuld the boundary for 6 and its negative counter part be?

A1:  It’s at the end of the list, and there isn’t really a negative counterpart (although theoretically, it’s 0)

Q: so do you use -0 to get the last value in the list?

A1:  '-1

Q: what is the result for alist[2:-2:-1]?

A1:  An empty list - you can’t go backwards from 2 towards the end of the list.

Q: but mehran just used -1 and it left out the last value

A1:  He sliced up until the last value - if you want to slice up until the end, you’ll go until len(list) or just omit the ending index

Q: can the step be like a decimal like 1.5

A1:  I don’t think so!

Q: Can you take slices backwards? So would [4:2] give d , c or would it give c, d or would it just not work?

A1:  It would have to be [4:2:-1] to indicate you want to go backward

A2:  it would be empty, unless the third part was -1 (eg [4:2:-1]

Q: Wait, if you are skipping just one, then wouldn’t f be included?

A1:  Nope, because you’re going up until but not including index 5

Q: would alist[::-1] give you the first and last element of a list?

A1:  would alist[::-1] give you the first and last element of a list?

A2:  That gives you the list in reverse!

Q: why not b?

A1:  For which slice?

Q: alis (4,:1:-1

A1:  You go up to, but not including, index 1

Q: can we sort lists that are in integers withou a loop?

A1:  Yep - we’ll get there soon

Q: having the brackets different mean something?

A1:  [] means indexing or slicing, and () means calling a function

Q: Why are there not colons between the start, end , and step?

A1:  There should be! Good catch

Q: what does for i in range do to lists again?

A1:  Check out last lecture - it gives you indices in the list

Q: if we want aslice=[e, f, a, b], how to make a code?

A1:  You can’t do that in one slice - you’ll have to do two slices and add them together

Q: In the slides. —> does for I in range (start, end, step): call the list? for example in the for elem in list(start,end,step) it actually calls list.

A1:  range just gives you indices (which you can use to index into the list) and for elem in list gives you actual elements from the list

Q: can you also pop a slice?

A1:  Nope

Q: can you use step in the del function?

A1:  Yep

Q: can you use list.sort to sort in descending order?

A1:  You can set an oprional reverse=True parameter

Q: How do I sort in decreasing order?

A1:  You can set an optional reverse=True parameter

Q: can we use list.reverse() for *individual* order numbers, like reversing the 2nd and 3rd items in list?

A1:  Not easily

Q: When sorting lists, how do you sort it in a decreasing order?

A1:  You can set an oprional reverse=True parameter

Q: How would sort work if you had both strings and numbers in a list?

A1:  It doesn’t, unless you specify a way to compare those two things! We’ll talk about how to do this later in the quarter

Q: If you have leading zeros, how does sort sort? is it lowest to greatest or digit by digit

A1:  numbers in Python don’t have leading zeros, so the former

Q: can you write grid = [list1, list2, list3]

A1:  Yep!

Q: why is an element of specifically 2 elements in it?

A1:  I’m not sure I totally understand your question, but the inner lists can have any number of elements in them

Q: Is there any rhyme or reason to the syntax of the various python functions? When do they have the form "new_var=function(old_var)", versus "variable.function()", versus "del list[i]"? It's just hard to keep the format straight, as there doesn't seem to be any consistency

A1:  1. function(params) is a function that gets applied to those parameters, but isn’t internal to those parameters
2. variable.function() is when function represents a behaviour that is internal to variable. 
3. del isn’t a function! 

Stop by office hours to explore these ideas in greater depth

Q: Can you have functions in lists?

A1:  Yes! It’s pretty uncommon, though.

Q: Is there a method to make matrices in Python?

A1:  2d lists are how you’d do it in vanilla Python, but there are libraries that do it much better.

Q: can you nest lists infinitely?

A1:  Within reason! At some point, your computer will run out of memory

Q: Is this how one mathematically represents matrices in Python or is that completely unrelated

A1:  Yes, it is! Usually, we don’t use vanilla Python but a library instead, but internally, matrices are usually represented as lists of lists

A2:  it is! Though there is a library called numpy which is spectacular for matrices

Q: Isn’t this indexing rule just like the way matrix works?

A1:  Yep

Q: what if you request grid[0,1] instead of grid[0][1]

A1:  error!

A2:  Python will throw a tantrum!

Q: are lists the same as arrays?

A1:  lists can grow in size! arrays typically cant

A2:  Arrays are a thing in Python (and in Python, they don’t have a fixed size, although they do in other languages), but can only store a very limited variety of objects, and only one type per list.

Q: wiill it give you both list 0 and 1?

A1:  Will what give you both list 0 and 1?

Q: What if you want a list of column?

A1:  What if you want a list of column?

A2:  very hard.. must write that out using a loop

Q: What happens if you add a 2D list to a 2D list? Do the smaller lists know to combine?

A1:  Depends on how you add them! Usually, no, they won’t combine.

Q: Sorry, to clarify, is there a way you can request two (or more) lists in a grid (or any larger list) at once?

A1:  Yep - just slice into the grid

Q: do you have to return anything when swapping

A1:  No, because we’re changing the list in place.

Q: What if you specify a list of arrays alist = [[0;1;2],[3,4,5]] Does alist[1][2] return 5? Does alist[1,2] return 5?

A1:  The first does, the second one breaks!

Q: how do you sort a list of lists?

A1:  You’d need to specify how to compare two lists - we’ll talk about how to do that later on this quarter.

Q: does the element of the 'list of lists' works as reference? contrary to simple integer of 1-D list?

A1:  Each element of the outer list is a reference to an inner list.

Q: in the case of looping thru a list of lists, the x and ys aren’t switched right?

A1:  list[1][2] means go to the row #1 column #2 - depends on what you mean by switched!

Q: how does python know the difference between row and elem?

A1:  They’re different variables with different names

Q: would you have to define elem as a variable or does python already know what that means

A1:  No, the for loop makes it for you

Q: Could you access the element position in a for each loop?

A1:  You can’t, you’ll need a range loop for that

A2:  Could you access the element position in a for each loop?

Q: what does append mean again

A1:  To append is to stick an element at the end of the list.

Q: whats the difference in between extend and append again?

A1:  If you append a list, it becomes an element in a larger list. If you extend it, that means to add each element individually to another one.

Q: in this scenario have you previously assigned value to rows, cols, and value?

A1:  Yeah, they’d be passed in to the function.

Q: so are rows lists in the grid and columns are the elements in the lists?

A1:  Yep!

Q: what about when you want to get different numbers(not only 1s and 5s?

A1:  You’d need to modify the function

Q: are 2-d lists treated like a matrix, like could you do matrix math with them directly?

A1:  Well, you’d need to write functions to do the operations, but yes. There’s a great library called numpy which does a lot of this for you!

Q: why is board[row][col] == True if it’s not empty?

A1:  Which line of code are you referring to? Any value that isn’t None ‘counts as’ True

Q: reference to line 42

A1:  Yeah, that’s just checking that it’s != None

Q: how do you check for a diagonal win?

A1:  Check out the lecture code!