|
9 | 9 | "\n", |
10 | 10 | "Actually, there are three functions that belong in itertools, but are so useful that they are included in Python by default, and do not need to be imported. It is essential that `range`, `enumerate`, and `zip` become tools that you are comfortable using.\n", |
11 | 11 | "\n", |
12 | | - "#### `range`\n", |
| 12 | + "**range**\n", |
| 13 | + "\n", |
13 | 14 | "generate a sequence of integers in the specified \"range\":\n", |
14 | 15 | "```python\n", |
15 | 16 | "# will generate 0.. 1.. 2.. ... 8.. 9\n", |
16 | 17 | "range(10)\n", |
17 | 18 | "```\n", |
18 | 19 | "\n", |
19 | | - "#### `enumerate`\n", |
| 20 | + "**enumerate**\n", |
| 21 | + "\n", |
20 | 22 | "\"enumerate\" the items in an iterable:\n", |
21 | 23 | "```python\n", |
22 | 24 | "# will generate (0, 'apple').. (1, 'banana').. (2, 'cat').. (3, 'dog')]\n", |
23 | 25 | "enumerate([\"apple\", \"banana\", \"cat\", \"dog\"])\n", |
24 | 26 | "```\n", |
25 | 27 | "\n", |
26 | | - "#### `zip`\n", |
| 28 | + "**zip**\n", |
| 29 | + "\n", |
27 | 30 | "\"zip\" together the corresponding elements of several iterables into tuples:\n", |
28 | 31 | "```python\n", |
29 | 32 | ">>> names = [\"Angie\", \"Brian\", \"Cassie\", \"David\"]\n", |
|
37 | 40 | "***\n", |
38 | 41 | "The following are some of the many useful tools provided by the `itertools` module:\n", |
39 | 42 | "\n", |
40 | | - "#### `itertools.chain`\n", |
| 43 | + "**itertools.chain**\n", |
| 44 | + "\n", |
41 | 45 | "\"chain\" together multiple iterables, end-to-end:\n", |
42 | 46 | "```python\n", |
43 | 47 | ">>> from itertools import chain\n", |
|
51 | 55 | "<itertools.chain at 0x20de109ec18>\n", |
52 | 56 | "```\n", |
53 | 57 | "\n", |
54 | | - "#### `itertools.combinations`\n", |
| 58 | + "**itertools.combinations**\n", |
55 | 59 | "Generate all length-n \"combinations\" from an iterable:\n", |
56 | 60 | "```python\n", |
57 | 61 | ">>> from itertools import combinations\n", |
|
66 | 70 | "cell_type": "markdown", |
67 | 71 | "metadata": {}, |
68 | 72 | "source": [ |
69 | | - "***\n", |
| 73 | + "<div class=\"alert alert-info\">\n", |
70 | 74 | "\n", |
71 | 75 | "**Reading Comprehension: Itertools I**\n", |
72 | 76 | "\n", |
73 | 77 | "Using the `itertools.combinations` function, find the probability that two randomly drawn items from the list `[\"apples\", \"bananas\", \"pears\", \"pears\", \"oranges\"]` would yield a combination of \"apples\" and \"pears\".\n", |
74 | 78 | "\n", |
75 | | - "***" |
| 79 | + "</div>" |
76 | 80 | ] |
77 | 81 | }, |
78 | 82 | { |
79 | 83 | "cell_type": "markdown", |
80 | 84 | "metadata": {}, |
81 | 85 | "source": [ |
82 | | - "***\n", |
| 86 | + "<div class=\"alert alert-info\">\n", |
83 | 87 | "\n", |
84 | 88 | "**Reading Comprehension: Itertools II**\n", |
85 | 89 | "\n", |
86 | 90 | "Given the list `x_vals = [0.1, 0.3, 0.6, 0.9]`, create a generator, `y_gen`, that will generate the y-value $y = x^2$ for each value of $x$. Then, using `zip`, create a list of the $(x, y)$ pairs, each pair stored in a tuple.\n", |
87 | 91 | "\n", |
88 | | - "***" |
| 92 | + "</div>" |
89 | 93 | ] |
90 | 94 | }, |
91 | 95 | { |
|
0 commit comments