Lecture Materials

Questions & Answers


Q: This question is very irrevelvant but I've always wondered why Chris's and Mehran's pictures flip places on the CS106A website sometimes. Is there a reason why?

A1:  We switch it up for fun!

A2:  The site is actually programmed to randomly pick whose picture goes first each time you load it. Just for fun!


Q: What’s a blob?

A1:  It's lets you represent any set of bits/bytes.


Q: so is this dictionary of dictionary of.. list?

A1:  It's a dictionary, where the value is a list of dictionaries.


Q: can you have a dictionary inside a dictionary?

A1:  Yes!


Q: With multiple nested structures, at what point do you decide that the overall object has become unmanageable?

A1:  Data structure design is an art (like program decomposition). It get's unmanagable if you don't have good functions to be able to access the parts of the data structure you care about.


Q: Why do Chris and Mehran use light mode rather than dark mode in pycharm?

A1:  Personal perference and it's generally easier to see in presentations.

A2:  Light mode is easier to see when presenting so that is my guess, but maybe they just like light mode better


Q: is this the terminial ?

A1:  It is the console.


Q: can the value of a key be empty?

A1:  You can set a value to be None.


Q: Can a key be a list? Also, can it be a number?

A1:  Keys have to be immutable types. So keys can be an integer or float, but it can't be a list.


Q: if the value is an int does that means it's immutable?

