You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: appendix/truthiness/README.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Truthiness: Boolean Evaluations
1
+
# Truthiness
2
2
3
3
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:
4
4
@@ -103,14 +103,14 @@ Great! So let's use that on the other values:
103
103
>>> d.get('bar', 'NA')
104
104
````
105
105
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:
107
107
108
108
````
109
109
>>> type(d.get('bar', 'NA'))
110
110
<class 'NoneType'>
111
111
````
112
112
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:
Copy file name to clipboardExpand all lines: article/discussion.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,17 @@ On line 26, we need to figure out whether the `article` should be `a` or `an`. W
8
8
9
9
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.
10
10
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
+
````
12
22
13
23
The `X in Y` form is a way to ask if element `X` is in the collection `Y`:
14
24
@@ -23,9 +33,7 @@ True
23
33
False
24
34
````
25
35
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:
Copy file name to clipboardExpand all lines: book.md
+15-7Lines changed: 15 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,7 +190,17 @@ On line 26, we need to figure out whether the `article` should be `a` or `an`. W
190
190
191
191
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.
192
192
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
+
````
194
204
195
205
The `X in Y` form is a way to ask if element `X` is in the collection `Y`:
196
206
@@ -205,9 +215,7 @@ True
205
215
False
206
216
````
207
217
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:
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:
5732
5740
@@ -5831,14 +5839,14 @@ Great! So let's use that on the other values:
5831
5839
>>> d.get('bar', 'NA')
5832
5840
````
5833
5841
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:
5835
5843
5836
5844
````
5837
5845
>>> type(d.get('bar', 'NA'))
5838
5846
<class 'NoneType'>
5839
5847
````
5840
5848
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:
0 commit comments