Lecture Materials

Questions & Answers

Q: what if we want to delete “ and ‘ as well?

A1:  You can add those to the punction string.

Q: Could punctuation be a list instead of a string>

A1:  could be!

Q: append function can’t be used with strings because they’re immutable, right?

A1:  that correct. Instead we are building result by concatenation (so it is rebinding the var name to a new, bigger string)

Q: Is Mehran blurry for anyone else?

A1:  live answered

Q: Does line.strip() just get rid of trailing and leading white space? It doesn’t git rid of white space in between words right?

A1:  Correct

Q: could we just get len(word_list) to get word count?

A1:  that would work!

Q: Why is word_list = line.split()? How does that create a list of words?

A1:  split (with no parameter) breaks the string up by white space — and gives you the result as a list of strings

Q: what does line.split() call do in the word count program?

A1:  it splits a string by whitespace (thats things like spaces tabs etc)

Q: Why isn’t print in the for line in file loop?

A1:  there could be, it would print more :)

Q: what does ‘r’ mean?

A1:  That means that you are opening the file with only reading permissions, but that is the default for the open function. You do not have to worry about that too much.

A2:  its optional — says you are only “reading”

Q: So split takes a list and makes each item it’s own element. IS there a function that makes each element it’s own list? So you can append for a grid

A1:  not that i know of — but you could do that manually with a for loop

Q: What happens when you have keys that aren't unique in a dictionary?

A1:  its a problem! values will get overwritten

Q: if you have a list of strings so like [[a, b, c, b]] do the commas separate each column?

A1:  that looks like a 2d grid! except you might want quotes around the strings

Q: Are dictionaries mutable?

A1:  yes!

Q: are dictionaries types of 2 dimensional lists?

A1:  they are their own type of collection — but like 2d lists they can hold lots of data!

Q: What is the different between a dictionary and a variable? Does a dictionary just contain multiple variables?

A1:  thats a nice way of thinking about it. A dictionary is a variable, but it can hold lots of data!

Q: dont we use braces for files also ???

A1:  Files don’t need to have braces — but dictionaries do!

Q: For sand question in assignment 4, why is the original code includes global variables (if global variable is totally replacable by parameters)?

A1:  good question! The starter code was written by someone outside the unversity and they used globals when it would have been much better to use parameters!

Q: what’s the difference between checking if value is in string or list? is one faster than the other if both works for a certain situation?

A1:  great question. If the list or string are “unsorted” then they are the same speed. But if you have a list which is sorted, you can check if a value is in it much faster!

Q: Are these actual ages?

A1:  perhaps :)

Q: are dictionaries mutable?

A1:  Yes!

Q: So we can have the same value associated to different keys, but one key for each value?

A1:  Yes! Keys must be unique but values do not have to be.

Q: why would you split a string by whitespace? wouldn’t it just stick together making it harder? e.g. ilovethisclass

A1:  If you had string s = “i love this class”, s.split() would return [“i”, “love”, “this”, “classs”]

Q: does the value have to be an integer?

A1:  No! The value can be any data type.

Q: Can you append to a dictionary by assigning a value to a new key that wasn’t initially defined?

A1:  yes >>> a = {'a':3} >>> a['b']=5 >>> a {'a': 3, 'b': 5} >>>

Q: how are dictionary elements better than variables? what would the use cases be?

A1:  a dictionary is a “collection” so you can think of it as a bag that can hold lots of data. There are so many use cases!

A2:  endless use cases! Perhaps you want to store a phonebook, or information about each student at stanford, or anything where you want to relate a list of keys to their values.

Q: so, do you always have to use single quotes on the names of a key in a dictionary to see if its in a dictionary? like ‘juliette’ ? or is ‘juliette’ the name of the key and its different than: juliette

A1:  In this case, the key is a string. So when looking up in the dictionary, you need to use the string ‘juliette’. If you tried to look up without the strings it would not work.

Q: can you add duplicates to a dictionary?

A1:  You cannot have duplicate keys. You can have duplicate values.

Q: Are the elements in the dictionary ordered in any way? Can you loop through a dictionary for example?

A1:  yes! they come back in alphabetical / sorted order. This is a new feature in python!!!

Q: is there a reason the values are in string form?

A1:  yes! It turns out phone numbers can start with a 0, which integers cant represent

Q: Are dictionaries indexed? As in, do they have an order, or is it just all in a big blob?

A1:  you can think about the keys as their index. They are now ordered, but that is a new feature of python3. Im still getting used to it :)

Q: Does sequence matter in dictionary?

A1:  they all get stored in alphabetical order. you can add in any order you want

Q: what happens if we call a key like Pat when the value is none?

A1:  it will return none!

Q: What happens if we set phone['Pat'] = phone['Jenny']?

A1:  In that case, the value for phone[‘Pat’] would be the phone number of Jenny.

Q: Why would one use a dictionary rather than a CSV file or soemthing?

A1:  Perhaps you don’t want to use an extra file! This is faster than reading from a file.

Q: if the value for ‘Pat’ is None, if you check to see if ‘Pat’ exists will it return True?

A1:  yes it will!

Q: can == and != be used when comparing strings?

A1:  Yes!

Q: if file mutable or immutable? whats an easy way to undersrand what types are im/mutable it without having to “remember” it?

