Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5fe599f

Browse filesBrowse files
baffelliedoardob90yakutovicha
authored
Mention enumerate in the "Control Flow" section (empa-scientific-it#90)
Co-authored-by: Edoardo Baldi <edoardob90@gmail.com> Co-authored-by: Aliaksandr Yakutovich <yakutovicha@gmail.com>
1 parent baf18d3 commit 5fe599f
Copy full SHA for 5fe599f

File tree

Expand file treeCollapse file tree

1 file changed

+56
-11
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+56
-11
lines changed

‎control_flow.ipynb

Copy file name to clipboardExpand all lines: control_flow.ipynb
+56-11Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,33 @@
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "7d456e6b-159d-4959-b258-60963c381ff6",
13+
"id": "5a1222fe",
1414
"metadata": {},
1515
"source": [
16-
"## Table of contents\n",
17-
"\n",
18-
"- [References](#References-{visibility=\"hidden\"})\n",
16+
"# Table of contents\n",
17+
"- [References](#References)\n",
1918
"- [Conditionals](#Conditionals)\n",
2019
"- [The `while` loop](#The-while-loop)\n",
2120
"- [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",
2223
"- [Nested loops](#Nested-loops)\n",
2324
"- [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",
2429
"- [Exceptions](#Exceptions)\n",
25-
"- [The `try-except` block](#The-try-except-block)\n",
30+
" - [The `try-except` block](#The-try-except-block)\n",
2631
"- [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"
3140
]
3241
},
3342
{
@@ -340,6 +349,37 @@
340349
"Why writing six lines of code when we can do the same with just two?"
341350
]
342351
},
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+
},
343383
{
344384
"cell_type": "markdown",
345385
"id": "75cb8fcd-6cb7-4c04-a843-3369eb41e066",
@@ -1111,10 +1151,15 @@
11111151
"name": "python",
11121152
"nbconvert_exporter": "python",
11131153
"pygments_lexer": "ipython3",
1114-
"version": "3.11.0"
1154+
"version": "3.11.3"
11151155
},
11161156
"toc-autonumbering": false,
1117-
"toc-showmarkdowntxt": false
1157+
"toc-showmarkdowntxt": false,
1158+
"vscode": {
1159+
"interpreter": {
1160+
"hash": "44a67ea841dc2166b3d2091af057378db3e19d02bed11714ee13c6843dc2a264"
1161+
}
1162+
}
11181163
},
11191164
"nbformat": 4,
11201165
"nbformat_minor": 5

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.