Skip to content

Commit b0c112a

Browse files
committed
Merge branch 'dabeaz-course:master' into master
2 parents b48e675 + 92a8a73 commit b0c112a

36 files changed

Lines changed: 482 additions & 107 deletions

Notes/00_Setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ exercises. Feel free to look at this if you need a hint. To get the
8686
most out of the course however, you should try to create your own
8787
solutions first.
8888

89-
[Contents](Contents.md)
89+
[Contents](Contents.md) \| [Next (1 Introduction to Python)](01_Introduction/00_Overview.md)
9090

9191

9292

Notes/01_Introduction/02_Hello_world.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interact with the interpreter.
5858
Let's take a closer look at the elements of the REPL:
5959

6060
- `>>>` is the interpreter prompt for starting a new statement.
61-
- `...` is the interpreter prompt for continuing a statements. Enter a blank line to finish typing and run the statements.
61+
- `...` is the interpreter prompt for continuing a statement. Enter a blank line to finish typing and run what you've entered.
6262

6363
The `...` prompt may or may not be shown depending on your environment. For this course,
6464
it is shown as blanks to make it easier to cut/paste code samples.
@@ -241,7 +241,7 @@ while num_bills * bill_thickness < sears_height:
241241
day = day + 1
242242
num_bills = num_bills * 2
243243

244-
print('Number of days', days)
244+
print('Number of days', day)
245245
```
246246

247247
The statements indented below the `while` will execute as long as the expression after the `while` is `true`.
@@ -257,7 +257,7 @@ while num_bills * bill_thickness < sears_height:
257257
day = day + 1
258258
num_bills = num_bills * 2
259259

260-
print('Number of days', days)
260+
print('Number of days', day)
261261
```
262262

263263
Indentation groups the following statements together as the operations that repeat:

Notes/01_Introduction/03_Numbers.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ c = -1.345e-10
7777
Floats are represented as double precision using the native CPU representation [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754).
7878
This is the same as the `double` type in the programming language C.
7979

80-
> 17 digits or precision
80+
> 17 digits of precision
8181
> Exponent from -308 to 308
8282
8383
Be aware that floating point numbers are inexact when representing decimals.
@@ -106,7 +106,7 @@ x ** y Power
106106
abs(x) Absolute Value
107107
```
108108

109-
Theses are the same operators as Integers, except for the bit-wise operators.
109+
These are the same operators as Integers, except for the bit-wise operators.
110110
Additional math functions are found in the `math` module.
111111

112112
```python
@@ -213,14 +213,15 @@ Modify the program so that extra payment information can be more generally handl
213213
Make it so that the user can set these variables:
214214

215215
```python
216-
extra_payment_start_month = 60
216+
extra_payment_start_month = 61
217217
extra_payment_end_month = 108
218218
extra_payment = 1000
219219
```
220220

221221
Make the program look at these variables and calculate the total paid appropriately.
222222

223-
How much will Dave pay if he pays an extra $1000/month for 4 years starting in year 5 of the mortgage?
223+
How much will Dave pay if he pays an extra $1000/month for 4 years starting after the first
224+
five years have already been paid?
224225

225226
### Exercise 1.10: Making a table
226227

@@ -234,15 +235,16 @@ The output should look something like this:
234235
4 10736.44 497581.83
235236
5 13420.55 496970.98
236237
...
237-
308 875705.88 674.44
238-
309 878389.99 -2006.86
239-
Total paid 878389.99
240-
Months 309
238+
308 874705.88 3478.83
239+
309 877389.99 809.21
240+
310 880074.1 -1871.53
241+
Total paid 880074.1
242+
Months 310
241243
```
242244

243245
### Exercise 1.11: Bonus
244246

245-
While you’re at it, fix the program to correct the for overpayment that occurs in the last month.
247+
While you’re at it, fix the program to correct for the overpayment that occurs in the last month.
246248

247249
### Exercise 1.12: A Mystery
248250

Notes/01_Introduction/04_Strings.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# 1.4 Strings
44

5-
This section introduces way to work with text.
5+
This section introduces ways to work with text.
66

77
### Representing Literal Text
88

@@ -28,8 +28,8 @@ Normally strings may only span a single line. Triple quotes capture all text enc
2828
including all formatting.
2929

3030
There is no difference between using single (') versus double (")
31-
quotes. The same type of quote used to start a string must be used to
32-
terminate it.
31+
quotes. *However, the same type of quote used to start a string must be used to
32+
terminate it*.
3333

3434
### String escape codes
3535

@@ -134,7 +134,7 @@ t = s.replace('Hello' , 'Hallo') # 'Hallo world'
134134
**More string methods:**
135135

136136
Strings have a wide variety of other methods for testing and manipulating the text data.
137-
This is small sample of methods:
137+
This is a small sample of methods:
138138

139139
```python
140140
s.endswith(suffix) # Check if string ends with suffix
@@ -144,7 +144,7 @@ s.isalpha() # Check if characters are alphabetic
144144
s.isdigit() # Check if characters are numeric
145145
s.islower() # Check if characters are lower-case
146146
s.isupper() # Check if characters are upper-case
147-
s.join(slist) # Joins lists using s as delimiter
147+
s.join(slist) # Join a list of strings using s as delimiter
148148
s.lower() # Convert to lower case
149149
s.replace(old,new) # Replace text
150150
s.rfind(t) # Search for t from end of string

