|
65 | 65 | "\n", |
66 | 66 | "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", |
67 | 67 | "\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", |
68 | 73 | "\n" |
69 | 74 | ] |
70 | 75 | }, |
|
189 | 194 | "cell_type": "markdown", |
190 | 195 | "metadata": {}, |
191 | 196 | "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", |
193 | 198 | "\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:" |
195 | 200 | ] |
196 | 201 | }, |
197 | 202 | { |
|
238 | 243 | "cell_type": "code", |
239 | 244 | "execution_count": 6, |
240 | 245 | "metadata": { |
241 | | - "collapsed": false, |
242 | 246 | "jupyter": { |
243 | 247 | "outputs_hidden": false |
244 | 248 | } |
|
274 | 278 | "cell_type": "code", |
275 | 279 | "execution_count": 8, |
276 | 280 | "metadata": { |
277 | | - "collapsed": false, |
278 | 281 | "jupyter": { |
279 | 282 | "outputs_hidden": false |
280 | 283 | } |
|
299 | 302 | "cell_type": "code", |
300 | 303 | "execution_count": 9, |
301 | 304 | "metadata": { |
302 | | - "collapsed": false, |
303 | 305 | "jupyter": { |
304 | 306 | "outputs_hidden": false |
305 | 307 | } |
|
322 | 324 | "cell_type": "code", |
323 | 325 | "execution_count": 10, |
324 | 326 | "metadata": { |
325 | | - "collapsed": false, |
326 | 327 | "jupyter": { |
327 | 328 | "outputs_hidden": false |
328 | 329 | } |
|
345 | 346 | "cell_type": "markdown", |
346 | 347 | "metadata": {}, |
347 | 348 | "source": [ |
348 | | - "### One thing per line \n", |
| 349 | + "### Write one statement per line \n", |
349 | 350 | "\n", |
350 | | - "Avoid compound statements (writing multiple statements on the same line).\n", |
| 351 | + "Avoid writing multiple statements on the same line:\n", |
351 | 352 | "https://www.python.org/dev/peps/pep-0008/#other-recommendations" |
352 | 353 | ] |
353 | 354 | }, |
354 | 355 | { |
355 | 356 | "cell_type": "code", |
356 | 357 | "execution_count": 11, |
357 | 358 | "metadata": { |
358 | | - "collapsed": false, |
359 | 359 | "jupyter": { |
360 | 360 | "outputs_hidden": false |
361 | 361 | } |
|
380 | 380 | "cell_type": "code", |
381 | 381 | "execution_count": 12, |
382 | 382 | "metadata": { |
383 | | - "collapsed": false, |
384 | 383 | "jupyter": { |
385 | 384 | "outputs_hidden": false |
386 | 385 | } |
|
416 | 415 | "cell_type": "code", |
417 | 416 | "execution_count": 14, |
418 | 417 | "metadata": { |
419 | | - "collapsed": false, |
420 | 418 | "jupyter": { |
421 | 419 | "outputs_hidden": false |
422 | 420 | } |
|
436 | 434 | "\n", |
437 | 435 | "**Advanced Note**\n", |
438 | 436 | "\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", |
440 | 438 | "\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", |
442 | 442 | "\n", |
443 | 443 | "```\n", |
444 | 444 | "squares = []\n", |
445 | 445 | "for x in range(10):\n", |
446 | 446 | " squares.append(x**2)\n", |
447 | 447 | "```\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", |
449 | 449 | "\n", |
450 | 450 | "```\n", |
451 | 451 | "squares = [x**2 for x in range(10)]\n", |
452 | 452 | "```\n", |
453 | 453 | "\n", |
| 454 | + "**Both approaches are fine, and you can choose the option that you think makes the code more readable.**\n", |
454 | 455 | "\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", |
456 | 457 | "</div>\n" |
457 | 458 | ] |
| 459 | + }, |
| 460 | + { |
| 461 | + "cell_type": "code", |
| 462 | + "execution_count": null, |
| 463 | + "metadata": {}, |
| 464 | + "outputs": [], |
| 465 | + "source": [] |
458 | 466 | } |
459 | 467 | ], |
460 | 468 | "metadata": { |
461 | 469 | "anaconda-cloud": {}, |
462 | 470 | "kernelspec": { |
463 | | - "display_name": "Python [conda env:Anaconda3]", |
| 471 | + "display_name": "Python 3", |
464 | 472 | "language": "python", |
465 | | - "name": "conda-env-Anaconda3-py" |
| 473 | + "name": "python3" |
466 | 474 | }, |
467 | 475 | "language_info": { |
468 | 476 | "codemirror_mode": { |
|
474 | 482 | "name": "python", |
475 | 483 | "nbconvert_exporter": "python", |
476 | 484 | "pygments_lexer": "ipython3", |
477 | | - "version": "3.5.6" |
| 485 | + "version": "3.7.4" |
478 | 486 | } |
479 | 487 | }, |
480 | 488 | "nbformat": 4, |
|
0 commit comments