February 5th, 2021
Q: Can you use in if you don’t know what’s in the string?
A1: Yep!
Q: if we did ‘cb’ in ‘abcd’, would it return False since it is not in the correct order, or does order matter?
A1: The order matters! So that would return False.
Q: Would 'ac' in 'abcd' return true? If not could you do 'a' or 'c' in 'abcd' or would you have to seperate it into 2 in statements?
A1: ‘ac’ in ‘abcd’ would return False. The order matters. To check if ‘a’ is in ‘abcd’ or if ‘c’ is in ‘abcd’ you could do: if ‘a’ in ‘abcd’ or ‘c’ in ‘abcd’:
Q: can you do 'b' in s?
A1: Yes! You could set s equal to a string and check if ‘b’ in s.
Q: hey Juliette! is hm4 due Weds as usual?
A1: Hi! Yes homework 4 is due weds feb 10th
Q: how is ‘for ch’ easier than ‘for i’? They seem to work the same?
A1: if you do for i in range(len(s)): you need to grab the character in the string by doing ch = s[i] With for ch in s, you have access to the character, not the index. The difference is in for i you have the index and for ch you only have the character.
Q: Could you reverse the for each loop with the reversed() function we talked about previously?
A1: Yes! for ch in reversed(s)
Q: is ch a function on its own in python?
A1: ch is just a name that Nick chose for the variable. You could call it something different if you want. When using strings, ch is common practice for programmers.
Q: in the for each loop, do we have to say “ch” or can we call it “char” or any other variable we want?
A1: You can call it whatever you want
Q: I get differernt result when I do result += result + ch + ch, can't I use += for this equation?
A1: When you add multiple things to a string in one step, you don’t want to use +=
Q: can we use any phrase before the “in” or is “ch” a python keywork that it recognizes?
A1: You can use any name for the ch. When working with strings, it is convention for programmers to use ch, but you won’t get errors if use a different name.
Q: could we have done a second for loop and returned a[i] that matched ch in b?
A1: Is this what you mean? for ch in a: for i in range(len(b)): if b[i] == ch that would also work.
Q: yes thanks!
A1: :)
Q: How is a list different from a grid? Was our grid in sand pretty much just a list?
A1: You grid in sand was similar to a list of lists!
Q: Do strings use () and lists use []?
A1: We think of it like strings use “” or ‘’ and lists use []
Q: If you did nums[0] would it return nothing or give an error message
A1: If you did nums[0] when to was an empy list it woud give an error message like “index our of range”
Q: why do we say that strings are immutable if we are able to change them with the “+” function? I’m not fully getting the difference between mutable and immutable.
A1: We wil go over this more in section this week, but if you have: a = ‘CS106A’ b = ‘ is awesome!’ a = a + b We are creating a new string (a+b) and updating the variable a to point to this new string. When we say that strings are immutable, we mean that you cannot change the value on the right side of the arrow pointing from a variable to a string. Wherase, with lists you can change what is on the right side of the arrow.
Q: can you append multiple elements in one command? nums.append (2, 3, etc.)
A1: No, append only takes one at a time.
Q: can u write nums.append(2, 3, 4)
A1: No, append only takes one at a time.
Q: is nums the common accepted name for lists, like s for string?
A1: It is for a list of numbers. I usually use lst as my default name for a list like s for a string.
Q: Are there special cases where we would want to say "not x in" as opposed to "x not in", or is the latter always preferable?
A1: The latter is always preferable.
Q: So what does .append do?
A1: It adds the value to the end of the list.
Q: Thank you for clarifying, Juliette!!
A1: :) Thanks for asking great questions!
Q: if you search in a list would it only return if the full string is in there? if the list contains 'computer' but I ask 'comp' in list, would it be true or false?
A1: That would return False. It searches for full string matches.
Q: You don’t have to use ‘’ when using in for the list?
A1: You do it you want to check if a string is in a list but not if you are checking if an int is in a list.
Q: Is num = i + 1 instead of i because of zero-based indexing?
A1: Yes! Exactly
Q: Is there a different thing for ls for windows? when i use it it says its not a recognized command
A1: it should be py affirm.py -affirm Hermione
Q: when setting the constant name does it have to be capitalized like in the notes? ex: ALPHABET = … & STATES = ..
A1: That is just a style preference that programmers have agreed on. Your code will run without errors if it is not capitalized, but the all caps is like a reminder “Hey this is a constant, please never change it”
Q: is it in - affirm Lisa, -affirm a function in affirm.py, Lisa is a parameter?
A1: You can think that -affirm and Lisa are both parameters to the main function. Then the main function sees the -affirm paramter and calls print_affirm passing Lisa as a parameter to that function.
Q: why does affirm have a dash in front of it
A1: We call that a flag argument. These arguments indicate which function to run in main. It is just convention to use the - infront of any flag arguments.
Q: why does prof. parlante switch off between # and ‘’’ to make a comment? Are they interchangeable or are there rules for when to use each?
A1: yes! Use the “”” “”” under the def line for each function to describe it and add doctests. Anywhere else in your code you should use # comments.
Q: why does he have to args[1] instead of just typing in bart
A1: Because that way we can generalize to any name! Otherwise it would only print nice things about Bart.
Q: So by default things we type into the command line are strings?
A1: Yes!
Q: Why do we need it to be a number? Thank you
A1: When you type things in as arguments, they are treated as strings. range needs an int as a parameter, so we have to change the string to an int so that we can use it in range.