Visitar URL original
Merge branch 'dabeaz-course:master' into master · dabeaz-course/practical-python@6b8784d · GitHub
Skip to content

Commit 6b8784d

Browse files
authored
Merge branch 'dabeaz-course:master' into master
2 parents e49f40c + 897e642 commit 6b8784d

43 files changed

Lines changed: 175 additions & 127 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎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/01_Python.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ will be able to use it everywhere else.
6161

6262
### Exercise 1.1: Using Python as a Calculator
6363

64-
On your machine, start Python and use it as a calulator to solve the
64+
On your machine, start Python and use it as a calculator to solve the
6565
following problem.
6666

6767
Lucky Larry bought 75 shares of Google stock at a price of $235.14 per
@@ -145,6 +145,11 @@ the class by typing in code slowly and thinking about it--not cut and pasting.
145145

146146
### Exercise 1.4: Where is My Bus?
147147

148+
Note: This was a whimsical example that was a real crowd-pleaser when
149+
I taught this course in my office. You could query the bus and then
150+
literally watch it pass by the window out front. Sadly, APIs rarely live
151+
forever and it seems that this one has now ridden off into the sunset. --Dave
152+
148153
Try something more advanced and type these statements to find out how
149154
long people waiting on the corner of Clark street and Balmoral in
150155
Chicago will have to wait for the next northbound CTA \#22 bus:

‎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/01_Introduction/07_Functions.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ Exceptions can be caught and handled.
7272
To catch, use the `try - except` statement.
7373

7474
```python
75-
for line in f:
76-
fields = line.split()
75+
for line in file:
76+
fields = line.split(',')
7777
try:
7878
shares = int(fields[1])
7979
except ValueError:

‎Notes/02_Working_with_data/01_Datatypes.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Sometimes the `()` are omitted in the syntax.
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: 19 additions & 11 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.
@@ -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)

0 commit comments

Comments
 (0)