Notes/01_Introduction/05_Lists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ t = sorted(s) # s unchanged, t holds sorted values
157157
>>> nums * 2
158158
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
159159
>>> nums + [10, 11, 12, 13, 14]
160-
[1, 2, 3, 4, 5, 10, 11, 12, 13, 14] >>>
160+
[1, 2, 3, 4, 5, 10, 11, 12, 13, 14]
161161
```
162162

163163
Specifically, lists don't represent vectors/matrices as in MATLAB, Octave, R, etc.

Notes/01_Introduction/06_Files.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,17 @@ Try it:
220220

221221
```python
222222
>>> import gzip
223-
>>> with gzip.open('Data/portfolio.csv.gz') as f:
223+
>>> with gzip.open('Data/portfolio.csv.gz', 'rt') as f:
224224
for line in f:
225225
print(line, end='')
226226

227227
... look at the output ...
228228
>>>
229229
```
230230

231+
Note: Including the file mode of `'rt'` is critical here. If you forget that,
232+
you'll get byte strings instead of normal text strings.
233+
231234
### Commentary: Shouldn't we being using Pandas for this?
232235

233236
Data scientists are quick to point out that libraries like

Notes/02_Working_with_data/01_Datatypes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ Example:
5252
s = ('GOOG', 100, 490.1)
5353
```
5454

55-
Sometimes the `()` are ommitted in the syntax.
55+
Sometimes the `()` are omitted in the syntax.
5656

5757
```python
5858
s = 'GOOG', 100, 490.1
5959
```
6060

61-
Special cases (0-tuple, 1-typle).
61+
Special cases (0-tuple, 1-tuple).
6262

6363
```python
6464
t = () # An empty tuple
@@ -338,7 +338,7 @@ above. Change the number of shares to 75.
338338
```python
339339
>>> d['shares'] = 75
340340
>>> d
341-
{'name': 'AA', 'shares': 75, 'price': 75}
341+
{'name': 'AA', 'shares': 75, 'price': 32.2 }
342342
>>>
343343
```
344344

Notes/02_Working_with_data/02_Containers.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ An example when reading records from a file.
5252
records = [] # Initial empty list
5353

5454
with open('Data/portfolio.csv', 'rt') as f:
55+
next(f) # Skip header
5556
for line in f:
5657
row = line.split(',')
57-
records.append((row[0], int(row[1])), float(row[2]))
58+
records.append((row[0], int(row[1]), float(row[2])))
5859
```
5960

