Lecture Materials

Questions & Answers


Q: I have a question: why do we have to initialize variables outside of for loops? For instance x = 0 for … if … x = …

A1:  Somtimes we want to add things to a variable or only change the variable if a certain condition is met. So it needs to have a certain value before the loop starts.


Q: Hi! When can we expect Section 6 solutions to go out? Also are there any addtional resources where we can get more practice for 2D arrays.

A1:  Hi! I usually post section solutions Friday evening, after all sections have met. Check out the lecture slides and code for more examples as well as the section problems that have to do with 2d lists.


Q: can you give a one-sentence (ish) definition of what a json is? I’m still confused.

A1:  its a text-file that stores a single python variable. Its easy to save a variable to file using json, and to load a variable. The variable can be a dictionary, list, number, string or some mix of the above

A2:  A json is a file that stores infomation in a special way such that when we do json.load(filename) it will return a dictionary representation of what is in the file.


Q: Since graphics/animation were not on the practice diagnostic, should we still expect them to be on the diagnostic?

A1:  Graphics/Animation are fair game. Check out the section problems and lecture examples to practice.


Q: Which section problems from the past few weeks would you recommend as preparation for the diagnostic?

A1:  All of them are designed to help you study for the diagnositc :) This diagnostic will focus on the material learned since the previous diagnostics, so perhaps focus on the sections that have happened since the previous diagnostic.


Q: could you have a coordinate as a dictionary key? (like (1,2) = s) if you wanted to assign a value to a coordinate what would you use?

A1:  yes! (1,2) is a tuple like Mehran is saying. Since they are immutable, they can be keys in a dictionary.


Q: Besides the practice diagnostic / ‘additional practice,’ are there other new materials to review?

A1:  Those are the new materials. Section problems from this week would also be good review material.


Q: Is the LaIR assignment server down? I tried submiting assignment 5 before 1pm and the server seems not be working.

A1:  No, Paperless is not down. Can you send me an email?


Q: what’s the best way to change a character in a string if strings are immutable? Is temporarily changing it into a list, doing the change, and then turning it back into a string after the change a good idea?

A1:  That is one idea! Another idea is to build up a new string that takes the characters from the original string and concatenates it with the character you want to insert.


Q: Can tuples only store immutable elements?

A1:  They can also store mutable elements.


Q: I feel like I’ve heard ‘tuple’ in maths before… how is that related?

A1:  It is a similar concept. Perhaps that is where the name came from.


Q: can we add things to tuples, similarly to how we add or concatonate to a string?

A1:  Adding things to tuples is similar to how you add to strings. You will have to build up a new tuple.


Q: If a tuple is identified by a comma, how can an empty tuple exist since it has nothing in it?

A1:  Empty tuple is just ()


Q: Why do immutable types exist in the first place? I feel like it simply limits the usability?

A1:  This is out of the scope of the class, but immutable objects improve runtime efficiency. Come to office hours to chat more about this!


Q: can you have steps in a tuple slice?

A1:  Yes!


Q: is an empty tuple (,) or ()?

A1:  ()


Q: does the if stanford also work for lists

A1:  Yes


Q: For the string.find() function, can you specify a start and end? What other functions allow you to specify start and end?

A1:  Yes! There are a lot of functions. Feel free to google optional parameters to a given python function to see if it can take in a start and end.


Q: What if the tuple has strings? What is max and min?

A1:  If the tuple is all strings, max will return the string that is alphabetically last. Min will return the string that is alphabetically first.


Q: what would the max be if the tuple has a string and an int?

A1:  If there are two different types in one tuple, max/min/sum will throw an error.


Q: do max, min, and sum only work for tuples that only have ints and floats? For example, if a tuple is a mix of strings and ints will they work?

A1:  A mix of strings/ints will not work. But if it is only strings, min will return the first string in alphabetical order.


Q: where would we want to use a string versus a tuple, since they are both immutable?

A1:  Use a string when you are storing a single piece of text. Use a tuple when you are storing mutliple related pieces of information. Tuples can store things other than just chars.


Q: Maybe a silly queston but: Is there an underlying reason (perhaps hardware-level?) for the types of data structures that are ‘allowable’ in python, or in programming in general? It feels like lists, tuples, arrays, dictionaries are all very similar renditions of each other, and I am curious why! I can’t come up with alternative structures off the top of my head probably because my imagination’s been squashed but… are there examples of other computational paradigms/languages that use novel data structures to represent information?

A1:  I am not positive, but I think that when people designed Python they came up with an inital list of data structures that they would optimize to store information. Yes there are languages that use other data structures! If you take CS106B you will learn about all of these other structures.


Q: Can you somehow refer to the members of this tuple using the tuple[i] syntax?

A1:  Yes! You can say first=tuple[0]


Q: so could you call x or y as variables outside the tuple?

A1:  Can you elaborate on this?


Q: Just curious: Can you have 2 dimensional tuples just like 2D lists? And if so, what would be an application of this?

