Lecture Materials

Class Announcements

Homework 3, Sand, is due on Wednesday at 11:55pm PT with a 48-hour grace period.

Questions & Answers


Q: Will part 1 of Assignment #2 be graded or is it just part 2?

A1:  Both parts will be graded!


Q: If we submitted Homework 3 yesterday on Paperless, will the submission link change or are we good to go?

A1:  I think that is fine. You can check if your submission is still there sometime before the deadline on Weds to make sure.


Q: when do we get quiz 1 results back

A1:  Before or during class on Weds.


Q: when will the quizzes be graded and how will we access them

A1:  You should have your grades back by or during class on Weds. We will post info for how to access it on the website


Q: can these lecutre notes be uploaded? they’re not accessible on the page yet

A1:  They should be now!


Q: So something like 'Δ'.isdigit() returns True?

A1:  I beleive isalpha only returns true if the character is between (a-z) or (A-Z)


Q: is a comma a space?

A1:  ‘,’.isspace() would return False


Q: If I am understanding it correctly, is '@'.isspace() true?

A1:  That would return False.


Q: can we use elif on the sand assignment?

A1:  Sure!


Q: in line with the question before, what would return @ true?? I am not sure if I am following :(

A1:  '@ is not alpha nor a digit nor a space. So none of those would return true for @.


Q: if you did ‘Kitten123’ .lower(), would it return false since not all of the alphabetic characters are lowercase?

A1:  It will actually work and ignore any characters that are not alpha.


Q: so only space and tab are considered "space" characters?

A1:  Yes and whitespace characters like ‘\n’ which means newline. We will talk about that in section / on the next assignment.


Q: when do you start off with result = s and result = ''

A1:  It depends on what you want the result string to start with. If you want to build off of s, you can do result = s. If you want it to start as an empty string, then you do result = ‘’


Q: oof regarding the question of ‘Kitten123’, i meant .islower(), would it then return false?

A1:  That will return True because islower also ignores any nonalpha characters.


Q: where do we find this example?

A1:  In the string2 section of this link: https://wopr-service-qbrbcbuzwa-uw.a.run.app/


Q: just to confirm: the lower() and upper() functions don’t modify the string they are called from, right?

A1:  Correct.


Q: how does making it .lower() help it distinguish between both upper and lower?

A1:  In this example, we want to grab all ‘c’, ‘a’, ’t’ in a word whether they are uppercase or lowercase. So rather than checking if a char == ‘c’ or char == ‘C’ we can just make char lower and then check if it equals the lowercase version of the letter.


Q: when we have a bucn of "if" conditions, how do we prevent that line from becoming really wide? can we do "if" and then another "if" directly below, each with different parameters?

A1:  Yes! You can do an if with another if below and they will each have their different conditions.


Q: How does s[i].lower() return true when you get an uppercase C

A1:  .lower() returns the lowercase version of s[i]. Not to be confused with .islower() which returns True or False.


Q: how does .lower work for capital letters

A1:  .lower() returns the lowercase version of s[i] (whether s[i] is a capital letter or a lower case letter). Not to be confused with .islower() which returns True or False.


Q: here could you just do if s[i] != 'x'

A1:  In the above example, yes. But we want it to work for general strings that have other values besides c, a, t, and x.


Q: does th technique just demonstrated improve the performance of a program or is it just more readable

A1:  It just makes it more readable.


Q: Could we make return False or return grid into a variable like that?

A1:  You cannot set a variable equal to a return statement. But you could return a variable.


Q: why is it returning a capital T in the catty example?

A1:  lower does not modify the original char. It makes a new char that is the lowercase version of the original one. At the end of the above example, we added the original char (T) to the result string.


Q: so does lower() NOT modify the actual letter but just check what letter is it?

A1:  lower() gives back the lower case letter of the char it was called on. Where as islower() checks if a letter is lowercase. So: ch = ‘J’ x = ch.lower() print(x) print(ch) This would print: j J


Q: is elif like the else if in other coding languages?

A1:  yes


Q: we can use if/elif for sand right?

A1:  Yes


Q: why would you use ‘elif’ and not ‘if not’?

A1:  You want to use elif when you have multiple cases that are mutually exclusive.


Q: Can you not use if/elif by just using a lot of if statements, because python reads in order?

A1:  Sometimes you want cases to be mutually exclusive. In other wors, if one this is true you do not want it to go down the other path. Consecutive if statements do not give you that ability.


Q: can you define the if/elif function again?

A1:  pretend that says: elif day == ‘Wednesday’:

A2:  elif is a way to set up more than two cases. With if/else there are only two cases. With if/elif/else you can have more than two options. So somethng like: if day == ‘Monday’: print(‘mondays are tought’) elif day == ‘Wednesdays’ print(‘CS106A is the bright spot of my Wednesday’) else: print(‘Today is not Monday or Wednesday’) That is just a random example but I hope it shows how you can have more than just two options with an if statement.


Q: so we only need result if we’re adding something/changing the input?

A1:  Yes


Q: can we finish the sand assignment without using these elif statements?

A1:  Yes


Q: Where do we access the “has_digit” experimental server?

A1:  https://wopr-service-qbrbcbuzwa-uw.a.run.app/


Q: It’s hard to work on that problem with Nick’s beautiful singing in the background!

A1:  lol


Q: are we supposed to save this link ^? "wopr" or can you access it from the cs106a website - and where can we access it

A1:  You can access it from the lecture notes. I can add a link on the main website.


Q: why does Nick leave line 5 empty? Could he write ‘return false’ in line 5?

A1:  Yes! It just improves readability to include an extra line.


Q: why is s[3:3] not equal to s[3]

A1:  Because slices are inclusive of the first but exclusive of the second index. So that would be the empy string. To get s[3] you would need to so s[3:4]


Q: how could you include the last char?

A1:  You can not specify an end index, or go to len(s). So s[4:] or s[4:len(s)] would give you from the 4th index to the end of the string.


Q: if you did 0:3 would it include the ‘p’?

A1:  what was the string for this?


Q: Is there any way to get a slice including the last char?

A1:  Yes! To do that you can either not specify an end index, or go to len(s). So s[4:] or s[4:len(s)] would give you from the 4th index to the end of the string.


Q: Do you want to sing jeopardy theme song?

A1:  Lol I should have done this until he came back. Nex time.


Q: for individual office hours should we make an appointment w you or just show up?

A1:  Just show up! It is first come first serve, so you might have to wait a bit


Q: Would putting s[:] give us the whole string

A1:  Yes!


Q: What does Nick mean by losing chars when you slice?

A1:  When you slice a string you choose a subset of the characters, so you “lose” the ones that you don’t pick.


Q: do square brackets count as a number

A1:  no


Q: would you ever need to slice for the whole string? Wouldn’t for i in range(len(s)) cover for that?

A1:  Yes, you do not need to slice to get the entire string.


Q: Are functions like s.find() universal? Or is specific to this class “library” like bit.move()?

A1:  They are part of the string library, but that library comes with the regular download of Python. So anyone who has python has s.find()


Q: “if you did 0:3 would it include the ‘p’?” for string python

A1:  Yes it would! if s = ‘python’ then s[0:3] would be ‘pyt’


Q: When you lose chars in the string, does python actually throw away the characters or break up the string in a way that I can’t call or do anything with the original string later?

A1:  It depends on how you save the slice. If you don’t overwrite the variable storing the unsliced string, then that string remains unchanged. So: s = ‘CS106A’ x = s[3:5] print(x) print(s) would print: 06 CS106A s remains unchaged by the line below it.


Q: if it gives us for could we just put 4:7

A1:  can you elaborate a bit on this?


Q: why don’t we need anything before the return inside the function?

A1:  Can you elaborate a bit on this?


Q: Is there a fast way to pull out a string comprised of a non-contiguous substring? i.e. Say I want a string comprised of every other char of s, is there some 'Python'.do_thing() which returns 'Pto' without a for loop?

A1:  There is a 3rd parameter to slice which takes in the step size. So if you had: s = ‘python’ s[::2] gives back ‘pto’ That line says slice the string from the beginning to the end in steps of size 2.


Q: Why does this current code work for ‘’? ***without the if no brakets case it still returned ‘’

A1:  In that example, the description said that if there are any brackets, there will be only one of each. So if there is a left bracket we can assume there is a right bracket. Not sure if this is what you are asking, so feel free to follow up if it is not.


Q: why -1 ?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: why is there left==-1 for the brackets program?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: why did we do -1?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: why it it == -1

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: what did the left == -1 mean?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: Why does -1 reference the case for no brackets?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: right is also looking for brackets. why didn’t we have to check if there are any brackets for right as we did for left?

A1:  In that example, the description said that if there are any brackets, there will be only one of each. So if there is a left bracket we can assume there is a right bracket.


Q: why did we write if left == -1?

A1:  '-1 is what find returns if a character isn’t found in a string.


Q: so if the command cannot find [, it will return -1?

A1:  Yes


Q: What is left -1? -1?

A1:  we do left == -1 because -1 is what find returns if a character isn’t found in a string.


Q: we can't see what he is coding

A1:  Thanks :)


Q: def brackets(s): left = s.find('[') if left == -1: return '' right = s.find(']') return s[left + 1: right] Why do we not have to put an if before right?

A1:  In that example, the description said that if there are any brackets, there will be only one of each. So if there is a left bracket we can assume there is a right bracket.


Q: Why does s[-1] give the last character?

A1:  Nick will explain rn :)


