Quiz 4 Material Flashcards
Is repetition of items allowed in dictionaries?
no
Is indexing a dictionary the same as indexing a list? if no, what do you use instead?
you index with keys instead
dictionaries consist of _____-______ pairs
key-value
how would you add something to a dictionary?
d[key] = value
how would you delete something from a dictionary?
del d[key]
what would the len(d) give us?
how many key-value pairs are in the dictionary
what will each of these do?
dict_name.copy()
dict_name.keys()
dict_name.values()
dict_name.items
- creates a copy of the dictionary
- returns set of key values in the dictionary
- returns set of values in dictionary
- returns set of (key, value) pairs in the dictionary
what will each of these do?
list(dict_name)
key in dict_name
dict_name.popitem()
dict_name.pop(key[,default)
dict_name.get(key[, default)
- returns a list of all the keys in the dictionary
- returns True if dictionary has a key: key, else False
- remove and return a (key, value) pair from the dictionary
- if key is in the dictionary, remove it and return its value, else return default. if default is not given and key is not in the dictionary, a KeyError is raised
- return the value for key if it is in the dictionary, else default. if default is not given, it defaults to None, so it doesn’t raise a KeyError
what will this return?
str1 = ‘Have a nice day’
x = str1.split()
[‘Have’, ‘a’, ‘nice’, ‘day’]
(we can also pass in a argument to split the string into something other than the whitespace)
what will each of these return?
x,y = 4,7
print(y)
numbers = [1,2,3,4,5]
a,b,c,d,e = numbers
print(d)
print(y): 7
print(d): 4
what does this return?
my_list = [[8,6,7],[5,3,0,9]]
print(my_list[1][0])
5
color_str = ‘red,orange,yellow,green,blue,purple’
color_list = color_str.split()
print(color_list[0])
red,orange,yellow,green,blue,purple
color_str = ‘red,orange,yellow,green,blue,purple’
color_list = color_str.split(‘,’)
print(color_list[0])
red
steps to reading a file
- tell python to read from
- python sends it and your OS gives back a readable ‘stream’
- read the data from the stream (line-by-line or all at once)
- streams are always read-once
- close the file when you are done with it
what are the 2 ways to open a file?
file_connector = open(filename,’r’)
with open(filename, ‘r’) as a file_connector