Lecture Materials

Questions & Answers


Q: quick question: will assignment due dates always switch to wednesday when the diag comes around then switch back to friday a week or two after?

A1:  All the assignment due dates are on the class schedule on the CS106A website.


Q: can you have a list of strings?

A1:  Yes.


Q: what happens differently when we use the x = x[0] + ‘z’ + x[2] ? is it diving the original string x?

A1:  It's using some pieces of the original string x to make a new string, which is then assigned back to x.


Q: What would stop the counterfeiter from copying the legit codes from the box? Does the program only take the code once?

A1:  yes! Once a code has been used, it cannot be used again.


Q: Could it be problemetic since the unique number is only 4 digits?

A1:  You can increase the size of the unique number as needed.


Q: Just to be clear, the file you are calling (in this case, mydata.txt)must be in the same folder as the python file itself?

A1:  Yes


Q: How does it know to read a line for each iteration of the loop instead of like a character?

A1:  The for each loop is designed to read the file line by line. You could process each line (string) to get individual characters.


Q: How does Python recognize where does a sentence end?

A1:  It recognizes the end of the line by a newline character that is at the end of each line in the file.


Q: what”s the code for running doctests again?

A1:  py -m doctest filename.py -v (python3 instead of py on mac)


Q: how does the program know to take it line by line instead of word by word or character by character

A1:  The for each loop for reading files is designed to read line by line. You can then process each line (string) to get individual words or characters.


Q: But if you changed the variable to “character” instead of line in the for each loop, would it print out each character?

A1:  no. The for each loop for reading files is designed to read line by line. You can then process each line (string) to get individual words or characters.

A2:  No. The name of the variable does not effect how the file is read.


Q: so \n is “white space” I thought only “ “ space was white space — what other characters fall under “white space”?

A1:  space, tab (\t) and newline (\n) are the main whitespace characters.


Q: How would you access files that are not in the same folder as the pycharm file?

A1:  You can give the full path name of the file (i.e., specify all the directories to go through to get to the file).

A2:  You can list their full path.


Q: is next() a built in function?

A1:  yes


Q: what happens if you put next(f) inside the for loop?

A1:  You'll just skip all all the lines in the file


Q: What happens if your file isn’t separated by enter/isn’t in neat lines?

A1:  If there are no newlines in the file, you will get the entire file as one big line.


Q: How did Chris clear the terminal so quickly?

A1:  you can type clear


Q: how did chris clear his terminal in pycharm?

A1:  You can type clear


Q: is .txt the only file type we can read? how do we loop over and access other types of file like .py?

A1:  There are various libraries in python that allow you to read files in other formats. In this class, we'll mostly deal with text files.


Q: Question about graphics/animations. Are shapes/text/canvas immutable or mutable?

A1:  They are mutuble.


Q: Can next() be used on something else besides files?

A1:  What else would you want to use it for?


Q: can you indicate a line in the middle of the file to skip/return with the next function?

A1:  Generally, you'd just read the file to then determine if you want to do something with it or not.


Q: how can I make the program print only when pressing ‘enter’?

A1:  pressing ‘enter’ is the equivalent of the user entering the empty string. You can check for that condition and only print once that has happened.


Q: why are file names considered strings

A1:  That's the type that allows for naturally representing the file name.


Q: are all lines from .txt converted to the string type in Python?

A1:  Yes!


Q: How come Chris uses only python instead of python3 to run his program?

A1:  He create an "alias" that allows one command to do the same thing as another command.


Q: if you did words_list[0] would that print the first word?

A1:  Yes, if you did print(words_list[0])


Q: Wait, so if you want to have your file separated into lines, do you have to end each line in the file with /n?

A1:  Yes


Q: should it be for line in f?

A1:  Yes


Q: Are the words delimited by a comma and in quotes in the word document or does python do this by default when reading a text file?

A1:  The words are not delimited by a comma and in quotes in the txt file. Chris’s code read the file and put each word in a list and printed that out. When the list was printed, the quotes and commas show up.

A2:  The file only contains words -- no quotes, no commas. The quotes are the way python represents strings. The commas are separators for items in a list.


