You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Notes/02_Working_with_data/02_Containers.md
+53-32Lines changed: 53 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,12 +194,12 @@ s1 - s2 # Set difference
194
194
195
195
## Exercises
196
196
197
-
### Objectives
197
+
### Exercise 2.4: A list of tuples
198
198
199
-
### Exercise A: A list of tuples
200
-
201
-
The file `Data/portfolio.csv` contains a list of stocks in a portfolio.
202
-
In [Section 1.7](), you wrote a function `portfolio_cost(filename)` that read this file and performed a simple calculation.
199
+
The file `Data/portfolio.csv` contains a list of stocks in a
200
+
portfolio. In [Exercise 1.30](../01_Introduction/07_Functions), you
201
+
wrote a function `portfolio_cost(filename)` that read this file and
202
+
performed a simple calculation.
203
203
204
204
Your code should have looked something like this:
205
205
@@ -245,7 +245,9 @@ for row in rows:
245
245
246
246
Finally, you’ll return the resulting `portfolio` list.
247
247
248
-
Experiment with your function interactively (just a reminder that in order to do this, you first have to run the `report.py` program in the interpreter):
248
+
Experiment with your function interactively (just a reminder that in
249
+
order to do this, you first have to run the `report.py` program in the
250
+
interpreter):
249
251
250
252
*Hint: Use `-i` when executing the file in the terminal*
251
253
@@ -270,8 +272,10 @@ Experiment with your function interactively (just a reminder that in order to do
270
272
>>>
271
273
```
272
274
273
-
This list of tuples that you have created is very similar to a 2-D array.
274
-
For example, you can access a specific column and row using a lookup such as `portfolio[row][column]` where `row` and `column` are integers.
275
+
This list of tuples that you have created is very similar to a 2-D
276
+
array. For example, you can access a specific column and row using a
277
+
lookup such as `portfolio[row][column]` where `row` and `column` are
278
+
integers.
275
279
276
280
That said, you can also rewrite the last for-loop using a statement like this:
277
281
@@ -285,12 +289,14 @@ That said, you can also rewrite the last for-loop using a statement like this:
285
289
>>>
286
290
```
287
291
288
-
### (b) List of Dictionaries
292
+
### Exercise 2.5: List of Dictionaries
289
293
290
-
Take the function you wrote in part (a) and modify to represent each stock in the portfolio with a dictionary instead of a tuple.
291
-
In this dictionary use the field names of "name", "shares", and "price" to represent the different columns in the input file.
294
+
Take the function you wrote in part (a) and modify to represent each
295
+
stock in the portfolio with a dictionary instead of a tuple. In this
296
+
dictionary use the field names of "name", "shares", and "price" to
297
+
represent the different columns in the input file.
292
298
293
-
Experiment with this new function in the same manner as you did in part (a).
299
+
Experiment with this new function in the same manner as you did in Exercise 2.4.
294
300
295
301
```pycon
296
302
>>> portfolio = read_portfolio('portfolio.csv')
@@ -314,10 +320,12 @@ Experiment with this new function in the same manner as you did in part (a).
314
320
>>>
315
321
```
316
322
317
-
Here, you will notice that the different fields for each entry are accessed by key names instead of numeric column numbers.
318
-
This is often preferred because the resulting code is easier to read later.
323
+
Here, you will notice that the different fields for each entry are
324
+
accessed by key names instead of numeric column numbers. This is
325
+
often preferred because the resulting code is easier to read later.
319
326
320
-
Viewing large dictionaries and lists can be messy. To clean up the output for debugging, considering using the `pprint` function.
327
+
Viewing large dictionaries and lists can be messy. To clean up the
328
+
output for debugging, considering using the `pprint` function.
321
329
322
330
```pycon
323
331
>>> from pprint import pprint
@@ -332,10 +340,11 @@ Viewing large dictionaries and lists can be messy. To clean up the output for de
332
340
>>>
333
341
```
334
342
335
-
### (c) Dictionaries as a container
343
+
### Exercise 2.6: Dictionaries as a container
336
344
337
-
A dictionary is a useful way to keep track of items where you want to look up items using an index other than an integer.
338
-
In the Python shell, try playing with a dictionary:
345
+
A dictionary is a useful way to keep track of items where you want to
346
+
look up items using an index other than an integer. In the Python
347
+
shell, try playing with a dictionary:
339
348
340
349
```pycon
341
350
>>> prices = { }
@@ -364,14 +373,20 @@ The file looks something like this:
364
373
...
365
374
```
366
375
367
-
Write a function `read_prices(filename)` that reads a set of prices such as this into a dictionary where the keys of the dictionary are the stock names and the values in the dictionary are the stock prices.
376
+
Write a function `read_prices(filename)` that reads a set of prices
377
+
such as this into a dictionary where the keys of the dictionary are
378
+
the stock names and the values in the dictionary are the stock prices.
368
379
369
-
To do this, start with an empty dictionary and start inserting values into it just
370
-
as you did above. However, you are reading the values from a file now.
380
+
To do this, start with an empty dictionary and start inserting values
381
+
into it just as you did above. However, you are reading the values
382
+
from a file now.
371
383
372
-
We’ll use this data structure to quickly lookup the price of a given stock name.
384
+
We’ll use this data structure to quickly lookup the price of a given
385
+
stock name.
373
386
374
-
A few little tips that you’ll need for this part. First, make sure you use the `csv` module just as you did before—there’s no need to reinvent the wheel here.
387
+
A few little tips that you’ll need for this part. First, make sure you
388
+
use the `csv` module just as you did before—there’s no need to
389
+
reinvent the wheel here.
375
390
376
391
```pycon
377
392
>>> import csv
@@ -388,12 +403,16 @@ A few little tips that you’ll need for this part. First, make sure you use the
388
403
>>>
389
404
```
390
405
391
-
The other little complication is that the `Data/prices.csv` file may have some blank lines in it. Notice how the last row of data above is an empty list—meaning no data was present on that line.
406
+
The other little complication is that the `Data/prices.csv` file may
407
+
have some blank lines in it. Notice how the last row of data above is
408
+
an empty list—meaning no data was present on that line.
392
409
393
-
There’s a possibility that this could cause your program to die with an exception.
394
-
Use the `try` and `except` statements to catch this as appropriate.
410
+
There’s a possibility that this could cause your program to die with
411
+
an exception. Use the `try` and `except` statements to catch this as
412
+
appropriate.
395
413
396
-
Once you have written your `read_prices()` function, test it interactively to make sure it works:
414
+
Once you have written your `read_prices()` function, test it
415
+
interactively to make sure it works:
397
416
398
417
```python
399
418
>>> prices = read_prices('Data/prices.csv')
@@ -404,10 +423,12 @@ Once you have written your `read_prices()` function, test it interactively to ma
404
423
>>>
405
424
```
406
425
407
-
### (e) Finding out if you can retire
426
+
### Exercise 2.7: Finding out if you can retire
427
+
428
+
Tie all of this work together by adding the statements to your
429
+
`report.py` program. It takes the list of stocks in Exercise 2.5 and
430
+
the dictionary of prices in Exercise 2.6 and computes the current
431
+
value of the portfolio along with the gain/loss.
408
432
409
-
Tie all of this work together by adding the statements to your `report.py` program.
410
-
It takes the list of stocks in part (b) and the dictionary of prices in part (c) and
411
-
computes the current value of the portfolio along with the gain/loss.
0 commit comments