A1:  files are mutable. There are very few immutable types — just int, float, string, tuple. The rest are mutable (with a few strange exceptions like the elusive frozendict)

Q: Is phonebook actually programmed as dictionary since it allows you to use identical first and last names as keys?

A1:  It is probably more complicated to allow users to have duplicate keys, but I bet that at some point in the code they use a dictionary! (or the equivalent in the programming language they use)

Q: how would you remove an entire key/value pair?

A1:  I think you can do del dict[key]

Q: wait I thought dictionary keys are immutable? how did we change mehran’s birthday?

A1:  “mehran” the string is immutible, but you can change the value its associated with (in other words you mutated the dictionary, but didn’t mutate the key)

Q: Ages are int which are immutable. how can you change that again? or are you changing the memory location that the name is associated to?

A1:  the later is correct!

Q: does the default value have to have the same type as the values in the dict

A1:  doesnt have to be, but it almost always is

Q: Must the type of key or value be consistent throughout the dictionary?

A1:  No! But its often the case

Q: What does the (key, default) do again?

A1:  If the key is not in the dictionary, it returns the default value.

A2:  its for the get function — so it does a lookup in the dictionary. if it finds the key, it returns the corresponding value. If it doesn’t find the key, it returns the default you provided

Q: What’s the difference between a tupple and a list? Are tupples immutable while lists are mutable?

A1:  That is one difference! We will go into more depth in the lecture on tuples.

A2:  yes! We will talk about this later in class — but you guessed it perfectly

Q: will there be a handout on dictionaries on the website?

A1:  there is a chapter in the python reader!

Q: By appending the keys to a list using the dict.keys() function, do they still have their dictionary values associated with them if they're called from the list?

A1:  it doesn’t matter how you get the key — if you do a lookup with a key, and its in the dictionary, you will get the corresponding value.

Q: if you clear a dictionary, do you still get a value back?

A1:  no

Q: What’s the best way to create an empty dictionary?

A1:  dict = {}

Q: can you specify a key when you use .pop or does it only remove the last key in the dictionary?

A1:  you can specify a key!

Q: Do we always use ‘r’ in with open…?

A1:  Its optional, it says “i am only going to read”

Q: Is there a way to order the dictionary by the most used to least used words?

A1:  yes there is! We dont cover it in cs106a and it uses some strange syntax: https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

Q: could we sort the dict based on high word count? how to do it?

A1:  Dicts are sorted alphabetically by keys. But you could write a program to create a list of keys where their order is their word count !

Q: can we order the keys in the dictionary according to value size?

A1:  This question showed up a lot. Here is a resource, though we dont cover it in cs106a https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

Q: is there a way to sort a dictionary alphabetically?

A1:  Dictionaries are sorted alphabetically.

Q: is there a way for us to sort dictionary values from greatest to least?

A1:  This question showed up a lot. Here is a resource, though we dont cover it in cs106a https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

Q: Is there a sort function for dictionaries?

A1:  This question showed up a lot. Here is a resource, though we dont cover it in cs106a https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

Q: who has office hours after this?

A1:  Mehran!

Q: Can you make the dictionary print/return values in ascending order? (like for the count word example)

A1:  This question showed up a lot. Here is a resource, though we dont cover it in cs106a https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

Q: Will we learn about sets?

A1:  in cs106a we stick to lists and dictionaries. sets are in cs106b (though they aren’t very different than dictionaries)

Q: if you break, where does it go?

A1:  live answered

Q: How are names added to the phonebook in this loop?

A1:  live answered

Q: how does the computer know that the enter keystroke returns the user input?

A1:  enter keystroke is represented by the empty string in this case. If the user types in the empty string (which means they didn’t type anything in) then we break.

Q: Why are you able to print without +’s?

A1:  its legal python :)

Q: How do you create an empty text file through python/through the terminal? And what in Mehran’s count_word program allows him to specify the txt file that he wants when he calls the program?

A1:  you can right click on your project and chose new -> file

Q: is there a if ____ in _____ function? or only: if ____ not in ____?

A1:  There is an if in!

Q: is there a way to check if a ‘value’ exists in the dict without having to loop through all keyes in the program?

A1:  Yes! You can ask if key in dict

Q: We aren’t using “else if” in this function - why do we every use elif? Does it make a difference vs. using if and then if again? It the first if condition is true, it won’t move forward to the second if condition anyway?

A1:  sometimes you dont want to check the second if in the case that the first passes

Q: Does phonebook need to be returned in read_phone_numbers if dictionaries are mutable?

A1:  We don’t have the dictionary in main. It was created in read_phone_numbers, so we need to give it to main so that it can be used there.

Q: Why we have to return phonebook since dictionary is mutable?

A1:  live answered

Q: Does Juilette ever sleep? She is always answering questions on Ed. PS: Thanks for the commitment! Quick responses save us a lot of anxiety:)

A1:  shes amazing!

Q: is it better to use functions in main thare are equal to a function? create_phonebook = read_phone_numbers

A1:  live answered

Q: If you were to make the phonebook so that it isn’t case sensitive, would you have to incorporate the ASCII values of the characters in your method?

A1:  live answered

Q: What if we can find a phone number based on the first letters of the name

A1:  live answered

Q: On the actual phonebook though, how is it that it is not case sensitive?

A1:  live answered