A1:  Yes! One use case could be if you wanted to store the location of a pixel its rgb components. You could say pixel =((x,y), (r,g,b))


Q: Can you not use lists to return multiple values from a function? Is it bad stylistically?

A1:  It is better style to use tuples to return multiple values


Q: does the name of the variable have to be the same length as the variable? so does yyy for year not work?

A1:  no (to your first question). Name lengths are independent of the length of the value


Q: Mehran’s birthday????

A1:  yes :)


Q: Do the variables returned need to be in the same order as the tuple of variables?

A1:  yes!


Q: Do we need the parentheses? Can we also write: dd, mm, yyyy = get_date()

A1:  you may, that works!


Q: How do programmers account for all the ways a program might crash? Do we just add multiple if conditions?

A1:  Better yet, you should design it to be “simple” so you dont have to worry about it returning tuples of different sizes


Q: what is the range of content on the diagnostic? what’s the last lecture we’ll need to watch to be prepared?

A1:  The diagnostic covers everything up to and including Monday’s lecture (lecture 18)


Q: What if the function returns three values and you only use two variables, which one does it leave out?

A1:  it crashes…


Q: So for example, when we’re initiating vars, can we do: x, y = 0, 0 or should they still be separate lines?

A1:  That will work, but it is stylistically preferred to use separate lines


Q: does dict.items work when there are multiple values for a key?

A1:  dict.times will return a list of tuples where each tuple is a key value pair. So if a key has a list as a value, the second part of that tuple pair would be a list.


Q: When can you leave out the brackets when specifying a tuple?

A1:  When you return multiple values from a function


Q: What does dict.items() return? Is it not a list?

A1:  A list of tuples where each tuple has a key and a value


Q: even though a tuple is immutable, can you reassign values to variables afterwards assigning them using a tuple?

A1:  Try it out in pycharm! If you do something like a = [1,2,3] b = (a, ‘juliette’) a = [3,4,5] print(b) This will print: ([1,2,3], ‘juliette’)


Q: When can use apply the list() function to turn something into a list? Would that work when applied to strings?

A1:  It can be applied to anything that you can iterate over. So it would work for strings.


Q: whats the difference between .keys and .getkey?

A1:  .keys() gives back a list of all keys in a dictionary. .get(key) will get the value associated with they given key.


Q: so basically tuples are u when the length is set? (like x,y coordinates, rgb values) cause they seem like just more limited lists otherwise

A1:  That is one use case!


Q: Can you add tuples ?

A1:  Yes!


Q: I'm still a little confused--what is the advantage of a tuple over a list? When should we use tuples?

A1:  One time to use tuples is when you are returning something from a function. Otherwise, tuples are used to store values that are logically related, like storing an address.


Q: Do you not beed brackets around the tuple we are returning?

A1:  No. Tuples are defined by the commas


Q: how come when we return a tuple it is not surrounded by parentheses?

A1:  Tuples are defined by the commas separating the value. The parens are just stylistic convention.


Q: Wait, how did Mehran specify that he was returning a tuple or is it automatically a tuple bc it’s three values?

A1:  If you return multiple values separated by a comma, it is automatically interpreted as a tuple


Q: so each “r”, “g”, and “b” holds one value gotten from the user but rgb as a whole is taken in as one variable?

A1:  Yes.


Q: Did we indicate that rgb will be a tuple? When we return more than one element, does it convert the result automatically to a tuple?

A1:  If you return multiple values separated by a comma, it is automatically interpreted as a tuple


Q: What does he mean by collection? Dictionary or list?

A1:  A collection is any datastructure that holds information so that could be a list or a dictionary or a tuple


Q: does the diagnostic include everything up to this lecture?

A1:  Up to and including monday’s lecture. Tuples will not be on the second diagnostic.


Q: what’s the difference between sorted() and .sort()

A1:  sort can only be used with lists.


Q: do we always get lists when we use sorted()?

A1:  Yes. sorted returns a sorted list of the given object.


Q: what’s the difference between .sort() and sorted()?

A1:  sort can only be used with lists.


Q: Can you sort with different types of information in a single tuple, and what are the orders of each type if we can? For example, how would we reorganize (a, 2, B, 3.0)?

A1:  You would have to write a custom sort function to do this.


Q: why wouldn’t it be [‘CHERRY’, ‘banana’,…]?

A1:  Because lower case letters come before upper case letters in python.


Q: why do you have to define get_len- could you just use key=len

A1:  live answered


Q: for this example function get_len, if there are two strings in list strs that are the same length, then what will the order of the sorted list be

A1:  I think the default tie breaker is aplhabetical order.


Q: Why do we not have to use a for each loop and then apply the length function to each word?

A1:  sorted does that for you!


Q: Is there a way to concatenate tuples like strings? As in combine them together?

A1:  Yes! a = (1, 2, 3) b = (4, 5, 6) print(a+b) would print: (1, 2, 3, 4, 5, 6)

A2:  live answered


Q: If you are graphing text on a canvas, how do you change the text?

A1:  live answered