|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "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." |
15 | 16 | ] |
16 | 17 | }, |
17 | 18 | { |
|
30 | 31 | }, |
31 | 32 | { |
32 | 33 | "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, |
46 | 35 | "metadata": {}, |
47 | 36 | "outputs": [ |
48 | 37 | { |
|
53 | 42 | "41\n", |
54 | 43 | "12\n", |
55 | 44 | "16\n", |
56 | | - "7\n" |
| 45 | + "7\n", |
| 46 | + "Hi\n" |
57 | 47 | ] |
58 | 48 | } |
59 | 49 | ], |
|
63 | 53 | "\n", |
64 | 54 | "# take the first member of the list (iterable), call it number temporarily do something with it (print it)\n", |
65 | 55 | "# 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')" |
68 | 59 | ] |
69 | 60 | }, |
70 | 61 | { |
|
78 | 69 | "cell_type": "markdown", |
79 | 70 | "metadata": {}, |
80 | 71 | "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:" |
82 | 73 | ] |
83 | 74 | }, |
84 | 75 | { |
85 | 76 | "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 | + ], |
91 | 92 | "source": [ |
92 | 93 | "friends = ['steve', 'rachel', 'michael', 'adam', 'monica']\n", |
93 | 94 | "for index, friend in enumerate(friends):\n", |
|
105 | 106 | "cell_type": "markdown", |
106 | 107 | "metadata": {}, |
107 | 108 | "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", |
109 | 110 | "\n", |
110 | 111 | "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" |
111 | 112 | ] |
112 | 113 | }, |
113 | 114 | { |
114 | 115 | "cell_type": "code", |
115 | | - "execution_count": 5, |
| 116 | + "execution_count": 62, |
116 | 117 | "metadata": { |
117 | 118 | "collapsed": true |
118 | 119 | }, |
|
123 | 124 | }, |
124 | 125 | { |
125 | 126 | "cell_type": "code", |
126 | | - "execution_count": 6, |
| 127 | + "execution_count": 63, |
127 | 128 | "metadata": {}, |
128 | 129 | "outputs": [ |
129 | 130 | { |
|
147 | 148 | }, |
148 | 149 | { |
149 | 150 | "cell_type": "code", |
150 | | - "execution_count": 7, |
| 151 | + "execution_count": 65, |
151 | 152 | "metadata": {}, |
152 | 153 | "outputs": [ |
153 | 154 | { |
154 | 155 | "name": "stdout", |
155 | 156 | "output_type": "stream", |
156 | 157 | "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" |
158 | 159 | ] |
159 | 160 | } |
160 | 161 | ], |
161 | 162 | "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", |
164 | 165 | "print(text)" |
165 | 166 | ] |
166 | 167 | }, |
167 | 168 | { |
168 | 169 | "cell_type": "code", |
169 | | - "execution_count": 8, |
| 170 | + "execution_count": 9, |
170 | 171 | "metadata": {}, |
171 | 172 | "outputs": [ |
172 | 173 | { |
173 | 174 | "data": { |
174 | 175 | "text/plain": [ |
175 | | - "['a',\n", |
| 176 | + "['On',\n", |
| 177 | + " 'a',\n", |
176 | 178 | " 'dark',\n", |
177 | 179 | " 'desert',\n", |
178 | 180 | " 'highway',\n", |
|
193 | 195 | " 'the']" |
194 | 196 | ] |
195 | 197 | }, |
196 | | - "execution_count": 8, |
| 198 | + "execution_count": 9, |
197 | 199 | "metadata": {}, |
198 | 200 | "output_type": "execute_result" |
199 | 201 | } |
200 | 202 | ], |
201 | 203 | "source": [ |
202 | 204 | "# Split converts string to list.\n", |
203 | 205 | "# Each item in list is split on spaces\n", |
204 | | - "text.split(' ')[1:20]" |
| 206 | + "text.split(' ')[0:20]" |
205 | 207 | ] |
206 | 208 | }, |
207 | 209 | { |
|
228 | 230 | }, |
229 | 231 | { |
230 | 232 | "cell_type": "code", |
231 | | - "execution_count": 10, |
| 233 | + "execution_count": 66, |
232 | 234 | "metadata": { |
233 | 235 | "collapsed": true |
234 | 236 | }, |
|
240 | 242 | }, |
241 | 243 | { |
242 | 244 | "cell_type": "code", |
243 | | - "execution_count": 11, |
| 245 | + "execution_count": 67, |
244 | 246 | "metadata": { |
245 | 247 | "collapsed": true |
246 | 248 | }, |
|
254 | 256 | }, |
255 | 257 | { |
256 | 258 | "cell_type": "code", |
257 | | - "execution_count": 12, |
| 259 | + "execution_count": 68, |
258 | 260 | "metadata": {}, |
259 | 261 | "outputs": [ |
260 | 262 | { |
261 | 263 | "data": { |
262 | 264 | "text/plain": [ |
263 | | - "['a',\n", |
| 265 | + "['On',\n", |
| 266 | + " 'a',\n", |
264 | 267 | " 'dark',\n", |
265 | 268 | " 'desert',\n", |
266 | 269 | " 'highway',\n", |
|
281 | 284 | " 'Up']" |
282 | 285 | ] |
283 | 286 | }, |
284 | | - "execution_count": 12, |
| 287 | + "execution_count": 68, |
285 | 288 | "metadata": {}, |
286 | 289 | "output_type": "execute_result" |
287 | 290 | } |
288 | 291 | ], |
289 | 292 | "source": [ |
290 | | - "cleaned_list[1:20]" |
| 293 | + "cleaned_list[0:20]" |
291 | 294 | ] |
292 | 295 | }, |
293 | 296 | { |
|
301 | 304 | "cell_type": "markdown", |
302 | 305 | "metadata": {}, |
303 | 306 | "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", |
305 | 308 | "continue used for ignoring certain values, but not break out of loop " |
306 | 309 | ] |
307 | 310 | }, |
308 | 311 | { |
309 | 312 | "cell_type": "code", |
310 | | - "execution_count": 13, |
| 313 | + "execution_count": 69, |
311 | 314 | "metadata": {}, |
312 | 315 | "outputs": [ |
313 | 316 | { |
314 | 317 | "data": { |
315 | 318 | "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']" |
317 | 338 | ] |
318 | 339 | }, |
319 | | - "execution_count": 13, |
| 340 | + "execution_count": 69, |
320 | 341 | "metadata": {}, |
321 | 342 | "output_type": "execute_result" |
322 | 343 | } |
323 | 344 | ], |
324 | 345 | "source": [ |
325 | | - "### Continue\n", |
326 | 346 | "cleaned_list = []\n", |
327 | 347 | "\n", |
328 | 348 | "for word in text.split(' '): \n", |
329 | 349 | " if word == '':\n", |
330 | 350 | " continue\n", |
331 | | - " cleaned_list.append(len(word))\n", |
| 351 | + " cleaned_list.append(word)\n", |
332 | 352 | "cleaned_list[1:20]" |
333 | 353 | ] |
334 | 354 | }, |
|
343 | 363 | "cell_type": "markdown", |
344 | 364 | "metadata": {}, |
345 | 365 | "source": [ |
346 | | - "break keyword will completely break out of loop " |
| 366 | + "break statement will completely break out of loop " |
347 | 367 | ] |
348 | 368 | }, |
349 | 369 | { |
350 | 370 | "cell_type": "code", |
351 | | - "execution_count": 16, |
| 371 | + "execution_count": 71, |
352 | 372 | "metadata": { |
353 | 373 | "collapsed": true |
354 | 374 | }, |
355 | 375 | "outputs": [], |
356 | 376 | "source": [ |
357 | | - "### Break and Continue\n", |
358 | 377 | "cleaned_list = []" |
359 | 378 | ] |
360 | 379 | }, |
361 | 380 | { |
362 | 381 | "cell_type": "code", |
363 | | - "execution_count": 17, |
| 382 | + "execution_count": 72, |
364 | 383 | "metadata": {}, |
365 | 384 | "outputs": [ |
| 385 | + { |
| 386 | + "name": "stdout", |
| 387 | + "output_type": "stream", |
| 388 | + "text": [ |
| 389 | + "I found the word I was looking for\n" |
| 390 | + ] |
| 391 | + }, |
366 | 392 | { |
367 | 393 | "data": { |
368 | 394 | "text/plain": [ |
369 | | - "[2, 1, 4, 6, 7, 0, 4, 4, 2, 2, 4]" |
| 395 | + "['On', 'a', 'dark']" |
370 | 396 | ] |
371 | 397 | }, |
372 | | - "execution_count": 17, |
| 398 | + "execution_count": 72, |
373 | 399 | "metadata": {}, |
374 | 400 | "output_type": "execute_result" |
375 | 401 | } |
376 | 402 | ], |
377 | 403 | "source": [ |
378 | 404 | "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", |
380 | 407 | " break\n", |
381 | | - " cleaned_list.append(len(word))\n", |
| 408 | + " cleaned_list.append(word)\n", |
382 | 409 | "cleaned_list" |
383 | 410 | ] |
384 | 411 | }, |
|
393 | 420 | "cell_type": "markdown", |
394 | 421 | "metadata": {}, |
395 | 422 | "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" |
397 | 424 | ] |
398 | 425 | }, |
399 | 426 | { |
400 | 427 | "cell_type": "code", |
401 | | - "execution_count": 18, |
| 428 | + "execution_count": 75, |
402 | 429 | "metadata": { |
403 | 430 | "collapsed": true |
404 | 431 | }, |
|
417 | 444 | }, |
418 | 445 | { |
419 | 446 | "cell_type": "code", |
420 | | - "execution_count": 20, |
| 447 | + "execution_count": 76, |
421 | 448 | "metadata": {}, |
422 | 449 | "outputs": [ |
423 | 450 | { |
|
434 | 461 | }, |
435 | 462 | { |
436 | 463 | "cell_type": "code", |
437 | | - "execution_count": 21, |
| 464 | + "execution_count": 77, |
438 | 465 | "metadata": {}, |
439 | 466 | "outputs": [ |
440 | 467 | { |
|
0 commit comments