Skip to content

Commit 887f3c6

Browse files
committed
Added links. Renumber exercises
1 parent e26c630 commit 887f3c6

7 files changed

Lines changed: 123 additions & 86 deletions

File tree

Notes/02_Working_with_data/01_Datatypes.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# 2.1 Datatypes and Data structures
22

3-
This section introduces data structures in the form of tuples and dicts.
4-
53
### Primitive Datatypes
64

75
Python has a few primitive types of data:
@@ -10,7 +8,7 @@ Python has a few primitive types of data:
108
* Floating point numbers
119
* Strings (text)
1210

13-
We have learned about these in the previous section.
11+
We learned about these in the introduction.
1412

1513
### None type
1614

@@ -212,7 +210,7 @@ TypeError: can't multiply sequence by non-int of type 'str'
212210
To do more, you typically want to interpret the raw data in some way and turn it into a more useful kind of object so that you can work with it later.
213211
Two simple options are tuples or dictionaries.
214212

215-
### (a) Tuples
213+
### Exercise 2.1: Tuples
216214

217215
At the interactive prompt, create the following tuple that represents
218216
the above row, but with the numeric columns converted to proper
@@ -298,7 +296,7 @@ Take the above variables and pack them back into a tuple
298296
>>>
299297
```
300298

301-
### (b) Dictionaries as a data structure
299+
### Exercise 2.2: Dictionaries as a data structure
302300

303301
An alternative to a tuple is to create a dictionary instead.
304302

@@ -341,7 +339,7 @@ Unlike tuples, dictionaries can be freely modified. Add some attributes:
341339
>>>
342340
```
343341

344-
### (c) Some additional dictionary operations
342+
### Exercise 2.3: Some additional dictionary operations
345343

346344
If you turn a dictionary into a list, you’ll get all of its keys:
347345

@@ -428,4 +426,4 @@ dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 20
428426
>>>
429427
```
430428

431-
[Next](02_Containers)
429+
[Contents](../Contents) \| [Previous (1.6 Files)](../01_Introduction/06_Files) \| [Next (2.2 Containers)](02_Containers)

Notes/02_Working_with_data/02_Containers.md

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ s1 - s2 # Set difference
194194

195195
## Exercises
196196

197-
### Objectives
197+
### Exercise 2.4: A list of tuples
198198

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.
203203

204204
Your code should have looked something like this:
205205

@@ -245,7 +245,9 @@ for row in rows:
245245

246246
Finally, you’ll return the resulting `portfolio` list.
247247

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):
249251

250252
*Hint: Use `-i` when executing the file in the terminal*
251253

@@ -270,8 +272,10 @@ Experiment with your function interactively (just a reminder that in order to do
270272
>>>
271273
```
272274

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.
275279

276280
That said, you can also rewrite the last for-loop using a statement like this:
277281

