Skip to content

Commit a5cae9c

Browse files
committed
Editing. Added images
1 parent 3bfa365 commit a5cae9c

9 files changed

+377
-256
lines changed

Notes/02_Working_with_data/01_Datatypes.md

+76-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
[Contents](../Contents) \| [Previous (1.6 Files)](../01_Introduction/06_Files) \| [Next (2.2 Containers)](02_Containers)
2+
13
# 2.1 Datatypes and Data structures
24

5+
This section introduces data structures in the form of tuples and dictionaries.
6+
37
### Primitive Datatypes
48

59
Python has a few primitive types of data:
@@ -16,7 +20,8 @@ We learned about these in the introduction.
1620
email_address = None
1721
```
1822

19-
This type is often used as a placeholder for optional or missing value.
23+
`None` is often used as a placeholder for optional or missing value. It
24+
evaluates as `False` in conditionals.
2025

2126
```python
2227
if email_address:
@@ -25,8 +30,7 @@ if email_address:
2530

2631
### Data Structures
2732

28-
Real programs have more complex data than the ones that can be easily represented by the datatypes learned so far.
29-
For example information about a stock:
33+
Real programs have more complex data. For example information about a stock holding:
3034

3135
```code
3236
100 shares of GOOG at $490.10
@@ -61,7 +65,7 @@ t = () # An empty tuple
6165
w = ('GOOG', ) # A 1-item tuple
6266
```
6367

64-
Tuples are usually used to represent *simple* records or structures.
68+
Tuples are often used to represent *simple* records or structures.
6569
Typically, it is a single *object* of multiple parts. A good analogy: *A tuple is like a single row in a database table.*
6670

6771
Tuple contents are ordered (like an array).
@@ -73,9 +77,9 @@ shares = s[1] # 100
7377
price = s[2] # 490.1
7478
```
7579

76-
However, th contents can't be modified.
80+
However, the contents can't be modified.
7781

78-
```pycon
82+
```python
7983
>>> s[1] = 75
8084
TypeError: object does not support item assignment
8185
```
@@ -88,7 +92,7 @@ s = (s[0], 75, s[2])
8892

8993
### Tuple Packing
9094

91-
Tuples are focused more on packing related items together into a single *entity*.
95+
Tuples are more about packing related items together into a single *entity*.
9296

9397
```python
9498
s = ('GOOG', 100, 490.1)
@@ -105,7 +109,7 @@ name, shares, price = s
105109
print('Cost', shares * price)
106110
```
107111

108-
The number of variables must match the tuple structure.
112+
The number of variables on the left must match the tuple structure.
109113

110114
```python
111115
name, shares = s # ERROR
@@ -116,19 +120,20 @@ ValueError: too many values to unpack
116120

117121
### Tuples vs. Lists
118122

119-
Tuples are NOT just read-only lists. Tuples are most ofter used for a *single item* consisting of multiple parts.
120-
Lists are usually a collection of distinct items, usually all of the same type.
123+
Tuples look like read-only lists. However, tuples are most often used
124+
for a *single item* consisting of multiple parts. Lists are usually a
125+
collection of distinct items, usually all of the same type.
121126

122127
```python
123-
record = ('GOOG', 100, 490.1) # A tuple representing a stock in a portfolio
128+
record = ('GOOG', 100, 490.1) # A tuple representing a record in a portfolio
124129

