Skip to content

Commit df86500

Browse files
committed
Merge branch 'part4'
Conflicts: part-4.ipynb
2 parents 26949af + e68b5dd commit df86500

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

part-4.ipynb

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:20f6b02042c480fae323fa6c98332c749bfe323d54bb24066930544c93c73a33"
4+
"signature": "sha256:826b0e4b3d39f844b868b8370fcaac110d06dbe6afb167b149a60d16aea5592e"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -23,7 +23,7 @@
2323
"\n",
2424
"Now we're going to learn about list comprehensions.\n",
2525
"\n",
26-
"A list comprehension is kind of like an inside-out for loop. It makes it easy to do operations on elements in a list and return a new list."
26+
"A list comprehension is kind of like a reverse for-loop. It makes it easy to do operations on elements in a list and return a new list."
2727
]
2828
},
2929
{
@@ -83,7 +83,7 @@
8383
"cell_type": "markdown",
8484
"metadata": {},
8585
"source": [
86-
"Now let's do the same thing with a list comprehension:"
86+
"Now let's do the same thing by using a list comprehension instead:"
8787
]
8888
},
8989
{
@@ -110,7 +110,14 @@
110110
"cell_type": "markdown",
111111
"metadata": {},
112112
"source": [
113-
"Let's revisit a problem we've already solved:\n",
113+
"Note how our list comprehension is written within the brackets: The for-loop statement is written at the end whereas the action inside of the for-loop is written first: n * n"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"Let's revisit a problem we've already solved in Danny's lecture on list:\n",
114121
"\n",
115122
"Pick every name from a list that begins with a vowel.\n",
116123
"\n",
@@ -121,7 +128,7 @@
121128
"cell_type": "code",
122129
"collapsed": false,
123130
"input": [
124-
"names = [\"Alice\", \"Bob\", \"Cassie\", \"Diane\", \"Ellen\"]"
131+
"names = [\"Danny\", \"Audrey\", \"Rise\", \"Alain\"]"
125132
],
126133
"language": "python",
127134
"metadata": {},
@@ -147,6 +154,16 @@
147154
"metadata": {},
148155
"outputs": []
149156
},
157+
{
158+
"cell_type": "code",
159+
"collapsed": false,
160+
"input": [
161+
"vowel_names"
162+
],
163+
"language": "python",
164+
"metadata": {},
165+
"outputs": []
166+
},
150167
{
151168
"cell_type": "markdown",
152169
"metadata": {},
@@ -180,9 +197,9 @@
180197
"source": [
181198
"## Dictionaries\n",
182199
"\n",
183-
"Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are used for lookup and their values are returned.\n",
200+
"Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are unique and are used for looking up values assigned to them.\n",
184201
"\n",
185-
"Let's make an Spanish to English translator. We'll ignore grammar and just translate word-by-word for now."
202+
"Let's make a Spanish to English translator. We'll ignore grammar and just translate word-by-word for now."
186203
]
187204
},
188205
{
@@ -269,35 +286,23 @@
269286
"cell_type": "markdown",
270287
"metadata": {},
271288
"source": [
272-
"Let's loop over each word in the list and translate each one."
289+
"Let's translate each word and save them in the list called translated_words. We'll generate this list by using a list comprehension."
273290
]
274291
},
275292
{
276293
"cell_type": "code",
277294
"collapsed": false,
278295
"input": [
279-
"for spanish_word in sentence_words:\n",
280-
" print(words[spanish_word])"
296+
"translated_words = [print(words[spanish_word]) for spanish_word in sentence_words]"
281297
],
282298
"language": "python",
283299
"metadata": {},
284300
"outputs": []
285301
},
286-
{
287-
"cell_type": "markdown",
288-
"metadata": {},
289-
"source": [
290-
"Now let's make a list from the translated words."
291-
]
292-
},
293302
{
294303
"cell_type": "code",
295304
"collapsed": false,
296305
"input": [
297-
"translated_words = []\n",
298-
"for spanish_word in sentence_words:\n",
299-
" translated_words.append(words[spanish_word])\n",
300-
"\n",
301306
"translated_words"
302307
],
303308
"language": "python",
@@ -391,7 +396,7 @@
391396
"cell_type": "markdown",
392397
"metadata": {},
393398
"source": [
394-
"**Extra Credit:** turn the translate function into a single list comprehension."
399+
"**Extra Credit:** Try using a list comprehension in the translate function."
395400
]
396401
},
397402
{
@@ -549,6 +554,14 @@
549554
"language": "python",
550555
"metadata": {},
551556
"outputs": []
557+
},
558+
{
559+
"cell_type": "code",
560+
"collapsed": false,
561+
"input": [],
562+
"language": "python",
563+
"metadata": {},
564+
"outputs": []
552565
}
553566
],
554567
"metadata": {}

0 commit comments

Comments
 (0)