@@ -285,12 +289,14 @@ That said, you can also rewrite the last for-loop using a statement like this:
285289
>>>
286290
```
287291

288-
### (b) List of Dictionaries
292+
### Exercise 2.5: List of Dictionaries
289293

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.
292298

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.
294300

295301
```pycon
296302
>>> portfolio = read_portfolio('portfolio.csv')
@@ -314,10 +320,12 @@ Experiment with this new function in the same manner as you did in part (a).
314320
>>>
315321
```
316322

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.
319326

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.
321329

322330
```pycon
323331
>>> from pprint import pprint
@@ -332,10 +340,11 @@ Viewing large dictionaries and lists can be messy. To clean up the output for de
332340
>>>
333341
```
334342

335-
### (c) Dictionaries as a container
343+
### Exercise 2.6: Dictionaries as a container
336344

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:
339348

340349
```pycon
341350
>>> prices = { }
@@ -364,14 +373,20 @@ The file looks something like this:
364373
...
365374
```
366375

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.
368379

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.
371383

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.
373386

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.
375390

376391
```pycon
377392
>>> import csv
@@ -388,12 +403,16 @@ A few little tips that you’ll need for this part. First, make sure you use the
388403
>>>
389404
```
390405

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.
392409

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.
395413

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:
397416

398417
```python
399418
>>> prices = read_prices('Data/prices.csv')
@@ -404,10 +423,12 @@ Once you have written your `read_prices()` function, test it interactively to ma
404423
>>>
405424
```
406425

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.
408432

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.
433+
[Contents](../Contents) \| [Previous (2.1 Datatypes)](01_Datatypes) \| [Next (2.3 Formatting)](03_Formatting)
412434

413-
[Next](03_Formatting)

Notes/02_Working_with_data/03_Formatting.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ b'Dave has 37 messages'
101101

102102
## Exercises
103103

104-
In the previous exercise, you wrote a program called `report.py` that computed the gain/loss of a
104+
In Exercise 2.7, you wrote a program called `report.py` that computed the gain/loss of a
105105
stock portfolio. In this exercise, you're going to modify it to produce a table like this:
106106

107107
```code
@@ -116,12 +116,15 @@ stock portfolio. In this exercise, you're going to modify it to produce a table
116116
IBM 100 106.28 35.84
117117
```
118118

119-
In this report, "Price" is the current share price of the stock and "Change" is the change in the share price from the initial purchase price.
119+
In this report, "Price" is the current share price of the stock and
120+
"Change" is the change in the share price from the initial purchase
121+
price.
120122

121-
### (a) How to format numbers
123+
### Exercise 2.8: How to format numbers
122124

123-
A common problem with printing numbers is specifying the number of decimal places. One way to fix this is to use f-strings. Try
124-
these examples:
125+
A common problem with printing numbers is specifying the number of
126+
decimal places. One way to fix this is to use f-strings. Try these
127+
examples:
125128

126129
```python
127130
>>> value = 42863.1
@@ -150,7 +153,8 @@ is also sometimes performed using the `%` operator of strings.
150153
>>>
151154
```
152155

153-
Documentation on various codes used with `%` can be found [here](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
156+
Documentation on various codes used with `%` can be found
157+
[here](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
154158

155159
Although it’s commonly used with `print`, string formatting is not tied to printing.
156160
If you want to save a formatted string. Just assign it to a variable.
@@ -162,14 +166,15 @@ If you want to save a formatted string. Just assign it to a variable.
162166
>>>
163167
```
164168

165-
### (b) Collecting Data
169+
### Exercise 2.9: Collecting Data
166170

167171
In order to generate the above report, you’ll first want to collect
168172
all of the data shown in the table. Write a function `make_report()`
169173
that takes a list of stocks and dictionary of prices as input and
170174
returns a list of tuples containing the rows of the above table.
171175

172-
Add this function to your `report.py` file. Here’s how it should work if you try it interactively:
176+
Add this function to your `report.py` file. Here’s how it should work
177+
if you try it interactively:
173178

174179
```pycon
175180
>>> portfolio = read_portfolio('Data/portfolio.csv')
@@ -187,9 +192,10 @@ Add this function to your `report.py` file. Here’s how it should work if you t
187192
>>>
188193
```
189194

190-
### (c) Printing a formatted table
195+
### Exercise 2.10: Printing a formatted table
191196

192-
Redo the above for-loop, but change the print statement to format the tuples.
197+
Redo the for-loop in Exercise 2.9, but change the print statement to
198+
format the tuples.
193199

194200
```pycon
195201
>>> for r in report:
@@ -220,7 +226,7 @@ You can also expand the values and use f-strings. For example:
220226
Take the above statements and add them to your `report.py` program.
221227
Have your program take the output of the `make_report()` function and print a nicely formatted table as shown.
222228

223-
### (d) Adding some headers
229+
### Exercise 2.11: Adding some headers
224230

225231
Suppose you had a tuple of header names like this:
226232

@@ -257,7 +263,7 @@ When you’re done, your program should produce the table shown at the top of th
257263
IBM 100 106.28 35.84
258264
```
259265

260-
### (e) Formatting Challenge
266+
### Exercise 2.12: Formatting Challenge
261267

262268
How would you modify your code so that the price includes the currency symbol ($) and the output looks like this:
263269

@@ -273,4 +279,4 @@ How would you modify your code so that the price includes the currency symbol ($
273279
IBM 100 $106.28 35.84
274280
```
275281

276-
[Next](04_Sequences)
282+
[Contents](../Contents) \| [Previous (2.2 Containers)](02_Containers) \| [Next (2.4 Sequences)](04_Sequences)

0 commit comments

Comments
 (0)