125130
symbols = [ 'GOOG', 'AAPL', 'IBM' ] # A List representing three stock symbols
126131
```
127132

128133
### Dictionaries
129134

130-
A dictionary is a hash table or associative array.
131-
It is a collection of values indexed by *keys*. These keys serve as field names.
135+
A dictionary is mapping of keys to values. It's also sometimes called a hash table or
136+
associative array. The keys serve as indices for accessing values.
132137

133138
```python
134139
s = {
@@ -140,9 +145,9 @@ s = {
140145

141146
### Common operations
142147

143-
To read values from a dictionary use the key names.
148+
To get values from a dictionary use the key names.
144149

145-
```pycon
150+
```python
146151
>>> print(s['name'], s['shares'])
147152
GOOG 100
148153
>>> s['price']
@@ -152,15 +157,15 @@ GOOG 100
152157

153158
To add or modify values assign using the key names.
154159

155-
```pycon
160+
```python
156161
>>> s['shares'] = 75
157162
>>> s['date'] = '6/6/2007'
158163
>>>
159164
```
160165

161166
To delete a value use the `del` statement.
162167

163-
```pycon
168+
```python
164169
>>> del s['date']
165170
>>>
166171
```
@@ -178,11 +183,11 @@ s[2]
178183

179184
## Exercises
180185

181-
### Note
186+
In the last few exercises, you wrote a program that read a datafile
187+
`Data/portfolio.csv`. Using the `csv` module, it is easy to read the
188+
file row-by-row.
182189

183-
In the last few exercises, you wrote a program that read a datafile `Data/portfolio.csv`. Using the `csv` module, it is easy to read the file row-by-row.
184-
185-
```pycon
190+
```python
186191
>>> import csv
187192
>>> f = open('Data/portfolio.csv')
188193
>>> rows = csv.reader(f)
@@ -194,11 +199,13 @@ In the last few exercises, you wrote a program that read a datafile `Data/portfo
194199
>>>
195200
```
196201

197-
Although reading the file is easy, you often want to do more with the data than read it.
198-
For instance, perhaps you want to store it and start performing some calculations on it.
199-
Unfortunately, a raw "row" of data doesn’t give you enough to work with. For example, even a simple math calculation doesn’t work:
202+
Although reading the file is easy, you often want to do more with the
203+
data than read it. For instance, perhaps you want to store it and
204+
start performing some calculations on it. Unfortunately, a raw "row"
205+
of data doesn’t give you enough to work with. For example, even a
206+
simple math calculation doesn’t work:
200207

201-
```pycon
208+
```python
202209
>>> row = ['AA', '100', '32.20']
203210
>>> cost = row[1] * row[2]
204211
Traceback (most recent call last):
@@ -207,25 +214,27 @@ TypeError: can't multiply sequence by non-int of type 'str'
207214
>>>
208215
```
209216

210-
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.
211-
Two simple options are tuples or dictionaries.
217+
To do more, you typically want to interpret the raw data in some way
218+
and turn it into a more useful kind of object so that you can work
219+
with it later. Two simple options are tuples or dictionaries.
212220

213221
### Exercise 2.1: Tuples
214222

215223
At the interactive prompt, create the following tuple that represents
216224
the above row, but with the numeric columns converted to proper
217225
numbers:
218226

219-
```pycon
227+
```python
220228
>>> t = (row[0], int(row[1]), float(row[2]))
221229
>>> t
222230
('AA', 100, 32.2)
223231
>>>
224232
```
225233

226-
Using this, you can now calculate the total cost by multiplying the shares and the price:
234+
Using this, you can now calculate the total cost by multiplying the
235+
shares and the price:
227236

228-
```pycon
237+
```python
229238
>>> cost = t[1] * t[2]
230239
>>> cost
231240
3220.0000000000005
@@ -244,25 +253,27 @@ surprising if you haven’t seen it before.
244253
This happens in all programming languages that use floating point
245254
decimals, but it often gets hidden when printing. For example:
246255

247-
```pycon
256+
```python
248257
>>> print(f'{cost:0.2f}')
249258
3220.00
250259
>>>
251260
```
252261

253-
Tuples are read-only. Verify this by trying to change the number of shares to 75.
262+
Tuples are read-only. Verify this by trying to change the number of
263+
shares to 75.
254264

255-
```pycon
265+
```python
256266
>>> t[1] = 75
257267
Traceback (most recent call last):
258268
File "<stdin>", line 1, in <module>
259269
TypeError: 'tuple' object does not support item assignment
260270
>>>
261271
```
262272

263-
Although you can’t change tuple contents, you can always create a completely new tuple that replaces the old one.
273+
Although you can’t change tuple contents, you can always create a
274+
completely new tuple that replaces the old one.
264275

265-
```pycon
276+
```python
266277
>>> t = (t[0], 75, t[2])
267278
>>> t
268279
('AA', 75, 32.2)
@@ -274,9 +285,10 @@ value is discarded. Although the above assignment might look like you
274285
are modifying the tuple, you are actually creating a new tuple and
275286
throwing the old one away.
276287

277-
Tuples are often used to pack and unpack values into variables. Try the following:
288+
Tuples are often used to pack and unpack values into variables. Try
289+
the following:
278290

279-
```pycon
291+
```python
280292
>>> name, shares, price = t
281293
>>> name
282294
'AA'
@@ -289,7 +301,7 @@ Tuples are often used to pack and unpack values into variables. Try the followin
289301

290302
Take the above variables and pack them back into a tuple
291303

292-
```pycon
304+
```python
293305
>>> t = (name, 2*shares, price)
294306
>>> t
295307
('AA', 150, 32.2)
@@ -300,7 +312,7 @@ Take the above variables and pack them back into a tuple
300312

301313
An alternative to a tuple is to create a dictionary instead.
302314

303-
```pycon
315+
```python
304316
>>> d = {
305317
'name' : row[0],
306318
'shares' : int(row[1]),
@@ -313,25 +325,27 @@ An alternative to a tuple is to create a dictionary instead.
313325

314326
Calculate the total cost of this holding:
315327

316-
```pycon
328+
```python
317329
>>> cost = d['shares'] * d['price']
318330
>>> cost
319331
3220.0000000000005
320332
>>>
321333
```
322334

323-
Compare this example with the same calculation involving tuples above. Change the number of shares to 75.
335+
Compare this example with the same calculation involving tuples
336+
above. Change the number of shares to 75.
324337

325-
```pycon
338+
```python
326339
>>> d['shares'] = 75
327340
>>> d
328341
{'name': 'AA', 'shares': 75, 'price': 75}
329342
>>>
330343
```
331344

332-
Unlike tuples, dictionaries can be freely modified. Add some attributes:
345+
Unlike tuples, dictionaries can be freely modified. Add some
346+
attributes:
333347

334-
```pycon
348+
```python
335349
>>> d['date'] = (6, 11, 2007)
336350
>>> d['account'] = 12345
337351
>>> d
@@ -343,15 +357,16 @@ Unlike tuples, dictionaries can be freely modified. Add some attributes:
343357

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

346-
```pycon
360+
```python
347361
>>> list(d)
348362
['name', 'shares', 'price', 'date', 'account']
349363
>>>
350364
```
351365

352-
Similarly, if you use the `for` statement to iterate on a dictionary, you will get the keys:
366+
Similarly, if you use the `for` statement to iterate on a dictionary,
367+
you will get the keys:
353368

354-
```pycon
369+
```python
355370
>>> for k in d:
356371
print('k =', k)
357372

@@ -365,7 +380,7 @@ k = account
365380

366381
Try this variant that performs a lookup at the same time:
367382

368-
```pycon
383+
```python
369384
>>> for k in d:
370385
print(k, '=', d[k])
371386

@@ -379,7 +394,7 @@ account = 12345
379394

380395
You can also obtain all of the keys using the `keys()` method:
381396

382-
```pycon
397+
```python
383398
>>> keys = d.keys()
384399
>>> keys
385400
dict_keys(['name', 'shares', 'price', 'date', 'account'])
@@ -388,20 +403,24 @@ dict_keys(['name', 'shares', 'price', 'date', 'account'])
388403

389404
`keys()` is a bit unusual in that it returns a special `dict_keys` object.
390405

391-
This is an overlay on the original dictionary that always gives you the current keys—even if the dictionary changes. For example, try this:
406+
This is an overlay on the original dictionary that always gives you
407+
the current keys—even if the dictionary changes. For example, try
408+
this:
392409

393-
```pycon
410+
```python
394411
>>> del d['account']
395412
>>> keys
396413
dict_keys(['name', 'shares', 'price', 'date'])
397414
>>>
398415
```
399416

400-
Carefully notice that the `'account'` disappeared from `keys` even though you didn’t call `d.keys()` again.
417+
Carefully notice that the `'account'` disappeared from `keys` even
418+
though you didn’t call `d.keys()` again.
401419

402-
A more elegant way to work with keys and values together is to use the `items()` method. This gives you `(key, value)` tuples:
420+
A more elegant way to work with keys and values together is to use the
421+
`items()` method. This gives you `(key, value)` tuples:
403422

404-
```pycon
423+
```python
405424
>>> items = d.items()
406425
>>> items
407426
dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 2007))])
@@ -415,9 +434,10 @@ date = (6, 11, 2007)
415434
>>>
416435
```
417436

418-
If you have tuples such as `items`, you can create a dictionary using the `dict()` function. Try it:
437+
If you have tuples such as `items`, you can create a dictionary using
438+
the `dict()` function. Try it:
419439

420-
```pycon
440+
```python
421441
>>> items
422442
dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 2007))])
423443
>>> d = dict(items)

0 commit comments

Comments
 (0)