@@ -52,9 +52,10 @@ An example when reading records from a file.
5252records = [] # Initial empty list
5353
5454with 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
110116You can test the existence of a key.
@@ -161,7 +167,7 @@ Sets are collection of unordered unique items.
161167
162168``` python
163169tech_stocks = { ' IBM' ,' AAPL' ,' MSFT' }
164- # Alternative sintax
170+ # Alternative syntax
165171tech_stocks = set ([' IBM' , ' AAPL' , ' MSFT' ])
166172```
167173
@@ -189,12 +195,14 @@ unique = set(names)
189195Additional 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
309317Exercise 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
335343often preferred because the resulting code is easier to read later.
336344
337345Viewing 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
439447Tie 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
441449should 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
443451along 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