Skip to content

Commit b80502c

Browse files
committed
updating good coding practices notebook
1 parent ee842d3 commit b80502c

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

notebooks/L3/gcp-3-pep8.ipynb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
"\n",
6666
"PEP 8 goes far beyond the scope of what we have learned so far during this course, and we recommend that you re-visit the guidelines every now and then when learning new things. **Here, we summarize some highlights that you can start applying to your code right away!**\n",
6767
"\n",
68+
"- [Maximum line length](#Maximum-line-length)\n",
69+
"- [Indentation](#Indentation)\n",
70+
"- [Whitespace and binary operators](#Whitespace-and-binary-operators)\n",
71+
"- [Avoid extraneous whitespace](#Avoid-extraneous-whitespace)\n",
72+
"- [Write one statement per line](#Write-one-statement-per-line)\n",
6873
"\n"
6974
]
7075
},
@@ -189,9 +194,9 @@
189194
"cell_type": "markdown",
190195
"metadata": {},
191196
"source": [
192-
"In addition, indentation is needed when breaking one command into multiple lines, such as in our example with the list `us_cities` above. \n",
197+
"In our case, the first option with the conditional expression on one line is ok, as it is not that long afterall.\n",
193198
"\n",
194-
"Following PEP 8 indentation guidelines, we can define `us_cities` also using a [hanging indent](https://www.python.org/dev/peps/pep-0008/#fn-hi). Note that there is no value on the first line of the list, and the closing bracket is lined up with the last line of the list:"
199+
"In addition, indentation is needed when breaking one command into multiple lines, such as in our example with the list `us_cities` above, where we used the implied line continuation inside the brackets. Following PEP 8 indentation guidelines, we can define `us_cities` also using a [hanging indent](https://www.python.org/dev/peps/pep-0008/#fn-hi). Note that there is no value on the first line of the list, and the closing bracket is lined up with the last line of the list:"
195200
]
196201
},
197202
{
@@ -238,7 +243,6 @@
238243
"cell_type": "code",
239244
"execution_count": 6,
240245
"metadata": {
241-
"collapsed": false,
242246
"jupyter": {
243247
"outputs_hidden": false
244248
}
@@ -274,7 +278,6 @@
274278
"cell_type": "code",
275279
"execution_count": 8,
276280
"metadata": {
277-
"collapsed": false,
278281
"jupyter": {
279282
"outputs_hidden": false
280283
}
@@ -299,7 +302,6 @@
299302
"cell_type": "code",
300303
"execution_count": 9,
301304
"metadata": {
302-
"collapsed": false,
303305
"jupyter": {
304306
"outputs_hidden": false
305307
}
@@ -322,7 +324,6 @@
322324
"cell_type": "code",
323325
"execution_count": 10,
324326
"metadata": {
325-
"collapsed": false,
326327
"jupyter": {
327328
"outputs_hidden": false
328329
}
@@ -345,17 +346,16 @@
345346
"cell_type": "markdown",
346347
"metadata": {},
347348
"source": [
348-
"### One thing per line \n",
349+
"### Write one statement per line \n",
349350
"\n",
350-
"Avoid compound statements (writing multiple statements on the same line).\n",
351+
"Avoid writing multiple statements on the same line:\n",
351352
"https://www.python.org/dev/peps/pep-0008/#other-recommendations"
352353
]
353354
},
354355
{
355356
"cell_type": "code",
356357
"execution_count": 11,
357358
"metadata": {
358-
"collapsed": false,
359359
"jupyter": {
360360
"outputs_hidden": false
361361
}
@@ -380,7 +380,6 @@
380380
"cell_type": "code",
381381
"execution_count": 12,
382382
"metadata": {
383-
"collapsed": false,
384383
"jupyter": {
385384
"outputs_hidden": false
386385
}
@@ -416,7 +415,6 @@
416415
"cell_type": "code",
417416
"execution_count": 14,
418417
"metadata": {
419-
"collapsed": false,
420418
"jupyter": {
421419
"outputs_hidden": false
422420
}
@@ -436,33 +434,43 @@
436434
"\n",
437435
"**Advanced Note**\n",
438436
"\n",
439-
"You often have to **balance between code readability and code length**. While the rule of thumb is to write one statement per line, Python also has a set of compound statements which are able to squeeze multiple operations into one line. **List comprehensions** are a useful approach for creating lists in a consise way. We are not covering list comprehensions during the lessons, but here is a short example from the [Python documentation](https://docs.python.org/3/tutorial/datastructures.html):\n",
437+
"You often have to **balance between code readability and code length**. PEP 8 guides us to generally avoid [compound statements](https://docs.python.org/3/reference/compound_stmts.html#compound-statements) (writing multiple statements on the same line). However, according to PEP 8, it might be sometimes ok to squeeze a short piece of code into one line, for example, with the`for` -statement. Sometimes you just have to judge yourself which option makes the code more readable and go for that.\n",
440438
"\n",
441-
"This for loop iterates over a range, squares all values and appends them to a list: \n",
439+
"One puzzling example regarding this guideline is the use of list comprehensions when defining lists. [List comprehensions](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) are a useful approach for creating lists in a consise way. We are not covering list comprehensions during the lessons, but here is a short example from the [Python documentation](https://docs.python.org/3/tutorial/datastructures.html). Let's have a look at two options that produce the same output:\n",
440+
"\n",
441+
"*Option A) This for loop iterates over a range, squares all values and appends them to a list:*\n",
442442
"\n",
443443
"```\n",
444444
"squares = []\n",
445445
"for x in range(10):\n",
446446
" squares.append(x**2)\n",
447447
"```\n",
448-
"We can achieve the same output using a list comprehension:\n",
448+
"*Option B) Square all values in the range using a list comprehension:*\n",
449449
"\n",
450450
"```\n",
451451
"squares = [x**2 for x in range(10)]\n",
452452
"```\n",
453453
"\n",
454+
"**Both approaches are fine, and you can choose the option that you think makes the code more readable.**\n",
454455
"\n",
455-
"In some cases, list comprehensions might make your code more readable and consise. In other cases, you might end up writing an excessively long statement which is difficult to read. In cases when PEP 8 (or your team) doesn't have a clear say on how to write the code, you can choose the option that you think makes the code more readable.\n",
456+
"In some cases, list comprehensions might make your code more readable and consise. In other cases, you might end up writing an excessively long statement which is difficult to read. \n",
456457
"</div>\n"
457458
]
459+
},
460+
{
461+
"cell_type": "code",
462+
"execution_count": null,
463+
"metadata": {},
464+
"outputs": [],
465+
"source": []
458466
}
459467
],
460468
"metadata": {
461469
"anaconda-cloud": {},
462470
"kernelspec": {
463-
"display_name": "Python [conda env:Anaconda3]",
471+
"display_name": "Python 3",
464472
"language": "python",
465-
"name": "conda-env-Anaconda3-py"
473+
"name": "python3"
466474
},
467475
"language_info": {
468476
"codemirror_mode": {
@@ -474,7 +482,7 @@
474482
"name": "python",
475483
"nbconvert_exporter": "python",
476484
"pygments_lexer": "ipython3",
477-
"version": "3.5.6"
485+
"version": "3.7.4"
478486
}
479487
},
480488
"nbformat": 4,

0 commit comments

Comments
 (0)