Skip to content

Commit 31bedab

Browse files
committed
fixes
1 parent a2f37e6 commit 31bedab

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

appendix/truthiness/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Truthiness: Boolean Evaluations
1+
# Truthiness
22

33
While it would seem Python has an actual Boolean (Yes/No, True/False) type, this idea can be seriously abused in many odd and confusing ways. First off, there are actual `True` and `False` values:
44

@@ -103,14 +103,14 @@ Great! So let's use that on the other values:
103103
>>> d.get('bar', 'NA')
104104
````
105105

106-
The call for `bar` was weird, but remember that we put an actual `None` as the value:
106+
The call for `bar` returned nothing because we put an actual `None` as the value:
107107

108108
````
109109
>>> type(d.get('bar', 'NA'))
110110
<class 'NoneType'>
111111
````
112112

113-
OK, so we go back to this:
113+
The key `bar` didn't fail because that key exists in the dictionary. The `dict.get` method only returns the second, default argument *if the key does not exist in the dictionary* which is entirely different from checking the *value* of the key in the dictionary. OK, so we go back to this:
114114

115115
````
116116
>>> d.get('bar') or 'NA'

article/discussion.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ On line 26, we need to figure out whether the `article` should be `a` or `an`. W
88

99
We can access the first character of the `word` with `word[0]` which looks the same as how we access the first element of a list. Strings are really list of characters, so this isn't so far-fetched, but we do have to remember that Python, like so many programming languages, starts numbering at `0`, so we often talked about the first element of a list as the "zeroth" element.
1010

11-
To decided if the given word starts with a vowel, we ask if `word[0].lower() in 'aeiou'`. So, to unpack that, `word[0]` returns a one-character-long `str` type which has the method `.lower()` which we call using the parentheses. Without the parens, this would just be the *idea* of the function that returns a lowercased version of the string. Understand that the `word` remains unchanged. The function does not lowercase `word[0]`, it only *returns a lowercase version* of that character.
11+
To decide if the given word starts with a vowel, we ask is `word[0].lower() in 'aeiou'`. So, to unpack that, `word[0]` returns a one-character-long `str` type which has the method `.lower()` which we call using the parentheses. Without the parens, this would just be the *idea* of the function that returns a lowercased version of the string. Understand that the `word` remains unchanged. The function does not lowercase `word[0]`, it only *returns a lowercase version* of that character.
12+
13+
````
14+
>>> word = 'APPLE'
15+
>>> word
16+
'APPLE'
17+
>>> word[0].lower()
18+
'a'
19+
>>> word
20+
'APPLE'
21+
````
1222

1323
The `X in Y` form is a way to ask if element `X` is in the collection `Y`:
1424

@@ -23,9 +33,7 @@ True
2333
False
2434
````
2535

26-
So we get the first character of `word` and ask if the lowercased version is in the list of characters `aeiou`.
27-
28-
The `if` *expression* is different from an `if` *statement*. An expression returns a value, and a statement does not. The `if` expression must have an `else`, but the `if` statement does not have this requirement. The first value is returned if the predicate (the bit after the `if`) evaluates to `True` in a Boolean context (more on that later), otherwise the last value is returned:
36+
The `if` *expression* is different from an `if` *statement*. An expression returns a value, and a statement does not. The `if` expression must have an `else`, but the `if` statement does not have this requirement. The first value is returned if the predicate (the bit after the `if`) evaluates to `True` in a Boolean context (cf. "Truthiness"), otherwise the last value is returned:
2937

3038
````
3139
>>> 'Hooray!' if True else 'Shucks!'

book.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,17 @@ On line 26, we need to figure out whether the `article` should be `a` or `an`. W
190190

191191
We can access the first character of the `word` with `word[0]` which looks the same as how we access the first element of a list. Strings are really list of characters, so this isn't so far-fetched, but we do have to remember that Python, like so many programming languages, starts numbering at `0`, so we often talked about the first element of a list as the "zeroth" element.
192192

193-
To decided if the given word starts with a vowel, we ask if `word[0].lower() in 'aeiou'`. So, to unpack that, `word[0]` returns a one-character-long `str` type which has the method `.lower()` which we call using the parentheses. Without the parens, this would just be the *idea* of the function that returns a lowercased version of the string. Understand that the `word` remains unchanged. The function does not lowercase `word[0]`, it only *returns a lowercase version* of that character.
193+
To decide if the given word starts with a vowel, we ask is `word[0].lower() in 'aeiou'`. So, to unpack that, `word[0]` returns a one-character-long `str` type which has the method `.lower()` which we call using the parentheses. Without the parens, this would just be the *idea* of the function that returns a lowercased version of the string. Understand that the `word` remains unchanged. The function does not lowercase `word[0]`, it only *returns a lowercase version* of that character.
194+
195+
````
196+
>>> word = 'APPLE'
197+
>>> word
198+
'APPLE'
199+
>>> word[0].lower()
200+
'a'
201+
>>> word
202+
'APPLE'
203+
````
194204

195205
The `X in Y` form is a way to ask if element `X` is in the collection `Y`:
196206

@@ -205,9 +215,7 @@ True
205215
False
206216
````
207217

208-
So we get the first character of `word` and ask if the lowercased version is in the list of characters `aeiou`.
209-
210-
The `if` *expression* is different from an `if` *statement*. An expression returns a value, and a statement does not. The `if` expression must have an `else`, but the `if` statement does not have this requirement. The first value is returned if the predicate (the bit after the `if`) evaluates to `True` in a Boolean context (more on that later), otherwise the last value is returned:
218+
The `if` *expression* is different from an `if` *statement*. An expression returns a value, and a statement does not. The `if` expression must have an `else`, but the `if` statement does not have this requirement. The first value is returned if the predicate (the bit after the `if`) evaluates to `True` in a Boolean context (cf. "Truthiness"), otherwise the last value is returned:
211219

212220
````
213221
>>> 'Hooray!' if True else 'Shucks!'
@@ -5726,7 +5734,7 @@ positional = "foo" (<class 'str'>)
57265734

57275735
\newpage
57285736

5729-
# Appendix 2: Truthiness: Boolean Evaluations
5737+
# Appendix 2: Truthiness
57305738

57315739
While it would seem Python has an actual Boolean (Yes/No, True/False) type, this idea can be seriously abused in many odd and confusing ways. First off, there are actual `True` and `False` values:
57325740

@@ -5831,14 +5839,14 @@ Great! So let's use that on the other values:
58315839
>>> d.get('bar', 'NA')
58325840
````
58335841

5834-
The call for `bar` was weird, but remember that we put an actual `None` as the value:
5842+
The call for `bar` returned nothing because we put an actual `None` as the value:
58355843

58365844
````
58375845
>>> type(d.get('bar', 'NA'))
58385846
<class 'NoneType'>
58395847
````
58405848

5841-
OK, so we go back to this:
5849+
The key `bar` didn't fail because that key exists in the dictionary. The `dict.get` method only returns the second, default argument *if the key does not exist in the dictionary* which is entirely different from checking the *value* of the key in the dictionary. OK, so we go back to this:
58425850

58435851
````
58445852
>>> d.get('bar') or 'NA'

playful_python.pdf

317 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)