Lecture Materials

Questions & Answers

Q: Is the contest part of assignment #7, which is also due on June 10th?

A1:  No. It's separate from assign 7.

Q: do we have to use “key” in the for loop? Or anything works

A1:  Any variable name works

Q: can elements in a dictionary have the same value if associated with different keys?

A1:  Elements can’t be associated with different keys, but different keys can point to the same value

Q: Is .csv for any data?

A1:  It’s for spreadsheet-like data

Q: where do you write json?

A1:  We’ll talk about that soon

Q: So json is like a file to store dictionaries whereas txt is a file to store strings?

A1:  Yes, that’s a good way of thinking about it

Q: how does python know what the ages are since you have specified names but not ages

A1:  It’s specified in the JSON, which is then read into the program

Q: could he just say print(name, json.data[name])?

A1:  Yep

Q: Why is the example.json file written in that format?

A1:  Just to match the format in the Python file

Q: Can we use the traditional with open(file) as f with json too?

A1:  Yep

Q: how come our lists/dictionaries are formated in this way instead of just listing the elements one after another in one line? How come they are written on multiple lines with different tabbing of the brackets and squiggly brackets?

A1:  It’s just more readable for a programmer! Python doesn’t care

Q: So json saves it as a list variable?

A1:  No, it loads it either as a list or a dictionary, depending on the contents of the json file

Q: How does json.load(file) know if your datastrcture is a list or a dictionary?

A1:  live answered

A2:  It uses some cool string parsing! Remember that dictionaries start with { and lists start with [

A3:  The text in the file has either [] for lists or {} for dictionaries.

Q: What json.dump do?

A1:  It writes a collection to a json file.

Q: does the file name have to exist already? or can we create it in the .dump function?

A1:  does the file name have to exist already? or can we create it in the .dump function?

A2:  You can make it when you open the file by doing open(filename, ‘w’)

Q: isn’t age_data doing the same as the two prior lines (file and data)?

A1:  file was the opened file, data was the entire dictionary and age_data referred to a single element in the dict

Q: So we can’t do data[0][‘MaxTemp’]?

A1:  You can, but it’s a little less readable

Q: how could we devide the python window into two parts like what Chris has? or json files just show in a separate window?

A1:  Window -> Editor Tabs -> Split

Q: can we do data[0][‘MaxTemp]?

A1:  Yep

Q: what makes the dictionary be named Monday?

A1:  The data in the json file

Q: when should we use data = json.load in comparison to json.dump

A1:  live answered

A2:  .load reads in a json file, and .dump dumps to a json file

Q: Is it open(filename, ‘w’) or file = open(‘w’)?

A1:  file = open(filename, ‘w’)

Q: Is filename in open(filename, ‘w’) a placeholder for the name of the file?

A1:  Yep

Q: How did he upload the humidity?

A1:  There’s a website called WeatherUnderground which has this data!

Q: I can not access today's slide.

A1:  http://web.stanford.edu/class/cs106a/lectures/18-NestedStructures2/18-NestedStructures2.pdf

Q: I am not sure how monday[‘MaxTemp’] works?

A1:  monday is a dictionary, and we’re accessing the value associated with the key ‘MaxTemp’

Q: He did for day in ….? Is the variable day automatically connected to the weekdays? Was it defined

A1:  It’s just a variable name, there’s no connection to the days in the json file until it stores the dictionary

Q: Can we just do a recap of Chris’s code?

A1:  Feel free to ask questions you have about it on Ed

Q: Do true and false in .json files need to be lower case, or is it just that they can be?

A1:  Yep - if you’re interested in why we can chat about it after class!

A2:  Needs to be lower case. But Python knows to do the right thing when you read/write JSON files.

Q: How do we create a json file ?

A1:  You can use "dump" with a Python structure (e.g., list or dictionary) to create a JSON file.

Q: I think he will be able to answer this when Chris comes back on but did he delete his previous work or does he have multuple functions open on this program and now we are focused on a main function

A1:  He’s just reediting the one file

Q: Would it be possible to get original slides from the teaching team? Otherwise, we cannot see Chris's examples to take notes! (This is preferable to having to access the Canvas video if we want to see worked examples in progress)

A1:  The slides are on the lecture page

Q: Do we use “ “ or ‘ ‘ for keys? How about strings? Single characters?

A1:  Either is fine

Q: does it matter that the constant is in single quotes and the dictionary key is in double quotes ‘ ‘ vs “ “ ?

A1:  No. The key is a string, so you use either single or double quotes.

A2:  Nope

Q: how do you get a split screen view of two files on PyCharm?

A1:  Window -> Editor Tabs -> Split

Q: But the *examples* are not taken down in the slides. Would it be possible to take screenshots of important key points that are reviewed in lecture?

A1:  The examples are in the lecture code download - is there a particular part of lecture we’re missing?

Q: Remind me why we need the “ if country not in data[year]” line?

A1:  live answered