Skip to content

Commit 7610462

Browse files
committed
fix snippets
1 parent 8e4b4ce commit 7610462

10 files changed

+80
-67
lines changed

Python/Module2_EssentialsOfPython/Itertools.ipynb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
"#### `range`\n",
1313
"generate a sequence of integers in the specified \"range\":\n",
1414
"```python\n",
15-
">>> range(10)\n",
1615
"# will generate 0.. 1.. 2.. ... 8.. 9\n",
16+
"range(10)\n",
1717
"```\n",
1818
"\n",
1919
"#### `enumerate`\n",
2020
"\"enumerate\" the items in an iterable:\n",
2121
"```python\n",
22-
">>> enumerate([\"apple\", \"banana\", \"cat\", \"dog\"])\n",
2322
"# will generate (0, 'apple').. (1, 'banana').. (2, 'cat').. (3, 'dog')]\n",
23+
"enumerate([\"apple\", \"banana\", \"cat\", \"dog\"])\n",
2424
"```\n",
2525
"\n",
2626
"#### `zip`\n",
@@ -30,8 +30,9 @@
3030
">>> exam_1_scores = [90, 82, 79, 87]\n",
3131
">>> exam_2_scores = [95, 84, 72, 91]\n",
3232
"\n",
33-
">>> zip(names, exam_1_scores, exam_2_scores)\n",
3433
"# will generate ('Angie', 90, 95).. ('Brian', 82, 84).. ('Cassie', 79, 72).. ('David', 87, 91)]\n",
34+
">>> zip(names, exam_1_scores, exam_2_scores)\n",
35+
"<zip at 0x20de1082608>\n",
3536
"```\n",
3637
"***\n",
3738
"The following are some of the many useful tools provided by the `itertools` module:\n",
@@ -45,16 +46,19 @@
4546
">>> iter_3 = [\"moo\", \"cow\"]\n",
4647
">>> iter_4 = \"him\"\n",
4748
"\n",
48-
">>> chain(gen_1, gen_2, iter_3, iter_4)\n",
4949
"# will generate: 0.. 2.. 4.. 9.. 16.. 25.. 'moo'.. 'cow'.. 'h'.. 'i'.. 'm'\n",
50+
">>> chain(gen_1, gen_2, iter_3, iter_4)\n",
51+
"<itertools.chain at 0x20de109ec18>\n",
5052
"```\n",
5153
"\n",
5254
"#### `itertools.combinations`\n",
5355
"Generate all length-n \"combinations\" from an iterable:\n",
5456
"```python\n",
5557
">>> from itertools import combinations\n",
56-
">>> combinations([0, 1, 2, 3], 3) # generate all length-3 combinations from [0, 1, 2, 3]\n",
58+
"\n",
5759
"# will generate: (0, 1, 2).. (0, 1, 3).. (0, 2, 3).. (1, 2, 3)\n",
60+
">>> combinations([0, 1, 2, 3], 3) # generate all length-3 combinations from [0, 1, 2, 3]\n",
61+
"<itertools.combinations at 0x20de10a7728>\n",
5862
"```"
5963
]
6064
}
@@ -76,7 +80,7 @@
7680
"name": "python",
7781
"nbconvert_exporter": "python",
7882
"pygments_lexer": "ipython3",
79-
"version": "3.6.4"
83+
"version": "3.6.3"
8084
}
8185
},
8286
"nbformat": 4,

Python/Module2_EssentialsOfPython/SequenceTypes.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
"\n",
341341
"# start: 0, stop: 7, step: 1 \n",
342342
">>> seq[:] # equivalent: `seq[None:None]`\n",
343-
">>> seq = \"abcdefg\"\n",
343+
"'abcdefg'\n",
344344
"\n",
345345
"# start: 0, stop: 7, step: 2\n",
346346
">>> seq[::2]\n",
@@ -356,18 +356,18 @@
356356
"'gfedcba'\n",
357357
"\n",
358358
">>> seq[-2:]\n",
359-
"\"fg\"\n",
359+
"'fg'\n",
360360
"\n",
361361
">>> seq[:-2]\n",
362-
"\"abcde\"\n",
362+
"'abcde'\n",
363363
"```\n",
364364
"\n",
365365
"Although the colon-syntax for slicing, `seq[start:stop:step]`, appears nearly ubiquitously in Python code, it is important to know that there is a built-in `slice` object that Python uses to form slices. It accepts the same start, stop, and step values, and produces the same sort of slicing behavior:\n",
366366
"```python\n",
367367
"# using the `slice` object explicitly\n",
368368
">>> seq = \"abcdefg\"\n",
369369
">>> seq[slice(0, 3, 1)]\n",
370-
"\"abc\"\n",
370+
"'abc'\n",
371371
"```\n",
372372
"\n",
373373
"This gives you the ability to work with slices in more creative ways in your code, since it allows you to assign a variable to a slice.\n",
@@ -568,7 +568,7 @@
568568
"name": "python",
569569
"nbconvert_exporter": "python",
570570
"pygments_lexer": "ipython3",
571-
"version": "3.6.4"
571+
"version": "3.6.3"
572572
}
573573
},
574574
"nbformat": 4,