Q: What is the ‘f’ referring to in the line that has the ‘with’ operator?

A1:  That line is saying: open this file and store the contents in a variable f. We can then use f to read each line in the file.


Q: what are vars

A1:  variables


Q: Since the list does not get confused by spaces, is it necessary to remove the spaces?

A1:  It is necessary to remove the spaces. Otherise the elements inside the list would have spaces in them.


Q: What is a stack and a heap?

A1:  They are parts of memory. The stack is whether the variables in functions are stored. The heap with where the values for those variables are stored.


Q: What do the 42 and 28 mean? I’m quite confused

A1:  Those represent the memory addresses of these lists.


Q: is garbage collector auto for every coding language?

A1:  No. Many languages do not have garbage collectors (you have to manually free memory when you are done with it).


Q: is it possible overwrite/edit a variable with keeping the same url? or does it not really matter now with garbage collector

A1:  It depends on if the variable is mutable or immutable. For immutable variables, you have to change the url because you can't edit the value of the variable directly (such as strings).


Q: I’m confused by the whole situation of /n… is this something that is automatically at the end of all lines with multiple lines (between one line and the next), and would not be present if there’s only a single line?

A1:  Yes. You can think of \n as the "newline" character, which means start a new line (like hitting the "enter" key).


Q: what is a .csv file? how is it different from .txt?

A1:  csv stands for comma separated values. So it is a list of values separated by commas rather than new lines.

A2:  csv stands for comma separated values. It's a form of text file that contains values separated by commas.


Q: is values a list? so that when the line is split each string is appeneded to it?

A1:  The a line is split, it creates a list where each element of the list is a separate value (e.g., separated by commas or whitespace) from the line that is being split.


Q: can you import an image file into a canvas?

A1:  tkinter allows you to include images into a canvas, but we won't be talking about that in class. You can look for it online.


Q: why is Chris using ; at the end of constant assignment?

A1:  It's just a typo. He just fixed it.


Q: where it says ‘as file’ are file and f interchangeable?

A1:  Yes! That is just the name of the variable.


Q: I’m getting an error that says super() takes at least 1 argument

A1:  What are you trying to do?


Q: How do we format all the data in the .txt file (like with the consistent format in the example) with code before importing it in to a Python file?

A1:  That's something you could either do manually or write a program to format the data in a way that's easy to use.

A2:  You can format by putting each value onto a new line. Also remember to put the .txt file into the same folder as your .py file


Q: Can you do slice[2:4]

A1:  That would give you a list of [lat_str, long_str] but the helper function takes in the individual values.


Q: how do the lat/long correspond to Canvas x/y?

A1:  Chris wrote another program to do that translation.


Q: why are some lon and lat values negative

A1:  they are around the equator and prime meridian


Q: Why didn’t it print the comma in between the latitude and longitude?

A1:  It just prints out the separate value. The comma is separating the parameters to print. The comma itself doesn't get printed.


Q: could you use the next(line) more than once

A1:  Yes.

A2:  Yes!


Q: Do we not need to reference the first_line variable anywhere?

A1:  live answered


Q: I was thinking if you do a line in file loop but do the command that takes off and returns the first line why doesn’t the loop get thrown off since its a for each loop but its being manipulated each time

A1:  i agree that putting next inside the for-each loop would cause some strange phenomena. I don’t think ive ever done that


Q: can you skip more than one line (in the first line ) for example, skip the first 3 lines or the first paragraph ?

A1:  Yes! You can use next multiple times


Q: did the lat/long itself give the shape of USA or did you drow it as outline to begin with?

A1:  that came from the lat lon. No outline was drawn!


Q: can you dynamically move those data points?

A1:  you could animate them!!!


Q: Can the ‘nex’ function be used on lists?

A1:  No.


Q: Does the next function have to be outside the for loop?

A1:  If you put it inside the for loop, you would skip over all of the lines in the file.


Q: how were lon and lat values converted to x, y cords on canvas

A1:  Chris wrote a cool function to do this!


Q: How can we name a list (or any variable) using a string from a file? For example, create a list name “latitudes” and store every latitude in that list?

A1:  live answered


Q: Thank you!!

A1:  live answered