-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_18.py
More file actions
70 lines (53 loc) · 2.05 KB
/
Copy pathAssignment_18.py
File metadata and controls
70 lines (53 loc) · 2.05 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
1. Create a `zoo.py` file and define the `hours()` function that prints the string `'Open 9-5 daily'`. Then, use the interactive interpreter to import the `zoo` module and call its `hours()` function.
```python
# zoo.py
def hours():
print('Open 9-5 daily')
```
In the interactive interpreter:
```python
>>> import zoo
>>> zoo.hours()
Open 9-5 daily
```
2. In the interactive interpreter, import the `zoo` module as `menagerie` and call its `hours()` function.
```python
>>> import zoo as menagerie
>>> menagerie.hours()
Open 9-5 daily
```
3. Using the interpreter, explicitly import and call the `hours()` function from `zoo`.
```python
>>> from zoo import hours
>>> hours()
Open 9-5 daily
```
4. Import the `hours()` function as `info` and call it.
```python
>>> from zoo import hours as info
>>> info()
Open 9-5 daily
```
5. Create a plain dictionary with the key-value pairs `'a': 1, 'b': 2, and 'c': 3`, and print it out.
```python
>>> plain_dict = {'a': 1, 'b': 2, 'c': 3}
>>> print(plain_dict)
{'a': 1, 'b': 2, 'c': 3}
```
6. Make an `OrderedDict` called `fancy` from the same pairs listed in 5 and print it. Did it print in the same order as `plain`?
```python
>>> from collections import OrderedDict
>>> fancy = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> print(fancy)
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
```
The `OrderedDict` prints the key-value pairs in the order they were added, which is different from the order in the plain dictionary.
7. Make a `defaultdict` called `dict_of_lists` and pass it the argument `list`. Make the list `dict_of_lists['a']` and append the value `'something for a'` to it in one assignment. Print `dict_of_lists['a']`.
```python
>>> from collections import defaultdict
>>> dict_of_lists = defaultdict(list)
>>> dict_of_lists['a'].append('something for a')
>>> print(dict_of_lists['a'])
['something for a']
```
The `defaultdict` automatically creates a new list for the key `'a'` when it is accessed for the first time, and we can then append a value to it.