Chapter 5 Slowniki Flashcards

1
Q

What does the code for an empty dictionary look like?

A

{}

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

What does a dictionary value with a key ‘foo’ and a value 42 look like?

A

{‘foo’: 42}

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

What is the main difference between a dictionary and a list?

A

The items stored in a dictionary are unordered, while the items in a list are ordered.

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

What happens if you try to access spam[‘foo’] if spam is {‘bar’: 100}?

A

You get a KeyError error.

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

If a dictionary is stored in spam, what is the difference between the expressions ‘cat’ in spam and ‘cat’ in
spam.keys()?

A

There is no difference. The in operator checks whether a value exists as a key in the
dictionary.

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

If a dictionary is stored in spam, what is the difference between the expressions ‘cat’ in spam and ‘cat’ in
spam.values()?

A

‘cat’ in spam checks whether there is a ‘cat’ key in the dictionary, while ‘cat’
in spam.values() checks whether there is a value ‘cat’ for one of the keys in
spam.

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

What is a shortcut for the following code?

if ‘color’ not in spam: spam[‘color’] = ‘black’

A

spam.setdefault(‘color’, ‘black’)

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

What module and function can be used to “pretty print” dictionary values?

A

pprint.pprint()

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