-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_5.py
More file actions
34 lines (22 loc) · 1.38 KB
/
Assignment_5.py
File metadata and controls
34 lines (22 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1. An empty dictionary's code looks like this: `{}`.
2. The value of a dictionary with the key 'foo' and the value 42 is `42`.
3. The most significant distinction between a dictionary and a list is that a dictionary is an unordered collection of key-value pairs, while a list is an ordered collection of elements.
4. If you try to access `spam['foo']` when `spam` is `{'bar': 100}`, you will get a `KeyError` exception, as the key 'foo' does not exist in the dictionary.
5. The expression `'cat' in spam` checks if the string 'cat' is a key in the dictionary `spam`, while `'cat' in spam.keys()` does the same thing, but explicitly checks the dictionary's keys.
6. The expression `'cat' in spam` checks if the string 'cat' is a key in the dictionary `spam`, while `'cat' in spam.values()` checks if the string 'cat' is a value in the dictionary.
7. The shortcut for the following code:
```python
if 'color' not in spam:
spam['color'] = 'black'
```
is `spam.setdefault('color', 'black')`.
8. To "pretty print" dictionary values, you can use the `pprint` module and its `pprint()` function. For example:
```python
import pprint
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
pprint.pprint(my_dict)
```
This will output the dictionary in a more readable format:
```
{'age': 30, 'city': 'New York', 'name': 'Alice'}
```