A1:  Yes, the int is immutable. But you can still overwrite the value with a new value. (That's doing an assignment as opposed to a mutation.)


Q: What does it mean to be mutable vs immutable again?

A1:  Check out the handout: http://web.stanford.edu/class/cs106a/handouts/mutation.html for more information about that.


Q: What does the ' ?' mean in this example?

A1:  It's just the prompt printed in the console when asking the user for input.


Q: Just for clarification: a key is associated with one value only but the same value might be listed for multiple keys. Correct?

A1:  Correct.


Q: what happned to needing quotes around the key for lookup?

A1:  You need the quotes if you are using the actual key itself. You do not need the quotes when a variable is set equal to the key. animal = ‘dog’ sound = dict[animal] #no quotes because it is a variable

A2:  In this example, a variable was veing used as the key to loopup, so you didn't need quotes.


Q: do you have to convertthe animal input to a string?

A1:  The animal input will be a string (since the input function returns a string).


Q: so then the key variable acts like a index variable?

A1:  Yes.


Q: should it say my_dict[‘y’}?

A1:  Yes.


Q: are dictionaries ordered?

A1:  They are in the latest version of Python (they were not previously). They are now ordered by the order of insertion into the dictionary.


Q: If we go back on campus, can we get make-up candy from Mehran?

A1:  Yes!! When we're back on campus, come to my office hours and you can get make-up candy (and say hi in person)!!


Q: What does he mean by map?

A1:  Dictionary


Q: dont we want inverse[new_key] = [new value] because we want to creat the list?

A1:  That would work the first time! Then we want to think about how we can add the other values to the list as it goes through the loop.


Q: In inverse[new_key] = new_value , what is the new_key part for?

A1:  new_key is the value from the original dictionary. So it will be the age associated with that key.


Q: General question but are we able to work as software engineers with Python as our only known language?

A1:  Depends on the job. Generally, most software engineers know more than one language.


Q: Just to clarify, having a single key associated with a value will be like saying you can pick out one candy and so you pick out a packet of M&Ms thats one packet but technically has multiple candies in it?

A1:  You could think of it that way. The packet is one item that contains more things in it -- like a list is one item that can contain many elements.


Q: why the if condition, sorry not clear? if new_key not in inverse:

A1:  If the new key is not already a key in the dictionary, then we need to add it to the dictionary with an empty list so that we can later append new_value to that list.


Q: why is it inverse [new_key] instead of inverse[new_value]?

A1:  new_key is the value in the original dictionary.


Q: what does list_of_names = inverse[new_key] do?

A1:  That gets the list of names in the inverse dictionary at new key. So if the inverse dictonary had key 23 with value [‘juliette’] that line of code would return [‘juliette’]

A2:  It sets list_of_names to the existing list of names associated with the key new_key in the dictionary.


Q: couldn't we do just use inverse[new_key].append(new_value)?

A1:  Yes. It was done in two lines to make it a bit more clear pedagogically.


Q: list_of_names will be only a temporary list right? We won't use that to access list of names. Instead we will only say inverse[new_key]?

A1:  Yes (if I'm understanding your question correctly).


Q: if we didn’t understamd the inverse function, where should we go?

A1:  Office hours or LaIR. You can also try to trace through it after class again.

A2:  Come to office hours or Lair and we can talk about it.


Q: Once you split a line of text, how do you refer to the newly created elements? For example, if I split "CS106A is awesome" by using line.split(' ') (splitting by space character), how would I access the components "CS106A" "is" and "awesome?" Would line[1] give me "CS106A" after the split?

A1:  When you do split, you get a list. So line[0] would give you "CS106A" after the split.


Q: when we append to list_of_names why is our dictionary affected?

A1:  The value in the dictionary is a list, which is mutable. So when you append to the list, you are changing the list which is the value in the dictionary.


Q: In the beginning of the lecture when Chris said we’ve learned all the data types, what about Tuples?

A1:  We'll cover tuples in about a week. But tuples don't give you fundamentally more ability to represent data than a list. They are essentially just an immutable version of lists.

A2:  You have learned about all of the core data types. Tuples are a special data type that you will learn about soon.


Q: where can I find the ultimate question?

A1:  In the class slides. It's the problem of inverting a dictionary.


Q: Is the program supposed to return the whole grid for the key input it receives? Or is a specific RGB supposed to be chosen?

A1:  The program is going to show all the rgb colors associated with a key.


Q: how did he stop the program?

A1:  You can tyep CTRL-C in the terminal to stop a running program

A2:  ctrl+c stops the program


Q: Is it considered good style to have empty lines within a function, seperating blocks of code?

A1:  Sometimes having blank lines just makes it easier to read, so it's good to use when it may be helpful to separate different conceptual work in the function.

A2:  Yes! It can help make your code more reasonable.


Q: why are the colors in the shape of a circle?

A1:  That's the way Chris's plotting code makes things shows up (it maps RGB values to a circle).

A2:  That is how the Chris wrote the program to draw the coordinates.


Q: what does the line data[color_name] = [rgb] do?

A1:  It creates a list with the rgb value that is then assigned as the value to the key color_name in the dictionary data.

A2:  it sets the value for that key to be a list containing one element which the variable rgb.


Q: For our upcoming diagnostic, if we’re asked to write a program like this and connect it to a canvas, will we be able to run and see the canvas visually?

A1:  No you won’t be able to run your code on the next diagnostic.


Q: for color_list.append(rgb) are you associating the color_name with the corresponding rgb?

A1:  Yes! We are appending this rgb to the color_list associated with a given color key.


Q: I’m confused where Chris got color_list in that program and was able to append rgb values to it

A1:  Chris creates color_list if the key is not in the dictionary yet. If it is in the dictionary, that means that there is a color list associated with that key so he can append an rgb value to it.

A2:  color_list was used to look up the exsiting list of rgb values that might be in the dictionary associate with the key that was the color name.


Q: If we entered a key that wasn’t in the dict, is there a way that we could program so it wouldn’t crash?

A1:  Yes! You can do some error checking.


Q: is the wide variance in some color names maybe associated with color blindness?

A1:  live answered


Q: What was the process for deciding how many basic data types there are?

A1:  live answered


Q: can you do a color bar using a dictionary?

A1:  live answered


Q: how to add audio to animation?

A1:  You might to check out: https://wiki.python.org/moin/Audio


Q: how would the code know which rgb values belong to which color_name?

A1:  live answered