Lecture Materials

Questions & Answers

Q: I am working on programming a visualization tool for teaching science. I would really like to make a GUI to make the tool more approachable for users. Will we learn how to make a GUI in this class?

A1:  I can put together a video about this towards the end of the class! It’s not on our formal syllabus, though

Q: Could the constant punctuation be defined as a list?

A1:  Yep

Q: always got confused about the parameter filename - do we include the extension .txt in the name?

A1:  Yes

Q: is split() default delimiter for strings is spaces?

A1:  Yep

A2:  is split() default delimiter for strings is spaces?

Q: for line.split() don’t you have to pass in what to split it by? i.e. ‘,’ or ‘ ‘

A1:  It defaults to spaces

Q: Why don’t you have to cast filename as a string when printing?

A1:  It’s already a string

Q: Do the values have to be unique

A1:  No

Q: Sorry, I missed about "r" in open(filename, "r"). What is "r"?

A1:  You want to ‘r’ead through the file

Q: How is this different to a variable, as variables have some kind of unique identifier that’s linked to some piece of information?

A1:  You can store as many keys and values as you want in a dictionary, without havng to explicitly write their names when you’re writing the program.

Q: why not just make a list of lists?

A1:  A few reasons! 1) Lists require indices, and dictionaries use keys (which can be of any type) 2) Lists require a sense of order, and dictionaries don’t 3) DIctionaries are much, much faster (we’ll get there soon)

Q: how is this different from a list?

A1:  It’s not indexed, nor is it ordered

Q: What happens if we have commas or colons in the names within a dictionary?

A1:  If it’s within the quotes in a string, that’s fine

Q: do mixed-type dictionaries exist?

A1:  Can you clarify what you mean? Where are the types being mixed?

Q: can we add new pair directly using the assigning thing

A1:  Yep

Q: so dictionaries are passed y reference in the heap?

A1:  Yes

Q: e.g. a val of str, a val of float, and a val of int or a key of str a key of float and a key of int

A1:  Keys and values can have any types (although typically, all the keys are all the same type and the values are all the same type)

Q: So dictionaries are mutable?

A1:  Yep

Q: is there are names funtion where you can print all the keys in a dictionary?

A1:  Yep, it’s alled .keys()

Q: on a more general level when would we use list vs. dictionary vs. regular varible as they serve some what similar roles (storing information that can be acessed later )

A1:  We’ll talk about that later on!

Q: are values assumed to be strings?

A1:  No, they can be any type

Q: Is the dictionary more like 2-dimensional list or 1D list with additional info about elements? What happens if we ask a statement like "if 32 in ages" for the previous example?

A1:  1) It doesn’t really have a ‘dimension’
2) It searches through the keys, so that would be False

Q: Can values only be strings?

A1:  They can be any type

Q: Why do you add using [] instead of {}?

A1:  You index into a dict or list using []

Q: How do we create a dictionary out of a file (eg 2 columns of information)?

A1:  Once this lecture is over, you should try and write the code to do that!

Q: what is the value type of a key? char?

A1:  Any type you want

Q: are keys case sensitive?

A1:  Yep

Q: could you say phone[Pat] = Jenny?

A1:  Yep, although that would be a strange phone number :)

Q: What if you wanted to search through the values and not the keys?

A1:  That’s a pretty uncommon thing to need to do, but you can use the .values() function to get back the values

Q: Is that why there cannot be duplicate usernames on websites/runescape/etc? There needs to be unique key values?

A1:  Great thought! We don’t *quite* use dictionaries to represent this but the principle is exactly the same :)

Q: sorry, to clarify, could you alter Pat so that it holds whatever number is stored in Jenny’s value

A1:  Oh, that would be phones[‘Pat’] = phones[‘Jenny’]

Q: What is the difference between primitive and immutable?

A1:  ‘Immutable’ is the formal name!

Q: And what if you wanted it to print a list of the entire dictionary?

A1:  What do you mean by a list of the entire dictionary?

Q: instead of a list of all the keys, a list of the keys and their values

A1:  dict.items() does something similar, but it doesn’t return a *list*

Q: why do we need to string cast “key” in print(str(key) + … ?

A1:  We didn’t really need to because they were strings, good catch!

Q: Could we use the for loop for values to return spectific keys for those values?

A1:  live answered

Q: Do we have a summary of all the list and dictionary functions?

A1:  Do we have a summary of all the list and dictionary functions?

A2:  In the Python reader

Q: What does it mean to “return something similar to a range of keys/values”? Doesn’t it just return the exact range?

A1:  It does - it’s just not the same return type as the range function, although it behaves similarly and contains all the keys or values

Q: What is you had two keys with the same name, what would dict.pop(key) do then?

A1:  Keys have to be unique

Q: Do we always type the file name when running the program in the terminal?

A1:  Depends on how the program works

Q: Can you write a code that overrides the case sensitivity? So that it will could more and MORE as the same?

A1:  You can convert each of the words to lowercase or uppercase using .lower() or .upper()

Q: For a word-count tool it might be helpful to print the list of the words sorted by how many times they appear—would you have to convert to a list to do that, or is there a way to sort the entries in the dictionary and print that?

A1:  We’ll talk about sorting like that later this quarter! Great question :)

Q: can you not use curly brackets on the dictionary to look up the value. i.e. dict{key}?

A1:  Nope, you always use [] to ‘index’ into a data structure

A2:  can you not use curly brackets on the dictionary to look up the value. i.e. dict{key}?

Q: Is there a way to alter all values at once? Say add 1 to everyones' age?

A1:  Only in a for loop!

Q: Just to clarify, one key can have multiple values?

A1:  live answered

Q: If the Key’s are case sensative, why can I have multiple people with the same “key” and value in my phone contact list? That is probably a silly question — cellphones are pretty complicated. But conceptually, why can you do that?

A1:  live answered