|
10 | 10 | },
|
11 | 11 | {
|
12 | 12 | "cell_type": "markdown",
|
13 |
| - "id": "7d456e6b-159d-4959-b258-60963c381ff6", |
| 13 | + "id": "5a1222fe", |
14 | 14 | "metadata": {},
|
15 | 15 | "source": [
|
16 |
| - "## Table of contents\n", |
17 |
| - "\n", |
18 |
| - "- [References](#References-{visibility=\"hidden\"})\n", |
| 16 | + "# Table of contents\n", |
| 17 | + "- [References](#References)\n", |
19 | 18 | "- [Conditionals](#Conditionals)\n",
|
20 | 19 | "- [The `while` loop](#The-while-loop)\n",
|
21 | 20 | "- [The `for` loop](#The-for-loop)\n",
|
| 21 | + " - [The `enumerate` built-in](#The-enumerate-built-in)\n", |
| 22 | + " - [The `range` built-in](#The-range-built-in)\n", |
22 | 23 | "- [Nested loops](#Nested-loops)\n",
|
23 | 24 | "- [Altering loops](#Altering-loops)\n",
|
| 25 | + " - [`if` statement inside `for`/`while`](#if-statement-inside-for/while)\n", |
| 26 | + " - [The `break` keyword](#The-break-keyword)\n", |
| 27 | + " - [The `continue` keyword](#The-continue-keyword)\n", |
| 28 | + " - [`else` after a `for`/`while`](#else-after-a-for/while)\n", |
24 | 29 | "- [Exceptions](#Exceptions)\n",
|
25 |
| - "- [The `try-except` block](#The-try-except-block)\n", |
| 30 | + " - [The `try-except` block](#The-try-except-block)\n", |
26 | 31 | "- [Exercises](#Exercises)\n",
|
27 |
| - " - [Find the factors 🌶️](#Find-the-factors-🌶️)\n", |
28 |
| - " - [Find the pair 🌶️](#Find-the-pair-🌶️)\n", |
29 |
| - " - [Cats with hats 🌶️🌶️](#Cats-with-hats-🌶️🌶️)\n", |
30 |
| - " - [Toboggan trajectory 🌶️🌶️🌶️](#Toboggan-trajectory-🌶️🌶️🌶️)\n" |
| 32 | + " - [Find the factors 🌶️](#Find-the-factors-🌶️)\n", |
| 33 | + " - [Find the pair 🌶️](#Find-the-pair-🌶️)\n", |
| 34 | + " - [Part 1](#Part-1)\n", |
| 35 | + " - [Part 2](#Part-2)\n", |
| 36 | + " - [Cats with hats 🌶️🌶️](#Cats-with-hats-🌶️🌶️)\n", |
| 37 | + " - [Toboggan trajectory 🌶️🌶️🌶️](#Toboggan-trajectory-🌶️🌶️🌶️)\n", |
| 38 | + " - [Part 1 🌶️](#Part-1-🌶️)\n", |
| 39 | + " - [Part 2 🌶️🌶️](#Part-2-🌶️🌶️)\n" |
31 | 40 | ]
|
32 | 41 | },
|
33 | 42 | {
|
|
340 | 349 | "Why writing six lines of code when we can do the same with just two?"
|
341 | 350 | ]
|
342 | 351 | },
|
| 352 | + { |
| 353 | + "cell_type": "code", |
| 354 | + "execution_count": null, |
| 355 | + "id": "84c2e6b1", |
| 356 | + "metadata": {}, |
| 357 | + "outputs": [], |
| 358 | + "source": [] |
| 359 | + }, |
| 360 | + { |
| 361 | + "cell_type": "markdown", |
| 362 | + "id": "5b0747a4", |
| 363 | + "metadata": {}, |
| 364 | + "source": [ |
| 365 | + "### The `enumerate` built-in\n", |
| 366 | + "Often, you need to iterate through a collection and operate on each value while keeping track of the *position* (index) of the loop.\n", |
| 367 | + "In other languages, you would define an index variable and increment it at every loop. While this is possible in Python, as shown in the previous code snippet, we usually prefer to use the built-in `enumerate` function, documented [here](https://docs.python.org/3/library/functions.html#enumerate).\n", |
| 368 | + "\n", |
| 369 | + "This function takes any iterable object and returns a new iterable that yields a tuple `(index, value)` when you iterate over it:" |
| 370 | + ] |
| 371 | + }, |
| 372 | + { |
| 373 | + "cell_type": "code", |
| 374 | + "execution_count": null, |
| 375 | + "id": "1782af19", |
| 376 | + "metadata": {}, |
| 377 | + "outputs": [], |
| 378 | + "source": [ |
| 379 | + "for index, letter in enumerate(\"Python\"):\n", |
| 380 | + " print(index, letter)" |
| 381 | + ] |
| 382 | + }, |
343 | 383 | {
|
344 | 384 | "cell_type": "markdown",
|
345 | 385 | "id": "75cb8fcd-6cb7-4c04-a843-3369eb41e066",
|
|
1111 | 1151 | "name": "python",
|
1112 | 1152 | "nbconvert_exporter": "python",
|
1113 | 1153 | "pygments_lexer": "ipython3",
|
1114 |
| - "version": "3.11.0" |
| 1154 | + "version": "3.11.3" |
1115 | 1155 | },
|
1116 | 1156 | "toc-autonumbering": false,
|
1117 |
| - "toc-showmarkdowntxt": false |
| 1157 | + "toc-showmarkdowntxt": false, |
| 1158 | + "vscode": { |
| 1159 | + "interpreter": { |
| 1160 | + "hash": "44a67ea841dc2166b3d2091af057378db3e19d02bed11714ee13c6843dc2a264" |
| 1161 | + } |
| 1162 | + } |
1118 | 1163 | },
|
1119 | 1164 | "nbformat": 4,
|
1120 | 1165 | "nbformat_minor": 5
|
|
0 commit comments