Chapter 9 Flashcards

1
Q

An element in a dictionary has two parts. What are they called?

A

Key and value

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

Which part of a dictionary element must be immutable?

A

The key

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

Suppose ‘start’ : 1472 is an element in a dictionary. What is the key? What is the value?

A

The string ‘start’ is the key, and the integer 1472 is the value.

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

What will the following code display?

stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
 print(stuff[3])

A

ccc

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

Suppose a dictionary named employee has been created. What does the following statement do?

employee[‘id’] = 54321

A

It stores the key-value pair ‘id’ : 54321 in the employee dictionary.

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

Suppose a dictionary named inventory exists. What does the following statement do?

del inventory[654]

A

It deletes the element that has the key 654.

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

How can you determine whether a key-value pair exists in a dictionary?

A

You can use the in operator to test for a specific key.

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

What will the following code display?

stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
 print(len(stuff))

A

3

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

What will the following code display?

stuff = {1 : ‘aaa’, 2 : ‘bbb’, 3 : ‘ccc’}
 for k in stuff:
  print(k)

A

1
2
3

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

What is the difference between the dictionary methods pop and popitem?

A

The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary.

The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.

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

Assume the following list exists:

names = [‘Chris’, ‘Katie’, ‘Joanne’, ‘Kurt’]

Write a statement that uses a dictionary comprehension to create a dictionary in which each element contains a name from the names list as its key, and the length of that name as its value.

A

result = {item:len(item) for item in names}

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

What does the items method return?

A

It returns all a dictionary’s keys and their associated values as a sequence of tuples.

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

What does the keys method return?

A

It returns all the keys in a dictionary as a sequence of tuples.

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

What does the values method return?

A

It returns all the values in the dictionary as a sequence of tuples.

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

Are the elements of a set ordered or unordered?

A

Unordered

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

Assume the following dictionary exists:

phonebook = {‘Chris’:’919-555−1111’, ‘Katie’:’828-555−2222’,
 ’Joanne’:’704-555−3333’, ‘Kurt’:’919-555−3333’}

Write a statement that uses a dictionary comprehension to create a second dictionary containing the elements of phonebook that have a value starting with ‘919’.

A

phonebook_copy = {k:v for k,v in phonebook.items()
if v.startswith(‘919’)}

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

After the following statement executes, what elements will be stored in the myset set?

myset = set(‘Jupiter’)

A

particular order): ‘J’, ‘u’, ‘p’, ‘i’, ‘t’, ‘e’, and ‘r’.

15
Q

Does a set allow you to store duplicate elements?

16
Q

How do you create an empty set?

A

You call the built-in set function.

17
Q

After the following statement executes, what elements will be stored in the myset set?

myset = set(25)

A

The set will contain one element: 25.

18
Q

After the following statement executes, what elements will be stored in the myset set?

myset = set(‘www xxx yyy zzz’)

A

The set will contain these elements (in no particular order): ‘w’, ‘ ‘, ‘x’, ‘y’, and ‘z’.

19
Q

After the following statement executes, what elements will be stored in the myset set?

myset = set([‘www’, ‘xxx’, ‘yyy’, ‘zzz’])

A

The set will contain these elements (in no particular order): ‘www’, ‘xxx’, ‘yyy’, and ‘zzz’.

19
Q

After the following statement executes, what elements will be stored in the myset set?

myset = set([1, 2, 2, 3, 4, 4, 4])

A

The set will contain these elements (in no particular order): 1, 2, 3, and 4.

20
Q

How do you determine the number of elements in a set?

A

You pass the set as an argument to the len function.

21
After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8])  myset.update([1, 2, 3])
The set will contain these elements (in no particular order): 10, 9, 8, 1, 2, and 3.
22
After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8])  myset.update('abc')
The set will contain these elements (in no particular order): 10, 9, 8, 'a', 'b', and 'c'.
22
What is the difference between the remove and discard methods?
If the specified element to delete is not in the set, the remove method raises a KeyError exception, but the discard method does not raise an exception.
23
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4])  set2 = set([3, 4, 5, 6])  set3 = set1.intersection(set2)
{3, 4}
23
How can you determine whether a specific element exists in a set?
You can use the in operator to test for the element.
24
After the following code executes, what elements will be members of set3? set1 = set([10, 20, 30])  set2 = set([100, 200, 300])  set3 = set1.union(set2)
{10, 20, 30, 100, 200, 300}
25
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4])  set2 = set([3, 4, 5, 6])  set3 = set1.difference(set2)
{1, 2}
26
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4])  set2 = set([3, 4, 5, 6])  set3 = set2.difference(set1)
{5, 6}
27
After the following code executes, what elements will be members of set3? set1 = set(['a', 'b', 'c'])  set2 = set(['b', 'c', 'd'])  set3 = set1.symmetric_difference(set2)
{'a', 'd'}
27
Look at the following code: set1 = set([1, 2, 3, 4])  set2 = set([2, 3]) Which of the sets is a subset of the other? Which of the sets is a superset of the other?
set2 is a subset of set1, and set1 is a superset of set2.
28
What is object serialization?
The process of converting the object to a stream of bytes that can be saved to a file for later retrieval.
29
When you open a file for the purpose of saving a pickled object to it, what file access mode do you use?
'wb'
30
When you open a file for the purpose of retrieving a pickled object from it, what file access mode do you use?
'rb'
31
What module do you import if you want to pickle objects?
The pickle module
32
What function do you call to pickle an object?
pickle.dump
33
What function do you call to retrieve and unpickle an object?