├── .gitignore └── python_tips.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/python 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=python 4 | 5 | ### Python ### 6 | # Byte-compiled / optimized / DLL files 7 | __pycache__/ 8 | *.py[cod] 9 | *$py.class 10 | 11 | # C extensions 12 | *.so 13 | 14 | # Distribution / packaging 15 | .Python 16 | build/ 17 | develop-eggs/ 18 | dist/ 19 | downloads/ 20 | eggs/ 21 | .eggs/ 22 | lib/ 23 | lib64/ 24 | parts/ 25 | sdist/ 26 | var/ 27 | wheels/ 28 | pip-wheel-metadata/ 29 | share/python-wheels/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | MANIFEST 34 | 35 | # PyInstaller 36 | # Usually these files are written by a python script from a template 37 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 38 | *.manifest 39 | *.spec 40 | 41 | # Installer logs 42 | pip-log.txt 43 | pip-delete-this-directory.txt 44 | 45 | # Unit test / coverage reports 46 | htmlcov/ 47 | .tox/ 48 | .nox/ 49 | .coverage 50 | .coverage.* 51 | .cache 52 | nosetests.xml 53 | coverage.xml 54 | *.cover 55 | *.py,cover 56 | .hypothesis/ 57 | .pytest_cache/ 58 | pytestdebug.log 59 | 60 | # Translations 61 | *.mo 62 | *.pot 63 | 64 | # Django stuff: 65 | *.log 66 | local_settings.py 67 | db.sqlite3 68 | db.sqlite3-journal 69 | 70 | # Flask stuff: 71 | instance/ 72 | .webassets-cache 73 | 74 | # Scrapy stuff: 75 | .scrapy 76 | 77 | # Sphinx documentation 78 | docs/_build/ 79 | doc/_build/ 80 | 81 | # PyBuilder 82 | target/ 83 | 84 | # Jupyter Notebook 85 | .ipynb_checkpoints 86 | 87 | # IPython 88 | profile_default/ 89 | ipython_config.py 90 | 91 | # pyenv 92 | .python-version 93 | 94 | # pipenv 95 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 96 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 97 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 98 | # install all needed dependencies. 99 | #Pipfile.lock 100 | 101 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 102 | __pypackages__/ 103 | 104 | # Celery stuff 105 | celerybeat-schedule 106 | celerybeat.pid 107 | 108 | # SageMath parsed files 109 | *.sage.py 110 | 111 | # Environments 112 | .env 113 | .venv 114 | env/ 115 | venv/ 116 | ENV/ 117 | env.bak/ 118 | venv.bak/ 119 | pythonenv* 120 | 121 | # Spyder project settings 122 | .spyderproject 123 | .spyproject 124 | 125 | # Rope project settings 126 | .ropeproject 127 | 128 | # mkdocs documentation 129 | /site 130 | 131 | # mypy 132 | .mypy_cache/ 133 | .dmypy.json 134 | dmypy.json 135 | 136 | # Pyre type checker 137 | .pyre/ 138 | 139 | # pytype static type analyzer 140 | .pytype/ 141 | 142 | # profiling data 143 | .prof 144 | 145 | # End of https://www.toptal.com/developers/gitignore/api/python 146 | 147 | 148 | 149 | # Created by https://www.toptal.com/developers/gitignore/api/jupyternotebooks 150 | # Edit at https://www.toptal.com/developers/gitignore?templates=jupyternotebooks 151 | 152 | ### JupyterNotebooks ### 153 | # gitignore template for Jupyter Notebooks 154 | # website: http://jupyter.org/ 155 | 156 | .ipynb_checkpoints 157 | */.ipynb_checkpoints/* 158 | 159 | # IPython 160 | profile_default/ 161 | ipython_config.py 162 | 163 | # Remove previous ipynb_checkpoints 164 | # git rm -r .ipynb_checkpoints/ 165 | 166 | # End of https://www.toptal.com/developers/gitignore/api/jupyternotebooks 167 | -------------------------------------------------------------------------------- /python_tips.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python tips and tricks\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Assign Multiple Variables on One Line" 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 | "5 10\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "x, y = 5, 10\n", 32 | "print(x, y)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## Print colored text" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "\u001b[91mError: This is \u001b[96mcyan. \u001b[92mContinue?\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "print(f\"\\033[91mError: This is \\033[96mcyan. \\033[92mContinue?\")" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "## How to raise a number to a power" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "3125\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "x = 5**5\n", 81 | "print(x)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Banker's Rounding - half towards even" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "10\n", 101 | "12\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "print(round(10.5)) #down to 10\n", 107 | "print(round(11.5)) #up to 12" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "## Underscores in numbers" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "999999999\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "my_bank_account = 999_999_999\n", 132 | "print(my_bank_account)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "## Open a web browser" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "import webbrowser\n", 149 | "\n", 150 | "#webbrowser.open('https://www.google.com') #commented out because it's annoying" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "## Concatenation without +" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 7, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Hello this is a message\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "message = \"Hello this \" \"is a message\"\n", 175 | "print(message)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "## Split string on multiple lines" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 8, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "This is a really long message that I split up over multiple lines for convenience sake. The actual output is the same\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "print(\"This is a really long message that I split up over \"\n", 200 | "\"multiple lines for convenience sake. \"\n", 201 | "\"The actual output is the same\")" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "## Multiline string" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 9, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "This is a really long message that I split up over multiple lines for convenience sake.\n", 221 | "The output is affected.\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "# \\ to ignore newline. No spaces after!\n", 227 | "print('''This is a really long message that I split up over \\\n", 228 | "multiple lines for convenience sake.\n", 229 | "The output is affected.''')" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "## Multiline comment" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 10, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "5\n", 249 | "10\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "print(5)\n", 255 | "\"\"\"\n", 256 | "print('ignored!')\n", 257 | "print('pretty cool huh')\n", 258 | "\"\"\"\n", 259 | "print(10)" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "## Get last element of list" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 11, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "3\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "data = [1, 2, 3]\n", 284 | "print(data[-1])" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "## Reverse a list with slicing" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 12, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "[3, 2, 1]\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "data = [1, 2, 3]\n", 309 | "print(data[::-1])" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "## Reverse with method" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 13, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "name": "stdout", 326 | "output_type": "stream", 327 | "text": [ 328 | "[3, 2, 1]\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "data = [1, 2, 3]\n", 334 | "data.reverse()\n", 335 | "print(data)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "## Substring with in" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 14, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "0\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "if 'You' in 'YouTube':\n", 360 | " print('YouTube'.index('You')) #can throw error" 361 | ] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "metadata": {}, 366 | "source": [ 367 | "## Single line if" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 15, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "name": "stdout", 377 | "output_type": "stream", 378 | "text": [ 379 | "0\n" 380 | ] 381 | } 382 | ], 383 | "source": [ 384 | "if 'You' in 'YouTube': print('YouTube'.index('You')) #can throw error" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "## Get index with find" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 16, 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "data": { 401 | "text/plain": [ 402 | "-1" 403 | ] 404 | }, 405 | "execution_count": 16, 406 | "metadata": {}, 407 | "output_type": "execute_result" 408 | } 409 | ], 410 | "source": [ 411 | "'YouTube'.find('flowers')" 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "## Using id to get identity -- guarenteed to be unique for each object" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 17, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "140479393791744\n" 431 | ] 432 | } 433 | ], 434 | "source": [ 435 | "data = {\"Caleb\": 5}\n", 436 | "print(id(data))" 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "## Aliases" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "An alias is another name for something\n", 451 | "We use an alias if we want two variables pointing to the same data\n", 452 | "or if we want functions to be able to modify arguments passed it" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 18, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "name": "stdout", 462 | "output_type": "stream", 463 | "text": [ 464 | "140479393791904\n", 465 | "140479393791904\n", 466 | "{'Caleb': 5, 'Erin': 13}\n" 467 | ] 468 | } 469 | ], 470 | "source": [ 471 | "data1 = {\"Caleb\": 5}\n", 472 | "data2 = data1\n", 473 | "data2['Erin'] = 13\n", 474 | "\n", 475 | "print(id(data1))\n", 476 | "print(id(data2))\n", 477 | "print(data1)" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "## Primitive types are immutable" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 19, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | "4311143040\n", 497 | "4311142752\n", 498 | "1\n" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "data1 = 10\n", 504 | "data2 = data1\n", 505 | "data2 = 1 #replaced data2\n", 506 | "\n", 507 | "print(id(data1))\n", 508 | "print(id(data2))\n", 509 | "print(data2)" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": {}, 515 | "source": [ 516 | "## in-place methods" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 20, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "name": "stdout", 526 | "output_type": "stream", 527 | "text": [ 528 | "[1, 2, 3]\n" 529 | ] 530 | } 531 | ], 532 | "source": [ 533 | "data = [3, 2, 1]\n", 534 | "data.sort()\n", 535 | "print(data)" 536 | ] 537 | }, 538 | { 539 | "cell_type": "markdown", 540 | "metadata": {}, 541 | "source": [ 542 | "## replace list vs replace list content" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 21, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "name": "stdout", 552 | "output_type": "stream", 553 | "text": [ 554 | "['C++', 'Go', 'Python']\n", 555 | "['C++', 'Go', 'Python']\n", 556 | "['new']\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "def replace_list(data):\n", 562 | " data = ['new']\n", 563 | " \n", 564 | "def replace_list_content(data):\n", 565 | " data[:] = ['new']\n", 566 | " \n", 567 | "languages = ['C++', 'Go', 'Python']\n", 568 | "\n", 569 | "print(languages)\n", 570 | "replace_list(languages)\n", 571 | "print(languages)\n", 572 | "replace_list_content(languages)\n", 573 | "print(languages)" 574 | ] 575 | }, 576 | { 577 | "cell_type": "markdown", 578 | "metadata": {}, 579 | "source": [ 580 | "## Copy a list with slicing" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 22, 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "name": "stdout", 590 | "output_type": "stream", 591 | "text": [ 592 | "140479393918480 140479393910944\n" 593 | ] 594 | } 595 | ], 596 | "source": [ 597 | "languages = ['C++', 'Go', 'Python']\n", 598 | "learning = languages[:]\n", 599 | "\n", 600 | "print(id(languages), id(learning))" 601 | ] 602 | }, 603 | { 604 | "cell_type": "markdown", 605 | "metadata": {}, 606 | "source": [ 607 | "## Copy a list with method" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 23, 613 | "metadata": {}, 614 | "outputs": [ 615 | { 616 | "name": "stdout", 617 | "output_type": "stream", 618 | "text": [ 619 | "140479393912224 140479393916880\n" 620 | ] 621 | } 622 | ], 623 | "source": [ 624 | "languages = ['C++', 'Go', 'Python']\n", 625 | "learning = languages.copy()\n", 626 | "\n", 627 | "print(id(languages), id(learning))" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | "## Using deepcopy" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": {}, 640 | "source": [ 641 | "shallow copy copies each element's address to the new location\n", 642 | "This means that if one of the elements is an object itself, the address it points to copied.\n", 643 | "Now we have two different lists that point to the same object \n", 644 | "Deepcopy goes further by copying any of the objects as well, not just the pointer to them. " 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": 24, 650 | "metadata": {}, 651 | "outputs": [ 652 | { 653 | "name": "stdout", 654 | "output_type": "stream", 655 | "text": [ 656 | "False\n", 657 | "True\n", 658 | "False\n", 659 | "False\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "from copy import deepcopy\n", 665 | "\n", 666 | "tech = ['C++', 'Go', 'Python', ['html', 'css', 'pics']]\n", 667 | "learning = tech.copy()\n", 668 | "print(id(tech) == id(learning))\n", 669 | "print(id(tech[-1]) == id(learning[-1])) #true with shallow copy\n", 670 | "\n", 671 | "learning = deepcopy(tech)\n", 672 | "print(id(tech) == id(learning))\n", 673 | "print(id(tech[-1]) == id(learning[-1]))" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "## Concatenate lists" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 25, 686 | "metadata": {}, 687 | "outputs": [ 688 | { 689 | "name": "stdout", 690 | "output_type": "stream", 691 | "text": [ 692 | "[1, 2, 3, 4, 5, 6]\n" 693 | ] 694 | } 695 | ], 696 | "source": [ 697 | "beginning = [1, 2, 3]\n", 698 | "end = [4, 5, 6]\n", 699 | "print(beginning + end)" 700 | ] 701 | }, 702 | { 703 | "cell_type": "markdown", 704 | "metadata": {}, 705 | "source": [ 706 | "## Comparing not vs is None" 707 | ] 708 | }, 709 | { 710 | "cell_type": "markdown", 711 | "metadata": {}, 712 | "source": [ 713 | "not will check for implicit false values\n", 714 | "which includes empty lists, zero, and False" 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": 26, 720 | "metadata": {}, 721 | "outputs": [ 722 | { 723 | "name": "stdout", 724 | "output_type": "stream", 725 | "text": [ 726 | "True\n", 727 | "False\n", 728 | "True\n", 729 | "True\n" 730 | ] 731 | } 732 | ], 733 | "source": [ 734 | "data1 = []\n", 735 | "data2 = None\n", 736 | "print(not data1)\n", 737 | "print(data1 is None)\n", 738 | "\n", 739 | "print(not data2) # None evaluates to false\n", 740 | "print(data2 is None)\n" 741 | ] 742 | }, 743 | { 744 | "cell_type": "markdown", 745 | "metadata": {}, 746 | "source": [ 747 | "## Check empty list" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 27, 753 | "metadata": {}, 754 | "outputs": [ 755 | { 756 | "name": "stdout", 757 | "output_type": "stream", 758 | "text": [ 759 | "True\n", 760 | "True\n", 761 | "False\n" 762 | ] 763 | } 764 | ], 765 | "source": [ 766 | "data = []\n", 767 | "\n", 768 | "#often seen:\n", 769 | "#print(len(data) == 0): #not the Python way. Throws if None\n", 770 | "\n", 771 | "print(not data) #empty, None, or zero \n", 772 | "\n", 773 | "#You can also check these things:\n", 774 | "print(data is not None) #anything but None\n", 775 | "print(data is None) #None only\n", 776 | " " 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "metadata": {}, 782 | "source": [ 783 | "## Check if exists" 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "execution_count": 28, 789 | "metadata": {}, 790 | "outputs": [ 791 | { 792 | "name": "stdout", 793 | "output_type": "stream", 794 | "text": [ 795 | "True True\n", 796 | "False False\n", 797 | "True True\n" 798 | ] 799 | } 800 | ], 801 | "source": [ 802 | "age = 16\n", 803 | "\n", 804 | "print('age' in locals(), 'age' in globals())\n", 805 | "del age\n", 806 | "print('age' in locals(), 'age' in globals())\n", 807 | "\n", 808 | "age = None\n", 809 | "print('age' in locals(), 'age' in globals())" 810 | ] 811 | }, 812 | { 813 | "cell_type": "markdown", 814 | "metadata": {}, 815 | "source": [ 816 | "## Print end =\" \" to change ending" 817 | ] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "execution_count": 29, 822 | "metadata": {}, 823 | "outputs": [ 824 | { 825 | "name": "stdout", 826 | "output_type": "stream", 827 | "text": [ 828 | "C C++ Java C# Python Go Rust \n" 829 | ] 830 | } 831 | ], 832 | "source": [ 833 | "for language in ['C', 'C++', 'Java', 'C#', 'Python', 'Go', 'Rust']:\n", 834 | " print(language, end=\" \")\n", 835 | "print(\"\") #to go to next line for the next output" 836 | ] 837 | }, 838 | { 839 | "cell_type": "markdown", 840 | "metadata": {}, 841 | "source": [ 842 | "## Print multiple elements with commas " 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": 30, 848 | "metadata": {}, 849 | "outputs": [ 850 | { 851 | "name": "stdout", 852 | "output_type": "stream", 853 | "text": [ 854 | "Caleb 25 ['English', 'C++', 'Python']\n" 855 | ] 856 | } 857 | ], 858 | "source": [ 859 | "name = \"Caleb\"\n", 860 | "age = 25\n", 861 | "favorite_languages = [\"English\", \"C++\", \"Python\"]\n", 862 | "\n", 863 | "print(name, age, favorite_languages)" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "## String formatting for easy string making" 871 | ] 872 | }, 873 | { 874 | "cell_type": "markdown", 875 | "metadata": {}, 876 | "source": [ 877 | "Using commas to separate variables within print is nice and convenient, but what if you want a string variable?\n", 878 | "Using commas actually creates a tuple (not what we want).\n", 879 | "The common solution of using string concatentation is ugly. \n", 880 | "Instead, use f strings!" 881 | ] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "execution_count": 31, 886 | "metadata": {}, 887 | "outputs": [ 888 | { 889 | "name": "stdout", 890 | "output_type": "stream", 891 | "text": [ 892 | "('Caleb', 25)\n", 893 | "My name is Caleb. I am 25 years old.\n", 894 | "My name is Caleb. I am 25 years old.\n" 895 | ] 896 | } 897 | ], 898 | "source": [ 899 | "message = name, age\n", 900 | "print(message) #makes a tuple\n", 901 | "\n", 902 | "print(\"My name is \" + str(name) + \". I am \" + str(age) + \" years old.\")\n", 903 | "\n", 904 | "print(f\"My name is {name}. I am {age} years old.\") #implict string conversion" 905 | ] 906 | }, 907 | { 908 | "cell_type": "markdown", 909 | "metadata": {}, 910 | "source": [ 911 | "## Return multiple values and assign to multiple variables" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 32, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "name": "stdout", 921 | "output_type": "stream", 922 | "text": [ 923 | "(5, 10, 15, 20)\n", 924 | "5 10 15 20\n" 925 | ] 926 | } 927 | ], 928 | "source": [ 929 | "def get_position():\n", 930 | " #get from user or something\n", 931 | " return 5, 10, 15, 20\n", 932 | "\n", 933 | "print(get_position()) \n", 934 | "x, y, z, a = get_position()\n", 935 | "print(x, y, z, a)" 936 | ] 937 | }, 938 | { 939 | "cell_type": "markdown", 940 | "metadata": {}, 941 | "source": [ 942 | "## Ternary conditional operator" 943 | ] 944 | }, 945 | { 946 | "cell_type": "code", 947 | "execution_count": 33, 948 | "metadata": {}, 949 | "outputs": [ 950 | { 951 | "name": "stdout", 952 | "output_type": "stream", 953 | "text": [ 954 | "admin\n", 955 | "visitor\n" 956 | ] 957 | } 958 | ], 959 | "source": [ 960 | "#You'll typically see:\n", 961 | "reputation = 30\n", 962 | "if reputation > 25:\n", 963 | " name = \"admin\"\n", 964 | "else:\n", 965 | " name = \"visitor\"\n", 966 | "print(name)\n", 967 | "\n", 968 | "reputation = 20\n", 969 | "name = \"admin\" if reputation > 25 else \"visitor\"\n", 970 | "print(name)" 971 | ] 972 | }, 973 | { 974 | "cell_type": "markdown", 975 | "metadata": {}, 976 | "source": [ 977 | "## Else can be used with loop" 978 | ] 979 | }, 980 | { 981 | "cell_type": "markdown", 982 | "metadata": {}, 983 | "source": [ 984 | "Option 1 is yes or no\n", 985 | "option 2 uses a flag variable. can also remove break and continue iteration\n", 986 | "option 3 else will execute if no break is found (in other words--No cats)" 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "execution_count": 34, 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "name": "stdout", 996 | "output_type": "stream", 997 | "text": [ 998 | "eww a cat\n", 999 | "dog dog cat eww a cat\n", 1000 | "dog dog bird chicken dog No cat found\n" 1001 | ] 1002 | } 1003 | ], 1004 | "source": [ 1005 | "pets = ['dog', 'dog', 'cat', 'bird', 'chicken', 'dog']\n", 1006 | "\n", 1007 | "if 'cat' in pets: print(\"eww a cat\") #option 1\n", 1008 | " \n", 1009 | "cat_found = False \n", 1010 | "for pet in pets:\n", 1011 | " print(pet, end=\" \") #do something with each element if needed\n", 1012 | " if pet == 'cat':\n", 1013 | " cat_found = True #could count elements\n", 1014 | " break \n", 1015 | "\n", 1016 | "if cat_found: print(\"eww a cat\") #option 2\n", 1017 | " \n", 1018 | "\n", 1019 | "pets.remove('cat')\n", 1020 | "for pet in pets:\n", 1021 | " print(pet, end=\" \")\n", 1022 | " if pet == 'cat':\n", 1023 | " break\n", 1024 | " \n", 1025 | "else: #no break -- option 3\n", 1026 | " print(\"No cat found\")\n" 1027 | ] 1028 | }, 1029 | { 1030 | "cell_type": "markdown", 1031 | "metadata": {}, 1032 | "source": [ 1033 | "## Use range to generate lists" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "markdown", 1038 | "metadata": {}, 1039 | "source": [ 1040 | "type of range is list in python 2. Make sure you're running with Python 3. \n", 1041 | "For me, the python command defaults to Python2 so I use the python3 command" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": 35, 1047 | "metadata": {}, 1048 | "outputs": [ 1049 | { 1050 | "name": "stdout", 1051 | "output_type": "stream", 1052 | "text": [ 1053 | "0 1 2 3 4 5 6 7 8 9 \n", 1054 | "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n" 1055 | ] 1056 | } 1057 | ], 1058 | "source": [ 1059 | "for i in range(10):\n", 1060 | " print(i, end=\" \")\n", 1061 | "print(type(range(10)))\n", 1062 | "\n", 1063 | "print(list(range(0, 100, 3)))" 1064 | ] 1065 | }, 1066 | { 1067 | "cell_type": "markdown", 1068 | "metadata": {}, 1069 | "source": [ 1070 | "## Unpack operator" 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "markdown", 1075 | "metadata": {}, 1076 | "source": [ 1077 | "Given a list you can split it into individual values" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "code", 1082 | "execution_count": 36, 1083 | "metadata": {}, 1084 | "outputs": [ 1085 | { 1086 | "name": "stdout", 1087 | "output_type": "stream", 1088 | "text": [ 1089 | "moving character to 5 10 15\n" 1090 | ] 1091 | } 1092 | ], 1093 | "source": [ 1094 | "def move_position(x, y, z):\n", 1095 | " print(f'moving character to {x} {y} {z}')\n", 1096 | " \n", 1097 | "pos = [5, 10, 15]\n", 1098 | "move_position(*pos) " 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "markdown", 1103 | "metadata": {}, 1104 | "source": [ 1105 | "## How to do nothing in Python with pass\n", 1106 | "Python doesn't have { }, so this is how we do the equiv in Python" 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "code", 1111 | "execution_count": 37, 1112 | "metadata": {}, 1113 | "outputs": [], 1114 | "source": [ 1115 | "def move_position(x, y, z):\n", 1116 | " pass" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "markdown", 1121 | "metadata": {}, 1122 | "source": [ 1123 | "## Remove list duplicates" 1124 | ] 1125 | }, 1126 | { 1127 | "cell_type": "code", 1128 | "execution_count": 38, 1129 | "metadata": {}, 1130 | "outputs": [ 1131 | { 1132 | "name": "stdout", 1133 | "output_type": "stream", 1134 | "text": [ 1135 | "['chicken', 'bird', 'dog', 'cat']\n" 1136 | ] 1137 | } 1138 | ], 1139 | "source": [ 1140 | "pets = ['dog', 'dog', 'cat', 'bird', 'chicken', 'dog']\n", 1141 | "pet_types = list(set(pets))\n", 1142 | "print(pet_types)" 1143 | ] 1144 | }, 1145 | { 1146 | "cell_type": "markdown", 1147 | "metadata": {}, 1148 | "source": [ 1149 | "## Using in instead of complex conditional - Check against list of values" 1150 | ] 1151 | }, 1152 | { 1153 | "cell_type": "code", 1154 | "execution_count": 39, 1155 | "metadata": {}, 1156 | "outputs": [ 1157 | { 1158 | "name": "stdout", 1159 | "output_type": "stream", 1160 | "text": [ 1161 | "cancelled\n" 1162 | ] 1163 | } 1164 | ], 1165 | "source": [ 1166 | "weather = \"rainy\"\n", 1167 | "\n", 1168 | "if weather in ['rainy', 'cold', 'snowy']:\n", 1169 | " print(\"cancelled\")" 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "markdown", 1174 | "metadata": {}, 1175 | "source": [ 1176 | "## complex conditional - Evaluate against any conditon" 1177 | ] 1178 | }, 1179 | { 1180 | "cell_type": "code", 1181 | "execution_count": 40, 1182 | "metadata": {}, 1183 | "outputs": [ 1184 | { 1185 | "name": "stdout", 1186 | "output_type": "stream", 1187 | "text": [ 1188 | "You're an admin\n" 1189 | ] 1190 | } 1191 | ], 1192 | "source": [ 1193 | "age = 21\n", 1194 | "reputation = 20\n", 1195 | "\n", 1196 | "conditions = [ \n", 1197 | " age >= 21,\n", 1198 | " reputation > 25\n", 1199 | "]\n", 1200 | " \n", 1201 | "if any(conditions):\n", 1202 | " print(\"You're an admin\")" 1203 | ] 1204 | }, 1205 | { 1206 | "cell_type": "markdown", 1207 | "metadata": {}, 1208 | "source": [ 1209 | "## complex conditional - Evaluate against all conditions" 1210 | ] 1211 | }, 1212 | { 1213 | "cell_type": "code", 1214 | "execution_count": 41, 1215 | "metadata": {}, 1216 | "outputs": [ 1217 | { 1218 | "name": "stdout", 1219 | "output_type": "stream", 1220 | "text": [ 1221 | "You're a standard user\n" 1222 | ] 1223 | } 1224 | ], 1225 | "source": [ 1226 | "age = 21\n", 1227 | "reputation = 20\n", 1228 | "\n", 1229 | "conditions = [ \n", 1230 | " age >= 21,\n", 1231 | " reputation > 25\n", 1232 | "]\n", 1233 | " \n", 1234 | "if all(conditions):\n", 1235 | " print(\"You're an admin\")\n", 1236 | "else:\n", 1237 | " print(\"You're a standard user\")" 1238 | ] 1239 | }, 1240 | { 1241 | "cell_type": "markdown", 1242 | "metadata": {}, 1243 | "source": [ 1244 | "## Split from string to multiple variables" 1245 | ] 1246 | }, 1247 | { 1248 | "cell_type": "code", 1249 | "execution_count": 42, 1250 | "metadata": {}, 1251 | "outputs": [ 1252 | { 1253 | "name": "stdout", 1254 | "output_type": "stream", 1255 | "text": [ 1256 | "Caleb Curry\n", 1257 | "[1, 2, 3]\n" 1258 | ] 1259 | } 1260 | ], 1261 | "source": [ 1262 | "full_name = \"Caleb Curry\"\n", 1263 | "first, last = full_name.split() #returns list but can assign to individual variables\n", 1264 | "print(first, last)\n", 1265 | "\n", 1266 | "data = \"1 2 3\"\n", 1267 | "data = [int(d) for d in data.split()]\n", 1268 | "print(data)\n", 1269 | "\n", 1270 | "#This is most useful to get multiple inputs separated by spaces:\n", 1271 | "#first, last = input().split()" 1272 | ] 1273 | }, 1274 | { 1275 | "cell_type": "markdown", 1276 | "metadata": {}, 1277 | "source": [ 1278 | "## Join data " 1279 | ] 1280 | }, 1281 | { 1282 | "cell_type": "code", 1283 | "execution_count": 43, 1284 | "metadata": {}, 1285 | "outputs": [ 1286 | { 1287 | "name": "stdout", 1288 | "output_type": "stream", 1289 | "text": [ 1290 | "Caleb Curry\n" 1291 | ] 1292 | } 1293 | ], 1294 | "source": [ 1295 | "names = [\"Caleb\", \"Curry\"]\n", 1296 | "full_name = \" \".join(names)\n", 1297 | "print(full_name)\n" 1298 | ] 1299 | }, 1300 | { 1301 | "cell_type": "markdown", 1302 | "metadata": {}, 1303 | "source": [ 1304 | "## Removing certain elements from a list" 1305 | ] 1306 | }, 1307 | { 1308 | "cell_type": "code", 1309 | "execution_count": 44, 1310 | "metadata": {}, 1311 | "outputs": [ 1312 | { 1313 | "name": "stdout", 1314 | "output_type": "stream", 1315 | "text": [ 1316 | "['dog', 'dog', 'bird', 'dog']\n" 1317 | ] 1318 | } 1319 | ], 1320 | "source": [ 1321 | "pets = ['dog', 'dog', 'cat', 'bird', 'cat','chicken', 'cat', 'dog']\n", 1322 | "\n", 1323 | "clean_pets = [pet for pet in pets if pet not in['cat','chicken']]\n", 1324 | "print(clean_pets)" 1325 | ] 1326 | }, 1327 | { 1328 | "cell_type": "markdown", 1329 | "metadata": {}, 1330 | "source": [ 1331 | "## Iterate backwards with reversed" 1332 | ] 1333 | }, 1334 | { 1335 | "cell_type": "code", 1336 | "execution_count": 45, 1337 | "metadata": {}, 1338 | "outputs": [ 1339 | { 1340 | "name": "stdout", 1341 | "output_type": "stream", 1342 | "text": [ 1343 | "dog cat chicken cat bird cat dog dog " 1344 | ] 1345 | } 1346 | ], 1347 | "source": [ 1348 | "pets = ['dog', 'dog', 'cat', 'bird', 'cat','chicken', 'cat', 'dog']\n", 1349 | "for pet in reversed(pets):\n", 1350 | " print(pet, end=\" \")" 1351 | ] 1352 | }, 1353 | { 1354 | "cell_type": "markdown", 1355 | "metadata": {}, 1356 | "source": [ 1357 | "## No do-while loop in python" 1358 | ] 1359 | }, 1360 | { 1361 | "cell_type": "code", 1362 | "execution_count": 46, 1363 | "metadata": {}, 1364 | "outputs": [ 1365 | { 1366 | "name": "stdout", 1367 | "output_type": "stream", 1368 | "text": [ 1369 | "Choose an option:\n", 1370 | "1. play game\n", 1371 | "2. load game\n", 1372 | "3. high scores\n", 1373 | "4. quit\n" 1374 | ] 1375 | } 1376 | ], 1377 | "source": [ 1378 | "while True:\n", 1379 | " print(\"\"\"Choose an option:\n", 1380 | "1. play game\n", 1381 | "2. load game\n", 1382 | "3. high scores\n", 1383 | "4. quit\"\"\")\n", 1384 | " #if input() == \"4\":\n", 1385 | " if True: \n", 1386 | " break\n", 1387 | " \n", 1388 | " " 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "markdown", 1393 | "metadata": {}, 1394 | "source": [ 1395 | "## Assigning a function to a variable - just don’t use ()" 1396 | ] 1397 | }, 1398 | { 1399 | "cell_type": "code", 1400 | "execution_count": 47, 1401 | "metadata": {}, 1402 | "outputs": [ 1403 | { 1404 | "name": "stdout", 1405 | "output_type": "stream", 1406 | "text": [ 1407 | "Doing things....\n" 1408 | ] 1409 | } 1410 | ], 1411 | "source": [ 1412 | "def done():\n", 1413 | " print(\"done\")\n", 1414 | " \n", 1415 | "def do_something(callback):\n", 1416 | " print(\"Doing things....\")\n", 1417 | " \n", 1418 | "do_something(done)" 1419 | ] 1420 | }, 1421 | { 1422 | "cell_type": "markdown", 1423 | "metadata": {}, 1424 | "source": [ 1425 | "## Wait with time.sleep" 1426 | ] 1427 | }, 1428 | { 1429 | "cell_type": "code", 1430 | "execution_count": 48, 1431 | "metadata": {}, 1432 | "outputs": [ 1433 | { 1434 | "name": "stdout", 1435 | "output_type": "stream", 1436 | "text": [ 1437 | "Doing things....\n" 1438 | ] 1439 | } 1440 | ], 1441 | "source": [ 1442 | "import time\n", 1443 | "def done():\n", 1444 | " print(\"done\")\n", 1445 | " \n", 1446 | "def do_something(callback):\n", 1447 | " time.sleep(1)\n", 1448 | " print(\"Doing things....\")\n", 1449 | " \n", 1450 | "do_something(done)" 1451 | ] 1452 | }, 1453 | { 1454 | "cell_type": "markdown", 1455 | "metadata": {}, 1456 | "source": [ 1457 | "## Get index with for loop" 1458 | ] 1459 | }, 1460 | { 1461 | "cell_type": "code", 1462 | "execution_count": 49, 1463 | "metadata": {}, 1464 | "outputs": [ 1465 | { 1466 | "name": "stdout", 1467 | "output_type": "stream", 1468 | "text": [ 1469 | "0 dog dog\n", 1470 | "1 dog dog\n", 1471 | "2 cat dog\n", 1472 | "3 bird cat\n", 1473 | "4 cat bird\n", 1474 | "5 chicken cat\n", 1475 | "6 cat chicken\n", 1476 | "7 dog cat\n" 1477 | ] 1478 | } 1479 | ], 1480 | "source": [ 1481 | "pets = ['dog', 'dog', 'cat', 'bird', 'cat','chicken', 'cat', 'dog']\n", 1482 | "\n", 1483 | "for i, pet in enumerate(pets):\n", 1484 | " print(i, pet, pets[i-1]) #can use index in statements now" 1485 | ] 1486 | }, 1487 | { 1488 | "cell_type": "markdown", 1489 | "metadata": {}, 1490 | "source": [ 1491 | "## Flatten a list with nested list comprehension" 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "code", 1496 | "execution_count": 50, 1497 | "metadata": {}, 1498 | "outputs": [ 1499 | { 1500 | "name": "stdout", 1501 | "output_type": "stream", 1502 | "text": [ 1503 | "[5, 10, 15, 20, 25, 30, 35, 40]\n" 1504 | ] 1505 | } 1506 | ], 1507 | "source": [ 1508 | "pairs = [[5, 10],[15, 20],[25, 30, 35, 40]]\n", 1509 | "\n", 1510 | "flat = []\n", 1511 | "for pair in pairs:\n", 1512 | " for item in pair:\n", 1513 | " flat.append(item)\n", 1514 | "\n", 1515 | "print(flat)\n", 1516 | "\n", 1517 | "flat_list = [item for pair in pairs for item in pair]\n", 1518 | "#notice important sections: \n", 1519 | "#for pair in pairs\n", 1520 | "#for item in pair" 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "markdown", 1525 | "metadata": {}, 1526 | "source": [ 1527 | "## Wrapping a Primitive to change within function\n", 1528 | "You can also use a list for simplicity" 1529 | ] 1530 | }, 1531 | { 1532 | "cell_type": "code", 1533 | "execution_count": 51, 1534 | "metadata": {}, 1535 | "outputs": [ 1536 | { 1537 | "name": "stdout", 1538 | "output_type": "stream", 1539 | "text": [ 1540 | "3125\n" 1541 | ] 1542 | } 1543 | ], 1544 | "source": [ 1545 | "class Container:\n", 1546 | " def __init__(self, data):\n", 1547 | " self.data = data\n", 1548 | " \n", 1549 | "def calculate(input):\n", 1550 | " input.data **= 5\n", 1551 | " \n", 1552 | "container = Container(5)\n", 1553 | "calculate(container)\n", 1554 | "print(container.data)" 1555 | ] 1556 | }, 1557 | { 1558 | "cell_type": "markdown", 1559 | "metadata": {}, 1560 | "source": [ 1561 | "## Compare identity with is" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": 52, 1567 | "metadata": {}, 1568 | "outputs": [ 1569 | { 1570 | "name": "stdout", 1571 | "output_type": "stream", 1572 | "text": [ 1573 | "140479253959632 140479253959504\n", 1574 | "False\n", 1575 | "False\n" 1576 | ] 1577 | } 1578 | ], 1579 | "source": [ 1580 | "c1 = Container(5)\n", 1581 | "c2 = Container(5)\n", 1582 | "print(id(c1), id(c2))\n", 1583 | "print(id(c1) == id(c2)) \n", 1584 | "\n", 1585 | "print(c1 is c2) #better code" 1586 | ] 1587 | }, 1588 | { 1589 | "cell_type": "markdown", 1590 | "metadata": {}, 1591 | "source": [ 1592 | "## Add a method dynamically" 1593 | ] 1594 | }, 1595 | { 1596 | "cell_type": "code", 1597 | "execution_count": 53, 1598 | "metadata": {}, 1599 | "outputs": [ 1600 | { 1601 | "name": "stdout", 1602 | "output_type": "stream", 1603 | "text": [ 1604 | "\n" 1605 | ] 1606 | } 1607 | ], 1608 | "source": [ 1609 | "def __eq__(self, other):\n", 1610 | " return self.data == other.data\n", 1611 | "\n", 1612 | "Container.__eq__ = __eq__\n", 1613 | "\n", 1614 | "print(Container.__eq__)" 1615 | ] 1616 | }, 1617 | { 1618 | "cell_type": "markdown", 1619 | "metadata": {}, 1620 | "source": [ 1621 | "## Compare by Value" 1622 | ] 1623 | }, 1624 | { 1625 | "cell_type": "code", 1626 | "execution_count": 54, 1627 | "metadata": {}, 1628 | "outputs": [ 1629 | { 1630 | "name": "stdout", 1631 | "output_type": "stream", 1632 | "text": [ 1633 | "True\n", 1634 | "False\n" 1635 | ] 1636 | } 1637 | ], 1638 | "source": [ 1639 | "c1 = Container(5)\n", 1640 | "c2 = Container(5)\n", 1641 | "\n", 1642 | "print(c1 == c2) #same values\n", 1643 | "print(c1 is c2) #different objects" 1644 | ] 1645 | }, 1646 | { 1647 | "cell_type": "markdown", 1648 | "metadata": {}, 1649 | "source": [ 1650 | "## Class parenthesis optional but not function parenthesis\n", 1651 | "The () are optional for classes. Only needed if you're inheriting. However, function () are always required when defining." 1652 | ] 1653 | }, 1654 | { 1655 | "cell_type": "code", 1656 | "execution_count": 55, 1657 | "metadata": {}, 1658 | "outputs": [], 1659 | "source": [ 1660 | "class Container():\n", 1661 | " def __init__(self, data):\n", 1662 | " self.data = data\n", 1663 | "\n", 1664 | "class Container: \n", 1665 | " def __init__(self, data):\n", 1666 | " self.data = data\n", 1667 | "\n", 1668 | "#def function:\n", 1669 | "# print(\"oopsies\")" 1670 | ] 1671 | }, 1672 | { 1673 | "cell_type": "markdown", 1674 | "metadata": {}, 1675 | "source": [ 1676 | "## Get current date and time" 1677 | ] 1678 | }, 1679 | { 1680 | "cell_type": "code", 1681 | "execution_count": 56, 1682 | "metadata": {}, 1683 | "outputs": [ 1684 | { 1685 | "name": "stdout", 1686 | "output_type": "stream", 1687 | "text": [ 1688 | "2020-12-03 14:08:16.251001\n", 1689 | "3 12 2020 14 8 16\n" 1690 | ] 1691 | } 1692 | ], 1693 | "source": [ 1694 | "from datetime import datetime\n", 1695 | "now = datetime.now()\n", 1696 | "print(now)\n", 1697 | "print(now.day, now.month, now.year, now.hour, now.minute, now.second)" 1698 | ] 1699 | }, 1700 | { 1701 | "cell_type": "markdown", 1702 | "metadata": {}, 1703 | "source": [ 1704 | "## Countdown\n", 1705 | "You can do this in a loop if you need." 1706 | ] 1707 | }, 1708 | { 1709 | "cell_type": "code", 1710 | "execution_count": 57, 1711 | "metadata": {}, 1712 | "outputs": [ 1713 | { 1714 | "name": "stdout", 1715 | "output_type": "stream", 1716 | "text": [ 1717 | "-3 days, 9:51:43.745464\n" 1718 | ] 1719 | } 1720 | ], 1721 | "source": [ 1722 | "from datetime import datetime\n", 1723 | "now = datetime.now()\n", 1724 | "end = datetime(2020, 12, 1)\n", 1725 | "print(end-now) " 1726 | ] 1727 | }, 1728 | { 1729 | "cell_type": "markdown", 1730 | "metadata": {}, 1731 | "source": [ 1732 | "## Elapsed time" 1733 | ] 1734 | }, 1735 | { 1736 | "cell_type": "code", 1737 | "execution_count": 58, 1738 | "metadata": {}, 1739 | "outputs": [ 1740 | { 1741 | "name": "stdout", 1742 | "output_type": "stream", 1743 | "text": [ 1744 | "\n", 1745 | "0:00:02.298135\n", 1746 | "2 298135\n" 1747 | ] 1748 | } 1749 | ], 1750 | "source": [ 1751 | "from datetime import datetime\n", 1752 | "start = datetime.now()\n", 1753 | "\n", 1754 | "for i in range(100_000_000): #to pass time\n", 1755 | " pass\n", 1756 | "\n", 1757 | "end = datetime.now()\n", 1758 | "\n", 1759 | "print(type(end-start))\n", 1760 | "elapsed = end-start\n", 1761 | "print(elapsed)\n", 1762 | "print(elapsed.seconds, elapsed.microseconds)" 1763 | ] 1764 | }, 1765 | { 1766 | "cell_type": "markdown", 1767 | "metadata": {}, 1768 | "source": [ 1769 | "## Parentheses for operations with object members\n", 1770 | "operator precedence - dot operator comes ahead of +/- operator." 1771 | ] 1772 | }, 1773 | { 1774 | "cell_type": "code", 1775 | "execution_count": 59, 1776 | "metadata": {}, 1777 | "outputs": [ 1778 | { 1779 | "name": "stdout", 1780 | "output_type": "stream", 1781 | "text": [ 1782 | "16\n", 1783 | "Wrong\n" 1784 | ] 1785 | } 1786 | ], 1787 | "source": [ 1788 | "now = datetime.now()\n", 1789 | "then = datetime.now()\n", 1790 | "\n", 1791 | "elapsed = (then-now).microseconds\n", 1792 | "print(elapsed)\n", 1793 | "\n", 1794 | "try:\n", 1795 | " print(then-now.microseconds)\n", 1796 | "except:\n", 1797 | " print(\"Wrong\")" 1798 | ] 1799 | }, 1800 | { 1801 | "cell_type": "markdown", 1802 | "metadata": {}, 1803 | "source": [ 1804 | "## Runtime error vs syntax error\n", 1805 | "Syntax errors are impossible to be correct and will prevent execution \n", 1806 | "Runtime errors deal with incorrect data found during runtime" 1807 | ] 1808 | }, 1809 | { 1810 | "cell_type": "code", 1811 | "execution_count": 60, 1812 | "metadata": {}, 1813 | "outputs": [ 1814 | { 1815 | "name": "stdout", 1816 | "output_type": "stream", 1817 | "text": [ 1818 | "Wrong\n" 1819 | ] 1820 | } 1821 | ], 1822 | "source": [ 1823 | "#def function:\n", 1824 | "# print('wrong syntax')\n", 1825 | " \n", 1826 | "try:\n", 1827 | " print(then-now.microseconds)\n", 1828 | "except:\n", 1829 | " print(\"Wrong\") \n", 1830 | "\n" 1831 | ] 1832 | }, 1833 | { 1834 | "cell_type": "markdown", 1835 | "metadata": {}, 1836 | "source": [ 1837 | "## Get a random number" 1838 | ] 1839 | }, 1840 | { 1841 | "cell_type": "code", 1842 | "execution_count": 61, 1843 | "metadata": {}, 1844 | "outputs": [ 1845 | { 1846 | "name": "stdout", 1847 | "output_type": "stream", 1848 | "text": [ 1849 | "4\n" 1850 | ] 1851 | } 1852 | ], 1853 | "source": [ 1854 | "from random import randint\n", 1855 | "print(randint(0, 12)) #inclusive #inclusive" 1856 | ] 1857 | }, 1858 | { 1859 | "cell_type": "markdown", 1860 | "metadata": {}, 1861 | "source": [ 1862 | "## Import with Alias\n", 1863 | "This capability came in handy for me when I needed a module \n", 1864 | "to be a specific name for my code to work" 1865 | ] 1866 | }, 1867 | { 1868 | "cell_type": "code", 1869 | "execution_count": 62, 1870 | "metadata": {}, 1871 | "outputs": [ 1872 | { 1873 | "name": "stdout", 1874 | "output_type": "stream", 1875 | "text": [ 1876 | "52 1\n" 1877 | ] 1878 | } 1879 | ], 1880 | "source": [ 1881 | "def randint():\n", 1882 | " return 52\n", 1883 | " \n", 1884 | "from random import randint as r\n", 1885 | " \n", 1886 | "print(randint(), r(0, 100))" 1887 | ] 1888 | }, 1889 | { 1890 | "cell_type": "markdown", 1891 | "metadata": {}, 1892 | "source": [ 1893 | "## Generators and Yield" 1894 | ] 1895 | }, 1896 | { 1897 | "cell_type": "code", 1898 | "execution_count": 63, 1899 | "metadata": {}, 1900 | "outputs": [ 1901 | { 1902 | "name": "stdout", 1903 | "output_type": "stream", 1904 | "text": [ 1905 | "0 1 1 2 3\n", 1906 | "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 " 1907 | ] 1908 | } 1909 | ], 1910 | "source": [ 1911 | "def fib(count):\n", 1912 | " a, b = 0, 1\n", 1913 | " while count:\n", 1914 | " yield a\n", 1915 | " a, b, = b, b + a\n", 1916 | " count -= 1\n", 1917 | "\n", 1918 | "\n", 1919 | "gen = fib(100)\n", 1920 | "print(next(gen), next(gen), next(gen), next(gen), next(gen))\n", 1921 | "\n", 1922 | "for i in fib(20):\n", 1923 | " print(i, end=\" \")\n" 1924 | ] 1925 | }, 1926 | { 1927 | "cell_type": "markdown", 1928 | "metadata": {}, 1929 | "source": [ 1930 | "## infinite number" 1931 | ] 1932 | }, 1933 | { 1934 | "cell_type": "code", 1935 | "execution_count": 64, 1936 | "metadata": {}, 1937 | "outputs": [ 1938 | { 1939 | "name": "stdout", 1940 | "output_type": "stream", 1941 | "text": [ 1942 | "inf\n", 1943 | "inf inf\n" 1944 | ] 1945 | } 1946 | ], 1947 | "source": [ 1948 | "import math\n", 1949 | "#inf = (float('inf')) #other way without math.\n", 1950 | "print(math.inf)\n", 1951 | "\n", 1952 | "inf = math.inf\n", 1953 | "print(inf, inf-1) #always infinity" 1954 | ] 1955 | }, 1956 | { 1957 | "cell_type": "markdown", 1958 | "metadata": {}, 1959 | "source": [ 1960 | "## Infinite generator\n", 1961 | "You could make an infinite loop with a yield or use float('inf')) as the counter for more versatility \n", 1962 | "(You can use a smaller number if you desire a certain number of elements) \n", 1963 | "Bounded generator was used in documentation - https://wiki.python.org/moin/Generators \n", 1964 | "This stopping point allows you to do things like get the sum." 1965 | ] 1966 | }, 1967 | { 1968 | "cell_type": "code", 1969 | "execution_count": 65, 1970 | "metadata": {}, 1971 | "outputs": [ 1972 | { 1973 | "name": "stdout", 1974 | "output_type": "stream", 1975 | "text": [ 1976 | "0 1 1 2 3 5 8 13 21 34 55 89 144 " 1977 | ] 1978 | } 1979 | ], 1980 | "source": [ 1981 | "import math\n", 1982 | "\n", 1983 | "def fib(count):\n", 1984 | " a, b = 0, 1\n", 1985 | " while count:\n", 1986 | " yield a\n", 1987 | " a, b, = b, b + a\n", 1988 | " count -= 1\n", 1989 | "\n", 1990 | "f = fib(math.inf)\n", 1991 | "for i in f:\n", 1992 | " if i >= 200: break\n", 1993 | " print(i, end=\" \")\n", 1994 | " i += 1" 1995 | ] 1996 | }, 1997 | { 1998 | "cell_type": "markdown", 1999 | "metadata": {}, 2000 | "source": [ 2001 | "## list from generator" 2002 | ] 2003 | }, 2004 | { 2005 | "cell_type": "code", 2006 | "execution_count": 66, 2007 | "metadata": {}, 2008 | "outputs": [ 2009 | { 2010 | "name": "stdout", 2011 | "output_type": "stream", 2012 | "text": [ 2013 | "[0.0, 1.0, 1.0, 1.414, 1.732, 2.236, 2.828, 3.606, 4.583, 5.831]\n" 2014 | ] 2015 | } 2016 | ], 2017 | "source": [ 2018 | "import math\n", 2019 | "\n", 2020 | "def fib(count):\n", 2021 | " a, b = 0, 1\n", 2022 | " while count:\n", 2023 | " yield a\n", 2024 | " a, b, = b, b + a\n", 2025 | " count -= 1\n", 2026 | " \n", 2027 | "f = fib(10)\n", 2028 | "data = [round(math.sqrt(i), 3) for i in f]\n", 2029 | "print(data)" 2030 | ] 2031 | }, 2032 | { 2033 | "cell_type": "markdown", 2034 | "metadata": {}, 2035 | "source": [ 2036 | "## itertools for simple infinite generator\n", 2037 | "The generator function could be simpler without having to take a max count property \n", 2038 | "This can be done easily with itertools. " 2039 | ] 2040 | }, 2041 | { 2042 | "cell_type": "code", 2043 | "execution_count": 67, 2044 | "metadata": {}, 2045 | "outputs": [ 2046 | { 2047 | "name": "stdout", 2048 | "output_type": "stream", 2049 | "text": [ 2050 | "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]\n" 2051 | ] 2052 | } 2053 | ], 2054 | "source": [ 2055 | "import itertools\n", 2056 | "\n", 2057 | "def fib():\n", 2058 | " a, b = 0, 1\n", 2059 | " while True:\n", 2060 | " yield a\n", 2061 | " a, b, = b, b + a\n", 2062 | " \n", 2063 | "print(list(itertools.islice(fib(), 20)))" 2064 | ] 2065 | }, 2066 | { 2067 | "cell_type": "markdown", 2068 | "metadata": {}, 2069 | "source": [ 2070 | "## Iterate through custom type with __iter__" 2071 | ] 2072 | }, 2073 | { 2074 | "cell_type": "code", 2075 | "execution_count": 68, 2076 | "metadata": {}, 2077 | "outputs": [ 2078 | { 2079 | "name": "stdout", 2080 | "output_type": "stream", 2081 | "text": [ 2082 | "5\n", 2083 | "10\n", 2084 | "15\n", 2085 | "20\n" 2086 | ] 2087 | } 2088 | ], 2089 | "source": [ 2090 | "class Node:\n", 2091 | " def __init__(self, data, next_node=None):\n", 2092 | " self.data = data\n", 2093 | " self.next = next_node\n", 2094 | "\n", 2095 | "class LinkedList:\n", 2096 | "\n", 2097 | " def __init__(self, start):\n", 2098 | " self.start = start\n", 2099 | " \n", 2100 | " def __iter__(self):\n", 2101 | " node = self.start\n", 2102 | " while node:\n", 2103 | " yield node\n", 2104 | " node = node.next\n", 2105 | " \n", 2106 | "ll = LinkedList(Node(5, Node(10, Node(15, Node(20)))))\n", 2107 | "for node in ll:\n", 2108 | " print(node.data)" 2109 | ] 2110 | }, 2111 | { 2112 | "cell_type": "markdown", 2113 | "metadata": {}, 2114 | "source": [ 2115 | "## Falsey for custom types (not)" 2116 | ] 2117 | }, 2118 | { 2119 | "cell_type": "code", 2120 | "execution_count": 69, 2121 | "metadata": {}, 2122 | "outputs": [ 2123 | { 2124 | "name": "stdout", 2125 | "output_type": "stream", 2126 | "text": [ 2127 | "True\n", 2128 | "True\n", 2129 | "4\n", 2130 | "True\n" 2131 | ] 2132 | } 2133 | ], 2134 | "source": [ 2135 | "print(not []) #true because it's empty\n", 2136 | "print(len([]) == 0) #This is how not is evaluated\n", 2137 | "\n", 2138 | "def __len__(self):\n", 2139 | " count = 0\n", 2140 | " for i in self:\n", 2141 | " count += 1\n", 2142 | " return count\n", 2143 | " \n", 2144 | "\n", 2145 | "LinkedList.__len__ = __len__\n", 2146 | " \n", 2147 | " \n", 2148 | "ll = LinkedList(Node(5, Node(10, Node(15, Node(20)))))\n", 2149 | "print(len(ll))\n", 2150 | "ll = LinkedList(None)\n", 2151 | "print(not ll)" 2152 | ] 2153 | }, 2154 | { 2155 | "cell_type": "markdown", 2156 | "metadata": {}, 2157 | "source": [ 2158 | "## Combine two lists as a list of lists. " 2159 | ] 2160 | }, 2161 | { 2162 | "cell_type": "code", 2163 | "execution_count": 70, 2164 | "metadata": {}, 2165 | "outputs": [ 2166 | { 2167 | "name": "stdout", 2168 | "output_type": "stream", 2169 | "text": [ 2170 | "[('Caleb', 100), ('Corey', 250), ('Chris', 30), ('Samantha', 600)]\n" 2171 | ] 2172 | } 2173 | ], 2174 | "source": [ 2175 | "names = ['Caleb', 'Corey', 'Chris', 'Samantha']\n", 2176 | "points = [100, 250, 30, 600]\n", 2177 | "\n", 2178 | "zipped = list(zip(names, points))\n", 2179 | "\n", 2180 | "print(zipped)" 2181 | ] 2182 | }, 2183 | { 2184 | "cell_type": "markdown", 2185 | "metadata": {}, 2186 | "source": [ 2187 | "## Convert list of tuples to list of lists" 2188 | ] 2189 | }, 2190 | { 2191 | "cell_type": "code", 2192 | "execution_count": 71, 2193 | "metadata": {}, 2194 | "outputs": [ 2195 | { 2196 | "name": "stdout", 2197 | "output_type": "stream", 2198 | "text": [ 2199 | "[['Caleb', 100], ['Corey', 250], ['Chris', 30], ['Samantha', 600]]\n" 2200 | ] 2201 | } 2202 | ], 2203 | "source": [ 2204 | "names = ['Caleb', 'Corey', 'Chris', 'Samantha']\n", 2205 | "points = [100, 250, 30, 600]\n", 2206 | "\n", 2207 | "zipped = list(zip(names, points))\n", 2208 | "\n", 2209 | "data = [list(item) for item in zipped]\n", 2210 | "print(data)" 2211 | ] 2212 | }, 2213 | { 2214 | "cell_type": "markdown", 2215 | "metadata": {}, 2216 | "source": [ 2217 | "## Preserving all data when zipping with zip_longest" 2218 | ] 2219 | }, 2220 | { 2221 | "cell_type": "code", 2222 | "execution_count": 72, 2223 | "metadata": {}, 2224 | "outputs": [ 2225 | { 2226 | "name": "stdout", 2227 | "output_type": "stream", 2228 | "text": [ 2229 | "[('Caleb', 100), ('Corey', 250), ('Chris', 30), ('Samantha', 600)]\n", 2230 | "[('Caleb', 100), ('Corey', 250), ('Chris', 30), ('Samantha', 600), ('Hannah', None), ('Kelly', None)]\n" 2231 | ] 2232 | } 2233 | ], 2234 | "source": [ 2235 | "names = ['Caleb', 'Corey', 'Chris', 'Samantha', \"Hannah\", \"Kelly\"]\n", 2236 | "points = [100, 250, 30, 600]\n", 2237 | "zipped = list(zip(names, points))\n", 2238 | "print(zipped)\n", 2239 | "\n", 2240 | "from itertools import zip_longest\n", 2241 | "names = ['Caleb', 'Corey', 'Chris', 'Samantha', \"Hannah\", \"Kelly\"]\n", 2242 | "points = [100, 250, 30, 600]\n", 2243 | "\n", 2244 | "zipped = list(zip_longest(names, points))\n", 2245 | "\n", 2246 | "print(zipped)" 2247 | ] 2248 | }, 2249 | { 2250 | "cell_type": "markdown", 2251 | "metadata": {}, 2252 | "source": [ 2253 | "## Default Arguments" 2254 | ] 2255 | }, 2256 | { 2257 | "cell_type": "code", 2258 | "execution_count": 73, 2259 | "metadata": {}, 2260 | "outputs": [ 2261 | { 2262 | "name": "stdout", 2263 | "output_type": "stream", 2264 | "text": [ 2265 | "[['Caleb', 100], ['Corey', 250], ['Chris', 30], ['Samantha', 600], ['Hannah', None], ['Kelly', None]]\n" 2266 | ] 2267 | } 2268 | ], 2269 | "source": [ 2270 | "from itertools import zip_longest\n", 2271 | "\n", 2272 | "\n", 2273 | "def zip_lists(list1=[], list2=[], longest=True):\n", 2274 | " if longest:\n", 2275 | " return [list(item) for item in zip_longest(list1, list2)]\n", 2276 | " else:\n", 2277 | " return [list(item) for item in zip(list1, list2)]\n", 2278 | " \n", 2279 | "names = ['Caleb', 'Corey', 'Chris', 'Samantha', \"Hannah\", \"Kelly\"]\n", 2280 | "points = [100, 250, 30, 600]\n", 2281 | "\n", 2282 | "print(zip_lists(names, points))" 2283 | ] 2284 | }, 2285 | { 2286 | "cell_type": "markdown", 2287 | "metadata": {}, 2288 | "source": [ 2289 | "## Keyword Arguments\n", 2290 | "\n", 2291 | "When you have default values, you can pass arugments by name\n", 2292 | "positional arguments must remain on the left\n", 2293 | "You can pass named arguments in any order and can skip them even." 2294 | ] 2295 | }, 2296 | { 2297 | "cell_type": "code", 2298 | "execution_count": 74, 2299 | "metadata": {}, 2300 | "outputs": [ 2301 | { 2302 | "name": "stdout", 2303 | "output_type": "stream", 2304 | "text": [ 2305 | "[[None, 'Caleb']]\n" 2306 | ] 2307 | } 2308 | ], 2309 | "source": [ 2310 | "from itertools import zip_longest\n", 2311 | "\n", 2312 | "\n", 2313 | "def zip_lists(list1=[], list2=[], longest=True):\n", 2314 | " if longest:\n", 2315 | " return [list(item) for item in zip_longest(list1, list2)]\n", 2316 | " else:\n", 2317 | " return [list(item) for item in zip(list1, list2)]\n", 2318 | "\n", 2319 | "\n", 2320 | "print(zip_lists(longest=True, list2=['Caleb']))" 2321 | ] 2322 | }, 2323 | { 2324 | "cell_type": "markdown", 2325 | "metadata": {}, 2326 | "source": [ 2327 | "## Get the Python version" 2328 | ] 2329 | }, 2330 | { 2331 | "cell_type": "code", 2332 | "execution_count": 75, 2333 | "metadata": {}, 2334 | "outputs": [ 2335 | { 2336 | "name": "stdout", 2337 | "output_type": "stream", 2338 | "text": [ 2339 | "3.7.8\n" 2340 | ] 2341 | } 2342 | ], 2343 | "source": [ 2344 | "from platform import python_version\n", 2345 | "\n", 2346 | "print(python_version())" 2347 | ] 2348 | } 2349 | ], 2350 | "metadata": { 2351 | "kernelspec": { 2352 | "display_name": "Python 3", 2353 | "language": "python", 2354 | "name": "python3" 2355 | }, 2356 | "language_info": { 2357 | "codemirror_mode": { 2358 | "name": "ipython", 2359 | "version": 3 2360 | }, 2361 | "file_extension": ".py", 2362 | "mimetype": "text/x-python", 2363 | "name": "python", 2364 | "nbconvert_exporter": "python", 2365 | "pygments_lexer": "ipython3", 2366 | "version": "3.7.8" 2367 | } 2368 | }, 2369 | "nbformat": 4, 2370 | "nbformat_minor": 4 2371 | } 2372 | --------------------------------------------------------------------------------