Skip to content

Commit 16dab49

Browse files
committed
Updated python basics for loop tutorial for the final version of the video
1 parent b945df4 commit 16dab49

File tree

2 files changed

+170
-116
lines changed

2 files changed

+170
-116
lines changed

Python_Basics/Intro/.ipynb_checkpoints/PythonBasicsForLoops-checkpoint.ipynb

Lines changed: 85 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"For loops are a common way to iterate over the elements of a list </br>"
14+
"For loops are a common way to iterate over the elements of an object (in this first example, a list)<br>\n",
15+
"Any object with an iterable method can be used in a for loop."
1516
]
1617
},
1718
{
@@ -30,19 +31,7 @@
3031
},
3132
{
3233
"cell_type": "code",
33-
"execution_count": 2,
34-
"metadata": {
35-
"collapsed": true
36-
},
37-
"outputs": [],
38-
"source": [
39-
"# Assign list to variable\n",
40-
"numbers = [23, 41, 12, 16, 7]"
41-
]
42-
},
43-
{
44-
"cell_type": "code",
45-
"execution_count": 3,
34+
"execution_count": 57,
4635
"metadata": {},
4736
"outputs": [
4837
{
@@ -53,7 +42,8 @@
5342
"41\n",
5443
"12\n",
5544
"16\n",
56-
"7\n"
45+
"7\n",
46+
"Hi\n"
5747
]
5848
}
5949
],
@@ -63,8 +53,9 @@
6353
"\n",
6454
"# take the first member of the list (iterable), call it number temporarily do something with it (print it)\n",
6555
"# take second member of the list (iterable), call it number temporarily do something through and so on...\n",
66-
"for number in numbers: \n",
67-
" print(number)"
56+
"for number in [23, 41, 12, 16, 7]: \n",
57+
" print(number)\n",
58+
"print('Hi')"
6859
]
6960
},
7061
{
@@ -78,16 +69,26 @@
7869
"cell_type": "markdown",
7970
"metadata": {},
8071
"source": [
81-
"the next() method of the iterator returned by enumerate returns a tuple containing a count for every iteration (from start which defaults to 0) and the values obtained from iterating over sequence:"
72+
"Returns a tuple containing a count for every iteration (from start which defaults to 0) and the values obtained from iterating over sequence:"
8273
]
8374
},
8475
{
8576
"cell_type": "code",
86-
"execution_count": null,
87-
"metadata": {
88-
"collapsed": true
89-
},
90-
"outputs": [],
77+
"execution_count": 43,
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"name": "stdout",
82+
"output_type": "stream",
83+
"text": [
84+
"0 steve\n",
85+
"1 rachel\n",
86+
"2 michael\n",
87+
"3 adam\n",
88+
"4 monica\n"
89+
]
90+
}
91+
],
9192
"source": [
9293
"friends = ['steve', 'rachel', 'michael', 'adam', 'monica']\n",
9394
"for index, friend in enumerate(friends):\n",
@@ -105,14 +106,14 @@
105106
"cell_type": "markdown",
106107
"metadata": {},
107108
"source": [
108-
"<b>Calculate the average word length for the following text:</b> </br>\n",
109+
"<b>Remove the punctuation from the text and convert the final product to a list:</b> </br>\n",
109110
"\n",
110111
"On a dark desert highway, cool wind in my hair Warm smell of colitas, rising up through the air Up ahead in the distance, I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway; I heard the mission bell And I was thinking to myself, \"This could be Heaven or this could be Hell\" Then she lit up a candle and she showed me the way"
111112
]
112113
},
113114
{
114115
"cell_type": "code",
115-
"execution_count": 5,
116+
"execution_count": 62,
116117
"metadata": {
117118
"collapsed": true
118119
},
@@ -123,7 +124,7 @@
123124
},
124125
{
125126
"cell_type": "code",
126-
"execution_count": 6,
127+
"execution_count": 63,
127128
"metadata": {},
128129
"outputs": [
129130
{
@@ -147,32 +148,33 @@
147148
},
148149
{
149150
"cell_type": "code",
150-
"execution_count": 7,
151+
"execution_count": 65,
151152
"metadata": {},
152153
"outputs": [
153154
{
154155
"name": "stdout",
155156
"output_type": "stream",
156157
"text": [
157-
"On a dark desert highway cool wind in my hair Warm smell of colitas rising up through the air Up ahead in the distance I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway; I heard the mission bell And I was thinking to myself This could be Heaven or this could be Hell Then she lit up a candle and she showed me the way\n"
158+
"On a dark desert highway cool wind in my hair Warm smell of colitas rising up through the air Up ahead in the distance I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway I heard the mission bell And I was thinking to myself This could be Heaven or this could be Hell Then she lit up a candle and she showed me the way\n"
158159
]
159160
}
160161
],
161162
"source": [
162-
"for char in '-.,\\n\"\\'':\n",
163-
" text =text.replace(char,' ')\n",
163+
"for char in '-.,;\\n\"\\'':\n",
164+
" text = text.replace(char,' ')\n",
164165
"print(text)"
165166
]
166167
},
167168
{
168169
"cell_type": "code",
169-
"execution_count": 8,
170+
"execution_count": 9,
170171
"metadata": {},
171172
"outputs": [
172173
{
173174
"data": {
174175
"text/plain": [
175-
"['a',\n",
176+
"['On',\n",
177+
" 'a',\n",
176178
" 'dark',\n",
177179
" 'desert',\n",
178180
" 'highway',\n",
@@ -193,15 +195,15 @@
193195
" 'the']"
194196
]
195197
},
196-
"execution_count": 8,
198+
"execution_count": 9,
197199
"metadata": {},
198200
"output_type": "execute_result"
199201
}
200202
],
201203
"source": [
202204
"# Split converts string to list.\n",
203205
"# Each item in list is split on spaces\n",
204-
"text.split(' ')[1:20]"
206+
"text.split(' ')[0:20]"
205207
]
206208
},
207209
{
@@ -228,7 +230,7 @@
228230
},
229231
{
230232
"cell_type": "code",
231-
"execution_count": 10,
233+
"execution_count": 66,
232234
"metadata": {
233235
"collapsed": true
234236
},
@@ -240,7 +242,7 @@
240242
},
241243
{
242244
"cell_type": "code",
243-
"execution_count": 11,
245+
"execution_count": 67,
244246
"metadata": {
245247
"collapsed": true
246248
},
@@ -254,13 +256,14 @@
254256
},
255257
{
256258
"cell_type": "code",
257-
"execution_count": 12,
259+
"execution_count": 68,
258260
"metadata": {},
259261
"outputs": [
260262
{
261263
"data": {
262264
"text/plain": [
263-
"['a',\n",
265+
"['On',\n",
266+
" 'a',\n",
264267
" 'dark',\n",
265268
" 'desert',\n",
266269
" 'highway',\n",
@@ -281,13 +284,13 @@
281284
" 'Up']"
282285
]
283286
},
284-
"execution_count": 12,
287+
"execution_count": 68,
285288
"metadata": {},
286289
"output_type": "execute_result"
287290
}
288291
],
289292
"source": [
290-
"cleaned_list[1:20]"
293+
"cleaned_list[0:20]"
291294
]
292295
},
293296
{
@@ -301,34 +304,51 @@
301304
"cell_type": "markdown",
302305
"metadata": {},
303306
"source": [
304-
"continue keyword will move onto the next iteration of the loop <br>\n",
307+
"continue statement will move onto the next iteration of the loop <br>\n",
305308
"continue used for ignoring certain values, but not break out of loop "
306309
]
307310
},
308311
{
309312
"cell_type": "code",
310-
"execution_count": 13,
313+
"execution_count": 69,
311314
"metadata": {},
312315
"outputs": [
313316
{
314317
"data": {
315318
"text/plain": [
316-
"[1, 4, 6, 7, 4, 4, 2, 2, 4, 4, 5, 2, 7, 6, 2, 7, 3, 3, 2]"
319+
"['a',\n",
320+
" 'dark',\n",
321+
" 'desert',\n",
322+
" 'highway',\n",
323+
" 'cool',\n",
324+
" 'wind',\n",
325+
" 'in',\n",
326+
" 'my',\n",
327+
" 'hair',\n",
328+
" 'Warm',\n",
329+
" 'smell',\n",
330+
" 'of',\n",
331+
" 'colitas',\n",
332+
" 'rising',\n",
333+
" 'up',\n",
334+
" 'through',\n",
335+
" 'the',\n",
336+
" 'air',\n",
337+
" 'Up']"
317338
]
318339
},
319-
"execution_count": 13,
340+
"execution_count": 69,
320341
"metadata": {},
321342
"output_type": "execute_result"
322343
}
323344
],
324345
"source": [
325-
"### Continue\n",
326346
"cleaned_list = []\n",
327347
"\n",
328348
"for word in text.split(' '): \n",
329349
" if word == '':\n",
330350
" continue\n",
331-
" cleaned_list.append(len(word))\n",
351+
" cleaned_list.append(word)\n",
332352
"cleaned_list[1:20]"
333353
]
334354
},
@@ -343,42 +363,49 @@
343363
"cell_type": "markdown",
344364
"metadata": {},
345365
"source": [
346-
"break keyword will completely break out of loop "
366+
"break statement will completely break out of loop "
347367
]
348368
},
349369
{
350370
"cell_type": "code",
351-
"execution_count": 16,
371+
"execution_count": 71,
352372
"metadata": {
353373
"collapsed": true
354374
},
355375
"outputs": [],
356376
"source": [
357-
"### Break and Continue\n",
358377
"cleaned_list = []"
359378
]
360379
},
361380
{
362381
"cell_type": "code",
363-
"execution_count": 17,
382+
"execution_count": 72,
364383
"metadata": {},
365384
"outputs": [
385+
{
386+
"name": "stdout",
387+
"output_type": "stream",
388+
"text": [
389+
"I found the word I was looking for\n"
390+
]
391+
},
366392
{
367393
"data": {
368394
"text/plain": [
369-
"[2, 1, 4, 6, 7, 0, 4, 4, 2, 2, 4]"
395+
"['On', 'a', 'dark']"
370396
]
371397
},
372-
"execution_count": 17,
398+
"execution_count": 72,
373399
"metadata": {},
374400
"output_type": "execute_result"
375401
}
376402
],
377403
"source": [
378404
"for word in text.split(' '): \n",
379-
" if word == 'Warm':\n",
405+
" if word == 'desert':\n",
406+
" print('I found the word I was looking for')\n",
380407
" break\n",
381-
" cleaned_list.append(len(word))\n",
408+
" cleaned_list.append(word)\n",
382409
"cleaned_list"
383410
]
384411
},
@@ -393,12 +420,12 @@
393420
"cell_type": "markdown",
394421
"metadata": {},
395422
"source": [
396-
"1. Write a Python program which iterates through integers from 1 to 50 (using a for loop). For an integer that is even, append it to the list even_numbers. For an integer that is off, append it the list odd_numbers"
423+
"1. Write a Python program which iterates through integers from 1 to 50 (using a for loop). For an integer that is even, append it to the list even_numbers. For an integer that is odd, append it the list odd_numbers"
397424
]
398425
},
399426
{
400427
"cell_type": "code",
401-
"execution_count": 18,
428+
"execution_count": 75,
402429
"metadata": {
403430
"collapsed": true
404431
},
@@ -417,7 +444,7 @@
417444
},
418445
{
419446
"cell_type": "code",
420-
"execution_count": 20,
447+
"execution_count": 76,
421448
"metadata": {},
422449
"outputs": [
423450
{
@@ -434,7 +461,7 @@
434461
},
435462
{
436463
"cell_type": "code",
437-
"execution_count": 21,
464+
"execution_count": 77,
438465
"metadata": {},
439466
"outputs": [
440467
{

0 commit comments

Comments
 (0)