Python/Module2_EssentialsOfPython/Variables_and_Assignment.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"# shorthand for: x = x + (4, 5)\n",
134134
"# this creates a new tuple\n",
135135
">>> x += (4, 5)\n",
136+
">>> x \n",
136137
"(1, 2, 3, 4, 5)\n",
137138
"\n",
138139
"# y still references the original tuple\n",

Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.ipynb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
"\n",
3636
"Similar to Python's sequences, we use 0-based indices and slicing to access the content of an array. However, we must specify an index/slice for *each* dimension of an array:\n",
3737
"```python\n",
38+
">>> import numpy as np\n",
39+
"\n",
3840
"# A 3-D array\n",
3941
">>> x = np.array([[[0, 1],\n",
40-
" [2, 3]],\n",
41-
" \n",
42-
" [[4, 5],\n",
43-
" [6, 7]]])\n",
42+
"... [2, 3]],\n",
43+
"... \n",
44+
"... [[4, 5],\n",
45+
"... [6, 7]]])\n",
4446
"\n",
4547
"# get: sheet-0, both rows, flip order of columns\n",
4648
">>> x[0, :, ::-1]\n",
@@ -57,7 +59,7 @@
5759
"Let's being our discussion by constructing a simple ND-array containing three floating point numbers. \n",
5860
"\n",
5961
"```python\n",
60-
"simple_array = np.array([2.3, 0.1, -9.1])\n",
62+
">>> simple_array = np.array([2.3, 0.1, -9.1])\n",
6163
"```\n",
6264
"This array supports the same indexing scheme as Python's sequences (lists, tuples, and strings):\n",
6365
"\n",
@@ -122,9 +124,9 @@
122124
"\n",
123125
"```python\n",
124126
"# using a 2-dimensional array to store the grades\n",
125-
"grades = np.array([[93, 95], \n",
126-
" [84, 100], \n",
127-
" [99, 87]])\n",
127+
">>> grades = np.array([[93, 95], \n",
128+
"... [84, 100], \n",
129+
"... [99, 87]])\n",
128130
"```\n",
129131
"\n",
130132
"NumPy is able to see the repeated structure among the list-of-lists-of-numbers passed to `np.array`, and resolve the two dimensions of data, which we deem the 'student' dimension and the 'exam' dimension, respectively. \n",
@@ -195,6 +197,7 @@
195197
"```python\n",
196198
"# using a negative index and a slice\n",
197199
">>> grades[:, -1] # Latest exam scores (Exam 2), for all students\n",
200+
"array([ 95, 100, 87])\n",
198201
"```\n",
199202
"Note the value of using the negative index is that it will always provide you with the latest exam score - you need not check how many exams the students have taken.\n",
200203
"\n",
@@ -235,10 +238,11 @@
235238
"## N-dimensional Arrays\n",
236239
"Let's build up some intuition for arrays with a dimensionality higher than 2. The following code creates a 3-dimensional array:\n",
237240
"```python\n",
238-
"d3_array = np.array([[[0, 1],\n",
239-
" [2, 3]],\n",
240-
" [[4, 5],\n",
241-
" [6, 7]]])\n",
241+
"# a 3D array\n",
242+
">>> d3_array = np.array([[[0, 1],\n",
243+
"... [2, 3]],\n",
244+
"... [[4, 5],\n",
245+
"... [6, 7]]])\n",
242246
"```\n",
243247
"You can think of axis-0 denoting which of the 2x2 \"sheets\" to select from. Then axis-1 specifies the row along the sheets, and axis-2 the column within the row:\n",
244248
"\n",

Python/Module3_IntroducingNumpy/BasicArrayAttributes.ipynb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
"We will use the following array to provide context for our discussion:\n",
1111
" \n",
1212
"```python\n",
13+
">>> import numpy as np\n",
1314
">>> example_array = np.array([[[ 0, 1, 2, 3],\n",
14-
" [ 4, 5, 6, 7]],\n",
15-
"\n",
16-
" [[ 8, 9, 10, 11],\n",
17-
" [12, 13, 14, 15]],\n",
18-
"\n",
19-
" [[16, 17, 18, 19],\n",
20-
" [20, 21, 22, 23]]])\n",
15+
"... [ 4, 5, 6, 7]],\n",
16+
"...\n",
17+
"... [[ 8, 9, 10, 11],\n",
18+
"... [12, 13, 14, 15]],\n",
19+
"...\n",
20+
"... [[16, 17, 18, 19],\n",
21+
"... [20, 21, 22, 23]]])\n",
2122
"```\n",
2223
"According to the preceding discussion, it is a 3-dimensional array structured such that:\n",
2324
"\n",

