Lecture Materials

Questions & Answers

Q: Can homeworks be regraded as well?

A1:  not in the same way but if you have a question you can reach out to your section leader or to me

Q: Will we be graded for mid-quarter evaluation responses?

A1:  No, but we appreciate your throughtful feedback!

Q: For sand.py i try to right click on a doctest but no option to run them shows up? What should I do?

A1:  Try running them from the command line: python3 -m doctest sand.py -v (use py instead of python3 on windows)

Q: i didn’t know how to correctly do the ghost program so i just submitted what i have but im pretty sure most of it is wrong. how much will one program affect my grade? thanks!

A1:  Each assignment is worth about 10% of your grade. So its not the end of the world if you struggle with one assignment. But having said that you will want to make sure that you feel back on track pretty soon

Q: Do we have all of the info we need to start on the last problem of Assignment 4? Or will we be learning more about animation next week?

A1:  yes! You have it all

Q: if i am having trouble with the class content now and have used most available resources (lair, OH, CTL, ED) what tips do u have for understanding the material from the remaining half of this course

A1:  practice! Set yourself lots of easy challenges (print out odd numbers, reverse a string, etc) and code it up!

Q: can we do slices with strings

A1:  you can!

Q: will this lecture be needed to complete assignment 4?

A1:  nope. You already have everything you need for a4

Q: could we do for elem in example?

A1:  Yes!

Q: is there for each loop for string like in list?

A1:  Yes! You can do ‘for elem in example:’ in the code that Mehran has on the screen!

A2:  yes there is!

Q: Do you have to reference a string as a variable in order to access the indexes? Can I do “hello”[0] ?

A1:  I didn’t know the answer so I looked it up. It works! >>> "hello"[0] 'h' >>>

Q: for sand.py, r we supposed to make sure that clicking on rocks when we are on the sand setting won’t let the sand replace the rock? or is that an extension

A1:  That is part of the assignment

A2:  I was wrong in my previous answer. This would be an extension.

Q: What is a better style for string - double quote or single?

A1:  equally good. I use single quotes

Q: are strings also mutable like lists?

A1:  (spoiler alert, they are immutable)

Q: what is last day of lecture?

A1:  Nov 18th http://web.stanford.edu/class/cs106a/schedule.html

Q: Do you create new list too when you slice list?

A1:  you do

Q: why do we have to use ch?

A1:  You can use a different name for ch. This is just one conventional way to loop over characters in a string.

Q: Is there a way in Python to erase text that has been printed? for example, if I wanted to print a count from 1 to 10 but wanted the numbers to replace each other instead of all printing out in a vertical list, is there a command to remove the last value printed before printing the next one?

A1:  Its a good question. In standard python, no, the terminal can only be written to. But you can go straight to the operating system to work around that limitation. https://stackoverflow.com/questions/19596750/is-there-a-way-to-clear-your-printed-text-in-python

Q: can you use .append., .remove, etc on strings like on lists?

A1:  no! those are all “mutations” and strings are immutable

Q: So for sand.py, if we click on a rock it shouldn’t replace it with sand? I didn’t see any part of the assignment about that.

A1:  I was wrong in the last answer, my bad! You should replace it with sand.

Q: i missed the lecture in the beginning, when will the lecture be posted to canvas?

A1:  Later tonight!

Q: what lectures are relevant to assignment 4?

A1:  11, 12, 13 have new information pertinent to assignment 4.

Q: is string.find case sensitive

A1:  yes

Q: Can you pass a letter into split so that it separates the string around that letter?

A1:  yes you can! and it can be more than one character long

Q: I sent an e-mail about this, but when it comes to animation. If you have a starting point on a canvas and the width of a shape, you should add the width-1 to create the shape right?

A1:  My understanding is that the last pixel isn’t included (a little like a list splice)

A2:  You can just add the width. It may be helpful to draw this out to see why!

Q: why not return error when finding “bad”?

A1:  The function returns -1 rather than throwing an error to let you know that “bad” is not in “chris”

Q: I drew it out, and I’m just wondering. If you add the width, doesn’t that fail to account for the first point? So it would actually be one more than the width?

A1:  Not if the right most pixel isn’t included in the shape. Its inclusive of the left most pixel and exclusive of the right most

Q: is there a way to combine string.isalpha() and string.isspace()?