Q: but if there are no brackets at all, what does right get?

A1:  If there are no brackets, we would never make it to that line because it would return the empty string for not finding a left bracket.


Q: is there a way to access a backwards string? Like what would happen if we did s[6:0]?

A1:  There is a way, but you can’t do it like that. Come to office hours and we can chat about it!


Q: If -1 is the right most character then why does the find test return -1 if it cannot find

A1:  great question! The -1 depends on the context you are using it in. Find returning -1 doesn’t the last index, it means it isn’t in the word. The inventors of find should have surely picked another value, but alas.


Q: how do we distinguish between the -1 that refers to a character not in the string vs the last character?

A1:  Great q! It depends on how you are using the -1. If it what find returnes, then it means that the value is not present in the string. If you are using slices, then -1 refers to the last character of the string.


Q: how am i supposed to take notes for this class LOL i been holding it in my head, but as we learn more i feel like im forgetting stuff. like do i write computer code on paper?

A1:  Taking notes in CS is tough for me still. I try to just listen and absorb in lecture and then practice the learning by coding along with examples or going through any examples I missed during lecture on my own afterwards. Feel free to come to office hours to chat more about this if you want !!


Q: why did Nick say “if left == -1:” for if its the bracket. Why would “-1” = the left bracket

A1:  if find returns -1, that means that the character is not present in the string.


Q: yes thank you!!

A1:  :)