├── .gitignore ├── Pipfile ├── Pipfile.lock ├── Practicing Python 3.pdf ├── README.md ├── handouts ├── 01_data_types_primitives.ipynb ├── 02_control_flow_if_while.ipynb ├── 03_data_types_composites.ipynb ├── 04_control_flow_for.ipynb ├── 05_control_flow_try.ipynb ├── 06_control_flow_def.ipynb ├── 07_libraries_common_functions.ipynb ├── 08_libraries_input_output.ipynb ├── 09_libraries_command_line_arguments.py ├── 10_control_flow_yield.ipynb ├── 11_control_flow_comprehensions.ipynb ├── 12_libraries_functional_tricks.ipynb ├── 13_1_libraries_import_antigravity_utils.ipynb ├── 13_2_libraries_import_antigravity_datatypes.ipynb ├── 13_3_libraries_import_antigravity_re_n_persistence.ipynb ├── 13_4_libraries_import_antigravity_concurrency.ipynb ├── 13_5_libraries_import_antigravity_crawling.ipynb ├── 13_6_libraries_import_antigravity_numpy_n_pandas.ipynb ├── 13_7_libraries_import_antigravity_seaborn.ipynb ├── 13_8_libraries_import_antigravity_scikitlearn.ipynb ├── 14_libraries_moudle_and_package │ ├── m.py │ ├── p │ │ ├── __init__.py │ │ ├── ma.py │ │ └── mb.py │ └── use_them.py ├── 15_data_types_class.ipynb └── dataset_howell1.csv └── showcases ├── showcase_01_a_website_in_a_minute.py └── showcase_02_sympy_and_03_pandas.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | .DS_Store 3 | 4 | # 5 | __pycache__/ 6 | .ipynb_checkpoints/ 7 | 8 | # 9 | *.key 10 | *.numbers 11 | /private_showcases 12 | 13 | # 14 | /handouts/08_libraries_input_output.txt 15 | /handouts/09_libraries_command_line_arguments.txt 16 | -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | name = "pypi" 3 | url = "https://pypi.org/simple" 4 | verify_ssl = true 5 | 6 | [dev-packages] 7 | 8 | [packages] 9 | requests = "==2.21.0" 10 | beautifulsoup4 = "==4.7.1" 11 | flask = "==1.0.2" 12 | numpy = "==1.16.2" 13 | scipy = "==1.2.1" 14 | sympy = "==1.4" 15 | matplotlib = "==3.0.3" 16 | ipython = "==7.4.0" 17 | pandas = "==0.24.2" 18 | ipykernel = "==5.1.0" 19 | seaborn = "==0.9.0" 20 | statsmodels = "==0.9.0" 21 | scikit-learn = "==0.20.3" 22 | 23 | [requires] 24 | python_version = "3.7" 25 | -------------------------------------------------------------------------------- /Practicing Python 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/moskytw/practicing-python-3/652813122be42fdcd75439c8e60a8d610a2ac8d3/Practicing Python 3.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Practicing Python 3 2 | 3 | It's a full material help you learning Python from zero to intermediate! 🚀 4 | 5 | I've used it for my 12-hour Python course since 2015. It covers various topics, from the simplest for-loop to the classification in machine learning. 6 | 7 | The both deck and handouts are available online: 8 | 9 | * Deck: https://speakerdeck.com/mosky/practicing-python-3 10 | * Handouts: https://github.com/moskytw/practicing-python-3 11 | 12 | Enjoy! 😉 13 | 14 | The full outline: 15 | 16 | * Showcases 17 | * Our Toolbox 18 | * Hello, Python! 19 | * Checkpoint: Say Hi to Python 20 | * Data Types – Primitives 21 | * Checkpoint: Calculate BMR 22 | * Control Flow – If & While 23 | * Keep Learning 24 | * Data Types – Composites 25 | * Control Flow – For 26 | * Checkpoint: Calculate Average BMR 27 | * Control Flow – Try 28 | * Control Flow – Def 29 | * Checkpoint: Calculate Average BMR With Functions 30 | * Libraries – Common Functions 31 | * Libraries – Input & Output 32 | * Checkpoint: Calculate Average BMR From the Dataset 33 | * Libraries – Command Line Arguments 34 | * Checkpoint: Calculate BMR From Command Line 35 | * Control Flow – Yield 36 | * Control Flow – Comprehensions 37 | * Libraries – Functional Tricks 38 | * Checkpoint: Calculate Average BMR With Comprehensions 39 | * Libraries – Import Antigravity (has around 8 notebooks) 40 | * Checkpoint: Visualization 41 | * Libraries – Module & Package 42 | * Data Types – Class 43 | * Checkpoint: Classification 44 | -------------------------------------------------------------------------------- /handouts/01_data_types_primitives.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Data Types – Primitives\n", 8 | "\n", 9 | "## Numeric Types – `int`, `float`, `complex`" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "123" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "# basic precedence works\n", 30 | "10**2 + 2*10 + 2*(3-1)-1" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "100000000000000000000000000000000000000000000000000" 42 | ] 43 | }, 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "# ints have unlimited precision\n", 51 | "10**50" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "1.23" 63 | ] 64 | }, 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "1 + .2 + 0.03" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "(1.2+3j)" 83 | ] 84 | }, 85 | "execution_count": 4, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "# complex\n", 92 | "1.2 + 3j" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "### More Operations\n", 100 | "\n", 101 | "* `+`, `-`, `*`, `/`, `//`, `%`, `**`, `abs`\n", 102 | " * `a // b`: $ \\lfloor\\dfrac{a}{b}\\rfloor $.\n", 103 | " * `%`: $ 5 \\% 2 \\to 1 $.\n", 104 | " * `a ** b`: $ a^b $.\n", 105 | " * `abs(x)`: a function returns the absolute value.\n", 106 | "\n", 107 | "### Dig More\n", 108 | "\n", 109 | "* https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## Text Sequence Types – `str`" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "I am a string.\n", 129 | "<- There is a newline char.\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "# \\n is an escape sequence\n", 135 | "print('I am a string.\\n<- There is a newline char.')" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 6, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "Fun with triple single-quote pair.\n", 148 | "<- There is a newline char.\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "print('''Fun with triple single-quote pair.\n", 154 | "<- There is a newline char.''')" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 7, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "Escape the newline char.\n", 167 | "<- There is a newline char.\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "print('''\\\n", 173 | "Escape the newline char.\n", 174 | "<- There is a newline char.''')" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 8, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "A double-quote pair works like a singe-quote pair.\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "print(\"A double-quote pair works like a singe-quote pair.\")" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 9, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "A string's method: format.\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "# method is a function belonging to an object\n", 209 | "# everything in Python is an object\n", 210 | "print(\"A string's method: {}.\".format('format'))" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 10, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "I am a string.\\n<- There is a newline char.\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "# raw strings\n", 228 | "print(r'I am a string.\\n<- There is a newline char.')" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "### Dig More\n", 236 | "\n", 237 | "* Escape sequences: https://docs.python.org/3/reference/lexical_analysis.html#strings\n", 238 | "* Operations: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "## The None Type – `None`" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 11, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "None\n" 258 | ] 259 | } 260 | ], 261 | "source": [ 262 | "print(None)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "## Binary Sequence Types — `bytes`" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 12, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "abc\n", 282 | "b'abc'\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "print('abc')\n", 288 | "print(b'abc')" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 13, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "一串字串\n", 301 | "b'\\xe4\\xbd\\x8d\\xe5\\x85\\x83\\xe7\\xb5\\x84\\xe5\\x80\\x91'\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "print('一串字串')\n", 307 | "print(bytes('位元組們', 'utf-8'))" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 14, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "The length of '一串字串' -> 4 .\n", 320 | "The length of b'位元組們' -> 12 .\n" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "print(\"The length of '一串字串' ->\", len('一串字串'), '.')\n", 326 | "print(\"The length of b'位元組們' ->\", len(bytes('位元組們', 'utf-8')), '.')" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "### Dig More\n", 334 | "\n", 335 | "* UTF-8: https://en.wikipedia.org/wiki/UTF-8" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "## Boolean Types – `bool`" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 15, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "True" 354 | ] 355 | }, 356 | "execution_count": 15, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "True" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 16, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "data": { 372 | "text/plain": [ 373 | "False" 374 | ] 375 | }, 376 | "execution_count": 16, 377 | "metadata": {}, 378 | "output_type": "execute_result" 379 | } 380 | ], 381 | "source": [ 382 | "False" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 17, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "data": { 392 | "text/plain": [ 393 | "False" 394 | ] 395 | }, 396 | "execution_count": 17, 397 | "metadata": {}, 398 | "output_type": "execute_result" 399 | } 400 | ], 401 | "source": [ 402 | "not True" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 18, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "data": { 412 | "text/plain": [ 413 | "True" 414 | ] 415 | }, 416 | "execution_count": 18, 417 | "metadata": {}, 418 | "output_type": "execute_result" 419 | } 420 | ], 421 | "source": [ 422 | "True or False" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 19, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "False" 434 | ] 435 | }, 436 | "execution_count": 19, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "True and False" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 20, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "data": { 452 | "text/plain": [ 453 | "True" 454 | ] 455 | }, 456 | "execution_count": 20, 457 | "metadata": {}, 458 | "output_type": "execute_result" 459 | } 460 | ], 461 | "source": [ 462 | "100 > 10" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 21, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "data": { 472 | "text/plain": [ 473 | "False" 474 | ] 475 | }, 476 | "execution_count": 21, 477 | "metadata": {}, 478 | "output_type": "execute_result" 479 | } 480 | ], 481 | "source": [ 482 | "100 <= 10" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 22, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "data": { 492 | "text/plain": [ 493 | "True" 494 | ] 495 | }, 496 | "execution_count": 22, 497 | "metadata": {}, 498 | "output_type": "execute_result" 499 | } 500 | ], 501 | "source": [ 502 | "100 == 100" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 23, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "data": { 512 | "text/plain": [ 513 | "False" 514 | ] 515 | }, 516 | "execution_count": 23, 517 | "metadata": {}, 518 | "output_type": "execute_result" 519 | } 520 | ], 521 | "source": [ 522 | "'Mosky' == 'mosky'" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 24, 528 | "metadata": {}, 529 | "outputs": [ 530 | { 531 | "data": { 532 | "text/plain": [ 533 | "True" 534 | ] 535 | }, 536 | "execution_count": 24, 537 | "metadata": {}, 538 | "output_type": "execute_result" 539 | } 540 | ], 541 | "source": [ 542 | "'Mosky' != 'mosky'" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 25, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "data": { 552 | "text/plain": [ 553 | "True" 554 | ] 555 | }, 556 | "execution_count": 25, 557 | "metadata": {}, 558 | "output_type": "execute_result" 559 | } 560 | ], 561 | "source": [ 562 | "'mo' in 'mosky'" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": 26, 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "data": { 572 | "text/plain": [ 573 | "False" 574 | ] 575 | }, 576 | "execution_count": 26, 577 | "metadata": {}, 578 | "output_type": "execute_result" 579 | } 580 | ], 581 | "source": [ 582 | "None == 0" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 27, 588 | "metadata": {}, 589 | "outputs": [ 590 | { 591 | "data": { 592 | "text/plain": [ 593 | "False" 594 | ] 595 | }, 596 | "execution_count": 27, 597 | "metadata": {}, 598 | "output_type": "execute_result" 599 | } 600 | ], 601 | "source": [ 602 | "None == True" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": 28, 608 | "metadata": {}, 609 | "outputs": [ 610 | { 611 | "data": { 612 | "text/plain": [ 613 | "False" 614 | ] 615 | }, 616 | "execution_count": 28, 617 | "metadata": {}, 618 | "output_type": "execute_result" 619 | } 620 | ], 621 | "source": [ 622 | "None == False" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 29, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "data": { 632 | "text/plain": [ 633 | "True" 634 | ] 635 | }, 636 | "execution_count": 29, 637 | "metadata": {}, 638 | "output_type": "execute_result" 639 | } 640 | ], 641 | "source": [ 642 | "# None is equal to nothing but itself\n", 643 | "None == None" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 30, 649 | "metadata": {}, 650 | "outputs": [ 651 | { 652 | "data": { 653 | "text/plain": [ 654 | "True" 655 | ] 656 | }, 657 | "execution_count": 30, 658 | "metadata": {}, 659 | "output_type": "execute_result" 660 | } 661 | ], 662 | "source": [ 663 | "# same object?\n", 664 | "None is None" 665 | ] 666 | }, 667 | { 668 | "cell_type": "markdown", 669 | "metadata": {}, 670 | "source": [ 671 | "### Truth Value Testing" 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": 31, 677 | "metadata": {}, 678 | "outputs": [ 679 | { 680 | "data": { 681 | "text/plain": [ 682 | "True" 683 | ] 684 | }, 685 | "execution_count": 31, 686 | "metadata": {}, 687 | "output_type": "execute_result" 688 | } 689 | ], 690 | "source": [ 691 | "bool(1)" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": 32, 697 | "metadata": {}, 698 | "outputs": [ 699 | { 700 | "data": { 701 | "text/plain": [ 702 | "False" 703 | ] 704 | }, 705 | "execution_count": 32, 706 | "metadata": {}, 707 | "output_type": "execute_result" 708 | } 709 | ], 710 | "source": [ 711 | "bool(0)" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 33, 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "data": { 721 | "text/plain": [ 722 | "True" 723 | ] 724 | }, 725 | "execution_count": 33, 726 | "metadata": {}, 727 | "output_type": "execute_result" 728 | } 729 | ], 730 | "source": [ 731 | "bool(-1)" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 34, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "data": { 741 | "text/plain": [ 742 | "True" 743 | ] 744 | }, 745 | "execution_count": 34, 746 | "metadata": {}, 747 | "output_type": "execute_result" 748 | } 749 | ], 750 | "source": [ 751 | "bool('Mosky')" 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 35, 757 | "metadata": {}, 758 | "outputs": [ 759 | { 760 | "data": { 761 | "text/plain": [ 762 | "False" 763 | ] 764 | }, 765 | "execution_count": 35, 766 | "metadata": {}, 767 | "output_type": "execute_result" 768 | } 769 | ], 770 | "source": [ 771 | "bool('')" 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "metadata": {}, 777 | "source": [ 778 | "#### The Rules\n", 779 | "\n", 780 | "* `None` and `False` are false\n", 781 | "* Zeros are false\n", 782 | "* Empty collections are false" 783 | ] 784 | }, 785 | { 786 | "cell_type": "markdown", 787 | "metadata": {}, 788 | "source": [ 789 | "### Dig More\n", 790 | "\n", 791 | "* Truth Value Testing: https://docs.python.org/3/library/stdtypes.html#truth-value-testing\n", 792 | "* Boolean Operations & Comparisons: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not" 793 | ] 794 | }, 795 | { 796 | "cell_type": "markdown", 797 | "metadata": {}, 798 | "source": [ 799 | "## Variables" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": 36, 805 | "metadata": {}, 806 | "outputs": [ 807 | { 808 | "data": { 809 | "text/plain": [ 810 | "0" 811 | ] 812 | }, 813 | "execution_count": 36, 814 | "metadata": {}, 815 | "output_type": "execute_result" 816 | } 817 | ], 818 | "source": [ 819 | "# =: points to\n", 820 | "# count points to 0\n", 821 | "count = 0\n", 822 | "count" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 37, 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "data": { 832 | "text/plain": [ 833 | "1" 834 | ] 835 | }, 836 | "execution_count": 37, 837 | "metadata": {}, 838 | "output_type": "execute_result" 839 | } 840 | ], 841 | "source": [ 842 | "count = count + 1\n", 843 | "count" 844 | ] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "execution_count": 38, 849 | "metadata": {}, 850 | "outputs": [ 851 | { 852 | "data": { 853 | "text/plain": [ 854 | "2" 855 | ] 856 | }, 857 | "execution_count": 38, 858 | "metadata": {}, 859 | "output_type": "execute_result" 860 | } 861 | ], 862 | "source": [ 863 | "# +=: is an augmented assignment operators\n", 864 | "count += 1\n", 865 | "count" 866 | ] 867 | }, 868 | { 869 | "cell_type": "code", 870 | "execution_count": 39, 871 | "metadata": {}, 872 | "outputs": [ 873 | { 874 | "name": "stdout", 875 | "output_type": "stream", 876 | "text": [ 877 | "The formatted string literals, count -> 2.\n" 878 | ] 879 | } 880 | ], 881 | "source": [ 882 | "print(f'The formatted string literals, count -> {count}.')" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": 40, 888 | "metadata": {}, 889 | "outputs": [ 890 | { 891 | "name": "stdout", 892 | "output_type": "stream", 893 | "text": [ 894 | "Your BMI is 19.60.\n" 895 | ] 896 | } 897 | ], 898 | "source": [ 899 | "height_cm = 166\n", 900 | "weight_kg = 54\n", 901 | "\n", 902 | "height_m = height_cm / 100\n", 903 | "\n", 904 | "# https://en.wikipedia.org/wiki/Body_mass_index\n", 905 | "bmi = weight_kg / height_m**2\n", 906 | "\n", 907 | "print(f'Your BMI is {bmi:.2f}.')" 908 | ] 909 | }, 910 | { 911 | "cell_type": "markdown", 912 | "metadata": {}, 913 | "source": [ 914 | "### Dig More\n", 915 | "\n", 916 | "* Augmented assignment operators: https://docs.python.org/3/reference/lexical_analysis.html#delimiters\n", 917 | "* Format string syntax: https://docs.python.org/3/library/string.html#formatstrings" 918 | ] 919 | } 920 | ], 921 | "metadata": { 922 | "kernelspec": { 923 | "display_name": "Python 3 (practicing-python-3)", 924 | "language": "python", 925 | "name": "python3-practicing-python-3" 926 | }, 927 | "language_info": { 928 | "codemirror_mode": { 929 | "name": "ipython", 930 | "version": 3 931 | }, 932 | "file_extension": ".py", 933 | "mimetype": "text/x-python", 934 | "name": "python", 935 | "nbconvert_exporter": "python", 936 | "pygments_lexer": "ipython3", 937 | "version": "3.6.3" 938 | }, 939 | "toc": { 940 | "nav_menu": {}, 941 | "number_sections": true, 942 | "sideBar": true, 943 | "skip_h1_title": true, 944 | "toc_cell": false, 945 | "toc_position": {}, 946 | "toc_section_display": "block", 947 | "toc_window_display": false 948 | } 949 | }, 950 | "nbformat": 4, 951 | "nbformat_minor": 2 952 | } 953 | -------------------------------------------------------------------------------- /handouts/02_control_flow_if_while.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – If & While\n", 8 | "\n", 9 | "## If" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "10 is even.\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "x = 10\n", 27 | "\n", 28 | "if x % 2 == 0:\n", 29 | " print(x, 'is even.')\n", 30 | "else:\n", 31 | " print(x, 'is odd.')" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "very significant.\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "p = 0.005\n", 49 | "\n", 50 | "if p < 0.001:\n", 51 | " print('extremely significant.')\n", 52 | "elif p < 0.01:\n", 53 | " print('very significant.')\n", 54 | "elif p < 0.05:\n", 55 | " print('significant.')\n", 56 | "else:\n", 57 | " print('not significant')" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "very significant.\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "# === if 0.001 < p and p <= 0.01:\n", 75 | "if 0.001 < p <= 0.01:\n", 76 | " print('very significant.')" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "-161" 88 | ] 89 | }, 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "gender_key = 'female'\n", 97 | "\n", 98 | "if gender_key == 'male':\n", 99 | " s = 5\n", 100 | "else:\n", 101 | " s = -161\n", 102 | " \n", 103 | "s" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 5, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "-161" 115 | ] 116 | }, 117 | "execution_count": 5, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "s = 5 if gender_key == 'male' else -161\n", 124 | "# like `gender_key == 'male' ? 5 : -161` in other langs\n", 125 | "\n", 126 | "s" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "### Dig More\n", 134 | "\n", 135 | "* https://en.wikipedia.org/wiki/Statistical_significance" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "## While" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 6, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "count: 2.\n", 155 | "count: 1.\n", 156 | "count: 0.\n" 157 | ] 158 | } 159 | ], 160 | "source": [ 161 | "count = 3\n", 162 | "# using the truth value testing\n", 163 | "while count:\n", 164 | " count -= 1\n", 165 | " print(f'count: {count}.')" 166 | ] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Python 3 (practicing-python-3)", 172 | "language": "python", 173 | "name": "python3-practicing-python-3" 174 | }, 175 | "language_info": { 176 | "codemirror_mode": { 177 | "name": "ipython", 178 | "version": 3 179 | }, 180 | "file_extension": ".py", 181 | "mimetype": "text/x-python", 182 | "name": "python", 183 | "nbconvert_exporter": "python", 184 | "pygments_lexer": "ipython3", 185 | "version": "3.6.3" 186 | }, 187 | "toc": { 188 | "nav_menu": {}, 189 | "number_sections": true, 190 | "sideBar": true, 191 | "skip_h1_title": true, 192 | "toc_cell": false, 193 | "toc_position": {}, 194 | "toc_section_display": "block", 195 | "toc_window_display": false 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 2 200 | } 201 | -------------------------------------------------------------------------------- /handouts/04_control_flow_for.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – For" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "0 1 2 3 4 5 6 7 8 9 \n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "for i in range(10):\n", 25 | " print(i, end=' ')\n", 26 | "print()" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | " \n", 39 | " *\n", 40 | " ***\n", 41 | " *****\n", 42 | " *******\n", 43 | " *********\n", 44 | " ***********\n", 45 | " *************\n", 46 | " ***************\n", 47 | "*****************\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "n = 10\n", 53 | "for i in range(n):\n", 54 | " print(' '*(n-i-1) + '*'*(i*2-1))" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "mosky.liu\n", 67 | "mosky.bot\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "id_name_map = {\n", 73 | " 'mosky.liu': 'Mosky Liu',\n", 74 | " 'mosky.bot': 'Mosky Bot'\n", 75 | "}\n", 76 | "\n", 77 | "# for ... in :\n", 78 | "# iterable objects: str, list, tuple, range, set, dict, etc.\n", 79 | "for id_ in id_name_map:\n", 80 | " print(id_)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 4, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "mosky.liu: Mosky Liu\n", 93 | "mosky.bot: Mosky Bot\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "for id_, name in id_name_map.items():\n", 99 | " print(f'{id_}: {name}')" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "0 a\n", 112 | "1 b\n", 113 | "2 c\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "# https://docs.python.org/3/library/functions.html#zip\n", 119 | "for i, c in zip(range(3), 'abc'):\n", 120 | " print(i, c)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "0 a\n", 133 | "1 b\n", 134 | "2 c\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "# https://docs.python.org/3/library/functions.html#enumerate\n", 140 | "for i, c in enumerate('abc'):\n", 141 | " print(i, c)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "10 a\n", 154 | "11 b\n", 155 | "12 c\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "for i, c in enumerate('abc', start=10):\n", 161 | " print(i, c)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 8, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "2 is a prime.\n", 174 | "3 is a prime.\n", 175 | "5 is a prime.\n", 176 | "7 is a prime.\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# https://en.wikipedia.org/wiki/Prime_number\n", 182 | "for n in range(2, 10):\n", 183 | " for i in range(2, n):\n", 184 | " if n % i == 0:\n", 185 | " # break the whole loop\n", 186 | " break\n", 187 | " else: # loop ... else!\n", 188 | " print(n, 'is a prime.')" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "Retry task for mosky.\n" 201 | ] 202 | } 203 | ], 204 | "source": [ 205 | "id_task_succeed_map = {\n", 206 | " 'mosky': False,\n", 207 | " 'apple': True,\n", 208 | " 'orange': True,\n", 209 | "}\n", 210 | "\n", 211 | "for id_, task_succeed in id_task_succeed_map.items():\n", 212 | " \n", 213 | " if task_succeed:\n", 214 | " # continue to the next iteration\n", 215 | " continue\n", 216 | " \n", 217 | " print(f'Retry task for {id_}.')" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 10, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "# pass DOES nothing\n", 227 | "for i in range(10):\n", 228 | " pass" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 11, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "Average height (cm): 146.50\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "health_dicts = [\n", 246 | " {'height_cm': 152, 'weight_kg': 48, 'age': 63, 'male_yn': 1},\n", 247 | " {'height_cm': 157, 'weight_kg': 53, 'age': 41, 'male_yn': 1},\n", 248 | " {'height_cm': 140, 'weight_kg': 37, 'age': 63, 'male_yn': 0},\n", 249 | " {'height_cm': 137, 'weight_kg': 32, 'age': 65, 'male_yn': 0},\n", 250 | "]\n", 251 | "\n", 252 | "# s: sum of heights in cm\n", 253 | "s = 0\n", 254 | "for d in health_dicts:\n", 255 | " s += d['height_cm']\n", 256 | " \n", 257 | "# a: average height in cm\n", 258 | "a = s/len(health_dicts)\n", 259 | " \n", 260 | "print(f'Average height (cm): {a:.2f}')" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "## Visualize Execution\n", 268 | "\n", 269 | "→ https://goo.gl/R1Wt8P" 270 | ] 271 | } 272 | ], 273 | "metadata": { 274 | "kernelspec": { 275 | "display_name": "Python 3 (practicing-python-3)", 276 | "language": "python", 277 | "name": "python3-practicing-python-3" 278 | }, 279 | "language_info": { 280 | "codemirror_mode": { 281 | "name": "ipython", 282 | "version": 3 283 | }, 284 | "file_extension": ".py", 285 | "mimetype": "text/x-python", 286 | "name": "python", 287 | "nbconvert_exporter": "python", 288 | "pygments_lexer": "ipython3", 289 | "version": "3.6.3" 290 | }, 291 | "toc": { 292 | "nav_menu": {}, 293 | "number_sections": true, 294 | "sideBar": true, 295 | "skip_h1_title": true, 296 | "toc_cell": false, 297 | "toc_position": {}, 298 | "toc_section_display": "block", 299 | "toc_window_display": false 300 | } 301 | }, 302 | "nbformat": 4, 303 | "nbformat_minor": 2 304 | } 305 | -------------------------------------------------------------------------------- /handouts/05_control_flow_try.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – Try" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Your BMI is 19.60.\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "height_cm = 166\n", 25 | "weight_kg = 54\n", 26 | "\n", 27 | "height_m = height_cm / 100\n", 28 | "\n", 29 | "# https://en.wikipedia.org/wiki/Body_mass_index\n", 30 | "bmi = weight_kg / height_m**2\n", 31 | "\n", 32 | "print(f'Your BMI is {bmi:.2f}.')" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "# # how about\n", 42 | "\n", 43 | "# height_cm = '166'\n", 44 | "# weight_kg = '54'\n", 45 | "\n", 46 | "# height_m = height_cm/100\n", 47 | "\n", 48 | "# # https://en.wikipedia.org/wiki/Body_mass_index\n", 49 | "# bmi = weight_kg / height_m**2\n", 50 | "\n", 51 | "# print(f'Your BMI is {bmi:.2f}.')" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Error: unsupported operand type(s) for /: 'str' and 'int'\n", 64 | "Thanks for using.\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "height_cm = '166'\n", 70 | "weight_kg = '54'\n", 71 | "\n", 72 | "try:\n", 73 | " height_m = height_cm / 100\n", 74 | " bmi = weight_kg / height_m**2\n", 75 | "except Exception as e:\n", 76 | " # when an exception happens\n", 77 | " print('Error:', e)\n", 78 | "else:\n", 79 | " # when no exception happens\n", 80 | " print(f'Your BMI is {bmi:.2f}.')\n", 81 | "finally:\n", 82 | " # in any case\n", 83 | " # usually for releasing non-Python resource\n", 84 | " print('Thanks for using.')" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "## The Syntax\n", 92 | "\n", 93 | "* `except` can be many.\n", 94 | "* `else` and `finally` are optional.\n", 95 | "\n", 96 | "## Dig More\n", 97 | "\n", 98 | "* https://docs.python.org/3/library/exceptions.html#exception-hierarchy." 99 | ] 100 | } 101 | ], 102 | "metadata": { 103 | "kernelspec": { 104 | "display_name": "Python 3 (practicing-python-3)", 105 | "language": "python", 106 | "name": "python3-practicing-python-3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 3 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython3", 118 | "version": "3.6.3" 119 | }, 120 | "toc": { 121 | "nav_menu": {}, 122 | "number_sections": true, 123 | "sideBar": true, 124 | "skip_h1_title": true, 125 | "toc_cell": false, 126 | "toc_position": {}, 127 | "toc_section_display": "block", 128 | "toc_window_display": false 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 2 133 | } 134 | -------------------------------------------------------------------------------- /handouts/06_control_flow_def.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – Def\n", 8 | "\n", 9 | "## Functions Are Bricks" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def is_prime(n):\n", 19 | "\n", 20 | " if n <= 1:\n", 21 | " return False\n", 22 | "\n", 23 | " for i in range(2, n):\n", 24 | " if n % i == 0:\n", 25 | " return False\n", 26 | "\n", 27 | " return True" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "2 is a prime.\n", 40 | "3 is a prime.\n", 41 | "5 is a prime.\n", 42 | "7 is a prime.\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "for n in range(10):\n", 48 | " if is_prime(n):\n", 49 | " print(n, 'is a prime.')" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "def calc_bmi(height_cm, weight_kg):\n", 59 | " height_m = height_cm / 100\n", 60 | " return weight_kg / height_m**2" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 4, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "#1 bmi: 20.78\n", 73 | "#2 bmi: 21.50\n", 74 | "#3 bmi: 18.88\n", 75 | "#4 bmi: 17.05\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "health_dicts = [\n", 81 | " {'height_cm': 152, 'weight_kg': 48, 'age': 63, 'male_yn': 1},\n", 82 | " {'height_cm': 157, 'weight_kg': 53, 'age': 41, 'male_yn': 1},\n", 83 | " {'height_cm': 140, 'weight_kg': 37, 'age': 63, 'male_yn': 0},\n", 84 | " {'height_cm': 137, 'weight_kg': 32, 'age': 65, 'male_yn': 0},\n", 85 | "]\n", 86 | "\n", 87 | "# s: sum of heights in cm\n", 88 | "s = 0\n", 89 | "for no, d in enumerate(health_dicts, start=1):\n", 90 | " bmi = calc_bmi(d['height_cm'], d['weight_kg'])\n", 91 | " print(f'#{no} bmi: {bmi:.2f}')" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "## When Calling Functions" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "def print_n_add(a, b):\n", 108 | " '''Print a, b, and add them.\n", 109 | " \n", 110 | " * a, b: anything supporting +\n", 111 | " '''\n", 112 | "\n", 113 | " print('a :', a)\n", 114 | " print('b :', b)\n", 115 | " print('a+b:', a+b)\n", 116 | " print()\n", 117 | "\n", 118 | " return a+b" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "### Keyword Arguments" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "a : 1\n", 138 | "b : 2\n", 139 | "a+b: 3\n", 140 | "\n", 141 | "a : 1\n", 142 | "b : 2\n", 143 | "a+b: 3\n", 144 | "\n", 145 | "a : 1\n", 146 | "b : 2\n", 147 | "a+b: 3\n", 148 | "\n" 149 | ] 150 | }, 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "3" 155 | ] 156 | }, 157 | "execution_count": 6, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "print_n_add(1, 2)\n", 164 | "print_n_add(b=2, a=1)\n", 165 | "print_n_add(1, b=2)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "### Unpacking Argument List" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 7, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "a : 1\n", 185 | "b : 2\n", 186 | "a+b: 3\n", 187 | "\n", 188 | "a : 1\n", 189 | "b : 2\n", 190 | "a+b: 3\n", 191 | "\n" 192 | ] 193 | }, 194 | { 195 | "data": { 196 | "text/plain": [ 197 | "3" 198 | ] 199 | }, 200 | "execution_count": 7, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "seq = [1, 2]\n", 207 | "print_n_add(*seq)\n", 208 | "\n", 209 | "# print_n_add(seq)\n", 210 | "# === print_n_add([1, 2])\n", 211 | "\n", 212 | "# print_n_add(*seq)\n", 213 | "# === print_n_add(1, 2)\n", 214 | "\n", 215 | "map_ = {'a': 1, 'b': 2}\n", 216 | "print_n_add(**map_)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "## When Defining Functions\n", 224 | "\n", 225 | "### Default Values" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 8, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [ 234 | "def inc(x, delta=1):\n", 235 | " return x+delta" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 9, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "2\n", 248 | "101\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "print(inc(1))\n", 254 | "print(inc(1, 100))" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "### Arbitrary Argument List" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 10, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "mosky\n", 274 | "apple\n", 275 | "orange\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "print('mosky')\n", 281 | "print('apple')\n", 282 | "print('orange')" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 11, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "# s: space\n", 292 | "def print_s(*args, **kwargs):\n", 293 | " return print(*args, end=' ', **kwargs)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 12, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "mosky apple orange " 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "print_s('mosky')\n", 311 | "print_s('apple')\n", 312 | "print_s('orange')" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 13, 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [ 321 | "def print_args(positional_1, positional_2, *positional_others, keyword_only, **other_keywords):\n", 322 | " print('positional_1:', positional_1)\n", 323 | " print('positional_2:', positional_2)\n", 324 | " print('positional_others:', positional_others)\n", 325 | " print('keyword_only:', keyword_only)\n", 326 | " print('other_keywords:', other_keywords)" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 14, 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "name": "stdout", 336 | "output_type": "stream", 337 | "text": [ 338 | "positional_1: 1\n", 339 | "positional_2: 2\n", 340 | "positional_others: ()\n", 341 | "keyword_only: k_1\n", 342 | "other_keywords: {}\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "print_args(1, 2, keyword_only='k_1')" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 15, 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "name": "stdout", 357 | "output_type": "stream", 358 | "text": [ 359 | "positional_1: 1\n", 360 | "positional_2: 2\n", 361 | "positional_others: (3,)\n", 362 | "keyword_only: k_1\n", 363 | "other_keywords: {'keyword_2': 'k_2'}\n" 364 | ] 365 | } 366 | ], 367 | "source": [ 368 | "print_args(1, 2, 3, keyword_only='k_1', keyword_2='k_2')" 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "### Use Immutable Types as Default Values" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 16, 381 | "metadata": {}, 382 | "outputs": [], 383 | "source": [ 384 | "def bad_default_value_f(l=[]):\n", 385 | " l.append(1)\n", 386 | " return l" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 17, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "[1]\n", 399 | "[1, 1]\n", 400 | "[1, 1, 1]\n" 401 | ] 402 | } 403 | ], 404 | "source": [ 405 | "print(bad_default_value_f())\n", 406 | "print(bad_default_value_f())\n", 407 | "print(bad_default_value_f())" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 18, 413 | "metadata": {}, 414 | "outputs": [], 415 | "source": [ 416 | "def better_default_value_f(l=None):\n", 417 | " if l is None:\n", 418 | " l = []\n", 419 | " l.append(1)\n", 420 | " return l" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 19, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "name": "stdout", 430 | "output_type": "stream", 431 | "text": [ 432 | "[1]\n", 433 | "[1]\n", 434 | "[1]\n" 435 | ] 436 | } 437 | ], 438 | "source": [ 439 | "print(better_default_value_f())\n", 440 | "print(better_default_value_f())\n", 441 | "print(better_default_value_f())" 442 | ] 443 | }, 444 | { 445 | "cell_type": "markdown", 446 | "metadata": {}, 447 | "source": [ 448 | "Another pro is the caller can set `None` to use the default value. It'll be a great design in a large system." 449 | ] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "## Max the Flexibility" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 20, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "name": "stdout", 465 | "output_type": "stream", 466 | "text": [ 467 | "a : 1\n", 468 | "b : 2\n", 469 | "a+b: 3\n", 470 | "\n", 471 | "a : 1.0\n", 472 | "b : 2.0\n", 473 | "a+b: 3.0\n", 474 | "\n", 475 | "a : a\n", 476 | "b : b\n", 477 | "a+b: ab\n", 478 | "\n" 479 | ] 480 | }, 481 | { 482 | "data": { 483 | "text/plain": [ 484 | "'ab'" 485 | ] 486 | }, 487 | "execution_count": 20, 488 | "metadata": {}, 489 | "output_type": "execute_result" 490 | } 491 | ], 492 | "source": [ 493 | "print_n_add(1, 2)\n", 494 | "print_n_add(1., 2.)\n", 495 | "print_n_add('a', 'b')" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 21, 501 | "metadata": {}, 502 | "outputs": [], 503 | "source": [ 504 | "def sum_args(*args):\n", 505 | " '''Sum variables.\n", 506 | "\n", 507 | " The arg can be any object which supports += delimiter.\n", 508 | " '''\n", 509 | "\n", 510 | " # https://docs.python.org/3/library/stdtypes.html#iterator-types\n", 511 | " args_it = iter(args)\n", 512 | " ret_val = next(args_it)\n", 513 | " for arg in args_it:\n", 514 | " ret_val += arg\n", 515 | "\n", 516 | " return ret_val" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 22, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "name": "stdout", 526 | "output_type": "stream", 527 | "text": [ 528 | "6\n", 529 | "abc\n" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "print(sum_args(1, 2, 3))\n", 535 | "print(sum_args('a', 'b' 'c'))" 536 | ] 537 | }, 538 | { 539 | "cell_type": "markdown", 540 | "metadata": {}, 541 | "source": [ 542 | "## Using Docstrings to Cooperate" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 23, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "name": "stdout", 552 | "output_type": "stream", 553 | "text": [ 554 | "Help on function print_n_add in module __main__:\n", 555 | "\n", 556 | "print_n_add(a, b)\n", 557 | " Print a, b, and add them.\n", 558 | " \n", 559 | " * a, b: anything supporting +\n", 560 | "\n" 561 | ] 562 | } 563 | ], 564 | "source": [ 565 | "help(print_n_add)" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "### The Jupyter Notebook Way" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": 24, 578 | "metadata": {}, 579 | "outputs": [], 580 | "source": [ 581 | "# print_n_add?" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 25, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [ 590 | "# print_n_add??" 591 | ] 592 | }, 593 | { 594 | "cell_type": "markdown", 595 | "metadata": {}, 596 | "source": [ 597 | "## The Small Functions – `lambda` functions" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 26, 603 | "metadata": {}, 604 | "outputs": [], 605 | "source": [ 606 | "items = ['Python', 'Ruby', 'JavaScript']" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 27, 612 | "metadata": {}, 613 | "outputs": [ 614 | { 615 | "data": { 616 | "text/plain": [ 617 | "['JavaScript', 'Python', 'Ruby']" 618 | ] 619 | }, 620 | "execution_count": 27, 621 | "metadata": {}, 622 | "output_type": "execute_result" 623 | } 624 | ], 625 | "source": [ 626 | "items.sort()\n", 627 | "items" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 28, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/plain": [ 638 | "['Ruby', 'Python', 'JavaScript']" 639 | ] 640 | }, 641 | "execution_count": 28, 642 | "metadata": {}, 643 | "output_type": "execute_result" 644 | } 645 | ], 646 | "source": [ 647 | "items.sort(key=len)\n", 648 | "items" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": 29, 654 | "metadata": {}, 655 | "outputs": [ 656 | { 657 | "data": { 658 | "text/plain": [ 659 | "['Python', 'Ruby', 'JavaScript']" 660 | ] 661 | }, 662 | "execution_count": 29, 663 | "metadata": {}, 664 | "output_type": "execute_result" 665 | } 666 | ], 667 | "source": [ 668 | "item_weight_map = {'Python': -90, 'Ruby': -50}\n", 669 | "items.sort(\n", 670 | " key=lambda item: item_weight_map.get(item, 0)\n", 671 | ")\n", 672 | "items" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 30, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "data": { 682 | "text/plain": [ 683 | "[{'age': 65, 'height_cm': 137, 'male_yn': 0, 'weight_kg': 32},\n", 684 | " {'age': 63, 'height_cm': 140, 'male_yn': 0, 'weight_kg': 37},\n", 685 | " {'age': 63, 'height_cm': 152, 'male_yn': 1, 'weight_kg': 48},\n", 686 | " {'age': 41, 'height_cm': 157, 'male_yn': 1, 'weight_kg': 53}]" 687 | ] 688 | }, 689 | "execution_count": 30, 690 | "metadata": {}, 691 | "output_type": "execute_result" 692 | } 693 | ], 694 | "source": [ 695 | "health_dicts = [\n", 696 | " {'height_cm': 152, 'weight_kg': 48, 'age': 63, 'male_yn': 1},\n", 697 | " {'height_cm': 157, 'weight_kg': 53, 'age': 41, 'male_yn': 1},\n", 698 | " {'height_cm': 140, 'weight_kg': 37, 'age': 63, 'male_yn': 0},\n", 699 | " {'height_cm': 137, 'weight_kg': 32, 'age': 65, 'male_yn': 0},\n", 700 | "]\n", 701 | "\n", 702 | "health_dicts.sort(key=lambda d: d['height_cm'])\n", 703 | "health_dicts" 704 | ] 705 | }, 706 | { 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "## The Beautiful Functions – The Decorators" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 31, 716 | "metadata": {}, 717 | "outputs": [], 718 | "source": [ 719 | "def debugee_f(arg):\n", 720 | " print('Do something ...')\n", 721 | " return arg" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 32, 727 | "metadata": {}, 728 | "outputs": [ 729 | { 730 | "name": "stdout", 731 | "output_type": "stream", 732 | "text": [ 733 | "Do something ...\n" 734 | ] 735 | }, 736 | { 737 | "data": { 738 | "text/plain": [ 739 | "1" 740 | ] 741 | }, 742 | "execution_count": 32, 743 | "metadata": {}, 744 | "output_type": "execute_result" 745 | } 746 | ], 747 | "source": [ 748 | "debugee_f(1)" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 33, 754 | "metadata": {}, 755 | "outputs": [], 756 | "source": [ 757 | "def print_arg_n_call(f):\n", 758 | " \n", 759 | " def wrapped_f(*args, **kwargs):\n", 760 | " \n", 761 | " print('args:', args)\n", 762 | " print('kwargs:', kwargs)\n", 763 | " print()\n", 764 | " \n", 765 | " return f(*args, **kwargs)\n", 766 | " \n", 767 | " return wrapped_f" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 34, 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [ 776 | "wrapped_debugee_f = print_arg_n_call(debugee_f)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 35, 782 | "metadata": {}, 783 | "outputs": [ 784 | { 785 | "name": "stdout", 786 | "output_type": "stream", 787 | "text": [ 788 | "args: (2,)\n", 789 | "kwargs: {}\n", 790 | "\n", 791 | "Do something ...\n" 792 | ] 793 | }, 794 | { 795 | "data": { 796 | "text/plain": [ 797 | "2" 798 | ] 799 | }, 800 | "execution_count": 35, 801 | "metadata": {}, 802 | "output_type": "execute_result" 803 | } 804 | ], 805 | "source": [ 806 | "wrapped_debugee_f(2)" 807 | ] 808 | }, 809 | { 810 | "cell_type": "code", 811 | "execution_count": 36, 812 | "metadata": {}, 813 | "outputs": [], 814 | "source": [ 815 | "@print_arg_n_call # <- decorator\n", 816 | "def debuggee_g(arg):\n", 817 | " print('Do other things ...')\n", 818 | " return arg" 819 | ] 820 | }, 821 | { 822 | "cell_type": "code", 823 | "execution_count": 37, 824 | "metadata": {}, 825 | "outputs": [ 826 | { 827 | "name": "stdout", 828 | "output_type": "stream", 829 | "text": [ 830 | "args: (3,)\n", 831 | "kwargs: {}\n", 832 | "\n", 833 | "Do other things ...\n" 834 | ] 835 | }, 836 | { 837 | "data": { 838 | "text/plain": [ 839 | "3" 840 | ] 841 | }, 842 | "execution_count": 37, 843 | "metadata": {}, 844 | "output_type": "execute_result" 845 | } 846 | ], 847 | "source": [ 848 | "debuggee_g(3)" 849 | ] 850 | } 851 | ], 852 | "metadata": { 853 | "kernelspec": { 854 | "display_name": "Python 3 (practicing-python-3)", 855 | "language": "python", 856 | "name": "python3-practicing-python-3" 857 | }, 858 | "language_info": { 859 | "codemirror_mode": { 860 | "name": "ipython", 861 | "version": 3 862 | }, 863 | "file_extension": ".py", 864 | "mimetype": "text/x-python", 865 | "name": "python", 866 | "nbconvert_exporter": "python", 867 | "pygments_lexer": "ipython3", 868 | "version": "3.6.3" 869 | }, 870 | "toc": { 871 | "nav_menu": { 872 | "height": "280px", 873 | "width": "401px" 874 | }, 875 | "number_sections": true, 876 | "sideBar": true, 877 | "skip_h1_title": true, 878 | "toc_cell": false, 879 | "toc_position": {}, 880 | "toc_section_display": "block", 881 | "toc_window_display": false 882 | } 883 | }, 884 | "nbformat": 4, 885 | "nbformat_minor": 2 886 | } 887 | -------------------------------------------------------------------------------- /handouts/07_libraries_common_functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Libraries – Common Functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Common Functions" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "abs(-3.14) -> 3.14\n", 27 | "round(3.14, 1) -> 3.1\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "print(f'abs(-3.14) -> {abs(-3.14)!r}')\n", 33 | "print(f'round(3.14, 1) -> {round(3.14, 1)!r}')" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "sum([3, 1, 4]) -> 8\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "print(f'sum([3, 1, 4]) -> {sum([3, 1 ,4])!r}')" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "math.floor(3.14) -> 3\n", 63 | "math.floor(-3.14) -> -4\n", 64 | "math.ceil(3.14) -> 4\n", 65 | "math.ceil(-3.14) -> -3\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "import math\n", 71 | "print(f'math.floor(3.14) -> {math.floor(3.14)!r}')\n", 72 | "print(f'math.floor(-3.14) -> {math.floor(-3.14)!r}')\n", 73 | "print(f'math.ceil(3.14) -> {math.ceil(3.14)!r}')\n", 74 | "print(f'math.ceil(-3.14) -> {math.ceil(-3.14)!r}')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "statistics.mean([6, 8, 10]) -> 8\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "import statistics\n", 92 | "print(f'statistics.mean([6, 8, 10]) -> {statistics.mean([6, 8, 10])!r}')" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "chars.sort() -> None\n", 105 | "sorted(chars) -> ['a', 'e', 'l', 'p', 'p']\n", 106 | "chars is sorted(chars) -> False\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "chars = list('apple')\n", 112 | "print(f'chars.sort() -> {chars.sort()!r}')\n", 113 | "print(f'sorted(chars) -> {sorted(chars)!r}')\n", 114 | "print(f'chars is sorted(chars) -> {chars is sorted(chars)!r}')" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "## String Methods" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 6, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "Python is a cool programming language.\n", 134 | "['Python', 'is', 'a', 'cool', 'programming', 'language.']\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "sentence = 'Python is a cool programming language.'\n", 140 | "words = sentence.split(' ')\n", 141 | "print(sentence)\n", 142 | "print(words)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 7, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "'Python is a cool programming language.'" 154 | ] 155 | }, 156 | "execution_count": 7, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "' '.join(words)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 8, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/plain": [ 173 | "('zh', '_', 'TW')" 174 | ] 175 | }, 176 | "execution_count": 8, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "'zh_TW'.partition('_')" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 9, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "('Python is a cool programming language.', '', '')" 194 | ] 195 | }, 196 | "execution_count": 9, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "'Python is a cool programming language.'.partition('_')" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 10, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "('', '', 'Python is a cool programming language.')" 214 | ] 215 | }, 216 | "execution_count": 10, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "'Python is a cool programming language.'.rpartition('_')" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 11, 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "'07_libraries_common_functions'" 234 | ] 235 | }, 236 | "execution_count": 11, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "# _: for the values we don't need in convention\n", 243 | "stem, _, _ = '07_libraries_common_functions.ipynb'.partition('.')\n", 244 | "stem" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 12, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "data": { 254 | "text/plain": [ 255 | "'mosky.liu@gmail.com'" 256 | ] 257 | }, 258 | "execution_count": 12, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "'Mosky.Liu@gmail.com'.lower()" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 13, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "data": { 274 | "text/plain": [ 275 | "'Title'" 276 | ] 277 | }, 278 | "execution_count": 13, 279 | "metadata": {}, 280 | "output_type": "execute_result" 281 | } 282 | ], 283 | "source": [ 284 | "'title'.capitalize()" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 14, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "data": { 294 | "text/plain": [ 295 | "True" 296 | ] 297 | }, 298 | "execution_count": 14, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "'2017-09-17'.startswith('2017-09')" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 15, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "data": { 314 | "text/plain": [ 315 | "True" 316 | ] 317 | }, 318 | "execution_count": 15, 319 | "metadata": {}, 320 | "output_type": "execute_result" 321 | } 322 | ], 323 | "source": [ 324 | "# must also check the content\n", 325 | "# consider user uploads a malware whose name is “profile.png” \n", 326 | "'profile.png'.endswith('.png')" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "## Common Types\n", 334 | "\n", 335 | "Like functions." 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 16, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "name": "stdout", 345 | "output_type": "stream", 346 | "text": [ 347 | "Actually int is rather than .\n", 348 | "\n", 349 | "But it's okay, in Python, the syntaxes of them are same.\n" 350 | ] 351 | } 352 | ], 353 | "source": [ 354 | "print(f'''\\\n", 355 | "Actually int is {type(int)!r} rather than {type(abs)!r}.\n", 356 | "\n", 357 | "But it's okay, in Python, the syntaxes of them are same.''')" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 17, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "int() -> 0\n", 370 | "int(3) -> 3\n", 371 | "int(3.14) -> 3\n", 372 | "int(-3.14) -> -3\n", 373 | "int('3') -> 3\n", 374 | "int('3.14') -> ValueError\n", 375 | "int('x') -> ValueError\n", 376 | "int(None) -> TypeError\n", 377 | "int([3.14]) -> TypeError\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "print(f'int() -> {int()!r}')\n", 383 | "print(f'int(3) -> {int(3)!r}')\n", 384 | "print(f'int(3.14) -> {int(3.14)!r}') # truncates towards zero\n", 385 | "print(f'int(-3.14) -> {int(-3.14)!r}')\n", 386 | "print(f\"int('3') -> {int(3)!r}\")\n", 387 | "print(f\"int('3.14') -> ValueError\")\n", 388 | "print(f\"int('x') -> ValueError\")\n", 389 | "print(f'int(None) -> TypeError')\n", 390 | "print(f'int([3.14]) -> TypeError')" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 18, 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "float() -> 0.0\n", 403 | "float(3) -> 3.0\n", 404 | "float(3.14) -> 3.14\n", 405 | "float('3') -> 3.0\n", 406 | "float('3.14') -> 3.14\n", 407 | "float('x') -> ValueError\n", 408 | "float(None) -> TypeError\n", 409 | "float([3.14]) -> TypeError\n" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "print(f'float() -> {float()!r}')\n", 415 | "print(f'float(3) -> {float(3)!r}')\n", 416 | "print(f'float(3.14) -> {float(3.14)!r}')\n", 417 | "print(f\"float('3') -> {float('3')!r}\")\n", 418 | "print(f\"float('3.14') -> {float('3.14')!r}\")\n", 419 | "print(f\"float('x') -> ValueError\")\n", 420 | "print(f'float(None) -> TypeError')\n", 421 | "print(f'float([3.14]) -> TypeError')" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 19, 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "str() -> ''\n", 434 | "str(3) -> '3'\n", 435 | "str(3.14) -> '3.14'\n", 436 | "str('3') -> '3'\n", 437 | "str('3.14') -> '3.14'\n", 438 | "str('x') -> 'x'\n", 439 | "str(None) -> 'None'\n", 440 | "str([3.14]) -> '[3.14]'\n" 441 | ] 442 | } 443 | ], 444 | "source": [ 445 | "print(f'str() -> {str()!r}')\n", 446 | "print(f'str(3) -> {str(3)!r}')\n", 447 | "print(f'str(3.14) -> {str(3.14)!r}')\n", 448 | "print(f\"str('3') -> {str('3')!r}\")\n", 449 | "print(f\"str('3.14') -> {str('3.14')!r}\")\n", 450 | "print(f\"str('x') -> {str('x')!r}\")\n", 451 | "print(f\"str(None) -> {str(None)!r}\")\n", 452 | "print(f\"str([3.14]) -> {str([3.14])!r}\")" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 20, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "'是駭客!'" 464 | ] 465 | }, 466 | "execution_count": 20, 467 | "metadata": {}, 468 | "output_type": "execute_result" 469 | } 470 | ], 471 | "source": [ 472 | "str(b'\\xe6\\x98\\xaf\\xe9\\xa7\\xad\\xe5\\xae\\xa2\\xef\\xbc\\x81', 'utf-8')" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 21, 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "[] \n", 485 | "() \n", 486 | "{} \n", 487 | "\n", 488 | "list() -> []\n", 489 | "tuple() -> ()\n", 490 | "set() -> set()\n" 491 | ] 492 | } 493 | ], 494 | "source": [ 495 | "print([], type([]))\n", 496 | "print((), type(()))\n", 497 | "print({}, type({}))\n", 498 | "print()\n", 499 | "\n", 500 | "print(f\"list() -> {list()!r}\")\n", 501 | "print(f\"tuple() -> {tuple()!r}\")\n", 502 | "print(f\"set() -> {set()!r}\")" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 22, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "name": "stdout", 512 | "output_type": "stream", 513 | "text": [ 514 | "list('apple') -> ['a', 'p', 'p', 'l', 'e']\n", 515 | "tuple('apple') -> ('a', 'p', 'p', 'l', 'e')\n", 516 | "set('apple') -> {'e', 'l', 'a', 'p'}\n" 517 | ] 518 | } 519 | ], 520 | "source": [ 521 | "print(f\"list('apple') -> {list('apple')!r}\")\n", 522 | "print(f\"tuple('apple') -> {tuple('apple')!r}\")\n", 523 | "print(f\"set('apple') -> {set('apple')!r}\")" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 23, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "{'a': 1, 'b': 2} -> {'a': 1, 'b': 2}\n", 536 | "dict(a=1, b=2) -> {'a': 1, 'b': 2}\n", 537 | "dict([('a', 1), ('b', 2)]) -> {'a': 1, 'b': 2}\n" 538 | ] 539 | } 540 | ], 541 | "source": [ 542 | "print(\"{{'a': 1, 'b': 2}} -> {!r}\".format(\n", 543 | " {'a': 1, 'b': 2}\n", 544 | "))\n", 545 | "print(\"dict(a=1, b=2) -> {!r}\".format(\n", 546 | " dict(a=1, b=2)\n", 547 | "))\n", 548 | "print(\"dict([('a', 1), ('b', 2)]) -> {!r}\".format(\n", 549 | " dict([\n", 550 | " ('a', 1),\n", 551 | " ('b', 2)\n", 552 | " ])\n", 553 | "))" 554 | ] 555 | }, 556 | { 557 | "cell_type": "markdown", 558 | "metadata": {}, 559 | "source": [ 560 | "## Dig More\n", 561 | "\n", 562 | "* All built-in functions: https://docs.python.org/3/library/functions.html\n", 563 | "* The math module: https://docs.python.org/3/library/math.html\n", 564 | "* All string methods: https://docs.python.org/3/library/stdtypes.html#string-methods" 565 | ] 566 | } 567 | ], 568 | "metadata": { 569 | "kernelspec": { 570 | "display_name": "Python 3 (practicing-python-3)", 571 | "language": "python", 572 | "name": "python3-practicing-python-3" 573 | }, 574 | "language_info": { 575 | "codemirror_mode": { 576 | "name": "ipython", 577 | "version": 3 578 | }, 579 | "file_extension": ".py", 580 | "mimetype": "text/x-python", 581 | "name": "python", 582 | "nbconvert_exporter": "python", 583 | "pygments_lexer": "ipython3", 584 | "version": "3.6.3" 585 | }, 586 | "toc": { 587 | "nav_menu": {}, 588 | "number_sections": true, 589 | "sideBar": true, 590 | "skip_h1_title": true, 591 | "toc_cell": false, 592 | "toc_position": {}, 593 | "toc_section_display": "block", 594 | "toc_window_display": false 595 | } 596 | }, 597 | "nbformat": 4, 598 | "nbformat_minor": 2 599 | } 600 | -------------------------------------------------------------------------------- /handouts/08_libraries_input_output.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Libraries – Input & Output" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Stdout, Stderr, and Stdin" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "The default print is just printing to stdout\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "print('The default print is just printing to stdout')" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stderr", 41 | "output_type": "stream", 42 | "text": [ 43 | "Error: height must > 0\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "import sys\n", 49 | "print('Error: height must > 0', file=sys.stderr)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# int(input('Enter the height: ')) # from stdin" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "## Files" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# variables in UPPERCASE are constants in convention\n", 75 | "# more conventions: https://www.python.org/dev/peps/pep-0008/\n", 76 | "#\n", 77 | "# about the dataset:\n", 78 | "#\n", 79 | "# - https://github.com/rmcelreath/rethinking/blob/master/data/Howell1.csv\n", 80 | "# - https://opendata.stackexchange.com/a/7798 \n", 81 | "#\n", 82 | "DATASET_FILENAME = 'dataset_howell1.csv'\n", 83 | "OUTPUT_FILENAME = '08_libraries_input_output.txt'" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Open With the Naive Way" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "f = open(DATASET_FILENAME)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "\"height\";\"weight\";\"age\";\"male\"\n", 112 | "151.765;47.8256065;63;1\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "print(next(f), end='')\n", 118 | "print(next(f), end='')" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "### Open Using Context Manager" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 7, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "\"height\";\"weight\";\"age\";\"male\"\n", 138 | "151.765;47.8256065;63;1\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "with open(DATASET_FILENAME) as f:\n", 144 | " print(next(f), end='')\n", 145 | " print(next(f), end='')" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "When leave the `with` suite, the `f` will be closed automatically." 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "### Write and Read" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 8, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# # use Cmd-/ or Ctrl-/ to uncomment\n", 169 | "\n", 170 | "# with open(OUTPUT_FILENAME, 'w') as f:\n", 171 | "\n", 172 | "# while True:\n", 173 | "\n", 174 | "# line = input('')\n", 175 | "# if line == '':\n", 176 | "# break\n", 177 | "\n", 178 | "# f.write(line)\n", 179 | "# f.write('\\n')\n", 180 | "\n", 181 | "# with open(OUTPUT_FILENAME) as f:\n", 182 | " \n", 183 | "# for line in f:\n", 184 | "# print(line, end='')" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "### Load CSV" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 9, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "skip ['height', 'weight', 'age', 'male']\n", 204 | "['151.765', '47.8256065', '63', '1']\n", 205 | "['139.7', '36.4858065', '63', '0']\n", 206 | "['136.525', '31.864838', '65', '0']\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "import csv\n", 212 | "\n", 213 | "with open(DATASET_FILENAME) as f:\n", 214 | " \n", 215 | " csvr = csv.reader(f, delimiter=';')\n", 216 | " for line_no, row in enumerate(csvr, start=1):\n", 217 | " \n", 218 | " if line_no == 1:\n", 219 | " print('skip', row)\n", 220 | " continue\n", 221 | " \n", 222 | " if line_no >= 5:\n", 223 | " break\n", 224 | " \n", 225 | " print(row)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "## Dig More\n", 233 | "\n", 234 | "- https://docs.python.org/3/library/functions.html#open\n", 235 | "- https://docs.python.org/3/reference/compound_stmts.html#the-with-statement\n", 236 | "- https://docs.python.org/3/library/io.html\n", 237 | "- https://docs.python.org/3/library/csv.html" 238 | ] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 3 (practicing-python-3)", 244 | "language": "python", 245 | "name": "python3-practicing-python-3" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 3 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython3", 257 | "version": "3.7.3" 258 | }, 259 | "toc": { 260 | "base_numbering": 1, 261 | "nav_menu": {}, 262 | "number_sections": true, 263 | "sideBar": true, 264 | "skip_h1_title": true, 265 | "title_cell": "Table of Contents", 266 | "title_sidebar": "Contents", 267 | "toc_cell": false, 268 | "toc_position": {}, 269 | "toc_section_display": "block", 270 | "toc_window_display": false 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 2 275 | } 276 | -------------------------------------------------------------------------------- /handouts/09_libraries_command_line_arguments.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | OUTPUT_FILENAME = '09_libraries_command_line_arguments.txt' 5 | 6 | 7 | def write_input_into_file(): 8 | 9 | with open(OUTPUT_FILENAME, 'w') as f: 10 | 11 | while True: 12 | 13 | line = input('| ') 14 | if line == '': 15 | break 16 | 17 | f.write(line) 18 | f.write('\n') 19 | 20 | 21 | def read_file_into_stdout(): 22 | 23 | with open(OUTPUT_FILENAME) as f: 24 | 25 | for line in f: 26 | print(line, end='') 27 | 28 | 29 | # pt: print to 30 | def ptstderr(*args, **argd): 31 | print(*args, file=sys.stderr, **argd) 32 | 33 | 34 | def main(): 35 | 36 | if len(sys.argv) < 2: 37 | ptstderr("Need a command: write or read") 38 | sys.exit(1) 39 | 40 | command = sys.argv[1] 41 | try: 42 | 43 | if command == 'write': 44 | print('Enter blank line to exit.') 45 | write_input_into_file() 46 | 47 | elif command == 'read': 48 | read_file_into_stdout() 49 | 50 | else: 51 | ptstderr('No such command:', command) 52 | sys.exit(1) 53 | 54 | except IOError as e: 55 | ptstderr('Error:', e) 56 | sys.exit(1) 57 | 58 | 59 | if __name__ == '__main__': 60 | main() 61 | -------------------------------------------------------------------------------- /handouts/10_control_flow_yield.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – Yield" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def make_a_list():\n", 17 | " a_list = []\n", 18 | " for i in range(10):\n", 19 | " print('preparing', i)\n", 20 | " a_list.append(i)\n", 21 | " return a_list" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "def make_a_gen():\n", 31 | " for i in range(10):\n", 32 | " print('preparing', i)\n", 33 | " yield i" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "preparing 0\n", 46 | "preparing 1\n", 47 | "preparing 2\n", 48 | "preparing 3\n", 49 | "preparing 4\n", 50 | "preparing 5\n", 51 | "preparing 6\n", 52 | "preparing 7\n", 53 | "preparing 8\n", 54 | "preparing 9\n" 55 | ] 56 | }, 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "make_a_list()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "" 81 | ] 82 | }, 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "make_a_gen()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "preparing 0\n", 102 | "preparing 1\n", 103 | "preparing 2\n", 104 | "preparing 3\n", 105 | "preparing 4\n", 106 | "preparing 5\n", 107 | "preparing 6\n", 108 | "preparing 7\n", 109 | "preparing 8\n", 110 | "preparing 9\n", 111 | "0\n", 112 | "1\n", 113 | "2\n", 114 | "3\n", 115 | "4\n", 116 | "5\n", 117 | "6\n", 118 | "7\n", 119 | "8\n", 120 | "9\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "for i in make_a_list():\n", 126 | " print(i)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 6, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "preparing 0\n", 139 | "0\n", 140 | "preparing 1\n", 141 | "1\n", 142 | "preparing 2\n", 143 | "2\n", 144 | "preparing 3\n", 145 | "3\n", 146 | "preparing 4\n", 147 | "4\n", 148 | "preparing 5\n", 149 | "5\n", 150 | "preparing 6\n", 151 | "6\n", 152 | "preparing 7\n", 153 | "7\n", 154 | "preparing 8\n", 155 | "8\n", 156 | "preparing 9\n", 157 | "9\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "for i in make_a_gen():\n", 163 | " print(i)" 164 | ] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Python 3 (practicing-python-3)", 170 | "language": "python", 171 | "name": "python3-practicing-python-3" 172 | }, 173 | "language_info": { 174 | "codemirror_mode": { 175 | "name": "ipython", 176 | "version": 3 177 | }, 178 | "file_extension": ".py", 179 | "mimetype": "text/x-python", 180 | "name": "python", 181 | "nbconvert_exporter": "python", 182 | "pygments_lexer": "ipython3", 183 | "version": "3.6.3" 184 | }, 185 | "toc": { 186 | "nav_menu": {}, 187 | "number_sections": true, 188 | "sideBar": true, 189 | "skip_h1_title": true, 190 | "toc_cell": false, 191 | "toc_position": {}, 192 | "toc_section_display": "block", 193 | "toc_window_display": false 194 | } 195 | }, 196 | "nbformat": 4, 197 | "nbformat_minor": 2 198 | } 199 | -------------------------------------------------------------------------------- /handouts/11_control_flow_comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow – Comprehensions\n", 8 | "\n", 9 | "## List Comprehensions" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "[i for i in range(10)]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "[0, 2, 4, 6, 8]" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "[i for i in range(10) if i % 2 == 0]" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "[(i, j) for i in range(2) for j in range(3)]" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "Just like:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "(0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) " 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "for i in range(2):\n", 94 | " for j in range(3):\n", 95 | " print((i, j), end=' ')" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "## Set Comprehensions" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 5, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}" 114 | ] 115 | }, 116 | "execution_count": 5, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "{i for i in range(10)}" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "{0, 2, 4, 6, 8}" 134 | ] 135 | }, 136 | "execution_count": 6, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "{i for i in range(10) if i % 2 == 0}" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 7, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "{(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)}" 154 | ] 155 | }, 156 | "execution_count": 7, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "{(i, j) for i in range(2) for j in range(3)}" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "## Dict Comprehensions" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 8, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "{0: 'A', 1: 'B', 2: 'C'}" 181 | ] 182 | }, 183 | "execution_count": 8, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "{i: chr(65+i) for i in range(3)}" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 9, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "{0: 'A', 2: 'C'}" 201 | ] 202 | }, 203 | "execution_count": 9, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "{i: chr(65+i) for i in range(3) if i % 2 == 0}" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 10, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "{(0, 0): ('A', 'A'),\n", 221 | " (0, 1): ('A', 'B'),\n", 222 | " (0, 2): ('A', 'C'),\n", 223 | " (1, 0): ('B', 'A'),\n", 224 | " (1, 1): ('B', 'B'),\n", 225 | " (1, 2): ('B', 'C')}" 226 | ] 227 | }, 228 | "execution_count": 10, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "{(i, j): (chr(65+i), chr(65+j)) for i in range(2) for j in range(3)}" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "## Generator Expression" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 11, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "data": { 251 | "text/plain": [ 252 | " at 0x103a70d58>" 253 | ] 254 | }, 255 | "execution_count": 11, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "g = ((i, chr(65+i)) for i in range(3))\n", 262 | "g" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 12, 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "name": "stdout", 272 | "output_type": "stream", 273 | "text": [ 274 | "(0, 'A')\n", 275 | "(1, 'B')\n", 276 | "(2, 'C')\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "for i in g:\n", 282 | " print(i)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "## In Practice" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "### BMI" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 13, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "def calc_bmi(height_cm, weight_kg):\n", 306 | " # https://en.wikipedia.org/wiki/Body_mass_index\n", 307 | " return weight_kg / (height_cm/100)**2" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 14, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [ 316 | "kgs = [50, 55, 60]" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 15, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "data": { 326 | "text/plain": [ 327 | "[18.144868631151112, 19.959355494266223, 21.773842357381334]" 328 | ] 329 | }, 330 | "execution_count": 15, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "[calc_bmi(166, kg) for kg in kgs]" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 16, 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [ 345 | "cms = [150, 160, 170]" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 17, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "data": { 355 | "text/plain": [ 356 | "[22.22222222222222,\n", 357 | " 24.444444444444443,\n", 358 | " 26.666666666666668,\n", 359 | " 19.531249999999996,\n", 360 | " 21.484374999999996,\n", 361 | " 23.437499999999996,\n", 362 | " 17.301038062283737,\n", 363 | " 19.031141868512112,\n", 364 | " 20.761245674740486]" 365 | ] 366 | }, 367 | "execution_count": 17, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "[calc_bmi(cm, kg)\n", 374 | " for cm in cms\n", 375 | " for kg in kgs]" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 18, 381 | "metadata": {}, 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | "[(150, 55), (150, 60)]" 387 | ] 388 | }, 389 | "execution_count": 18, 390 | "metadata": {}, 391 | "output_type": "execute_result" 392 | } 393 | ], 394 | "source": [ 395 | "[(cm, kg)\n", 396 | " for cm in cms\n", 397 | " for kg in kgs\n", 398 | " if calc_bmi(cm, kg) >= 24] # overweight in Taiwan" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "### Inverse a Dict" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 19, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "data": { 415 | "text/plain": [ 416 | "{0: 'A', 1: 'B', 2: 'C'}" 417 | ] 418 | }, 419 | "execution_count": 19, 420 | "metadata": {}, 421 | "output_type": "execute_result" 422 | } 423 | ], 424 | "source": [ 425 | "idx_chr_map = {i: chr(65+i) for i in range(3)}\n", 426 | "idx_chr_map" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 20, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "{'A': 0, 'B': 1, 'C': 2}" 438 | ] 439 | }, 440 | "execution_count": 20, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "{v: k for k, v in idx_chr_map.items()}" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 21, 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "name": "stdout", 456 | "output_type": "stream", 457 | "text": [ 458 | "CPU times: user 7 µs, sys: 1 µs, total: 8 µs\n", 459 | "Wall time: 12.2 µs\n" 460 | ] 461 | }, 462 | { 463 | "data": { 464 | "text/plain": [ 465 | "27" 466 | ] 467 | }, 468 | "execution_count": 21, 469 | "metadata": {}, 470 | "output_type": "execute_result" 471 | } 472 | ], 473 | "source": [ 474 | "a_long_str = 'The comprehensions are cool!'\n", 475 | "%time a_long_str.index('!')" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 22, 481 | "metadata": {}, 482 | "outputs": [], 483 | "source": [ 484 | "c_i_map = {c: i for i, c in enumerate(a_long_str)}" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 23, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | "CPU times: user 5 µs, sys: 1 µs, total: 6 µs\n", 497 | "Wall time: 11 µs\n" 498 | ] 499 | }, 500 | { 501 | "data": { 502 | "text/plain": [ 503 | "27" 504 | ] 505 | }, 506 | "execution_count": 23, 507 | "metadata": {}, 508 | "output_type": "execute_result" 509 | } 510 | ], 511 | "source": [ 512 | "%time c_i_map['!']" 513 | ] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "metadata": {}, 518 | "source": [ 519 | "### Lazy Contrustion" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 24, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | " at 0x103a70eb8>" 531 | ] 532 | }, 533 | "execution_count": 24, 534 | "metadata": {}, 535 | "output_type": "execute_result" 536 | } 537 | ], 538 | "source": [ 539 | "g = ((i, chr(65+i)) for i in range(3))\n", 540 | "g" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 25, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "data": { 550 | "text/plain": [ 551 | "{0: 'A', 1: 'B', 2: 'C'}" 552 | ] 553 | }, 554 | "execution_count": 25, 555 | "metadata": {}, 556 | "output_type": "execute_result" 557 | } 558 | ], 559 | "source": [ 560 | "dict(g)" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "### With Aggregation Function" 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "execution_count": 26, 573 | "metadata": {}, 574 | "outputs": [], 575 | "source": [ 576 | "def any_negative(*args):\n", 577 | " return any(arg < 0 for arg in args)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 27, 583 | "metadata": {}, 584 | "outputs": [], 585 | "source": [ 586 | "def all_non_negative(*args):\n", 587 | " return all(not arg < 0 for arg in args)\n", 588 | " # === all(arg >= 0 for ...)" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": 28, 594 | "metadata": {}, 595 | "outputs": [], 596 | "source": [ 597 | "def calc_subtotal(prices, quantities):\n", 598 | " return sum(p*q for p, q in zip(prices, quantities))" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 29, 604 | "metadata": {}, 605 | "outputs": [], 606 | "source": [ 607 | "def input_a_list():\n", 608 | " # numbers_str: '1 2 3'\n", 609 | " numbers_str = input('Please input numbers separated with space: ')\n", 610 | " # number_strs: ['1', '2', '3']\n", 611 | " number_strs = numbers_str.split(' ')\n", 612 | " # number_str: '1'\n", 613 | " return [int(number_str) for number_str in number_strs]" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": 30, 619 | "metadata": {}, 620 | "outputs": [ 621 | { 622 | "data": { 623 | "text/plain": [ 624 | "False" 625 | ] 626 | }, 627 | "execution_count": 30, 628 | "metadata": {}, 629 | "output_type": "execute_result" 630 | } 631 | ], 632 | "source": [ 633 | "any_negative(0, 1)" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": 31, 639 | "metadata": {}, 640 | "outputs": [ 641 | { 642 | "data": { 643 | "text/plain": [ 644 | "True" 645 | ] 646 | }, 647 | "execution_count": 31, 648 | "metadata": {}, 649 | "output_type": "execute_result" 650 | } 651 | ], 652 | "source": [ 653 | "any_negative(0, -1)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 32, 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": [ 664 | "True" 665 | ] 666 | }, 667 | "execution_count": 32, 668 | "metadata": {}, 669 | "output_type": "execute_result" 670 | } 671 | ], 672 | "source": [ 673 | "all_non_negative(0, 1)" 674 | ] 675 | }, 676 | { 677 | "cell_type": "code", 678 | "execution_count": 33, 679 | "metadata": {}, 680 | "outputs": [ 681 | { 682 | "data": { 683 | "text/plain": [ 684 | "False" 685 | ] 686 | }, 687 | "execution_count": 33, 688 | "metadata": {}, 689 | "output_type": "execute_result" 690 | } 691 | ], 692 | "source": [ 693 | "all_non_negative(0, -1)" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": 34, 699 | "metadata": {}, 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/plain": [ 704 | "1400" 705 | ] 706 | }, 707 | "execution_count": 34, 708 | "metadata": {}, 709 | "output_type": "execute_result" 710 | } 711 | ], 712 | "source": [ 713 | "calc_subtotal([100, 200, 300], [1, 2, 3])" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": 35, 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [ 722 | "# input_a_list()" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": 36, 728 | "metadata": {}, 729 | "outputs": [ 730 | { 731 | "data": { 732 | "text/plain": [ 733 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]" 734 | ] 735 | }, 736 | "execution_count": 36, 737 | "metadata": {}, 738 | "output_type": "execute_result" 739 | } 740 | ], 741 | "source": [ 742 | "# find primes in a single line\n", 743 | "[\n", 744 | " n for n in range(2, 50)\n", 745 | " if not any(n % i == 0 for i in range(2, n))\n", 746 | "]" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 37, 752 | "metadata": {}, 753 | "outputs": [ 754 | { 755 | "name": "stdout", 756 | "output_type": "stream", 757 | "text": [ 758 | "Average height (cm): 146.50\n" 759 | ] 760 | } 761 | ], 762 | "source": [ 763 | "health_dicts = [\n", 764 | " {'height_cm': 152, 'weight_kg': 48, 'age': 63, 'male_yn': 1},\n", 765 | " {'height_cm': 157, 'weight_kg': 53, 'age': 41, 'male_yn': 1},\n", 766 | " {'height_cm': 140, 'weight_kg': 37, 'age': 63, 'male_yn': 0},\n", 767 | " {'height_cm': 137, 'weight_kg': 32, 'age': 65, 'male_yn': 0},\n", 768 | "]\n", 769 | "\n", 770 | "from statistics import mean\n", 771 | "avg_height_cm = mean(d['height_cm'] for d in health_dicts)\n", 772 | "print(f'Average height (cm): {avg_height_cm:.2f}')" 773 | ] 774 | } 775 | ], 776 | "metadata": { 777 | "kernelspec": { 778 | "display_name": "Python 3 (practicing-python-3)", 779 | "language": "python", 780 | "name": "python3-practicing-python-3" 781 | }, 782 | "language_info": { 783 | "codemirror_mode": { 784 | "name": "ipython", 785 | "version": 3 786 | }, 787 | "file_extension": ".py", 788 | "mimetype": "text/x-python", 789 | "name": "python", 790 | "nbconvert_exporter": "python", 791 | "pygments_lexer": "ipython3", 792 | "version": "3.6.3" 793 | }, 794 | "toc": { 795 | "nav_menu": { 796 | "height": "203px", 797 | "width": "286px" 798 | }, 799 | "number_sections": true, 800 | "sideBar": true, 801 | "skip_h1_title": true, 802 | "toc_cell": false, 803 | "toc_position": {}, 804 | "toc_section_display": "block", 805 | "toc_window_display": false 806 | } 807 | }, 808 | "nbformat": 4, 809 | "nbformat_minor": 2 810 | } 811 | -------------------------------------------------------------------------------- /handouts/12_libraries_functional_tricks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Libraries – Functional Tricks\n", 8 | "\n", 9 | "## Compare With Comprehensions" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "[1, 2, 3]" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "[int(n_str) for n_str in '1 2 3'.split(' ')]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "map(int, '1 2 3'.split(' '))" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "[1, 2, 3]" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "list(map(int, '1 2 3'.split(' ')))" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "[2]" 81 | ] 82 | }, 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "[n for n in [1, 2, 3] if n % 2 == 0]" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "def is_even(n):\n", 99 | " return n % 2 == 0" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "" 111 | ] 112 | }, 113 | "execution_count": 6, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "filter(is_even, [1, 2, 3])" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 7, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "data": { 129 | "text/plain": [ 130 | "[2]" 131 | ] 132 | }, 133 | "execution_count": 7, 134 | "metadata": {}, 135 | "output_type": "execute_result" 136 | } 137 | ], 138 | "source": [ 139 | "list(filter(is_even, [1, 2, 3]))" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "## Advanced Tricks" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 8, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "from math import gcd\n", 156 | "from operator import add\n", 157 | "from functools import reduce, partial" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "### Add → Sum" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 9, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "3" 176 | ] 177 | }, 178 | "execution_count": 9, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "add(1, 2)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 10, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "6" 196 | ] 197 | }, 198 | "execution_count": 10, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "reduce(add, [1, 2, 3])" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 11, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "my_sum = partial(reduce, add)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 12, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "6" 225 | ] 226 | }, 227 | "execution_count": 12, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "my_sum([1, 2, 3])" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "### Enhanced GCD" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 13, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "data": { 250 | "text/plain": [ 251 | "77" 252 | ] 253 | }, 254 | "execution_count": 13, 255 | "metadata": {}, 256 | "output_type": "execute_result" 257 | } 258 | ], 259 | "source": [ 260 | "gcd(154, 3003)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 14, 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "7" 272 | ] 273 | }, 274 | "execution_count": 14, 275 | "metadata": {}, 276 | "output_type": "execute_result" 277 | } 278 | ], 279 | "source": [ 280 | "# 154 = 2*7*11\n", 281 | "# 3003 = 3*7*11*13\n", 282 | "reduce(gcd, [7, 154, 3003])" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 15, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "my_gcd = partial(reduce, gcd)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 16, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "77" 303 | ] 304 | }, 305 | "execution_count": 16, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "my_gcd([154, 3003])" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 17, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "7" 323 | ] 324 | }, 325 | "execution_count": 17, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "my_gcd([7, 154, 3003])" 332 | ] 333 | } 334 | ], 335 | "metadata": { 336 | "kernelspec": { 337 | "display_name": "Python 3 (practicing-python-3)", 338 | "language": "python", 339 | "name": "python3-practicing-python-3" 340 | }, 341 | "language_info": { 342 | "codemirror_mode": { 343 | "name": "ipython", 344 | "version": 3 345 | }, 346 | "file_extension": ".py", 347 | "mimetype": "text/x-python", 348 | "name": "python", 349 | "nbconvert_exporter": "python", 350 | "pygments_lexer": "ipython3", 351 | "version": "3.6.3" 352 | }, 353 | "toc": { 354 | "nav_menu": {}, 355 | "number_sections": true, 356 | "sideBar": true, 357 | "skip_h1_title": true, 358 | "toc_cell": false, 359 | "toc_position": {}, 360 | "toc_section_display": "block", 361 | "toc_window_display": false 362 | } 363 | }, 364 | "nbformat": 4, 365 | "nbformat_minor": 2 366 | } 367 | -------------------------------------------------------------------------------- /handouts/13_1_libraries_import_antigravity_utils.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python Parameters and Functions – `sys`" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "scrolled": true 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "3.6.3 (default, Nov 22 2017, 13:29:32) \n", 22 | "[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]\n", 23 | "\n", 24 | "sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import sys\n", 30 | "\n", 31 | "print(sys.version)\n", 32 | "print()\n", 33 | "print(sys.version_info)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "## Operating System Interfaces – `os`" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "name: posix\n", 53 | "cwd: /Users/mosky/Dropbox/Projects/Practicing Python 3/examples\n", 54 | "uid: 501\n", 55 | "pid: 8445\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "# os is the operating system which Python runs on\n", 61 | "import os\n", 62 | "\n", 63 | "print('name:', os.name)\n", 64 | "print('cwd:', os.getcwd())\n", 65 | "print('uid:', os.getuid())\n", 66 | "print('pid:', os.getpid())\n", 67 | "# os.chdir(...)\n", 68 | "# os.link(...)\n", 69 | "# os.mkdir(...)\n", 70 | "# os.remove(...)\n", 71 | "# os.fork(...)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "## Pathname Manipulations – `os.path`" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 3, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "abs: /Users/mosky/Dropbox/Projects/Practicing Python 3/examples/tmp\n", 91 | "base: a_file\n", 92 | "dir: path/to\n", 93 | "exists: False\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "from os.path import *\n", 99 | "\n", 100 | "print('abs:', abspath('tmp/'))\n", 101 | "print('base:', basename('path/to/a_file'))\n", 102 | "print('dir:', dirname('path/to/a_file'))\n", 103 | "print('exists:', exists('./a'))" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "## Object-Oriented Paths – `pathlib`" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 4, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "abs: /Users/mosky/Dropbox/Projects/Practicing Python 3/examples/tmp\n", 123 | "base: a_file\n", 124 | "dir: path/to\n", 125 | "exists: False\n", 126 | "\n", 127 | "PosixPath('/etc/init.d/reboot')\n", 128 | "/etc/init.d/reboot\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "from pathlib import Path\n", 134 | "\n", 135 | "print('abs:', Path('tmp/').resolve())\n", 136 | "print('base:', Path('path/to/a_file').name)\n", 137 | "print('dir:', Path('path/to/a_file').parent)\n", 138 | "print('exists:', Path('./a').exists())\n", 139 | "print()\n", 140 | "\n", 141 | "p = Path('/etc')\n", 142 | "q = p / 'init.d' / 'reboot'\n", 143 | "print(repr(q))\n", 144 | "print(q)" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": {}, 150 | "source": [ 151 | "## Pathname Pattern Expansion – `glob`" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 5, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "['09_libraries_command_line_arguments.py']\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "from glob import glob\n", 169 | "\n", 170 | "print(glob('*.py'))" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "## Generate Random Numbers – `random`" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 2, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "randrange: 1\n", 190 | "randint: 3\n", 191 | "\n", 192 | "choice: 4\n", 193 | "sample: [1, 2, 9]\n", 194 | "\n", 195 | "before shuffle: [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 196 | "after shuffle: [2, 7, 9, 6, 8, 1, 3, 4, 5]\n", 197 | "\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "# use `import *` with care\n", 203 | "from random import *\n", 204 | "\n", 205 | "print('randrange:', randrange(1, 3))\n", 206 | "print('randint:', randint(1, 3)) # alias for randrange(a, b+1)\n", 207 | "print()\n", 208 | "\n", 209 | "my_range = range(1, 10)\n", 210 | "print('choice:', choice(my_range))\n", 211 | "print('sample:', sample(my_range, 3))\n", 212 | "print()\n", 213 | "\n", 214 | "my_list = list(my_range)\n", 215 | "print('before shuffle:', my_list)\n", 216 | "# random.shuffle(my_range) # -> TypeError: 'range' object does not support item assignment\n", 217 | "shuffle(my_list) # -> None\n", 218 | "print('after shuffle:', my_list)\n", 219 | "print()" 220 | ] 221 | } 222 | ], 223 | "metadata": { 224 | "kernelspec": { 225 | "display_name": "Python 3", 226 | "language": "python", 227 | "name": "python3" 228 | }, 229 | "language_info": { 230 | "codemirror_mode": { 231 | "name": "ipython", 232 | "version": 3 233 | }, 234 | "file_extension": ".py", 235 | "mimetype": "text/x-python", 236 | "name": "python", 237 | "nbconvert_exporter": "python", 238 | "pygments_lexer": "ipython3", 239 | "version": "3.6.3" 240 | }, 241 | "toc": { 242 | "nav_menu": {}, 243 | "number_sections": true, 244 | "sideBar": true, 245 | "skip_h1_title": true, 246 | "toc_cell": false, 247 | "toc_position": { 248 | "height": "833px", 249 | "left": "0px", 250 | "right": "1228px", 251 | "top": "67px", 252 | "width": "212px" 253 | }, 254 | "toc_section_display": "block", 255 | "toc_window_display": false 256 | } 257 | }, 258 | "nbformat": 4, 259 | "nbformat_minor": 1 260 | } 261 | -------------------------------------------------------------------------------- /handouts/13_2_libraries_import_antigravity_datatypes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Human's Numbers – `decimal`" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "triple 1: True\n", 20 | "triple 0.1: False\n", 21 | "\n", 22 | "triple decimal 0.1: True\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "print('triple 1:', 1 + 1 + 1 == 3)\n", 28 | "print('triple 0.1:', 0.1 + 0.1 + 0.1 == 0.3)\n", 29 | "print()\n", 30 | "\n", 31 | "from decimal import Decimal as Dec\n", 32 | "print('triple decimal 0.1:', Dec('0.1') + Dec('0.1') + Dec('0.1') == Dec('0.3'))\n", 33 | "\n", 34 | "# see also:\n", 35 | "# IEEE floating point (IEEE 754)\n", 36 | "# https://en.wikipedia.org/wiki/IEEE_floating_point" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "## Basic Date and Time Types – `datetime`" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "now 2017-11-26 22:43:48.868468\n", 56 | "1 hour ago: 2017-11-26 21:43:48.868512\n", 57 | "1/1 to now: 695 days, 22:43:48.868468\n", 58 | "\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "from datetime import datetime, timedelta\n", 64 | "\n", 65 | "now = datetime.now()\n", 66 | "one_hour_ago = datetime.now() - timedelta(hours=1)\n", 67 | "\n", 68 | "print('now', now)\n", 69 | "print('1 hour ago:', one_hour_ago)\n", 70 | "print('1/1 to now:', now-datetime(2016, 1, 1))\n", 71 | "print()\n", 72 | "\n", 73 | "# see also:\n", 74 | "# dateutil\n", 75 | "# https://dateutil.readthedocs.io/en/stable/" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "## More Container Datatypes – `collections`" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 3, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "from collections import deque, Counter, OrderedDict, defaultdict" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 4, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "435 µs ± 74.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", 104 | "132 µs ± 9.98 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "def use_list_as_queue():\n", 110 | " que = list(range(1000))\n", 111 | " while que:\n", 112 | " que.pop(0)\n", 113 | " \n", 114 | " \n", 115 | "def use_deque_as_queue():\n", 116 | " que = deque(range(1000))\n", 117 | " while que:\n", 118 | " que.popleft()\n", 119 | "\n", 120 | "# output:\n", 121 | "# 1000000 loops, best of 3: 4.37 µs per loop\n", 122 | "# 1000000 loops, best of 3: 2.01 µs per loop\n", 123 | "%timeit use_list_as_queue()\n", 124 | "%timeit use_deque_as_queue()\n", 125 | "\n", 126 | "# see also:\n", 127 | "# TimeComplexity\n", 128 | "# https://wiki.python.org/moin/TimeComplexity" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 5, 134 | "metadata": { 135 | "scrolled": true 136 | }, 137 | "outputs": [ 138 | { 139 | "name": "stdout", 140 | "output_type": "stream", 141 | "text": [ 142 | "Counter({' ': 9, 's': 6, 'o': 4, 't': 4, 'a': 4, 'c': 4, 'u': 3, 'n': 3, 'e': 3, 'i': 3, 'b': 3, 'r': 2, 'l': 2, 'h': 2, 'A': 1, 'C': 1, 'd': 1, 'f': 1, 'g': 1, 'j': 1, '.': 1})\n", 143 | "[(' ', 9), ('s', 6), ('o', 4)]\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "c = Counter('A Counter is a dict subclass for counting hashable objects.')\n", 149 | "print(c)\n", 150 | "print(c.most_common(3))" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 6, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "['first', 'second', 'third']\n", 163 | "['first', 'second', 'third']\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "d = {}\n", 169 | "d['first'] = 1\n", 170 | "d['second'] = 2\n", 171 | "d['third'] = 3\n", 172 | "print([k for k in d])\n", 173 | "\n", 174 | "# in Python 3.6, the dict is always ordered\n", 175 | "od = OrderedDict()\n", 176 | "od['first'] = 1\n", 177 | "od['second'] = 2\n", 178 | "od['third'] = 3\n", 179 | "print([k for k in od])" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 7, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "defaultdict(, {'R': ['Returns'], 'a': ['a', 'a'], 'n': ['new'], 'd': ['dictionary-like', 'defaultdict', 'dict'], 'o': ['object.', 'of'], 'i': ['is'], 's': ['subclass'], 't': ['the'], 'b': ['built-in'], 'c': ['class.']})\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "# fchar: first char\n", 197 | "fchar_word_map = defaultdict(list)\n", 198 | "sentence = 'Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class.'\n", 199 | "for word in sentence.split(' '):\n", 200 | " fchar_word_map[word[0]].append(word)\n", 201 | "print(fchar_word_map)" 202 | ] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "codemirror_mode": { 213 | "name": "ipython", 214 | "version": 3 215 | }, 216 | "file_extension": ".py", 217 | "mimetype": "text/x-python", 218 | "name": "python", 219 | "nbconvert_exporter": "python", 220 | "pygments_lexer": "ipython3", 221 | "version": "3.7.4" 222 | }, 223 | "toc": { 224 | "base_numbering": 1, 225 | "nav_menu": {}, 226 | "number_sections": true, 227 | "sideBar": true, 228 | "skip_h1_title": true, 229 | "title_cell": "Table of Contents", 230 | "title_sidebar": "Contents", 231 | "toc_cell": false, 232 | "toc_position": {}, 233 | "toc_section_display": "block", 234 | "toc_window_display": false 235 | } 236 | }, 237 | "nbformat": 4, 238 | "nbformat_minor": 1 239 | } 240 | -------------------------------------------------------------------------------- /handouts/13_3_libraries_import_antigravity_re_n_persistence.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Regex, Regexp, Regular Expression – `re`\n", 8 | "\n", 9 | "### The Common Functions / Methods\n", 10 | "\n", 11 | "- [fullmatch](https://docs.python.org/3.5/library/re.html#re.fullmatch)\n", 12 | "- [match](https://docs.python.org/3.5/library/re.html#re.regex.match)\n", 13 | "- [search](https://docs.python.org/3.5/library/re.html#re.regex.search)\n", 14 | "- [findall](https://docs.python.org/3.5/library/re.html#re.regex.findall)\n", 15 | "- [sub](https://docs.python.org/3.5/library/re.html#re.regex.sub)\n", 16 | "- [split](https://docs.python.org/3.5/library/re.html#re.regex.split)\n", 17 | "- [compile](https://docs.python.org/3.5/library/re.html#re.compile)\n", 18 | "\n", 19 | "### The Useful Tool\n", 20 | "\n", 21 | "- https://regex101.com/#python" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import re" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "" 42 | ] 43 | }, 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "# the r prefix means a raw string, not regex\n", 51 | "match = re.match(r'[0-9]{1,3}(?:\\.[0-9]{1,3}){3}', '127.0.0.1')\n", 52 | "match # -> match object if match" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "" 64 | ] 65 | }, 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "match = re.match(r'[0-9]{1,3}(?:\\.[0-9]{1,3}){3}', '127.0.0.1aaaa')\n", 73 | "match" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 4, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "match = re.fullmatch(r'[0-9]{1,3}(?:\\.[0-9]{1,3}){3}', '127.0.0.1aaaa')\n", 83 | "match # -> None if no match" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 5, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "'127.0.0.1'" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "# more methods of the match object:\n", 104 | "# https://docs.python.org/3.5/library/re.html#match-objects\n", 105 | "match = re.fullmatch(r'[0-9]{1,3}(?:\\.[0-9]{1,3}){3}', '127.0.0.1')\n", 106 | "match.group(0)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 6, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "data": { 116 | "text/plain": [ 117 | "'192.168.0.1'" 118 | ] 119 | }, 120 | "execution_count": 6, 121 | "metadata": {}, 122 | "output_type": "execute_result" 123 | } 124 | ], 125 | "source": [ 126 | "# or compile the re str into a regex object first:\n", 127 | "# https://docs.python.org/3.5/library/re.html#regular-expression-objects\n", 128 | "ip_re = re.compile(r'[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}')\n", 129 | "match = ip_re.fullmatch('192.168.0.1')\n", 130 | "match.group(0)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 7, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "raw_text = 'My IP is 127.0.0.1.'" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 8, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "match = ip_re.fullmatch(raw_text)\n", 149 | "match # -> None" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 9, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "'127.0.0.1'" 161 | ] 162 | }, 163 | "execution_count": 9, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "match = ip_re.search(raw_text)\n", 170 | "match.group(0)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 10, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "['127.0.0.1', '127.0.0.2']" 182 | ] 183 | }, 184 | "execution_count": 10, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "ip_re.findall('127.0.0.1, 127.0.0.2')" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 11, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "\n", 203 | "\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "for m in ip_re.finditer('127.0.0.1, 127.0.0.2'):\n", 209 | " print(m)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "## JavaScript Object Notation – `json`\n", 217 | "\n", 218 | "- [load](https://docs.python.org/3.5/library/json.html#json.load)/[loads](https://docs.python.org/3.5/library/json.html#json.loads): JSON → Python object\n", 219 | "- [dump](https://docs.python.org/3.5/library/json.html#json.dump)/[dumps](https://docs.python.org/3.5/library/json.html#json.dumps): Python object → JSON" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 12, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "{'locale': {'currency': 'TWD', 'code': 'zh_TW', 'geo_name': '台灣'}, 'result': [{'total': 147, 'user_voted': '', 'vote_option_sum': {'1': 44, '3': 7, '2': 30, '4': 66}}]}\n", 232 | "\n", 233 | "147\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "import json\n", 239 | "from pprint import pprint\n", 240 | "\n", 241 | "# https://www.pinkoi.com/offers/92\n", 242 | "json_text = '''{\n", 243 | " \"locale\": {\n", 244 | " \"currency\": \"TWD\",\n", 245 | " \"code\": \"zh_TW\",\n", 246 | " \"geo_name\": \"\\u53f0\\u7063\"\n", 247 | " },\n", 248 | " \"result\": [\n", 249 | " {\n", 250 | " \"total\": 147,\n", 251 | " \"user_voted\": \"\",\n", 252 | " \"vote_option_sum\": {\n", 253 | " \"1\": 44,\n", 254 | " \"3\": 7,\n", 255 | " \"2\": 30,\n", 256 | " \"4\": 66\n", 257 | " }\n", 258 | " }\n", 259 | " ]\n", 260 | "}'''\n", 261 | "\n", 262 | "d = json.loads(json_text)\n", 263 | "print(d)\n", 264 | "print()\n", 265 | "print(d['result'][0]['total'])" 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "## CSV File Reading and Writing – `csv`" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 13, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "moskytw,mosky.tw@gmail.com\n", 285 | "moskyliu,mosky.liu@pinkoi.com\n", 286 | "moskybot,mosky.bot@gmail.com\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "# make a mock file first\n", 292 | "\n", 293 | "from io import StringIO\n", 294 | " \n", 295 | "csv_f = StringIO('''\\\n", 296 | "moskytw,mosky.tw@gmail.com\n", 297 | "moskyliu,mosky.liu@pinkoi.com\n", 298 | "moskybot,mosky.bot@gmail.com\\\n", 299 | "''')\n", 300 | "# ≈ csv_f = open(path)\n", 301 | "\n", 302 | "for line in csv_f:\n", 303 | " print(line, end='')\n", 304 | "print()\n", 305 | " \n", 306 | "_ = csv_f.seek(0)" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 14, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "[['moskytw', 'mosky.tw@gmail.com'], ['moskyliu', 'mosky.liu@pinkoi.com'], ['moskybot', 'mosky.bot@gmail.com']]\n" 319 | ] 320 | } 321 | ], 322 | "source": [ 323 | "# read from the file\n", 324 | "import csv\n", 325 | "reader = csv.reader(csv_f)\n", 326 | "rows = [row for row in reader]\n", 327 | "print(rows)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 15, 333 | "metadata": {}, 334 | "outputs": [ 335 | { 336 | "name": "stdout", 337 | "output_type": "stream", 338 | "text": [ 339 | "moskytw,mosky.tw@gmail.com\r\n", 340 | "moskyliu,mosky.liu@pinkoi.com\r\n", 341 | "moskybot,mosky.bot@gmail.com\r\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "# write to a file\n", 347 | "import sys\n", 348 | "writer = csv.writer(sys.stdout)\n", 349 | "for row in rows:\n", 350 | " writer.writerow(row)\n", 351 | " \n", 352 | "# see also:\n", 353 | "# csvkit is a suite of utilities for converting to and working with CSV\n", 354 | "# https://csvkit.readthedocs.org/en/0.9.1/" 355 | ] 356 | }, 357 | { 358 | "cell_type": "markdown", 359 | "metadata": {}, 360 | "source": [ 361 | "## Python Object Serialization – `pickle`" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 16, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "name": "stdout", 371 | "output_type": "stream", 372 | "text": [ 373 | "[['moskytw', 'mosky.tw@gmail.com'], ['moskyliu', 'mosky.liu@pinkoi.com'], ['moskybot', 'mosky.bot@gmail.com']]\n", 374 | "\n", 375 | "b'\\x80\\x04\\x95s\\x00\\x00\\x00\\x00\\x00\\x00\\x00]\\x94(]\\x94(\\x8c\\x07moskytw\\x94\\x8c\\x12mosky.tw@gmail.com\\x94e]\\x94(\\x8c\\x08moskyliu\\x94\\x8c\\x14mosky.liu@pinkoi.com\\x94e]\\x94(\\x8c\\x08moskybot\\x94\\x8c\\x13mosky.bot@gmail.com\\x94ee.'\n", 376 | "\n", 377 | "[['moskytw', 'mosky.tw@gmail.com'], ['moskyliu', 'mosky.liu@pinkoi.com'], ['moskybot', 'mosky.bot@gmail.com']]\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "import pickle\n", 383 | "\n", 384 | "print(rows)\n", 385 | "print()\n", 386 | "\n", 387 | "# negative to use the pickle.HIGHEST_PROTOCOL\n", 388 | "dumped_bin = pickle.dumps(rows, protocol=-1)\n", 389 | "print(dumped_bin)\n", 390 | "print()\n", 391 | "\n", 392 | "print(pickle.loads(dumped_bin))" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "## Dig More\n", 400 | "\n", 401 | "- https://docs.python.org/3/library/re.html\n", 402 | "- https://docs.python.org/3/library/json.html\n", 403 | "- https://docs.python.org/3/library/csv.html\n", 404 | "- http://csvkit.readthedocs.io/en/stable/\n", 405 | "- https://docs.python.org/3/library/pickle.html" 406 | ] 407 | } 408 | ], 409 | "metadata": { 410 | "kernelspec": { 411 | "display_name": "Python 3", 412 | "language": "python", 413 | "name": "python3" 414 | }, 415 | "language_info": { 416 | "codemirror_mode": { 417 | "name": "ipython", 418 | "version": 3 419 | }, 420 | "file_extension": ".py", 421 | "mimetype": "text/x-python", 422 | "name": "python", 423 | "nbconvert_exporter": "python", 424 | "pygments_lexer": "ipython3", 425 | "version": "3.7.4" 426 | }, 427 | "toc": { 428 | "base_numbering": 1, 429 | "nav_menu": {}, 430 | "number_sections": true, 431 | "sideBar": true, 432 | "skip_h1_title": true, 433 | "title_cell": "Table of Contents", 434 | "title_sidebar": "Contents", 435 | "toc_cell": false, 436 | "toc_position": {}, 437 | "toc_section_display": "block", 438 | "toc_window_display": false 439 | } 440 | }, 441 | "nbformat": 4, 442 | "nbformat_minor": 1 443 | } 444 | -------------------------------------------------------------------------------- /handouts/13_4_libraries_import_antigravity_concurrency.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from time import sleep" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def return_slowly(i):\n", 19 | " print(f'Task {i} is starting.')\n", 20 | " sleep(1)\n", 21 | " print(f'Task {i} is done.')\n", 22 | " return i" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "## Threads – `threading`" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "from threading import Thread" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "def test_thread():\n", 48 | " Thread(target=return_slowly, args=(1, )).start()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 5, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "From 1 s → ≈ 0 s:\n", 61 | "\n", 62 | "Task 1 is starting.\n", 63 | "Task 1 is done.\n", 64 | "Task 1 is starting.1\n", 65 | "\n", 66 | "CPU times: user 2.61 ms, sys: 2.31 ms, total: 4.92 ms\n", 67 | "Wall time: 1 s\n", 68 | "\n", 69 | "None\n", 70 | "CPU times: user 463 µs, sys: 214 µs, total: 677 µs\n", 71 | "Wall time: 635 µs\n", 72 | "\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "print('From 1 s → ≈ 0 s:')\n", 78 | "print()\n", 79 | "\n", 80 | "%time print(return_slowly(1))\n", 81 | "print()\n", 82 | "\n", 83 | "%time print(test_thread())\n", 84 | "print()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "## Process Pools – `multiprocessing`" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "from multiprocessing import Pool" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "Task 2 is starting.\n", 113 | "Task 1 is starting.\n", 114 | "Task 0 is starting.\n", 115 | "Task 3 is starting.\n", 116 | "Task 1 is done.\n", 117 | "Task 0 is done.\n", 118 | "Task 2 is done.\n", 119 | "Task 3 is done.\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "pool = Pool(4)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 8, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "From 4 s → 1 s:\n", 137 | "\n", 138 | "Task 1 is done.\n", 139 | "[0, 1, 2, 3]\n", 140 | "CPU times: user 8.61 ms, sys: 7.16 ms, total: 15.8 ms\n", 141 | "Wall time: 1.02 s\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print('From 4 s → 1 s:')\n", 147 | "print()\n", 148 | "\n", 149 | "%time print(pool.map(return_slowly, range(4)))" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "## Launching Parallel Tasks Simply – `concurrent.futures`" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "from concurrent.futures import ThreadPoolExecutor" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "From 4 s → 1 s:\n", 178 | "\n", 179 | "Task 0 is starting.\n", 180 | "Task 1 is starting.\n", 181 | "Task 2 is starting.Task 3 is starting.\n", 182 | "\n", 183 | "Task 0 is done.\n", 184 | "Task 1 is done.\n", 185 | "Task 2 is done.Task 3 is done.\n", 186 | "\n", 187 | "[0, 1, 2, 3]\n", 188 | "CPU times: user 8.93 ms, sys: 5.17 ms, total: 14.1 ms\n", 189 | "Wall time: 1.02 s\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "print('From 4 s → 1 s:')\n", 195 | "print()\n", 196 | "\n", 197 | "with ThreadPoolExecutor() as executor:\n", 198 | " %time print(list(executor.map(return_slowly, range(4))))" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "## Summary\n", 206 | "\n", 207 | "### Multithreading\n", 208 | "\n", 209 | "1. Basic: `threading`, and `queue`\n", 210 | "2. Advanced: `concurrent.futures`\n", 211 | "3. Note the GIL\n", 212 | "\n", 213 | "### Multiprocessing\n", 214 | "\n", 215 | "1. Basic: `subprocess`\n", 216 | "2. Advanced: `concurrent.futures`\n", 217 | "3. Fancy: `multiprocessing`\n", 218 | "\n", 219 | "Recommend to use `concurrent.futures` first." 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "## Dig More\n", 227 | "\n", 228 | "* https://docs.python.org/3/library/threading.html\n", 229 | "* https://docs.python.org/3/library/queue.html\n", 230 | "* https://docs.python.org/3/library/subprocess.html\n", 231 | "* https://docs.python.org/3/library/multiprocessing.html\n", 232 | "* https://docs.python.org/3/library/concurrent.futures.html" 233 | ] 234 | } 235 | ], 236 | "metadata": { 237 | "kernelspec": { 238 | "display_name": "Python 3", 239 | "language": "python", 240 | "name": "python3" 241 | }, 242 | "language_info": { 243 | "codemirror_mode": { 244 | "name": "ipython", 245 | "version": 3 246 | }, 247 | "file_extension": ".py", 248 | "mimetype": "text/x-python", 249 | "name": "python", 250 | "nbconvert_exporter": "python", 251 | "pygments_lexer": "ipython3", 252 | "version": "3.6.3" 253 | }, 254 | "toc": { 255 | "nav_menu": { 256 | "height": "147px", 257 | "width": "496px" 258 | }, 259 | "number_sections": true, 260 | "sideBar": true, 261 | "skip_h1_title": true, 262 | "toc_cell": false, 263 | "toc_position": {}, 264 | "toc_section_display": "block", 265 | "toc_window_display": false 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 1 270 | } 271 | -------------------------------------------------------------------------------- /handouts/13_5_libraries_import_antigravity_crawling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Crawling News Titles" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import requests\n", 17 | "from bs4 import BeautifulSoup" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "resp = requests.get(\n", 38 | " 'https://tw.news.yahoo.com/world',\n", 39 | " headers={'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}\n", 40 | ")\n", 41 | "resp" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "assert '最新國際新聞' in resp.text" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "bsoup = BeautifulSoup(resp.text, 'html.parser')" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "陸豬商為求好價格 竟對活豬狂灌水" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "a_tags = bsoup.select('h3 a')\n", 80 | "a_tags[0]" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "陸豬商為求好價格 竟對活豬狂灌水\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "print(a_tags[0].text)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 7, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "陸豬商為求好價格 竟對活豬狂灌水\n", 110 | "網友大推「YP燈飾」全台最大5萬款燈飾▶線上選 門市看 有保固\n", 111 | "6隻毛孩遭射殺爆頭!惡男:為了小孩\n", 112 | "消防員吃飯不拿筷?原因超辛酸逼哭網\n", 113 | "郊區驚見荒廢屋!20年後爽賺3千萬\n", 114 | "『蜂王乳+ 芝麻明E 』兩大成分助你睡好氣色紅潤\n", 115 | "想擁美尻!女注填充物險死…臀肉腐爛留疤\n", 116 | "女同居失戀男砸4萬!竟見他復合前任\n", 117 | "上學途中遇地雷爆炸 9名阿富汗兒童喪生\n", 118 | "月繳2萬元+彈性繳【第一桶金 從投開始】\n", 119 | "上學途中遇地雷爆炸 9名阿富汗兒童喪生\n", 120 | "網路價格透明、薄利時代來臨 雙11生意難做\n", 121 | "香港中環兩集會被迫腰斬 只能舉行半小時\n", 122 | "威尼斯面具嘉年華ಇ 2020浪漫啟程!獨家入住《色遇》酒店\n", 123 | "登新聞周刊封面 楊安澤大談未來世代危機\n", 124 | "三房改五房出租超吵 遭舉報下場悽慘\n", 125 | "7年愛犬被婆婆偷丟!孕媳怒墮胎離婚\n", 126 | "中華電信【瘋搶雙11購物節】來啦,全業務享優惠,申請再抽Hami Point 11,111點!\n", 127 | "快訊/世界盃橄欖球 南非32:12擊敗英格蘭奪冠\n", 128 | "港周末示威 新華社遭縱火 大門玻璃碎裂\n", 129 | "執行「藍鯨」最後任務!少女遭輾成肉醬 家屬崩潰\n", 130 | "開車遇拋錨 速看解套方案▶抽東京機票補助金\n", 131 | "太空人丟冠軍家具行賠慘了 3.6億投注金變空氣\n", 132 | "空氣品質不斷惡化 德里成大型毒氣室\n", 133 | "澀谷新地標開幕了 吃得到台灣貓熊芝麻包\n", 134 | "1111狂歡慶!全館88折再送下單滿額禮!\n", 135 | "愛喝酒!男子腹部爆痛休克 醫抽血…竟是牛奶色\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "for t in a_tags:\n", 141 | " print(t.text)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "## Dig More\n", 149 | "\n", 150 | "* http://docs.python-requests.org/en/master/\n", 151 | "* https://www.crummy.com/software/BeautifulSoup/bs4/doc/" 152 | ] 153 | } 154 | ], 155 | "metadata": { 156 | "kernelspec": { 157 | "display_name": "Python 3 (practicing-python-3)", 158 | "language": "python", 159 | "name": "python3-practicing-python-3" 160 | }, 161 | "language_info": { 162 | "codemirror_mode": { 163 | "name": "ipython", 164 | "version": 3 165 | }, 166 | "file_extension": ".py", 167 | "mimetype": "text/x-python", 168 | "name": "python", 169 | "nbconvert_exporter": "python", 170 | "pygments_lexer": "ipython3", 171 | "version": "3.7.3" 172 | }, 173 | "toc": { 174 | "base_numbering": 1, 175 | "nav_menu": {}, 176 | "number_sections": true, 177 | "sideBar": true, 178 | "skip_h1_title": true, 179 | "title_cell": "Table of Contents", 180 | "title_sidebar": "Contents", 181 | "toc_cell": false, 182 | "toc_position": {}, 183 | "toc_section_display": "block", 184 | "toc_window_display": false 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 1 189 | } 190 | -------------------------------------------------------------------------------- /handouts/13_6_libraries_import_antigravity_numpy_n_pandas.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## The Data" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# health_list: [height in cm, weight in kg, age, male_yn in int]\n", 17 | "health_lists = [\n", 18 | " [152, 48, 63, 1],\n", 19 | " [157, 53, 41, 1],\n", 20 | " [140, 37, 63, 0],\n", 21 | " [137, 32, 65, 0],\n", 22 | "]\n", 23 | "\n", 24 | "health_dicts = [\n", 25 | " {'height_cm': 152, 'weight_kg': 48, 'age': 63, 'male_yn': 1},\n", 26 | " {'height_cm': 157, 'weight_kg': 53, 'age': 41, 'male_yn': 1},\n", 27 | " {'height_cm': 140, 'weight_kg': 37, 'age': 63, 'male_yn': 0},\n", 28 | " {'height_cm': 137, 'weight_kg': 32, 'age': 65, 'male_yn': 0},\n", 29 | "]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## NumPy" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "import numpy as np" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "### Array Broadcasting\n", 53 | "\n", 54 | "$ BMI = \\dfrac{weight}{height^{2}} $\n", 55 | "\n", 56 | "Where:\n", 57 | "\n", 58 | "- $ weight $: weight in kg\n", 59 | "- $ height $: height in m" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "array([[152, 48, 63, 1],\n", 71 | " [157, 53, 41, 1],\n", 72 | " [140, 37, 63, 0],\n", 73 | " [137, 32, 65, 0]])" 74 | ] 75 | }, 76 | "execution_count": 3, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "# m: hints for 2d array\n", 83 | "health_m = np.array(health_lists)\n", 84 | "health_m" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "array([152, 48, 63, 1])" 96 | ] 97 | }, 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "# get the 0th row — a person's data\n", 105 | "health_m[0]" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "array([152, 157, 140, 137])" 117 | ] 118 | }, 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "# get the 0th col — all heights\n", 126 | "health_m[:, 0]" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 6, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "array([ 1.52, 1.57, 1.4 , 1.37])" 138 | ] 139 | }, 140 | "execution_count": 6, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "# not matrix multiplication, just array broadcasting\n", 147 | "# height in meter vector\n", 148 | "# v: hints for 1d array\n", 149 | "height_m_v = health_m[:, 0] * 0.01\n", 150 | "height_m_v" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 7, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "array([48, 53, 37, 32])" 162 | ] 163 | }, 164 | "execution_count": 7, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "weight_kg_v = health_m[:, 1]\n", 171 | "weight_kg_v" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 8, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "array([ 20.77562327, 21.50188649, 18.87755102, 17.04938995])" 183 | ] 184 | }, 185 | "execution_count": 8, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "bmi_v = weight_kg_v / height_m_v**2\n", 192 | "bmi_v" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 9, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "19.551112681722302" 204 | ] 205 | }, 206 | "execution_count": 9, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "bmi_v.mean()" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "### Matrix Multiplication\n", 220 | "\n", 221 | "$ P = 10m + 6.25h - 5a + s $\n", 222 | "\n", 223 | "Where:\n", 224 | "\n", 225 | "- $ P $: BMR, kcal / day\n", 226 | "- $ m $: weight in kg\n", 227 | "- $ h $: height in cm\n", 228 | "- $ a $: age in year\n", 229 | "- $ s $: +5 for males, -161 for females\n", 230 | "\n", 231 | "$ \\equiv $\n", 232 | "\n", 233 | "$ P = 10m + 6.25h - 5a + 5g - 161(1-g) $\n", 234 | "\n", 235 | "Where:\n", 236 | "\n", 237 | "- $ g $: gender in int, 0 is female, 1 is male.\n", 238 | "\n", 239 | "$ \\equiv $\n", 240 | "\n", 241 | "$\n", 242 | " \\begin{bmatrix}\n", 243 | " P\n", 244 | " \\end{bmatrix}\n", 245 | " = \n", 246 | " \\begin{bmatrix}\n", 247 | " m & h & a & g & 1-g\n", 248 | " \\end{bmatrix}\n", 249 | " \\begin{bmatrix}\n", 250 | " 10 \\\\ 6.25 \\\\ -5 \\\\ 5 \\\\ -161\n", 251 | " \\end{bmatrix}\n", 252 | "$" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 10, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "health_m = np.array(health_lists)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 11, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "array([[1],\n", 273 | " [1],\n", 274 | " [0],\n", 275 | " [0]])" 276 | ] 277 | }, 278 | "execution_count": 11, 279 | "metadata": {}, 280 | "output_type": "execute_result" 281 | } 282 | ], 283 | "source": [ 284 | "# g\n", 285 | "health_m[:, -1:]" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 12, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "array([[0],\n", 297 | " [0],\n", 298 | " [1],\n", 299 | " [1]])" 300 | ] 301 | }, 302 | "execution_count": 12, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "# 1-g\n", 309 | "1-health_m[:, -1:]" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 13, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "array([[152, 48, 63, 1, 0],\n", 321 | " [157, 53, 41, 1, 0],\n", 322 | " [140, 37, 63, 0, 1],\n", 323 | " [137, 32, 65, 0, 1]])" 324 | ] 325 | }, 326 | "execution_count": 13, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "# X: X matrix, n × 5\n", 333 | "X = np.hstack((\n", 334 | " health_m,\n", 335 | " 1-health_m[:, -1:]\n", 336 | "))\n", 337 | "X" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 14, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "data": { 347 | "text/plain": [ 348 | "array([[ 6.25],\n", 349 | " [ 10. ],\n", 350 | " [ -5. ],\n", 351 | " [ 5. ],\n", 352 | " [-161. ]])" 353 | ] 354 | }, 355 | "execution_count": 14, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "# beta: beta matrix, 5 × 1\n", 362 | "beta = np.array([6.25, 10, -5, 5, -161])[:, None]\n", 363 | "# # or\n", 364 | "# beta = [6.25, 10, -5, 5, -161]\n", 365 | "# # will also work well\n", 366 | "beta" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 15, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "array([[ 1120. ],\n", 378 | " [ 1311.25],\n", 379 | " [ 769. ],\n", 380 | " [ 690.25]])" 381 | ] 382 | }, 383 | "execution_count": 15, 384 | "metadata": {}, 385 | "output_type": "execute_result" 386 | } 387 | ], 388 | "source": [ 389 | "X@beta" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 16, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "972.625" 401 | ] 402 | }, 403 | "execution_count": 16, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "(X@beta).mean()" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": {}, 415 | "source": [ 416 | "## Pandas" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 17, 422 | "metadata": {}, 423 | "outputs": [], 424 | "source": [ 425 | "import pandas as pd" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 18, 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "data": { 435 | "text/html": [ 436 | "
\n", 437 | "\n", 450 | "\n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | "
height_cmweight_kgagemale_yn
015248631
115753411
214037630
313732650
\n", 491 | "
" 492 | ], 493 | "text/plain": [ 494 | " height_cm weight_kg age male_yn\n", 495 | "0 152 48 63 1\n", 496 | "1 157 53 41 1\n", 497 | "2 140 37 63 0\n", 498 | "3 137 32 65 0" 499 | ] 500 | }, 501 | "execution_count": 18, 502 | "metadata": {}, 503 | "output_type": "execute_result" 504 | } 505 | ], 506 | "source": [ 507 | "health_df = pd.DataFrame(data=health_lists, columns=['height_cm', 'weight_kg', 'age', 'male_yn'])\n", 508 | "# or\n", 509 | "# health_df = pd.DataFrame(data=health_dicts)\n", 510 | "health_df" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 19, 516 | "metadata": {}, 517 | "outputs": [ 518 | { 519 | "data": { 520 | "text/plain": [ 521 | "0 152\n", 522 | "1 157\n", 523 | "2 140\n", 524 | "3 137\n", 525 | "Name: height_cm, dtype: int64" 526 | ] 527 | }, 528 | "execution_count": 19, 529 | "metadata": {}, 530 | "output_type": "execute_result" 531 | } 532 | ], 533 | "source": [ 534 | "health_df.height_cm" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 20, 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "data": { 544 | "text/plain": [ 545 | "pandas.core.series.Series" 546 | ] 547 | }, 548 | "execution_count": 20, 549 | "metadata": {}, 550 | "output_type": "execute_result" 551 | } 552 | ], 553 | "source": [ 554 | "type(health_df.height_cm)" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 21, 560 | "metadata": {}, 561 | "outputs": [ 562 | { 563 | "data": { 564 | "text/plain": [ 565 | "0 20.775623\n", 566 | "1 21.501886\n", 567 | "2 18.877551\n", 568 | "3 17.049390\n", 569 | "dtype: float64" 570 | ] 571 | }, 572 | "execution_count": 21, 573 | "metadata": {}, 574 | "output_type": "execute_result" 575 | } 576 | ], 577 | "source": [ 578 | "bmi_s = health_df.weight_kg/(health_df.height_cm*0.01)**2\n", 579 | "bmi_s" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 22, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "data": { 589 | "text/plain": [ 590 | "pandas.core.series.Series" 591 | ] 592 | }, 593 | "execution_count": 22, 594 | "metadata": {}, 595 | "output_type": "execute_result" 596 | } 597 | ], 598 | "source": [ 599 | "type(bmi_s)" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 23, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "19.551112681722302" 611 | ] 612 | }, 613 | "execution_count": 23, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [ 619 | "bmi_s.mean()" 620 | ] 621 | }, 622 | { 623 | "cell_type": "markdown", 624 | "metadata": {}, 625 | "source": [ 626 | "### Add a Series Into DataFrames" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 24, 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "data": { 636 | "text/html": [ 637 | "
\n", 638 | "\n", 651 | "\n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | " \n", 689 | " \n", 690 | " \n", 691 | " \n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | " \n", 696 | "
height_cmweight_kgagemale_ynbmi
01524863120.775623
11575341121.501886
21403763018.877551
31373265017.049390
\n", 697 | "
" 698 | ], 699 | "text/plain": [ 700 | " height_cm weight_kg age male_yn bmi\n", 701 | "0 152 48 63 1 20.775623\n", 702 | "1 157 53 41 1 21.501886\n", 703 | "2 140 37 63 0 18.877551\n", 704 | "3 137 32 65 0 17.049390" 705 | ] 706 | }, 707 | "execution_count": 24, 708 | "metadata": {}, 709 | "output_type": "execute_result" 710 | } 711 | ], 712 | "source": [ 713 | "# just avoid to affect the other cells\n", 714 | "tmp_df = health_df.copy()\n", 715 | "tmp_df['bmi'] = bmi_s\n", 716 | "tmp_df" 717 | ] 718 | }, 719 | { 720 | "cell_type": "markdown", 721 | "metadata": {}, 722 | "source": [ 723 | "### Load CSV" 724 | ] 725 | }, 726 | { 727 | "cell_type": "code", 728 | "execution_count": 25, 729 | "metadata": {}, 730 | "outputs": [], 731 | "source": [ 732 | "tmp_df = pd.read_csv('dataset_howell1.csv', sep=';')" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": 26, 738 | "metadata": {}, 739 | "outputs": [ 740 | { 741 | "data": { 742 | "text/html": [ 743 | "
\n", 744 | "\n", 757 | "\n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | " \n", 775 | " \n", 776 | " \n", 777 | " \n", 778 | " \n", 779 | " \n", 780 | " \n", 781 | " \n", 782 | " \n", 783 | " \n", 784 | " \n", 785 | " \n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | " \n", 801 | " \n", 802 | " \n", 803 | " \n", 804 | "
heightweightagemale
0151.76547.82560663.01
1139.70036.48580763.00
2136.52531.86483865.00
3156.84553.04191541.01
4145.41541.27687251.00
\n", 805 | "
" 806 | ], 807 | "text/plain": [ 808 | " height weight age male\n", 809 | "0 151.765 47.825606 63.0 1\n", 810 | "1 139.700 36.485807 63.0 0\n", 811 | "2 136.525 31.864838 65.0 0\n", 812 | "3 156.845 53.041915 41.0 1\n", 813 | "4 145.415 41.276872 51.0 0" 814 | ] 815 | }, 816 | "execution_count": 26, 817 | "metadata": {}, 818 | "output_type": "execute_result" 819 | } 820 | ], 821 | "source": [ 822 | "tmp_df.head()" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 27, 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "data": { 832 | "text/plain": [ 833 | "height float64\n", 834 | "weight float64\n", 835 | "age float64\n", 836 | "male int64\n", 837 | "dtype: object" 838 | ] 839 | }, 840 | "execution_count": 27, 841 | "metadata": {}, 842 | "output_type": "execute_result" 843 | } 844 | ], 845 | "source": [ 846 | "tmp_df.dtypes" 847 | ] 848 | }, 849 | { 850 | "cell_type": "markdown", 851 | "metadata": {}, 852 | "source": [ 853 | "## Dig More\n", 854 | "\n", 855 | "* [NumPy User Guide](https://docs.scipy.org/doc/numpy/user/index.html)\n", 856 | " * [Quickstart tutorial](https://docs.scipy.org/doc/numpy/user/quickstart.html)\n", 857 | " * [Indexing](https://docs.scipy.org/doc/numpy/user/basics.indexing.html)\n", 858 | " * [Broadcasting](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\n", 859 | "* [10 Minutes to pandas](https://pandas.pydata.org/pandas-docs/stable/10min.html)" 860 | ] 861 | } 862 | ], 863 | "metadata": { 864 | "kernelspec": { 865 | "display_name": "Python 3 (practicing-python-3)", 866 | "language": "python", 867 | "name": "python3-practicing-python-3" 868 | }, 869 | "language_info": { 870 | "codemirror_mode": { 871 | "name": "ipython", 872 | "version": 3 873 | }, 874 | "file_extension": ".py", 875 | "mimetype": "text/x-python", 876 | "name": "python", 877 | "nbconvert_exporter": "python", 878 | "pygments_lexer": "ipython3", 879 | "version": "3.6.5" 880 | }, 881 | "toc": { 882 | "base_numbering": 1, 883 | "nav_menu": {}, 884 | "number_sections": true, 885 | "sideBar": true, 886 | "skip_h1_title": true, 887 | "title_cell": "Table of Contents", 888 | "title_sidebar": "Contents", 889 | "toc_cell": false, 890 | "toc_position": {}, 891 | "toc_section_display": "block", 892 | "toc_window_display": false 893 | } 894 | }, 895 | "nbformat": 4, 896 | "nbformat_minor": 2 897 | } 898 | -------------------------------------------------------------------------------- /handouts/14_libraries_moudle_and_package/m.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | print('f') 3 | -------------------------------------------------------------------------------- /handouts/14_libraries_moudle_and_package/p/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/moskytw/practicing-python-3/652813122be42fdcd75439c8e60a8d610a2ac8d3/handouts/14_libraries_moudle_and_package/p/__init__.py -------------------------------------------------------------------------------- /handouts/14_libraries_moudle_and_package/p/ma.py: -------------------------------------------------------------------------------- 1 | def fa(): 2 | print('fa') 3 | -------------------------------------------------------------------------------- /handouts/14_libraries_moudle_and_package/p/mb.py: -------------------------------------------------------------------------------- 1 | from p.ma import fa 2 | -------------------------------------------------------------------------------- /handouts/14_libraries_moudle_and_package/use_them.py: -------------------------------------------------------------------------------- 1 | # m 2 | 3 | print('Access m:') 4 | 5 | import m 6 | m.f() 7 | 8 | from m import f 9 | f() 10 | 11 | print() 12 | 13 | # ma 14 | 15 | print('Access ma:') 16 | 17 | import p.ma 18 | p.ma.fa() 19 | 20 | from p import ma 21 | ma.fa() 22 | 23 | from p.ma import fa 24 | fa() 25 | 26 | print() 27 | 28 | # mb 29 | 30 | print('Access mb:') 31 | 32 | import p.mb 33 | p.mb.fa() 34 | 35 | print() 36 | 37 | # as 38 | 39 | print('Use as:') 40 | 41 | import m as _m 42 | _m.f() 43 | 44 | from m import f as _f 45 | _f() 46 | -------------------------------------------------------------------------------- /handouts/15_data_types_class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Data Types – Class\n", 8 | "\n", 9 | "## Methods and Instance Variables" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Google:\n", 19 | "\n", 20 | " def __init__(self):\n", 21 | " print('Init...')\n", 22 | " self.username = ''\n", 23 | " self.password = ''\n", 24 | "\n", 25 | " def set_user_info(self, username, password):\n", 26 | " print('Set user info...')\n", 27 | " self.username = username\n", 28 | " self.password = password\n", 29 | " \n", 30 | " def log_in(self):\n", 31 | " print('Logging in Google as', self.username, '...')\n", 32 | "\n", 33 | " def query_mail(self):\n", 34 | " print('Query Gmail...')\n", 35 | "\n", 36 | " def query_calendar(self):\n", 37 | " print('Query Google Calendar...')" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Init...\n", 50 | "Set user info...\n", 51 | "Logging in Google as mosky ...\n", 52 | "Query Gmail...\n", 53 | "\n", 54 | "Init...\n", 55 | "Set user info...\n", 56 | "Logging in Google as andy ...\n", 57 | "Query Gmail...\n", 58 | "\n", 59 | "mosky\n", 60 | "andy\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "mosky_g = Google()\n", 66 | "mosky_g.set_user_info('mosky', 'mypassword')\n", 67 | "mosky_g.log_in()\n", 68 | "mosky_g.query_mail()\n", 69 | "# === Google.query_mail(mosky_g)\n", 70 | "print()\n", 71 | "\n", 72 | "andy_g = Google()\n", 73 | "andy_g.set_user_info('andy', 'andypassword')\n", 74 | "andy_g.log_in()\n", 75 | "andy_g.query_mail()\n", 76 | "print()\n", 77 | "\n", 78 | "print(mosky_g.username)\n", 79 | "print(andy_g.username)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "## Class Method and Class Variables" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "class Google:\n", 96 | "\n", 97 | " mail_url = 'http://mail.google.com'\n", 98 | " calendar_url = 'http://calendar.google.com'\n", 99 | " \n", 100 | " @classmethod\n", 101 | " def get_config_dict(cls):\n", 102 | " return {\n", 103 | " 'mail_url': cls.mail_url,\n", 104 | " 'calendar_url': cls.calendar_url,\n", 105 | " }\n", 106 | " \n", 107 | " def __init__(self):\n", 108 | " print('Init...')\n", 109 | " self.username = ''\n", 110 | " self.password = ''\n", 111 | "\n", 112 | " def set_user_info(self, username, password):\n", 113 | " print('Set user info...')\n", 114 | " self.username = username\n", 115 | " self.password = password\n", 116 | " \n", 117 | " def log_in(self):\n", 118 | " print('Logging in Google as', self.username, '...')\n", 119 | "\n", 120 | " def query_mail(self):\n", 121 | " print('Query Gmail from', self.mail_url, '...')\n", 122 | "\n", 123 | " def query_calendar(self):\n", 124 | " print('Query Google Calendar from,', self.calendar_url, '...')" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 4, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "http://mail.google.com\n", 137 | "http://calendar.google.com\n", 138 | "\n", 139 | "{'mail_url': 'http://mail.google.com', 'calendar_url': 'http://calendar.google.com'}\n", 140 | "\n", 141 | "Init...\n", 142 | "Set user info...\n", 143 | "Logging in Google as mosky ...\n", 144 | "Query Gmail from http://mail.google.com ...\n", 145 | "\n", 146 | "Init...\n", 147 | "Set user info...\n", 148 | "Logging in Google as andy ...\n", 149 | "Query Gmail from http://mail.google.com ...\n", 150 | "\n", 151 | "mosky\n", 152 | "andy\n", 153 | "http://mail.google.com\n", 154 | "http://calendar.google.com\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "print(Google.mail_url)\n", 160 | "print(Google.calendar_url)\n", 161 | "print()\n", 162 | "\n", 163 | "print(Google.get_config_dict())\n", 164 | "print()\n", 165 | "\n", 166 | "mosky_g = Google()\n", 167 | "mosky_g.set_user_info('mosky', 'mypassword')\n", 168 | "mosky_g.log_in()\n", 169 | "mosky_g.query_mail()\n", 170 | "# === Google.query_mail(mosky_g)\n", 171 | "print()\n", 172 | "\n", 173 | "andy_g = Google()\n", 174 | "andy_g.set_user_info('andy', 'andypassword')\n", 175 | "andy_g.log_in()\n", 176 | "andy_g.query_mail()\n", 177 | "print()\n", 178 | "\n", 179 | "print(mosky_g.username)\n", 180 | "print(andy_g.username)\n", 181 | "print(mosky_g.mail_url)\n", 182 | "print(andy_g.calendar_url)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 5, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "class Site:\n", 192 | "\n", 193 | " def __init__(self):\n", 194 | " print('Init...')\n", 195 | " self.username = ''\n", 196 | " self.password = ''\n", 197 | "\n", 198 | " def set_user_info(self, username, password):\n", 199 | " print('Set user info...')\n", 200 | " self.username = username\n", 201 | " self.password = password\n", 202 | " \n", 203 | " def log_in(self):\n", 204 | " raise NotImplementedError\n", 205 | " \n", 206 | " def query_mail(self):\n", 207 | " raise NotImplementedError\n", 208 | " \n", 209 | " def query_calendar(self):\n", 210 | " raise NotImplementedError\n", 211 | "\n", 212 | "\n", 213 | "class Google(Site):\n", 214 | "\n", 215 | " def log_in(self):\n", 216 | " print('Logging in Google as', self.username, '...')\n", 217 | "\n", 218 | " def query_mail(self):\n", 219 | " print('Query Gmail...')\n", 220 | "\n", 221 | " def query_calendar(self):\n", 222 | " print('Query Google Calendar...')\n", 223 | "\n", 224 | "\n", 225 | "class Yahoo(Site):\n", 226 | "\n", 227 | " yahoo_mail_url = 'http://mail.yahoo.com'\n", 228 | "\n", 229 | " def log_in(self):\n", 230 | " print('Logging in Yahoo! as', self.username, '...')\n", 231 | " \n", 232 | " def query_mail(self):\n", 233 | " print('Query Yahoo! Mail...')" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 6, 239 | "metadata": { 240 | "scrolled": true 241 | }, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "Init...\n", 248 | "Set user info...\n", 249 | "Logging in Google as mosky ...\n", 250 | "Query Gmail...\n", 251 | "\n", 252 | "Init...\n", 253 | "Set user info...\n", 254 | "Logging in Yahoo! as mosky@yahoo ...\n", 255 | "Query Yahoo! Mail...\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "mosky_g = Google()\n", 261 | "mosky_g.set_user_info('mosky', 'mypassword')\n", 262 | "mosky_g.log_in()\n", 263 | "mosky_g.query_mail()\n", 264 | "print()\n", 265 | "\n", 266 | "mosky_y = Yahoo()\n", 267 | "mosky_y.set_user_info('mosky@yahoo', 'mypassword@yahoo')\n", 268 | "mosky_y.log_in()\n", 269 | "mosky_y.query_mail()\n", 270 | "\n", 271 | "# mosky_y.query_calendar()" 272 | ] 273 | } 274 | ], 275 | "metadata": { 276 | "kernelspec": { 277 | "display_name": "Python 3 (practicing-python-3)", 278 | "language": "python", 279 | "name": "python3-practicing-python-3" 280 | }, 281 | "language_info": { 282 | "codemirror_mode": { 283 | "name": "ipython", 284 | "version": 3 285 | }, 286 | "file_extension": ".py", 287 | "mimetype": "text/x-python", 288 | "name": "python", 289 | "nbconvert_exporter": "python", 290 | "pygments_lexer": "ipython3", 291 | "version": "3.7.3" 292 | }, 293 | "toc": { 294 | "base_numbering": 1, 295 | "nav_menu": {}, 296 | "number_sections": true, 297 | "sideBar": true, 298 | "skip_h1_title": true, 299 | "title_cell": "Table of Contents", 300 | "title_sidebar": "Contents", 301 | "toc_cell": false, 302 | "toc_position": {}, 303 | "toc_section_display": "block", 304 | "toc_window_display": false 305 | } 306 | }, 307 | "nbformat": 4, 308 | "nbformat_minor": 2 309 | } 310 | -------------------------------------------------------------------------------- /handouts/dataset_howell1.csv: -------------------------------------------------------------------------------- 1 | "height";"weight";"age";"male" 2 | 151.765;47.8256065;63;1 3 | 139.7;36.4858065;63;0 4 | 136.525;31.864838;65;0 5 | 156.845;53.0419145;41;1 6 | 145.415;41.276872;51;0 7 | 163.83;62.992589;35;1 8 | 149.225;38.2434755;32;0 9 | 168.91;55.4799715;27;1 10 | 147.955;34.869885;19;0 11 | 165.1;54.487739;54;1 12 | 154.305;49.89512;47;0 13 | 151.13;41.220173;66;1 14 | 144.78;36.0322145;73;0 15 | 149.9;47.7;20;0 16 | 150.495;33.849303;65.3;0 17 | 163.195;48.5626935;36;1 18 | 157.48;42.3258035;44;1 19 | 143.9418;38.3568735;31;0 20 | 121.92;19.617854;12;1 21 | 105.41;13.947954;8;0 22 | 86.36;10.489315;6.5;0 23 | 161.29;48.987936;39;1 24 | 156.21;42.7226965;29;0 25 | 129.54;23.586784;13;1 26 | 109.22;15.989118;7;0 27 | 146.4;35.493574;56;1 28 | 148.59;37.9032815;45;0 29 | 147.32;35.4652245;19;0 30 | 137.16;27.328918;17;1 31 | 125.73;22.6796;16;0 32 | 114.3;17.860185;11;1 33 | 147.955;40.312989;29;1 34 | 161.925;55.111428;30;1 35 | 146.05;37.5063885;24;0 36 | 146.05;38.498621;35;0 37 | 152.7048;46.606578;33;0 38 | 142.875;38.838815;27;0 39 | 142.875;35.5786225;32;0 40 | 147.955;47.400364;36;0 41 | 160.655;47.8823055;24;1 42 | 151.765;49.4131785;30;1 43 | 162.8648;49.384829;24;1 44 | 171.45;56.5572525;52;1 45 | 147.32;39.12231;42;0 46 | 147.955;49.89512;19;0 47 | 144.78;28.803092;17;0 48 | 121.92;20.41164;8;1 49 | 128.905;23.359988;12;0 50 | 97.79;13.267566;5;0 51 | 154.305;41.2485225;55;1 52 | 143.51;38.55532;43;0 53 | 146.7;42.4;20;1 54 | 157.48;44.6504625;18;1 55 | 127;22.0105518;13;1 56 | 110.49;15.422128;9;0 57 | 97.79;12.757275;5;0 58 | 165.735;58.5984165;42;1 59 | 152.4;46.719976;44;0 60 | 141.605;44.22522;60;0 61 | 158.8;50.9;20;0 62 | 155.575;54.317642;37;0 63 | 164.465;45.8978405;50;1 64 | 151.765;48.024053;50;0 65 | 161.29;52.219779;31;1 66 | 154.305;47.62716;25;0 67 | 145.415;45.642695;23;0 68 | 145.415;42.410852;52;0 69 | 152.4;36.4858065;79.3;1 70 | 163.83;55.9335635;35;1 71 | 144.145;37.194544;27;0 72 | 129.54;24.550667;13;1 73 | 129.54;25.627948;14;0 74 | 153.67;48.307548;38;1 75 | 142.875;37.3362915;39;0 76 | 146.05;29.596878;12;0 77 | 167.005;47.173568;30;1 78 | 158.4198;47.286966;24;0 79 | 91.44;12.927372;0.599999999999909;1 80 | 165.735;57.549485;51;1 81 | 149.86;37.931631;46;0 82 | 147.955;41.900561;17;0 83 | 137.795;27.5840635;12;0 84 | 154.94;47.2019175;22;0 85 | 160.9598;43.204638;29;1 86 | 161.925;50.2636635;38;1 87 | 147.955;39.3774555;30;0 88 | 113.665;17.463292;6;1 89 | 159.385;50.689;45;1 90 | 148.59;39.4341545;47;0 91 | 136.525;36.28736;79;0 92 | 158.115;46.266384;45;1 93 | 144.78;42.2691045;54;0 94 | 156.845;47.62716;31;1 95 | 179.07;55.7067675;23;1 96 | 118.745;18.824068;9;0 97 | 170.18;48.5626935;41;1 98 | 146.05;42.807745;23;0 99 | 147.32;35.0683315;36;0 100 | 113.03;17.8885345;5;1 101 | 162.56;56.755699;30;0 102 | 133.985;27.442316;12;1 103 | 152.4;51.255896;34;0 104 | 160.02;47.230267;44;1 105 | 149.86;40.936678;43;0 106 | 142.875;32.715323;73.3;0 107 | 167.005;57.0675435;38;1 108 | 159.385;42.977842;43;1 109 | 154.94;39.9444455;33;0 110 | 148.59;32.4601775;16;0 111 | 111.125;17.123098;11;1 112 | 111.76;16.499409;6;1 113 | 162.56;45.9545395;35;1 114 | 152.4;41.106775;29;0 115 | 124.46;18.257078;12;0 116 | 111.76;15.081934;9;1 117 | 86.36;11.4815475;7.59999999999991;1 118 | 170.18;47.5988105;58;1 119 | 146.05;37.5063885;53;0 120 | 159.385;45.019006;51;1 121 | 151.13;42.2691045;48;0 122 | 160.655;54.8562825;29;1 123 | 169.545;53.523856;41;1 124 | 158.75;52.1914295;81.75;1 125 | 74.295;9.752228;1;1 126 | 149.86;42.410852;35;0 127 | 153.035;49.5832755;46;0 128 | 96.52;13.097469;5;1 129 | 161.925;41.730464;29;1 130 | 162.56;56.018612;42;1 131 | 149.225;42.1557065;27;0 132 | 116.84;19.391058;8;0 133 | 100.076;15.081934;6;1 134 | 163.195;53.0986135;22;1 135 | 161.925;50.235314;43;1 136 | 145.415;42.52425;53;0 137 | 163.195;49.101334;43;1 138 | 151.13;38.498621;41;0 139 | 150.495;49.8100715;50;0 140 | 141.605;29.313383;15;1 141 | 170.815;59.760746;33;1 142 | 91.44;11.7083435;3;0 143 | 157.48;47.9390045;62;1 144 | 152.4;39.292407;49;0 145 | 149.225;38.1300775;17;1 146 | 129.54;21.999212;12;0 147 | 147.32;36.8826995;22;0 148 | 145.415;42.127357;29;0 149 | 121.92;19.787951;8;0 150 | 113.665;16.782904;5;1 151 | 157.48;44.565414;33;1 152 | 154.305;47.853956;34;0 153 | 120.65;21.1770765;12;0 154 | 115.6;18.9;7;1 155 | 167.005;55.1964765;42;1 156 | 142.875;32.998818;40;0 157 | 152.4;40.879979;27;0 158 | 96.52;13.267566;3;0 159 | 160;51.2;25;1 160 | 159.385;49.044635;29;1 161 | 149.86;53.4388075;45;0 162 | 160.655;54.090846;26;1 163 | 160.655;55.3665735;45;1 164 | 149.225;42.240755;45;0 165 | 125.095;22.3677555;11;0 166 | 140.97;40.936678;85.5999999999999;0 167 | 154.94;49.6966735;26;1 168 | 141.605;44.338618;24;0 169 | 160.02;45.9545395;57;1 170 | 150.1648;41.95726;22;0 171 | 155.575;51.482692;24;0 172 | 103.505;12.757275;6;0 173 | 94.615;13.0124205;4;0 174 | 156.21;44.111822;21;0 175 | 153.035;32.205032;79;0 176 | 167.005;56.755699;50;1 177 | 149.86;52.673371;40;0 178 | 147.955;36.4858065;64;0 179 | 159.385;48.8461885;32;1 180 | 161.925;56.9541455;38.7;1 181 | 155.575;42.0990075;26;0 182 | 159.385;50.178615;63;1 183 | 146.685;46.549879;62;0 184 | 172.72;61.80191;22;1 185 | 166.37;48.987936;41;1 186 | 141.605;31.524644;19;1 187 | 142.875;32.205032;17;0 188 | 133.35;23.756881;14;0 189 | 127.635;24.4089195;9;1 190 | 119.38;21.5172705;7;1 191 | 151.765;35.2951275;74;0 192 | 156.845;45.642695;41;1 193 | 148.59;43.885026;33;0 194 | 157.48;45.5576465;53;0 195 | 149.86;39.008912;18;0 196 | 147.955;41.163474;37;0 197 | 102.235;13.1258185;6;0 198 | 153.035;45.245802;61;0 199 | 160.655;53.637254;44;1 200 | 149.225;52.3048275;35;0 201 | 114.3;18.3421265;7;1 202 | 100.965;13.7495075;4;1 203 | 138.43;39.0939605;23;0 204 | 91.44;12.530479;4;1 205 | 162.56;45.699394;55;1 206 | 149.225;40.3980375;53;0 207 | 158.75;51.482692;59;1 208 | 149.86;38.668718;57;0 209 | 158.115;39.235708;35;1 210 | 156.21;44.338618;29;0 211 | 148.59;39.519203;62;1 212 | 143.51;31.071052;18;0 213 | 154.305;46.776675;51;0 214 | 131.445;22.509503;14;0 215 | 157.48;40.6248335;19;1 216 | 157.48;50.178615;42;1 217 | 154.305;41.276872;25;0 218 | 107.95;17.57669;6;1 219 | 168.275;54.6;41;1 220 | 145.415;44.9906565;37;0 221 | 147.955;44.735511;16;0 222 | 100.965;14.401546;5;1 223 | 113.03;19.050864;9;1 224 | 149.225;35.8054185;82;1 225 | 154.94;45.2174525;28;1 226 | 162.56;48.1091015;50;1 227 | 156.845;45.6710445;43;0 228 | 123.19;20.808533;8;1 229 | 161.0106;48.420946;31;1 230 | 144.78;41.1918235;67;0 231 | 143.51;38.4135725;39;0 232 | 149.225;42.127357;18;0 233 | 110.49;17.6617385;11;0 234 | 149.86;38.2434755;48;0 235 | 165.735;48.3358975;30;1 236 | 144.145;38.9238635;64;0 237 | 157.48;40.029494;72;1 238 | 154.305;50.2069645;68;0 239 | 163.83;54.2892925;44;1 240 | 156.21;45.6;43;0 241 | 153.67;40.766581;16;0 242 | 134.62;27.1304715;13;0 243 | 144.145;39.4341545;34;0 244 | 114.3;20.4966885;10;0 245 | 162.56;43.204638;62;1 246 | 146.05;31.864838;44;0 247 | 120.65;20.8935815;11;1 248 | 154.94;45.4442485;31;1 249 | 144.78;38.045029;29;0 250 | 106.68;15.989118;8;0 251 | 146.685;36.0889135;62;0 252 | 152.4;40.879979;67;0 253 | 163.83;47.910655;57;1 254 | 165.735;47.7122085;32;1 255 | 156.21;46.379782;24;0 256 | 152.4;41.163474;77;1 257 | 140.335;36.5992045;62;0 258 | 158.115;43.09124;17;1 259 | 163.195;48.137451;67;1 260 | 151.13;36.7126025;70;0 261 | 171.1198;56.5572525;37;1 262 | 149.86;38.6970675;58;0 263 | 163.83;47.4854125;35;1 264 | 141.605;36.2023115;30;0 265 | 93.98;14.288148;5;0 266 | 149.225;41.276872;26;0 267 | 105.41;15.2236815;5;0 268 | 146.05;44.7638605;21;0 269 | 161.29;50.4337605;41;1 270 | 162.56;55.281525;46;1 271 | 145.415;37.931631;49;0 272 | 145.415;35.493574;15;1 273 | 170.815;58.456669;28;1 274 | 127;21.488921;12;0 275 | 159.385;44.4236665;83;0 276 | 159.4;44.4;54;1 277 | 153.67;44.565414;54;0 278 | 160.02;44.622113;68;1 279 | 150.495;40.483086;68;0 280 | 149.225;44.0834725;56;0 281 | 127;24.4089195;15;0 282 | 142.875;34.416293;57;0 283 | 142.113;32.772022;22;0 284 | 147.32;35.947166;40;0 285 | 162.56;49.5549;19;1 286 | 164.465;53.183662;41;1 287 | 160.02;37.081146;75.9000000000001;1 288 | 153.67;40.5114355;73.9000000000001;0 289 | 167.005;50.6038575;49;1 290 | 151.13;43.9700745;26;1 291 | 147.955;33.792604;17;0 292 | 125.3998;21.375523;13;0 293 | 111.125;16.669506;8;0 294 | 153.035;49.89;88;1 295 | 139.065;33.5941575;68;0 296 | 152.4;43.8566765;33;1 297 | 154.94;48.137451;26;0 298 | 147.955;42.751046;56;0 299 | 143.51;34.8415355;16;1 300 | 117.983;24.097075;13;0 301 | 144.145;33.906002;34;0 302 | 92.71;12.076887;5;0 303 | 147.955;41.276872;17;0 304 | 155.575;39.7176495;74;1 305 | 150.495;35.947166;69;0 306 | 155.575;50.915702;50;1 307 | 154.305;45.756093;44;0 308 | 130.6068;25.2594045;15;0 309 | 101.6;15.3370795;5;0 310 | 157.48;49.214732;18;0 311 | 168.91;58.8252125;41;1 312 | 150.495;43.4597835;27;0 313 | 111.76;17.8318355;8.90000000000009;1 314 | 160.02;51.9646335;38;1 315 | 167.64;50.688906;57;1 316 | 144.145;34.246196;64.5;0 317 | 145.415;39.3774555;42;0 318 | 160.02;59.5622995;24;1 319 | 147.32;40.312989;16;1 320 | 164.465;52.16308;71;1 321 | 153.035;39.972795;49.5;0 322 | 149.225;43.941725;33;1 323 | 160.02;54.601137;28;0 324 | 149.225;45.075705;47;0 325 | 85.09;11.453198;3;1 326 | 84.455;11.7650425;1;1 327 | 59.6138;5.896696;1;0 328 | 92.71;12.1052365;3;1 329 | 111.125;18.313777;6;0 330 | 90.805;11.3681495;5;0 331 | 153.67;41.333571;27;0 332 | 99.695;16.2442635;5;0 333 | 62.484;6.80388;1;0 334 | 81.915;11.8784405;2;1 335 | 96.52;14.968536;2;0 336 | 80.01;9.865626;1;1 337 | 150.495;41.900561;55;0 338 | 151.765;42.524;83.4000000000001;1 339 | 140.6398;28.859791;12;1 340 | 88.265;12.7856245;2;0 341 | 158.115;43.147939;63;1 342 | 149.225;40.82328;52;0 343 | 151.765;42.864444;49;1 344 | 154.94;46.209685;31;0 345 | 123.825;20.581737;9;0 346 | 104.14;15.87572;6;0 347 | 161.29;47.853956;35;1 348 | 148.59;42.52425;35;0 349 | 97.155;17.066399;7;0 350 | 93.345;13.1825175;5;1 351 | 160.655;48.5059945;24;1 352 | 157.48;45.869491;41;1 353 | 167.005;52.900167;32;1 354 | 157.48;47.570461;43;1 355 | 91.44;12.927372;6;0 356 | 60.452;5.6699;1;1 357 | 137.16;28.91649;15;1 358 | 152.4;43.544832;63;0 359 | 152.4;43.431434;21;0 360 | 81.28;11.509897;1;1 361 | 109.22;11.7083435;2;0 362 | 71.12;7.540967;1;1 363 | 89.2048;12.700576;3;0 364 | 67.31;7.200773;1;0 365 | 85.09;12.360382;1;1 366 | 69.85;7.7961125;1;0 367 | 161.925;53.2120115;55;0 368 | 152.4;44.678812;38;0 369 | 88.9;12.5588285;3;1 370 | 90.17;12.700576;3;1 371 | 71.755;7.37087;1;0 372 | 83.82;9.2135875;1;0 373 | 159.385;47.2019175;28;1 374 | 142.24;28.632995;16;0 375 | 142.24;31.6663915;36;0 376 | 168.91;56.4438545;38;1 377 | 123.19;20.014747;12;1 378 | 74.93;8.50485;1;1 379 | 74.295;8.3064035;1;0 380 | 90.805;11.623295;3;0 381 | 160.02;55.791816;48;1 382 | 67.945;7.9662095;1;0 383 | 135.89;27.21552;15;0 384 | 158.115;47.4854125;45;1 385 | 85.09;10.8011595;3;1 386 | 93.345;14.004653;3;0 387 | 152.4;45.1607535;38;0 388 | 155.575;45.529297;21;0 389 | 154.305;48.874538;50;0 390 | 156.845;46.5782285;41;1 391 | 120.015;20.128145;13;0 392 | 114.3;18.14368;8;1 393 | 83.82;10.9145575;3;1 394 | 156.21;43.885026;30;0 395 | 137.16;27.158821;12;1 396 | 114.3;19.050864;7;1 397 | 93.98;13.834556;4;0 398 | 168.275;56.0469615;21;1 399 | 147.955;40.086193;38;0 400 | 139.7;26.5634815;15;1 401 | 157.48;50.802304;19;0 402 | 76.2;9.2135875;1;1 403 | 66.04;7.5693165;1;1 404 | 160.7;46.3;31;1 405 | 114.3;19.4194075;8;0 406 | 146.05;37.9032815;16;1 407 | 161.29;49.3564795;21;1 408 | 69.85;7.314171;0;0 409 | 133.985;28.1510535;13;1 410 | 67.945;7.824462;0;1 411 | 150.495;44.111822;50;0 412 | 163.195;51.0291;39;1 413 | 148.59;40.766581;44;1 414 | 148.59;37.5630875;36;0 415 | 161.925;51.59609;36;1 416 | 153.67;44.8205595;18;0 417 | 68.58;8.0229085;0;0 418 | 151.13;43.4030845;58;0 419 | 163.83;46.719976;58;1 420 | 153.035;39.5475525;33;0 421 | 151.765;34.7848365;21.5;0 422 | 132.08;22.792998;11;1 423 | 156.21;39.292407;26;1 424 | 140.335;37.4496895;22;0 425 | 158.75;48.6760915;28;1 426 | 142.875;35.606972;42;0 427 | 84.455;9.3836845;2;1 428 | 151.9428;43.714929;21;1 429 | 161.29;48.19415;19;1 430 | 127.9906;29.8520235;13;1 431 | 160.9852;50.972401;48;1 432 | 144.78;43.998424;46;0 433 | 132.08;28.292801;11;1 434 | 117.983;20.354941;8;1 435 | 160.02;48.19415;25;1 436 | 154.94;39.179009;16;1 437 | 160.9852;46.6916265;51;1 438 | 165.989;56.415505;25;1 439 | 157.988;48.591043;28;1 440 | 154.94;48.2224995;26;0 441 | 97.9932;13.2959155;5;1 442 | 64.135;6.6621325;1;0 443 | 160.655;47.4854125;54;1 444 | 147.32;35.550273;66;0 445 | 146.7;36.6;20;0 446 | 147.32;48.9595865;25;0 447 | 172.9994;51.255896;38;1 448 | 158.115;46.5215295;51;1 449 | 147.32;36.967748;48;0 450 | 124.9934;25.117657;13;1 451 | 106.045;16.272613;6;1 452 | 165.989;48.647742;27;1 453 | 149.86;38.045029;22;0 454 | 76.2;8.50485;1;0 455 | 161.925;47.286966;60;1 456 | 140.0048;28.3495;15;0 457 | 66.675;8.1363065;0;0 458 | 62.865;7.200773;0;1 459 | 163.83;55.394923;43;1 460 | 147.955;32.488527;12;1 461 | 160.02;54.204244;27;1 462 | 154.94;48.477645;30;1 463 | 152.4;43.0628905;29;0 464 | 62.23;7.257472;0;0 465 | 146.05;34.189497;23;0 466 | 151.9936;49.951819;30;0 467 | 157.48;41.3052215;17;1 468 | 55.88;4.8477645;0;0 469 | 60.96;6.23689;0;1 470 | 151.765;44.338618;41;0 471 | 144.78;33.45241;42;0 472 | 118.11;16.896302;7;0 473 | 78.105;8.221355;3;0 474 | 160.655;47.286966;43;1 475 | 151.13;46.1246365;35;0 476 | 121.92;20.184844;10;0 477 | 92.71;12.757275;3;1 478 | 153.67;47.400364;75.5;1 479 | 147.32;40.8516295;64;0 480 | 139.7;50.348712;38;1 481 | 157.48;45.132404;24.2;0 482 | 91.44;11.623295;4;0 483 | 154.94;42.240755;26;1 484 | 143.51;41.6454155;19;0 485 | 83.185;9.1568885;2;1 486 | 158.115;45.2174525;43;1 487 | 147.32;51.255896;38;0 488 | 123.825;21.205426;10;1 489 | 88.9;11.5949455;3;1 490 | 160.02;49.271431;23;1 491 | 137.16;27.952607;16;0 492 | 165.1;51.199197;49;1 493 | 154.94;43.8566765;41;0 494 | 111.125;17.690088;6;1 495 | 153.67;35.5219235;23;0 496 | 145.415;34.246196;14;0 497 | 141.605;42.88542;43;0 498 | 144.78;32.545226;15;0 499 | 163.83;46.776675;21;1 500 | 161.29;41.8722115;24;1 501 | 154.9;38.2;20;1 502 | 161.3;43.3;20;1 503 | 170.18;53.637254;34;1 504 | 149.86;42.977842;29;0 505 | 123.825;21.54562;11;1 506 | 85.09;11.4248485;3;0 507 | 160.655;39.7743485;65;1 508 | 154.94;43.3463855;46;0 509 | 106.045;15.478827;8;0 510 | 126.365;21.9141635;15;1 511 | 166.37;52.673371;43;1 512 | 148.2852;38.441922;39;0 513 | 124.46;19.27766;12;0 514 | 89.535;11.113004;3;1 515 | 101.6;13.494362;4;0 516 | 151.765;42.807745;43;0 517 | 148.59;35.890467;70;0 518 | 153.67;44.22522;26;0 519 | 53.975;4.252425;0;0 520 | 146.685;38.0733785;48;0 521 | 56.515;5.159609;0;0 522 | 100.965;14.3164975;5;1 523 | 121.92;23.2182405;8;1 524 | 81.5848;10.659412;3;0 525 | 154.94;44.111822;44;1 526 | 156.21;44.0267735;33;0 527 | 132.715;24.9759095;15;1 528 | 125.095;22.5945515;12;0 529 | 101.6;14.344847;5;0 530 | 160.655;47.8823055;41;1 531 | 146.05;39.405805;37.4;0 532 | 132.715;24.777463;13;0 533 | 87.63;10.659412;6;0 534 | 156.21;41.050076;53;1 535 | 152.4;40.82328;49;0 536 | 162.56;47.0318205;27;0 537 | 114.935;17.519991;7;1 538 | 67.945;7.2291225;1;0 539 | 142.875;34.246196;31;0 540 | 76.835;8.0229085;1;1 541 | 145.415;31.127751;17;1 542 | 162.56;52.16308;31;1 543 | 156.21;54.0624965;21;0 544 | 71.12;8.051258;0;1 545 | 158.75;52.5316235;68;1 546 | -------------------------------------------------------------------------------- /showcases/showcase_01_a_website_in_a_minute.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route("/") 6 | def hello(): 7 | return "Hello World!" 8 | 9 | if __name__ == "__main__": 10 | app.run() 11 | --------------------------------------------------------------------------------