Quiz 4 Material Flashcards

1
Q

Is repetition of items allowed in dictionaries?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is indexing a dictionary the same as indexing a list? if no, what do you use instead?

A

you index with keys instead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

dictionaries consist of _____-______ pairs

A

key-value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how would you add something to a dictionary?

A

d[key] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how would you delete something from a dictionary?

A

del d[key]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what would the len(d) give us?

A

how many key-value pairs are in the dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what will each of these do?

dict_name.copy()
dict_name.keys()
dict_name.values()
dict_name.items

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what will this return?

str1 = ‘Have a nice day’
x = str1.split()

A

[‘Have’, ‘a’, ‘nice’, ‘day’]
(we can also pass in a argument to split the string into something other than the whitespace)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

print(y): 7
print(d): 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what does this return?
my_list = [[8,6,7],[5,3,0,9]]
print(my_list[1][0])

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

color_str = ‘red,orange,yellow,green,blue,purple’
color_list = color_str.split()
print(color_list[0])

A

red,orange,yellow,green,blue,purple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

color_str = ‘red,orange,yellow,green,blue,purple’
color_list = color_str.split(‘,’)
print(color_list[0])

A

red

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

steps to reading a file

A
  1. tell python to read from
  2. python sends it and your OS gives back a readable ‘stream’
  3. read the data from the stream (line-by-line or all at once)
  4. streams are always read-once
  5. close the file when you are done with it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are the 2 ways to open a file?

A

file_connector = open(filename,’r’)

with open(filename, ‘r’) as a file_connector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what are the 2 ways to read a file line-by-line?

A

file_connector.readline() – the next unread line

for line in file_connector – the variable line will contain the next unread line each time through the loop

17
Q

what is the difference between file_connector.read() and file_connector.readlines()

A
  • returns one string representing the entire file
  • returns a list of all the remaining lines as strings
18
Q

what does urllib.request.urlopen(url) do?

A

open a web file to be read

19
Q

steps for reading from a web file

A
  1. import urllib.request
  2. open the web file usingurllib.request.urlopen(url)
  3. f.read().decode(‘utf-8’) OR f.readline().decode(‘utf-8’) OR f.readlines.decode(‘utf-8’) OR
    for line in f:
    line = line.decode(‘utf-8)