Skip to content

Commit 5407dc7

Browse files
committed
Highlight example between for loop and list comprehension
Fixes #17
1 parent c9f0866 commit 5407dc7

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

part-4.ipynb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:40f96dd4a9a68767b80bf16809c2b3aebd08f6893eb2d01994fd18e611864f26"
4+
"signature": "sha256:d806710bd05c6f416dc3842515677b9246aa116881cfd0f7dd2c6b748a3a67e5"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -110,13 +110,44 @@
110110
"cell_type": "markdown",
111111
"metadata": {},
112112
"source": [
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"
113+
"Let's compare the two methods real quick by highlighting the different logical parts."
114114
]
115115
},
116116
{
117117
"cell_type": "markdown",
118118
"metadata": {},
119119
"source": [
120+
"First written as a conventional 'for' loop:\n",
121+
"<pre>\n",
122+
"<span style=\"background:orange\">squared_numbers</span> = []\n",
123+
"<span style=\"background:cyan\">for n in</span> <span style=\"background:yellowgreen\">my_favorite_numbers</span>:\n",
124+
"&nbsp;&nbsp;&nbsp;&nbsp;squared_numbers.append(<span style=\"background:pink\">n * n</span>)\n",
125+
"</pre>"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"And then written as a list comprehension:\n",
133+
"<pre>\n",
134+
"<span style=\"background:orange\">squared_numbers</span> = [<span style=\"background:pink\">n * n</span> <span style=\"background:cyan\">for n in</span> <span style=\"background:yellowgreen\">my_favorite_numbers</span>]\n",
135+
"</pre>"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"Look for the <span style=\"background:orange\">new list</span> being created, the <span style=\"background:cyan\">iteration</span> being done over the <span style=\"background:yellowgreen\">orignal list</span> and the <span style=\"background:pink\">transformation</span> being done on each element. List comprehensions are just a cleaner way of building lists from other iterables."
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"### Improving our work\n",
150+
"\n",
120151
"Let's revisit a problem we've already solved in Danny's lecture on list:\n",
121152
"\n",
122153
"Pick every name from a list that begins with a vowel.\n",

0 commit comments

Comments
 (0)