A1:  You could use an and to combine them in an if statement

Q: What’s the first character (character 0)?

A1:  ascii of 0 is a special NUL character. Doesn’t show up in standard text

Q: so where is this table of unicode/ ascii stored?

A1:  its encoded in the core python library

Q: what does sequential mean?

A1:  it means that ‘B’ is one number larger than ‘A’

Q: In sand.py, is there a reason we check if down-right is out of range but not just right?

A1:  You can add a doctest to check if right is out of range. But checking down-right also checks if right is in range because we are in a rectangle.

Q: does it already draw the character somewhere and use the labels of the numbers 65 for A etc?

A1:  Yes !!

Q: Zowie < Abc returns false?

A1:  Correct.

Q: Do programmers in other countries use the English basis of python or is python translated? Like would they be coding in CJK?

A1:  for CJK speakers, the often write comments in CJK but they write variable names in english. As a generalization

A2:  I wrote a paper on this! http://stanford.edu/~cpiech/bio/papers/codeInternational.pdf

Q: could we use append on strings?

A1:  no, thats a mutation! And strings are immutable

Q: why are some variable types mutable and others immutable?

A1:  Python can do some speedups if it can assume you wont change the value on the other side of the memory address.

Q: so is it like old style printers where they had “molds” of each character in various fonts - so Python already has these various characters already drawn somewhere and like a “google doc” link it just referes to it to create a string?

A1:  Yes, you can think of it that way!

Q: what are stack and heap

A1:  Stack is the name of the part of memory where your variables live. Heap is the name of the part of memory where the corresponding values live

Q: can you append to strings?

A1:  no, thats a mutation! And strings are immutable

Q: Does Chris have individual office hours?

A1:  Yes! Thursday at 11a

Q: Sorry what did the 28 and 91 represent in that slide?

A1:  the variable stores the memory address where the letters live. Those are memory addesses

A2:  Those were addresses where the strings are stored. You can think of those like the URLs pointing to the objects.

Q: What do the values 91 and 28 represent in that last example with abba?

A1:  Those were addresses where the strings are stored. You can think of those like the URLs pointing to the objects.

Q: What is the f for ?

A1:  That is part of the special format to use f strings.

Q: is the second diagnostic cumulative or does it cover just week 5-week 10 material

A1:  Cumulative! With an emphasis on the second half of the coruse

Q: When would be a good situation to use fstrings instead of normally using the + concatenation? More human-readable? Or because you can be more procise, i.e. with the decimal points? Thanks!

A1:  exactly! those are two great situations.

Q: Okay, thank you. Do the actual values matter or bear any significance?

A1:  no, we just make them up when writing demos. python can chose any part of memory so there are no standard addresses

Q: what is the behind-the-scene operation python performed when a person changes the a mutable input (why some value are mutable and some others are not)?

A1:  Its based on the type of your variable. Every string is immutable. Every int is immutable. Every float is immutable. Every list is *mutable*

Q: if string is immutatble, what is r = str[i] + r doing?

A1:  its binding! Its making a brand new string which is the concatination of two other ones and binding r to that new string. Old strings are unchanged

Q: since strings are immutable, is there a difference in using for i in range(len(str) or for ch in str?

A1:  you may want the index for other reasons — but generally for each is pretty useful

Q: I dont see the screen. Is it just me?

A1:  looks like its a zoom issue for some folks. if you change screen and come back it seems to fix it

Q: Will the ethics slides be posted on the website?

A1:  yes!

Q: So when we create new strings we always have to create a place to store them with something like result = ' ' ? And is this analogous to new_list = [] for lists?

A1:  yes!

Q: thanks! I left and joined again. It works now

A1:  team debugging :D

Q: hi team! :D

A1:  hello :)

Q: Thank you! And lastly, if we feel that we deserved our grade on the first diagnostic, should we not fill out the regrade form? i.e. is that form only for regaining points that were clearly lost by mistake?

A1:  No need to fill out the form! its only if you think there was an error in grading that changes your grade!

Q: Where will these slides be posted on the CS106A website please?

A1:  Under the lecture 14 tab on the website!

Q: Thank you Katie! I’m loving your presentations!!!

A1:  Thank you :) Very kind, and happy to chat about them any time!

Q: How is mutable list and immutable string store differently in python suitcases?

A1:  live answered