|
350 | 350 | "* Print the first three days of the week" |
351 | 351 | ] |
352 | 352 | }, |
| 353 | + { |
| 354 | + "cell_type": "markdown", |
| 355 | + "metadata": {}, |
| 356 | + "source": [ |
| 357 | + "## Loops\n", |
| 358 | + "\n", |
| 359 | + "Loops are a way to repeatedly execute some code. Here's an example:\n" |
| 360 | + ] |
| 361 | + }, |
353 | 362 | { |
354 | 363 | "cell_type": "code", |
355 | 364 | "execution_count": null, |
356 | 365 | "metadata": {}, |
357 | 366 | "outputs": [], |
358 | | - "source": [] |
| 367 | + "source": [ |
| 368 | + "planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']\n", |
| 369 | + "for planet in planets:\n", |
| 370 | + " print(planet, end=' ') # print all on same line\n" |
| 371 | + ] |
| 372 | + }, |
| 373 | + { |
| 374 | + "cell_type": "markdown", |
| 375 | + "metadata": {}, |
| 376 | + "source": [ |
| 377 | + "The for loop specifies\n", |
| 378 | + "\n", |
| 379 | + " * the variable name to use (in this case, planet)\n", |
| 380 | + " \n", |
| 381 | + " * the set of values to loop over (in this case, planets)\n", |
| 382 | + "\n", |
| 383 | + "You use the word \"in\" to link them together.\n", |
| 384 | + "\n", |
| 385 | + "The object to the right of the \"in\" can be any object that supports iteration. Basically, if it can be thought of as a group of things, you can probably loop over it. In addition to lists, we can iterate over the elements of a tuple:\n" |
| 386 | + ] |
| 387 | + }, |
| 388 | + { |
| 389 | + "cell_type": "code", |
| 390 | + "execution_count": null, |
| 391 | + "metadata": {}, |
| 392 | + "outputs": [], |
| 393 | + "source": [ |
| 394 | + "multiplicands = [2, 2, 2, 3, 3, 5]\n", |
| 395 | + "product = 1\n", |
| 396 | + "for mult in multiplicands:\n", |
| 397 | + " product = product * mult\n", |
| 398 | + "product\n" |
| 399 | + ] |
| 400 | + }, |
| 401 | + { |
| 402 | + "cell_type": "markdown", |
| 403 | + "metadata": {}, |
| 404 | + "source": [ |
| 405 | + "\n", |
| 406 | + "You can even loop through each character in a string:\n" |
| 407 | + ] |
| 408 | + }, |
| 409 | + { |
| 410 | + "cell_type": "code", |
| 411 | + "execution_count": null, |
| 412 | + "metadata": {}, |
| 413 | + "outputs": [], |
| 414 | + "source": [ |
| 415 | + "s = 'steganograpHy is the practicE of conceaLing a file, message, image, or video within another fiLe, message, image, Or video.'\n", |
| 416 | + "msg = ''\n", |
| 417 | + "# print all the uppercase letters in s, one at a time\n", |
| 418 | + "for char in s:\n", |
| 419 | + " if char.isupper():\n", |
| 420 | + " print(char, end='') " |
| 421 | + ] |
| 422 | + }, |
| 423 | + { |
| 424 | + "cell_type": "markdown", |
| 425 | + "metadata": {}, |
| 426 | + "source": [ |
| 427 | + "## Do it yourself\n", |
| 428 | + "\n", |
| 429 | + "## Task 1\n", |
| 430 | + "\n", |
| 431 | + "* Create a list of all the 7 days of the weeks and store it in the variable `weekdays`.\n", |
| 432 | + "\n", |
| 433 | + "* Iterate through the first 5 days of the week stored in the variable `weekdays` and print them.\n", |
| 434 | + "\n", |
| 435 | + "\n", |
| 436 | + "\n", |
| 437 | + "## Task 2\n", |
| 438 | + "\n", |
| 439 | + "* Iterate the elements in the list defined below, divide each number by 5 and print quotient for each of the element.\n", |
| 440 | + "\n", |
| 441 | + "elements = [10,20,30,40,50]\n" |
| 442 | + ] |
| 443 | + }, |
| 444 | + { |
| 445 | + "cell_type": "code", |
| 446 | + "execution_count": null, |
| 447 | + "metadata": {}, |
| 448 | + "outputs": [], |
| 449 | + "source": [ |
| 450 | + "# write the code here\n" |
| 451 | + ] |
359 | 452 | } |
360 | 453 | ], |
361 | 454 | "metadata": { |
|
374 | 467 | "name": "python", |
375 | 468 | "nbconvert_exporter": "python", |
376 | 469 | "pygments_lexer": "ipython3", |
377 | | - "version": "3.7.6" |
| 470 | + "version": "3.7.4" |
378 | 471 | } |
379 | 472 | }, |
380 | 473 | "nbformat": 4, |
|
0 commit comments