Chapter 9 Dictionaries and Sets Flashcards

1
Q

You can use the ___ operator to determine whether a key exists in a dictionary.

A

in

The in operator determine whether a value exists in a set.

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

You use ___ to delete an element from a dictionary.

A

the del method

You can delete an existing key-value pair from a dictionary with the del statement:
del dictionary_name[key]

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

The ___ function returns the number of elements in a dictionary:

A

len( )

You can use the built-in len function to get the number of elements in a dictionary.

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

You can use ___ to create an empty dictionary.

A

{ }

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

The ___ method returns a randomly selected key-value pair from a dictionary.

A

popitem

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

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

The ___ method returns the value associated with a specified key and removes that key_value pair from the dictionary.

A

pop()

The pop method returns the value associated with a specified key and removes that key-value pair from the dictionary.
if the key is not found, the method returns a default value.

dictionary.pop(key, default)

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

The ___ dictionary method returns the value associated with a specific key. If the key is not found, it returns a default value.

A

get()

The get method as an alternative to the [] operator for getting a value from a dictionary. The get method does not raise an exception if the specified key is not found.

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

The following function returns the number of elements in a set:

A

len()

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

You can add one element to a set with this method

A

add()

You use the add method to add n element to a set.

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

You can add a group of elements to a set with this method.

A

update

You can add a group of elements to a set all at one time with the update method.
Sets are mutable objects so you can add items and remove items from them.

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

The set method removes an element, but does not raise an exception if the element is not found.

A

discard

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

This set method removes an element and raises an exception if the element is not found.

A

remove

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

This operator can be used to find the union of two sets.

A

|

The union of two set that contains all the elements of both sets.

set1.union(set2) or set1 | set 2

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

This operator can be used to find the difference of two sets.

A

-

The difference method get the difference of two sets. The difference of set 1 and set 2 is the elements that appear in set 1 but not appear in set 2.

set1 - set2

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

This operator can be used to find the symmetric difference of two sets.

A

The symmetric difference of two sets is a set that contains the elements that are not shared by the sets

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

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

A

items()

The items method returns all of a dictionary’s key and their associated values.

17
Q

Which method would you use to get all the elements in a dictionary returned as a list of tuples?

A

items

18
Q

What will be the result of the following code?
ages = {‘Aaron’ : 6, ‘Kelly’ : 3, ‘Abigail’ : 1}
value = ages{‘Brianna’}

A

KeyError

19
Q

A dictionary can include the same value several times but cannot include the same key several times.
True or False?

A

True

20
Q

Sets are created using curly braces {}

True or False?

A

False

21
Q

The set remove and discard methods behave differently only when a specified item is not found in the set.
True or False?

A

True

22
Q

What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?

A

{1 : ‘January’, 2 : ‘February’, …. 12 : ‘December’}

23
Q

What is the value of the variable phones after the following code executes?
phone = {‘John’ : ‘5555555’, ‘Julie’ : ‘5557777’, }
phone {‘John’} = ‘5556666’

A

{‘John’ : ‘5556666’, ‘Julie’ : ‘5557777’, }

24
Q

Which method would you use to get all the elements in a dictionary returned a s a list of tuples?

a. pop
b. list
c. items
d. keys

A

items

Chapter 9 Q (page# 448)

The items method returns all of a dictionary’s keys and their associated values. They are returned as a special type of sequence known as a dictionary view. Each element in the dictionary view is a tuple, and each tuple contains a key and its associated value.

25
Q
A
26
Q

A dictionary can include the same value several times but cannot include the same key several times.
True or False?

A

True

Chapter 9 Q

27
Q

What does the get method do if the specified key is not found in the dictionary?

a. It throws an exception.
b. It does nothing.
c. it returns a default value.
d. You cannot use the get method to specify a key.

A

It returns a default value.

Chapter 9 Q

28
Q

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order).

cities = {'GA' : 'Atlanta', 'NY' ; 'Albany', 'CA' : 'San Diego'}
    if 'CA' in cities:
        del cities['CA']
        cities['CA'] = 'Sacramento'
        print(cities)

a. {‘CA’ : ‘Sacramento’}
b. {‘CA’ : ‘Sacramento’, ‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}
c. {‘CA’ : ‘Sacramento’}
d. {‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}

A

{‘CA’ : ‘Sacramento’, ‘NY’ : ‘Albany’, ‘GA’ : ‘Atlanta’}

Chapter 9 Q

29
Q
The issubset() method can be used to determine whether set1 is a subset of set2?
True or False?
A

True

Chapter 9 Q

30
Q

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
True or False?

A

True

Chapter 9 Q

31
Q

The difference of set1 and set2 is a set that contains only the elements that appear in set 1 but do not appear in set2.
True or False

A

True

Chapter 9 Q

>> set1 = set([1, 2, 3, 4])
>> set2 = set([3, 4, 5, 6])
>> set3 = set1 - set2
>> set3
{1, 2}
32
Q

The elements in a dictionary are store in ascending order, by the keys of the key-value pairs.
True or False?

A

False

Chapter 9 Q

33
Q

The union of two sets is a set that contains only the elements that appear in both sets.
True or False?

A

False

Chapter 9 Q (page# 467)

The union of two sets is a set that contains all the elements of both sets