6061
### Dicts as a Container
@@ -105,6 +106,11 @@ with open('Data/prices.csv', 'rt') as f:
105106
prices[row[0]] = float(row[1])
106107
```
107108

109+
Note: If you try this on the `Data/prices.csv` file, you'll find that
110+
it almost works--there's a blank line at the end that causes it to
111+
crash. You'll need to figure out some way to modify the code to
112+
account for that (see Exercise 2.6).
113+
108114
### Dictionary Lookups
109115

110116
You can test the existence of a key.
@@ -161,7 +167,7 @@ Sets are collection of unordered unique items.
161167

162168
```python
163169
tech_stocks = { 'IBM','AAPL','MSFT' }
164-
# Alternative sintax
170+
# Alternative syntax
165171
tech_stocks = set(['IBM', 'AAPL', 'MSFT'])
166172
```
167173

@@ -189,12 +195,14 @@ unique = set(names)
189195
Additional set operations:
190196

191197
```python
192-
names.add('CAT') # Add an item
193-
names.remove('YHOO') # Remove an item
194-
195-
s1 | s2 # Set union
196-
s1 & s2 # Set intersection
197-
s1 - s2 # Set difference
198+
unique.add('CAT') # Add an item
199+
unique.remove('YHOO') # Remove an item
200+
201+
s1 = { 'a', 'b', 'c'}
202+
s2 = { 'c', 'd' }
203+
s1 | s2 # Set union { 'a', 'b', 'c', 'd' }
204+
s1 & s2 # Set intersection { 'c' }
205+
s1 - s2 # Set difference { 'a', 'b' }
198206
```
199207

200208
## Exercises
@@ -309,7 +317,7 @@ Experiment with this new function in the same manner as you did in
309317
Exercise 2.4.
310318

311319
```python
312-
>>> portfolio = read_portfolio('portfolio.csv')
320+
>>> portfolio = read_portfolio('Data/portfolio.csv')
313321
>>> portfolio
314322
[{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1},
315323
{'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23},
@@ -335,7 +343,7 @@ accessed by key names instead of numeric column numbers. This is
335343
often preferred because the resulting code is easier to read later.
336344

337345
Viewing large dictionaries and lists can be messy. To clean up the
338-
output for debugging, considering using the `pprint` function.
346+
output for debugging, consider using the `pprint` function.
339347

340348
```python
341349
>>> from pprint import pprint
@@ -437,9 +445,9 @@ interactively to make sure it works:
437445
### Exercise 2.7: Finding out if you can retire
438446

439447
Tie all of this work together by adding a few additional statements to
440-
your `report.py` program that compute gain/loss. These statements
448+
your `report.py` program that computes gain/loss. These statements
441449
should take the list of stocks in Exercise 2.5 and the dictionary of
442-
prices in Exercise 2.6 and computes the current value of the portfolio
450+
prices in Exercise 2.6 and compute the current value of the portfolio
443451
along with the gain/loss.
444452

445453
[Contents](../Contents.md) \| [Previous (2.1 Datatypes)](01_Datatypes.md) \| [Next (2.3 Formatting)](03_Formatting.md)

Notes/02_Working_with_data/03_Formatting.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ b Binary integer
4949
x Hexadecimal integer
5050
f Float as [-]m.dddddd
5151
e Float as [-]m.dddddde+-xx
52-
g Float, but selective use of E notation s String
52+
g Float, but selective use of E notation
53+
s String
5354
c Character (from integer)
5455
```
5556

@@ -58,7 +59,8 @@ Common modifiers adjust the field width and decimal precision. This is a partia
5859
```code
5960
:>10d Integer right aligned in 10-character field
6061
:<10d Integer left aligned in 10-character field
61-
:^10d Integer centered in 10-character field :0.2f Float with 2 digit precision
62+
:^10d Integer centered in 10-character field
63+
:0.2f Float with 2 digit precision
6264
```
6365

6466
### Dictionary Formatting
@@ -113,7 +115,7 @@ modeled after the C `printf()` as well.
113115
*Note: This is the only formatting available on byte strings.*
114116

115117
```python
116-
>>> b'%s has %n messages' % (b'Dave', 37)
118+
>>> b'%s has %d messages' % (b'Dave', 37)
117119
b'Dave has 37 messages'
118120
>>>
119121
```

Notes/02_Working_with_data/04_Sequences.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ There are some common functions to reduce a sequence to a single value.
9898
>>> s = [1, 2, 3, 4]
9999
>>> sum(s)
100100
10
101-
>>> min(s) 1
102-
>>> max(s) 4
101+
>>> min(s)
102+
1
103+
>>> max(s)
104+
4
103105
>>> t = ['Hello', 'World']
104106
>>> max(t)
105107
'World'
@@ -240,7 +242,7 @@ for x, y in points:
240242
```
241243

242244
When using multiple variables, each tuple is *unpacked* into a set of iteration variables.
243-
The number of variables must match the of items in each tuple.
245+
The number of variables must match the number of items in each tuple.
244246

245247
### zip() function
246248

@@ -249,7 +251,7 @@ The `zip` function takes multiple sequences and makes an iterator that combines
249251
```python
250252
columns = ['name', 'shares', 'price']
251253
values = ['GOOG', 100, 490.1 ]
252-
pairs = zip(a, b)
254+
pairs = zip(columns, values)
253255
# ('name','GOOG'), ('shares',100), ('price',490.1)
254256
```
255257

@@ -463,7 +465,7 @@ The change made here is subtle, but significant. Instead of
463465
the new version reads any CSV file and picks the values of interest
464466
out of it. As long as the file has the required columns, the code will work.
465467

466-
Modify the `report.py` program you wrote in Section 2.3 that it uses
468+
Modify the `report.py` program you wrote in Section 2.3 so that it uses
467469
the same technique to pick out column headers.
468470

469471
Try running the `report.py` program on the `Data/portfoliodate.csv`

0 commit comments

Comments
 (0)