Python/Module3_IntroducingNumpy/Broadcasting.ipynb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@
242242
"# axis-0 (rows): student\n",
243243
"# axis-1 (columns): exams \n",
244244
">>> import numpy as np\n",
245-
">>> grades = array([[ 0.79, 0.84, 0.84],\n",
246-
"... [ 0.87, 0.93, 0.78],\n",
247-
"... [ 0.77, 1. , 0.87],\n",
248-
"... [ 0.66, 0.75, 0.82],\n",
249-
"... [ 0.84, 0.89, 0.76],\n",
250-
"... [ 0.83, 0.71, 0.85]])\n",
245+
">>> grades = np.array([[ 0.79, 0.84, 0.84],\n",
246+
"... [ 0.87, 0.93, 0.78],\n",
247+
"... [ 0.77, 1. , 0.87],\n",
248+
"... [ 0.66, 0.75, 0.82],\n",
249+
"... [ 0.84, 0.89, 0.76],\n",
250+
"... [ 0.83, 0.71, 0.85]])\n",
251251
"```\n",
252252
"\n",
253253
"We might be interested to see how each of these scores compare to the mean score for that specific exam. Based on our discussion from the last section, we can easily compute the mean-score for each exam (rounded to 2 decimal places):\n",
@@ -787,14 +787,15 @@
787787
">>> x = np.array([[[ 0, 1, 2, 3],\n",
788788
"... [ 4, 5, 6, 7],\n",
789789
"... [ 8, 9, 10, 11]],\n",
790-
"\n",
790+
"...\n",
791791
"... [[12, 13, 14, 15],\n",
792792
"... [16, 17, 18, 19],\n",
793793
"... [20, 21, 22, 23]]])\n",
794794
"\n",
795795
"\n",
796796
"# sum over each row, within a sheet\n",
797797
">>> summed_rows = x.sum(axis=1)\n",
798+
">>> summed_rows\n",
798799
"array([[12, 15, 18, 21],\n",
799800
" [48, 51, 54, 57]])\n",
800801
"\n",

Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"You can create an array from a Python `list` or `tuple` by using NumPy's `array` function. NumPy will interpret the structure of the data it receives to determine the dimensionality and shape of the array. For example, a single list of numbers will be used to create a 1-dimensional array: \n",
4040
"\n",
4141
"```python\n",
42-
">>> import numpy as np\n",
4342
"# a list of numbers will become a 1D-array\n",
4443
">>> np.array([1., 2., 3.]) # shape: (3,)\n",
4544
"array([ 1., 2., 3.])\n",

Python/Module3_IntroducingNumpy/IntroducingTheNDarray.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"Let's take the mean value over the three distinct columns in our data:\n",
4646
"```python\n",
4747
">>> np.mean(x, axis=1)\n",
48-
"array([ 3., 4., 5.])\n",
48+
"array([ 1., 4., 7.])\n",
4949
"```\n",
5050
"\n",
5151
"We can use broadcasting to raise each column of `x` to a different power:\n",

Python/Module3_IntroducingNumpy/ReshapingArrays.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
"Because the size of an input array and the resulting reshaped array must agree, you can specify *one* of the dimension-sizes in the reshape function to be -1, and this will cue NumPy to compute that dimension's size for you. For example, if you are reshaping a shape-(36,) array into a shape-(3, 4, 3) array. The following are valid:\n",
5959
"```python\n",
6060
"# Equivalent ways of specifying a reshape\n",
61-
">>> np.arange(36).reshape(3, 4, 3) # (36,) --reshape--> (3, 4, 3) \n",
62-
">>> np.arange(36).reshape(3, 4, -1) # NumPy replaces -1 with 36/(3*4) -> 3\n",
63-
">>> np.arange(36).reshape(3, -1, 3) # NumPy replaces -1 with 36/(3*3) -> 4\n",
64-
">>> np.arange(36).reshape(-1, 4, 3) # NumPy replaces -1 with 36/(3*4) -> 3\n",
61+
"np.arange(36).reshape(3, 4, 3) # (36,) --reshape--> (3, 4, 3) \n",
62+
"np.arange(36).reshape(3, 4, -1) # NumPy replaces -1 with 36/(3*4) -> 3\n",
63+
"np.arange(36).reshape(3, -1, 3) # NumPy replaces -1 with 36/(3*3) -> 4\n",
64+
"np.arange(36).reshape(-1, 4, 3) # NumPy replaces -1 with 36/(3*4) -> 3\n",
6565
"```\n",
6666
"\n",
6767
"You can use -1 to specify only one dimension: \n",

0 commit comments

Comments
 (0)