├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── .nojekyll ├── README.md ├── content ├── examples │ ├── array │ ├── ex_01_lists.ipynb │ ├── ex_02_tuple.ipynb │ ├── ex_03_dict.ipynb │ ├── ex_04_set.ipynb │ ├── ex_05_list_comprehensions.ipynb │ ├── ex_06_dict_comprehensions.ipynb │ ├── ex_07_set_comprehensions.ipynb │ ├── ex_08_generators.ipynb │ ├── ex_09_array.ipynb │ ├── ex_10_namedtuple.ipynb │ ├── ex_11_ordered_dict.ipynb │ ├── ex_12_default_dict.ipynb │ ├── ex_13_chain_map.ipynb │ ├── ex_14_deque.ipynb │ ├── ex_15_counter.ipynb │ ├── ex_16_custom_classes.ipynb │ ├── ex_17_numpy.ipynb │ ├── ex_18_numpy_example.ipynb │ ├── ex_19_numpy_example_timed.ipynb │ ├── ex_20_pandas_series.ipynb │ ├── ex_21_pandas_dataframe.ipynb │ ├── languages.csv │ └── numpy_file.npy └── project │ ├── data │ ├── Survey-2024-Jan.csv │ ├── Survey-2024-Jul.csv │ ├── Survey-2024-Mar.csv │ └── Survey-2024-May.csv │ ├── sample_solutions │ ├── proj_1_builtin.ipynb │ ├── proj_2_comprehensions.ipynb │ ├── proj_3_collections.ipynb │ └── proj_4_pandas.ipynb │ └── survey_analysis.ipynb ├── repl └── jupyter-lite.json └── requirements.txt /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Build and Deploy 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - '*' 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v3 17 | - name: Setup Python 18 | uses: actions/setup-python@v4 19 | with: 20 | python-version: '3.10' 21 | - name: Install the dependencies 22 | run: | 23 | python -m pip install -r requirements.txt 24 | - name: Build the JupyterLite site 25 | run: | 26 | cp README.md content 27 | jupyter lite build --contents content --output-dir dist 28 | - name: Upload artifact 29 | uses: actions/upload-pages-artifact@v1 30 | with: 31 | path: ./dist 32 | 33 | deploy: 34 | needs: build 35 | if: github.ref == 'refs/heads/main' 36 | permissions: 37 | pages: write 38 | id-token: write 39 | 40 | environment: 41 | name: github-pages 42 | url: ${{ steps.deployment.outputs.page_url }} 43 | 44 | runs-on: ubuntu-latest 45 | steps: 46 | - name: Deploy to GitHub Pages 47 | id: deployment 48 | uses: actions/deploy-pages@v1 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bundle.* 2 | lib/ 3 | node_modules/ 4 | .yarn-packages/ 5 | *.egg-info/ 6 | .ipynb_checkpoints 7 | *.tsbuildinfo 8 | 9 | # IDE settings 10 | .idea/ 11 | .vscode/ 12 | 13 | # Created by https://www.gitignore.io/api/python 14 | # Edit at https://www.gitignore.io/?templates=python 15 | 16 | ### Python ### 17 | # Byte-compiled / optimized / DLL files 18 | __pycache__/ 19 | *.py[cod] 20 | *$py.class 21 | 22 | # C extensions 23 | *.so 24 | 25 | # Distribution / packaging 26 | .Python 27 | build/ 28 | develop-eggs/ 29 | dist/ 30 | downloads/ 31 | eggs/ 32 | .eggs/ 33 | lib/ 34 | lib64/ 35 | parts/ 36 | sdist/ 37 | var/ 38 | wheels/ 39 | pip-wheel-metadata/ 40 | share/python-wheels/ 41 | .installed.cfg 42 | *.egg 43 | MANIFEST 44 | 45 | # PyInstaller 46 | # Usually these files are written by a python script from a template 47 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 48 | *.manifest 49 | *.spec 50 | 51 | # Installer logs 52 | pip-log.txt 53 | pip-delete-this-directory.txt 54 | 55 | # Unit test / coverage reports 56 | htmlcov/ 57 | .tox/ 58 | .nox/ 59 | .coverage 60 | .coverage.* 61 | .cache 62 | nosetests.xml 63 | coverage.xml 64 | *.cover 65 | .hypothesis/ 66 | .pytest_cache/ 67 | 68 | # Translations 69 | *.mo 70 | *.pot 71 | 72 | # Scrapy stuff: 73 | .scrapy 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | target/ 80 | 81 | # pyenv 82 | .python-version 83 | 84 | # celery beat schedule file 85 | celerybeat-schedule 86 | 87 | # SageMath parsed files 88 | *.sage.py 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # Mr Developer 98 | .mr.developer.cfg 99 | .project 100 | .pydevproject 101 | 102 | # mkdocs documentation 103 | /site 104 | 105 | # mypy 106 | .mypy_cache/ 107 | .dmypy.json 108 | dmypy.json 109 | 110 | # Pyre type checker 111 | .pyre/ 112 | 113 | # OS X stuff 114 | *.DS_Store 115 | 116 | # End of https://www.gitignore.io/api/python 117 | 118 | # jupyterlite 119 | *.doit.db 120 | _output 121 | -------------------------------------------------------------------------------- /.nojekyll: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Data Structures and Comprehensions 2 | 3 | This is the code for the **O'Reilly Live Training**, presented by **Arianne Dee**. 4 | 5 | ## Run the code in your browser 6 | 7 | ▶️ **https://ariannedee.github.io/python-data-structs/lab/index.html** 8 | 9 | ### Acknowledgments 10 | 11 | This site is run by [JupyterLite](https://jupyterlite.readthedocs.io/) 12 | 13 | JupyterLite is being tested against modern web browsers: 14 | 15 | - Firefox 90+ 16 | - Chromium 89+ 17 | 18 | If you have any questions, please send an email to **arianne.dee.studios** at gmail dot com. 19 | -------------------------------------------------------------------------------- /content/examples/array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ariannedee/python-data-structs/e515ca196a98ea1179adf63b53eb5c9be9fd55d2/content/examples/array -------------------------------------------------------------------------------- /content/examples/ex_01_lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6746aa20", 6 | "metadata": {}, 7 | "source": [ 8 | "# Lists\n", 9 | "Ordered collection of objects, with easy access to items by position (index).\n", 10 | "## Creating lists" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "16d6ae0f", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "[]" 23 | ] 24 | }, 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "output_type": "execute_result" 28 | } 29 | ], 30 | "source": [ 31 | "list()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "id": "750b9fb9", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "['a', 'b', 1, 2]" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "['a', 'b', 1, 2]" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "id": "64346743", 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "['1', '2', '3', '4', '5']" 65 | ] 66 | }, 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "list('12345')" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 4, 79 | "id": "932183fa", 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "['hello', 'foo']" 86 | ] 87 | }, 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "list({'hello': 'world', 'foo': 'bar'}) # Only takes keys" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "042703a2", 100 | "metadata": {}, 101 | "source": [ 102 | "You can also build lists using **list comprehensions**" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 5, 108 | "id": "293c60f4", 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]" 115 | ] 116 | }, 117 | "execution_count": 5, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "[2 ** i for i in range(12)]" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "id": "955c6410", 129 | "metadata": {}, 130 | "source": [ 131 | "We will look more at list comprehensions in the second hour of the class." 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "id": "e74b1a53", 137 | "metadata": {}, 138 | "source": [ 139 | "## Slicing and indexing lists\n", 140 | "These also work on any sequence (tuple, string, bytearray, etc)\n", 141 | "\n", 142 | "### Getting an item via index (indexing)\n", 143 | "The format is `sequence[index]`\n", 144 | "\n", 145 | "Indices start at `0` and go to `len(sequence) - 1`." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "id": "73fee9f7", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "['a', 'b', 'c', 'd', 'e']" 158 | ] 159 | }, 160 | "execution_count": 6, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "sequence = list('abcde')\n", 167 | "sequence" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 7, 173 | "id": "f5c90c7b", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "'a'" 180 | ] 181 | }, 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "sequence[0]" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 8, 194 | "id": "b0e00a98", 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "'e'" 201 | ] 202 | }, 203 | "execution_count": 8, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "end = len(sequence) - 1\n", 210 | "sequence[end]" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "id": "cb7c569c", 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "IndexError('list index out of range')\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "try:\n", 229 | " sequence[100]\n", 230 | "except Exception as e:\n", 231 | " print(repr(e))" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "4a0317ff", 237 | "metadata": {}, 238 | "source": [ 239 | "You can also go from the end of the list at `-1` to the beginning at `-len(sequence)`" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 10, 245 | "id": "b3b8e42e", 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "data": { 250 | "text/plain": [ 251 | "'e'" 252 | ] 253 | }, 254 | "execution_count": 10, 255 | "metadata": {}, 256 | "output_type": "execute_result" 257 | } 258 | ], 259 | "source": [ 260 | "sequence[-1]" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 11, 266 | "id": "870ef9a4", 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "'a'" 273 | ] 274 | }, 275 | "execution_count": 11, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [ 281 | "beginning = -len(sequence)\n", 282 | "sequence[beginning]" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "id": "d6f10249", 288 | "metadata": {}, 289 | "source": [ 290 | "### Getting a sublist (slicing)\n", 291 | "\n", 292 | "The format is `sequence[start:stop:step]`\n", 293 | "\n", 294 | "This will return a new sequence with items starting at `start`, ending at `stop - 1`, and skipping `step` each time.\n", 295 | "\n", 296 | "`step` is optional, and the default is `1`.\n", 297 | "\n", 298 | "If a number is missing, its value is assumed to be either beginning or end of the list, depending on the context." 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 12, 304 | "id": "64b65b41", 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "['b', 'c']" 311 | ] 312 | }, 313 | "execution_count": 12, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "sequence[1:3] # indices 1 and 2" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 13, 325 | "id": "6a1a64a6", 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "data": { 330 | "text/plain": [ 331 | "['a', 'b', 'c', 'd']" 332 | ] 333 | }, 334 | "execution_count": 13, 335 | "metadata": {}, 336 | "output_type": "execute_result" 337 | } 338 | ], 339 | "source": [ 340 | "sequence[:4] # beginning (0 to 3)" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 14, 346 | "id": "097a144a", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "['c', 'd', 'e']" 353 | ] 354 | }, 355 | "execution_count": 14, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "sequence[2:] # 2 to end (4)" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 15, 367 | "id": "03fa78cf", 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "data": { 372 | "text/plain": [ 373 | "['a', 'b', 'c', 'd', 'e']" 374 | ] 375 | }, 376 | "execution_count": 15, 377 | "metadata": {}, 378 | "output_type": "execute_result" 379 | } 380 | ], 381 | "source": [ 382 | "sequence[:] # full list" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 16, 388 | "id": "66a948b9", 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "['a', 'c', 'e']" 395 | ] 396 | }, 397 | "execution_count": 16, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "sequence[::2] # every second item" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 17, 409 | "id": "219fd0e3", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "['c', 'b', 'a']" 416 | ] 417 | }, 418 | "execution_count": 17, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "sequence[2::-1] # 2, 1, 0" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 18, 430 | "id": "95577c85", 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "data": { 435 | "text/plain": [ 436 | "['e', 'd', 'c', 'b', 'a']" 437 | ] 438 | }, 439 | "execution_count": 18, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "sequence[::-1] # reversed" 446 | ] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "id": "5025b52b", 451 | "metadata": {}, 452 | "source": [ 453 | "## Updating contents" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 19, 459 | "id": "525b0ac5", 460 | "metadata": {}, 461 | "outputs": [ 462 | { 463 | "data": { 464 | "text/plain": [ 465 | "['new', 'b', 'c', 'd', 'e']" 466 | ] 467 | }, 468 | "execution_count": 19, 469 | "metadata": {}, 470 | "output_type": "execute_result" 471 | } 472 | ], 473 | "source": [ 474 | "sequence[0] = 'new' # update existing item\n", 475 | "sequence" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 20, 481 | "id": "e1fd998b", 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "data": { 486 | "text/plain": [ 487 | "['new', 'b', 'c', 'd']" 488 | ] 489 | }, 490 | "execution_count": 20, 491 | "metadata": {}, 492 | "output_type": "execute_result" 493 | } 494 | ], 495 | "source": [ 496 | "del sequence[-1] # deleting by index\n", 497 | "sequence" 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "execution_count": 21, 503 | "id": "be890618", 504 | "metadata": {}, 505 | "outputs": [ 506 | { 507 | "data": { 508 | "text/plain": [ 509 | "['new', 'b', 'c', 'd', 'end']" 510 | ] 511 | }, 512 | "execution_count": 21, 513 | "metadata": {}, 514 | "output_type": "execute_result" 515 | } 516 | ], 517 | "source": [ 518 | "sequence.append('end') # add item to the end\n", 519 | "sequence" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 22, 525 | "id": "b179d32a", 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "data": { 530 | "text/plain": [ 531 | "['new', 'a', 'b', 'c', 'd', 'end']" 532 | ] 533 | }, 534 | "execution_count": 22, 535 | "metadata": {}, 536 | "output_type": "execute_result" 537 | } 538 | ], 539 | "source": [ 540 | "sequence.insert(1, 'a') # add item at index\n", 541 | "sequence" 542 | ] 543 | }, 544 | { 545 | "cell_type": "markdown", 546 | "id": "9f52c118", 547 | "metadata": {}, 548 | "source": [ 549 | "## Checking containment" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 23, 555 | "id": "2417160d", 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "False" 562 | ] 563 | }, 564 | "execution_count": 23, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "'e' in sequence" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 24, 576 | "id": "b81246b3", 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "data": { 581 | "text/plain": [ 582 | "True" 583 | ] 584 | }, 585 | "execution_count": 24, 586 | "metadata": {}, 587 | "output_type": "execute_result" 588 | } 589 | ], 590 | "source": [ 591 | "'a' in sequence" 592 | ] 593 | }, 594 | { 595 | "cell_type": "markdown", 596 | "id": "bc5367e7", 597 | "metadata": {}, 598 | "source": [ 599 | "## Looping" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 25, 605 | "id": "2f3fcd57", 606 | "metadata": {}, 607 | "outputs": [ 608 | { 609 | "name": "stdout", 610 | "output_type": "stream", 611 | "text": [ 612 | "newnew\n", 613 | "aa\n", 614 | "bb\n", 615 | "cc\n", 616 | "dd\n", 617 | "endend\n" 618 | ] 619 | } 620 | ], 621 | "source": [ 622 | "for value in sequence:\n", 623 | " print(value * 2)" 624 | ] 625 | }, 626 | { 627 | "cell_type": "markdown", 628 | "id": "2785cc8f", 629 | "metadata": {}, 630 | "source": [ 631 | "If you need the index as well, use `.enumerate()`" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": 26, 637 | "id": "99bee452", 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "name": "stdout", 642 | "output_type": "stream", 643 | "text": [ 644 | "0: new\n", 645 | "1: a\n", 646 | "2: b\n", 647 | "3: c\n", 648 | "4: d\n", 649 | "5: end\n" 650 | ] 651 | } 652 | ], 653 | "source": [ 654 | "for i, value in enumerate(sequence):\n", 655 | " print(f'{i}: {value}')" 656 | ] 657 | } 658 | ], 659 | "metadata": { 660 | "jupytext": { 661 | "cell_metadata_filter": "-all", 662 | "main_language": "python", 663 | "notebook_metadata_filter": "-all" 664 | }, 665 | "kernelspec": { 666 | "display_name": "Python 3 (ipykernel)", 667 | "language": "python", 668 | "name": "python3" 669 | }, 670 | "language_info": { 671 | "codemirror_mode": { 672 | "name": "ipython", 673 | "version": 3 674 | }, 675 | "file_extension": ".py", 676 | "mimetype": "text/x-python", 677 | "name": "python", 678 | "nbconvert_exporter": "python", 679 | "pygments_lexer": "ipython3", 680 | "version": "3.11.4" 681 | } 682 | }, 683 | "nbformat": 4, 684 | "nbformat_minor": 5 685 | } 686 | -------------------------------------------------------------------------------- /content/examples/ex_02_tuple.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "371d5bf4", 6 | "metadata": {}, 7 | "source": [ 8 | "# Tuples\n", 9 | "\n", 10 | "Immutable (non-modifiable) lists.\n", 11 | "\n", 12 | "## Creating tuples" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "id": "c27b4316", 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "(1, 2, 3)" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "(1, 2, 3)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "84cf1ca2", 39 | "metadata": {}, 40 | "source": [ 41 | "Be careful with single item tuples. \n", 42 | "\n", 43 | "They need a trailing comma or else it's just an expression." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "id": "192e9860", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "('Hello',)" 56 | ] 57 | }, 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "('Hello',)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 3, 70 | "id": "a34cd2a6", 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "'Goodbye'" 77 | ] 78 | }, 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "('Goodbye')" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "id": "142b8050-3470-4d1c-a2ff-755c37fbd15d", 91 | "metadata": {}, 92 | "source": [ 93 | "You can create a tuple from an existing collection/iterable, using `tuple()`." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 4, 99 | "id": "6f48fabf", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "('a', 'b', 'c')" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "tuple('abc')" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "id": "af668c86", 120 | "metadata": {}, 121 | "source": [ 122 | "## Cannot update tuples" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 5, 128 | "id": "db7aa756", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "NameError(\"name 'b' is not defined\")\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "try:\n", 141 | " b[0] = 'Howdy'\n", 142 | "except Exception as e:\n", 143 | " print(repr(e))" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "id": "18e74c28", 149 | "metadata": {}, 150 | "source": [ 151 | "## Slicing and indexing\n", 152 | "\n", 153 | "It's done just like lists!\n", 154 | "\n", 155 | "## Packing and unpacking\n", 156 | "\n", 157 | "**Packing** happens when you don't include the parentheses" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 6, 163 | "id": "9fade3cc", 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "(1.2, 3.4)" 170 | ] 171 | }, 172 | "execution_count": 6, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "coords = 1.2, 3.4\n", 179 | "coords" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "id": "e3753024", 185 | "metadata": {}, 186 | "source": [ 187 | "**Unpacking** happens when you expand a `tuple` into multiple variables" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 7, 193 | "id": "ed9f9d80", 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "1.2\n", 201 | "3.4\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "lat, lon = coords\n", 207 | "print(lat)\n", 208 | "print(lon)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 8, 214 | "id": "061b8d77", 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "[3.4, 5.6]" 221 | ] 222 | }, 223 | "execution_count": 8, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "a_tuple = 1.2, 3.4, 5.6, 7.8\n", 230 | "x, *y, z = a_tuple\n", 231 | "y" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "bdf85f3e", 237 | "metadata": {}, 238 | "source": [ 239 | "## When to use tuples" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "id": "439b863c", 245 | "metadata": {}, 246 | "source": [ 247 | "### Representing objects/heterogeneous data" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 9, 253 | "id": "735e749b", 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "blue = 0, 0, 255\n", 258 | "colours = ['red', 'green', blue]" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "id": "d60c7c01", 264 | "metadata": {}, 265 | "source": [ 266 | "### Swap variable names\n", 267 | "\n", 268 | "Use `tuple` packing and unpacking to do neat stuff.\n", 269 | "\n", 270 | "Without it, you'd need to introduce a temporary 3rd variable." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 10, 276 | "id": "ab9d1af2", 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "name": "stdout", 281 | "output_type": "stream", 282 | "text": [ 283 | "-100\n", 284 | "100\n" 285 | ] 286 | } 287 | ], 288 | "source": [ 289 | "d = 100\n", 290 | "e = -100\n", 291 | "\n", 292 | "d, e = e, d\n", 293 | "print(d)\n", 294 | "print(e)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "id": "4cc6c781", 300 | "metadata": {}, 301 | "source": [ 302 | "### Represent constants/immutable values" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 11, 308 | "id": "745e4111", 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "paris = (33.66, -95.54)\n", 313 | "athens = (32.20, -95.85)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "id": "8e58eca4", 319 | "metadata": {}, 320 | "source": [ 321 | "### Use as dict keys" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 12, 327 | "id": "e15411ba", 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "{(33.66, -95.54): 'Paris', (32.2, -95.85): 'Athens'}" 334 | ] 335 | }, 336 | "execution_count": 12, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "texas_cities = {\n", 343 | " paris: 'Paris',\n", 344 | " athens: 'Athens'\n", 345 | "}\n", 346 | "texas_cities" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "id": "1a18e3f3", 352 | "metadata": {}, 353 | "source": [ 354 | "### Return multiple values from a function" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 13, 360 | "id": "5dfaabc3", 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "11/2 is 5 remainder 1\n" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "def div_mod(x, y):\n", 373 | " div = x // y\n", 374 | " mod = x % y\n", 375 | " return div, mod\n", 376 | "\n", 377 | "quotient, remainder = div_mod(11, 2)\n", 378 | "print(f'11/2 is {quotient} remainder {remainder}')" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "id": "05845778", 384 | "metadata": {}, 385 | "source": [ 386 | "**Note**: the builtin `divmod()` exists because the CPU can determine them at the same time, so it saves computing time if you need both." 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "id": "a3bb6adc", 392 | "metadata": {}, 393 | "source": [ 394 | "### Access multiple items in a loop\n", 395 | "\n", 396 | "When you access multiple variables in `for` loops, you're actually unpacking a `tuple`." 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 14, 402 | "id": "cede3ca1", 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "name": "stdout", 407 | "output_type": "stream", 408 | "text": [ 409 | "0: a\n", 410 | "1: b\n", 411 | "2: c\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "for i, val in enumerate('abc'):\n", 417 | " print(f'{i}: {val}')" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 15, 423 | "id": "0ef37567", 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "(0, 'a')\n", 431 | "(1, 'b')\n", 432 | "(2, 'c')\n" 433 | ] 434 | } 435 | ], 436 | "source": [ 437 | "for tup in enumerate('abc'):\n", 438 | " print(tup)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "id": "1b306798", 444 | "metadata": {}, 445 | "source": [ 446 | "### The zip() function\n", 447 | "\n", 448 | "If you want to loop over items two or more sequences simultaneously, you can zip them and loop over the results." 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 16, 454 | "id": "beb51111", 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "[(1, 'a'), (2, 'b'), (3, 'c')]\n", 462 | "a\n", 463 | "bb\n", 464 | "ccc\n" 465 | ] 466 | } 467 | ], 468 | "source": [ 469 | "nums = [1, 2, 3]\n", 470 | "letters = ['a', 'b', 'c']\n", 471 | "\n", 472 | "print(list(zip(nums, letters)))\n", 473 | "\n", 474 | "for num, letter in zip(nums, letters):\n", 475 | " print(num * letter)" 476 | ] 477 | }, 478 | { 479 | "cell_type": "markdown", 480 | "id": "7c944643", 481 | "metadata": {}, 482 | "source": [ 483 | "### Practical uses of zip()\n", 484 | "\n", 485 | "Copied from this [Real Python](https://realpython.com/python-zip-function/) article\n", 486 | "\n", 487 | "#### Calculating in pairs\n", 488 | "\n", 489 | "If you have a spreadsheet of `total_sales` and `costs` for multiple months, you can calculate the `profit` for each month and for the whole period." 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 17, 495 | "id": "0e487e44", 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "name": "stdout", 500 | "output_type": "stream", 501 | "text": [ 502 | "Profit: $5200.0\n", 503 | "Profit: $5100.0\n", 504 | "Profit: $4800.0\n", 505 | "Total profit: $15,100.0\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "total_sales_q2 = [52000.00, 51000.00, 48000.00]\n", 511 | "costs_q2 = [46800.00, 45900.00, 43200.00]\n", 512 | "\n", 513 | "profit_q2 = 0\n", 514 | "for sales, costs in zip(total_sales_q2, costs_q2):\n", 515 | " profit = sales - costs\n", 516 | " print(f'Profit: ${profit}')\n", 517 | " profit_q2 += profit\n", 518 | "\n", 519 | "print(f'Total profit: ${profit_q2:,}')" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "id": "e1665760", 525 | "metadata": {}, 526 | "source": [ 527 | "#### Building dictionaries" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 18, 533 | "id": "2136d1d5", 534 | "metadata": { 535 | "lines_to_next_cell": 0 536 | }, 537 | "outputs": [ 538 | { 539 | "data": { 540 | "text/plain": [ 541 | "{'name': 'Jasmine', 'last_name': 'Doe', 'age': '45', 'job': 'Python Developer'}" 542 | ] 543 | }, 544 | "execution_count": 18, 545 | "metadata": {}, 546 | "output_type": "execute_result" 547 | } 548 | ], 549 | "source": [ 550 | "fields = ['name', 'last_name', 'age', 'job']\n", 551 | "values = ['Jasmine', 'Doe', '45', 'Python Developer']\n", 552 | "\n", 553 | "person = dict(zip(fields, values))\n", 554 | "person" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": null, 560 | "id": "c6be7a5a", 561 | "metadata": { 562 | "lines_to_next_cell": 2 563 | }, 564 | "outputs": [], 565 | "source": [] 566 | } 567 | ], 568 | "metadata": { 569 | "jupytext": { 570 | "cell_metadata_filter": "-all", 571 | "formats": "auto:light,ipynb", 572 | "main_language": "python", 573 | "notebook_metadata_filter": "-all" 574 | }, 575 | "kernelspec": { 576 | "display_name": "Python 3 (ipykernel)", 577 | "language": "python", 578 | "name": "python3" 579 | }, 580 | "language_info": { 581 | "codemirror_mode": { 582 | "name": "ipython", 583 | "version": 3 584 | }, 585 | "file_extension": ".py", 586 | "mimetype": "text/x-python", 587 | "name": "python", 588 | "nbconvert_exporter": "python", 589 | "pygments_lexer": "ipython3", 590 | "version": "3.11.4" 591 | } 592 | }, 593 | "nbformat": 4, 594 | "nbformat_minor": 5 595 | } 596 | -------------------------------------------------------------------------------- /content/examples/ex_03_dict.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "83373c68", 6 | "metadata": {}, 7 | "source": [ 8 | "# Dicts\n", 9 | "Dictionaries are a type of key-value mapping object.\n", 10 | "\n", 11 | "Instead of accessing items by index (position), we get them through via keys.\n", 12 | "## Creating dicts" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "id": "789b6d27", 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "{'a': 1, 'b': 2}" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "{'a': 1, 'b': 2}" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "id": "e14c35ef", 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "{}" 46 | ] 47 | }, 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "dict()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "id": "f10231ac", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "{1: 2, 3: 4}" 67 | ] 68 | }, 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "dict(((1, 2), [3, 4])) # Must be a sequence of 2-item lists/tuples" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "f7decb34", 81 | "metadata": {}, 82 | "source": [ 83 | "You can also build dicts using **dict comprehensions**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "id": "f780e799", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "{0: 1, 2: 4, 4: 16, 6: 64, 8: 256}" 96 | ] 97 | }, 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "{i: 2 ** i for i in range(0, 10, 2)}" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "id": "f7252a01", 110 | "metadata": {}, 111 | "source": [ 112 | "We will look more at dict comprehensions in the second hour of the class." 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "id": "75688975", 118 | "metadata": {}, 119 | "source": [ 120 | "### Valid dict types\n", 121 | "`dict` **values** can be anything.\n", 122 | "\n", 123 | "`dict` **keys** must be __hashable__. Basically, if it's a builtin type, it can't be mutable (like `list`, `dict` or `set`). If it's a custom class, it must implement a special method for hashing and checking equality.\n", 124 | "\n", 125 | "Keys use `==` to determine key equality. So `1`, `1.0` and `True` all map to the same key." 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 5, 131 | "id": "24c29589", 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "1" 138 | ] 139 | }, 140 | "execution_count": 5, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "hash(1)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 6, 152 | "id": "e62ca448", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "True" 159 | ] 160 | }, 161 | "execution_count": 6, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "1 == True" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 7, 173 | "id": "3246836b", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "{1: 'float'}" 180 | ] 181 | }, 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "{\n", 189 | " 1: ['a', 2,],\n", 190 | " True: False,\n", 191 | " 1.0: 'float',\n", 192 | "}" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 8, 198 | "id": "8eee0cc5", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "TypeError(\"unhashable type: 'list'\")\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "try:\n", 211 | " {[1, 2]: 'list'}\n", 212 | "except Exception as e:\n", 213 | " print(repr(e))" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "id": "f8ce4f2b", 219 | "metadata": {}, 220 | "source": [ 221 | "## Getting and updating contents" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 9, 227 | "id": "d314f5a8", 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "d = {'a': 1, 'b': 2}" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "0ec6e1a5", 237 | "metadata": {}, 238 | "source": [ 239 | "### Getting values\n", 240 | "We get items by key and not index (even if it's an int)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 10, 246 | "id": "5947a884", 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "data": { 251 | "text/plain": [ 252 | "1" 253 | ] 254 | }, 255 | "execution_count": 10, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "d['a']" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 11, 267 | "id": "a62154d5", 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "name": "stdout", 272 | "output_type": "stream", 273 | "text": [ 274 | "KeyError('c')\n" 275 | ] 276 | } 277 | ], 278 | "source": [ 279 | "try:\n", 280 | " print(d['c']) # Key doesn't exist\n", 281 | "except Exception as e:\n", 282 | " print(repr(e))" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 12, 288 | "id": "1259a415", 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "data": { 293 | "text/plain": [ 294 | "1" 295 | ] 296 | }, 297 | "execution_count": 12, 298 | "metadata": {}, 299 | "output_type": "execute_result" 300 | } 301 | ], 302 | "source": [ 303 | "d.get('a')" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 13, 309 | "id": "a9022e8a", 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "name": "stdout", 314 | "output_type": "stream", 315 | "text": [ 316 | "None\n" 317 | ] 318 | } 319 | ], 320 | "source": [ 321 | "print(d.get('c')) # Key doesn't exist" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 14, 327 | "id": "f0c5b218", 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "'Not found'" 334 | ] 335 | }, 336 | "execution_count": 14, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "d.get('c', 'Not found') # Can provide default value" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "id": "7c6249a1", 348 | "metadata": {}, 349 | "source": [ 350 | "### Updating values" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 15, 356 | "id": "7150340a", 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "{'a': 'new', 'b': 2}" 363 | ] 364 | }, 365 | "execution_count": 15, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "d['a'] = 'new' # update existing item\n", 372 | "d" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 16, 378 | "id": "02928b64", 379 | "metadata": {}, 380 | "outputs": [ 381 | { 382 | "data": { 383 | "text/plain": [ 384 | "{'a': 'new', 'b': 2, 'c': 3}" 385 | ] 386 | }, 387 | "execution_count": 16, 388 | "metadata": {}, 389 | "output_type": "execute_result" 390 | } 391 | ], 392 | "source": [ 393 | "d['c'] = 3 # add item a new item\n", 394 | "d" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 17, 400 | "id": "3252aca0", 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "data": { 405 | "text/plain": [ 406 | "{'a': 'new', 'c': 3}" 407 | ] 408 | }, 409 | "execution_count": 17, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "del d['b'] # deleting by key\n", 416 | "d" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "id": "357e1918", 422 | "metadata": {}, 423 | "source": [ 424 | "## Checking containment\n", 425 | "Checks keys, not values" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 18, 431 | "id": "5a5e6601", 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "True" 438 | ] 439 | }, 440 | "execution_count": 18, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "'a' in d" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 19, 452 | "id": "f3fa375b", 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "False" 459 | ] 460 | }, 461 | "execution_count": 19, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "'e' in d" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "id": "6dbce2f9", 473 | "metadata": {}, 474 | "source": [ 475 | "## Looping\n", 476 | "Basic loop is over keys" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 20, 482 | "id": "6b99824f", 483 | "metadata": {}, 484 | "outputs": [ 485 | { 486 | "name": "stdout", 487 | "output_type": "stream", 488 | "text": [ 489 | "a\n", 490 | "c\n" 491 | ] 492 | } 493 | ], 494 | "source": [ 495 | "for key in d:\n", 496 | " print(key)" 497 | ] 498 | }, 499 | { 500 | "cell_type": "markdown", 501 | "id": "0223cb5f", 502 | "metadata": {}, 503 | "source": [ 504 | "It's recommended to use `.keys()`, `.values()` or `.items()` to be explicit about what you're looping over." 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 21, 510 | "id": "8a12e8bb", 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "name": "stdout", 515 | "output_type": "stream", 516 | "text": [ 517 | "a: new\n", 518 | "c: 3\n" 519 | ] 520 | } 521 | ], 522 | "source": [ 523 | "for key, value in d.items():\n", 524 | " print(f\"{key}: {value}\")" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 22, 530 | "id": "535e3bf0", 531 | "metadata": {}, 532 | "outputs": [ 533 | { 534 | "data": { 535 | "text/plain": [ 536 | "{'a': 'new', 'c': 3, 'b': 3}" 537 | ] 538 | }, 539 | "execution_count": 22, 540 | "metadata": {}, 541 | "output_type": "execute_result" 542 | } 543 | ], 544 | "source": [ 545 | "d['b'] = d['c']\n", 546 | "d" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 23, 552 | "id": "97b99c4d", 553 | "metadata": {}, 554 | "outputs": [], 555 | "source": [ 556 | "del d['c']" 557 | ] 558 | }, 559 | { 560 | "cell_type": "code", 561 | "execution_count": 24, 562 | "id": "92cbb9e3", 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "{'a': 'new', 'b': 3}" 569 | ] 570 | }, 571 | "execution_count": 24, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "d" 578 | ] 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "id": "3a375857-61e5-488a-8a67-8908ad72809f", 583 | "metadata": {}, 584 | "source": [ 585 | "## Sorting lists of dicts" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": 25, 591 | "id": "f83d9909", 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "data": { 596 | "text/plain": [ 597 | "[{1: 'c'}, {1: 'a'}, {1: 'b'}]" 598 | ] 599 | }, 600 | "execution_count": 25, 601 | "metadata": {}, 602 | "output_type": "execute_result" 603 | } 604 | ], 605 | "source": [ 606 | "u1 = {1: 'a'}\n", 607 | "u2 = {1: 'b'}\n", 608 | "u3 = {1: 'c'}\n", 609 | "\n", 610 | "users = [u3, u1, u2]\n", 611 | "users" 612 | ] 613 | }, 614 | { 615 | "cell_type": "markdown", 616 | "id": "9935a105-c7ee-4985-9ff7-73e244d04992", 617 | "metadata": {}, 618 | "source": [ 619 | "Create a sort function that takes 1 argument and returns a value to sort by.\n", 620 | "Pass the function into the `.sort()` `key` argument (without calling it)." 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 26, 626 | "id": "5b02afd6", 627 | "metadata": {}, 628 | "outputs": [ 629 | { 630 | "data": { 631 | "text/plain": [ 632 | "[{1: 'a'}, {1: 'b'}, {1: 'c'}]" 633 | ] 634 | }, 635 | "execution_count": 26, 636 | "metadata": {}, 637 | "output_type": "execute_result" 638 | } 639 | ], 640 | "source": [ 641 | "def sort_by(u):\n", 642 | " return u[1]\n", 643 | "\n", 644 | "users.sort(key=sort_by)\n", 645 | "users" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "id": "f8748211-b6fb-4b6b-b15d-d6176067c68a", 651 | "metadata": {}, 652 | "source": [ 653 | "Or create an anonymous (lambda) function." 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 27, 659 | "id": "68c101ad", 660 | "metadata": {}, 661 | "outputs": [ 662 | { 663 | "data": { 664 | "text/plain": [ 665 | "[{1: 'a'}, {1: 'b'}, {1: 'c'}]" 666 | ] 667 | }, 668 | "execution_count": 27, 669 | "metadata": {}, 670 | "output_type": "execute_result" 671 | } 672 | ], 673 | "source": [ 674 | "users.sort(key=lambda u: u[1])\n", 675 | "users" 676 | ] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "id": "379acfb6-4fd4-4517-bb9d-db99540bb130", 681 | "metadata": {}, 682 | "source": [ 683 | "### Some methods change the object, and some return a new object\n", 684 | "`list.sort()` returns `None` but changes the list.\n", 685 | "\n", 686 | "`sorted(list)` returns a new sorted iterable, leaving the list unchanged." 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": 28, 692 | "id": "7a4a002a-66b6-45e5-89bd-424ad1c310ee", 693 | "metadata": {}, 694 | "outputs": [ 695 | { 696 | "name": "stdout", 697 | "output_type": "stream", 698 | "text": [ 699 | "{1: 'c'}\n", 700 | "{1: 'b'}\n", 701 | "{1: 'a'}\n" 702 | ] 703 | } 704 | ], 705 | "source": [ 706 | "for u in sorted(users, key=lambda u: u[1], reverse=True):\n", 707 | " print(u)" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 29, 713 | "id": "0c0d1086", 714 | "metadata": {}, 715 | "outputs": [], 716 | "source": [ 717 | "users.reverse() # Returns None, but changes users" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 30, 723 | "id": "07a861eb", 724 | "metadata": {}, 725 | "outputs": [ 726 | { 727 | "data": { 728 | "text/plain": [ 729 | "[{1: 'c'}, {1: 'b'}, {1: 'a'}]" 730 | ] 731 | }, 732 | "execution_count": 30, 733 | "metadata": {}, 734 | "output_type": "execute_result" 735 | } 736 | ], 737 | "source": [ 738 | "users" 739 | ] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "execution_count": 31, 744 | "id": "039551b2", 745 | "metadata": {}, 746 | "outputs": [ 747 | { 748 | "data": { 749 | "text/plain": [ 750 | "[{1: 'a'}, {1: 'b'}, {1: 'c'}]" 751 | ] 752 | }, 753 | "execution_count": 31, 754 | "metadata": {}, 755 | "output_type": "execute_result" 756 | } 757 | ], 758 | "source": [ 759 | "users[::-1] # Returns a new, reversed list, users is unchanged" 760 | ] 761 | } 762 | ], 763 | "metadata": { 764 | "jupytext": { 765 | "cell_metadata_filter": "-all", 766 | "formats": "auto:light,ipynb", 767 | "main_language": "python", 768 | "notebook_metadata_filter": "-all" 769 | }, 770 | "kernelspec": { 771 | "display_name": "Python 3 (ipykernel)", 772 | "language": "python", 773 | "name": "python3" 774 | }, 775 | "language_info": { 776 | "codemirror_mode": { 777 | "name": "ipython", 778 | "version": 3 779 | }, 780 | "file_extension": ".py", 781 | "mimetype": "text/x-python", 782 | "name": "python", 783 | "nbconvert_exporter": "python", 784 | "pygments_lexer": "ipython3", 785 | "version": "3.11.4" 786 | } 787 | }, 788 | "nbformat": 4, 789 | "nbformat_minor": 5 790 | } 791 | -------------------------------------------------------------------------------- /content/examples/ex_05_list_comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f965542d", 6 | "metadata": {}, 7 | "source": [ 8 | "# List Comprehensions\n", 9 | "General format:\n", 10 | "`new_list = [expression for member in iterable]`\n", 11 | "## Basic comprehensions" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "id": "6645930d", 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "data": { 22 | "text/plain": [ 23 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" 24 | ] 25 | }, 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "output_type": "execute_result" 29 | } 30 | ], 31 | "source": [ 32 | "squares = [i ** 2 for i in range(10)]\n", 33 | "squares" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "48adce36", 39 | "metadata": {}, 40 | "source": [ 41 | "### As for-loop" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "id": "49295b69", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "squares = []\n", 63 | "for i in range(10):\n", 64 | " squares.append(i ** 2)\n", 65 | "\n", 66 | "squares" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "bc661729", 72 | "metadata": {}, 73 | "source": [ 74 | "## Using conditionals\n", 75 | "Generalized:\n", 76 | "`new_list = [expression for member in iterable if condition]`" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "id": "d6dbe602", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "[0, 2, 4, 6, 8]" 89 | ] 90 | }, 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "evens = [i for i in range(10) if i % 2 == 0]\n", 98 | "evens" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "id": "765f5439", 104 | "metadata": {}, 105 | "source": [ 106 | "### As for-loop" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 4, 112 | "id": "2f85ee00", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "[0, 2, 4, 6, 8]" 119 | ] 120 | }, 121 | "execution_count": 4, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "evens = []\n", 128 | "\n", 129 | "for i in range(10):\n", 130 | " if i % 2 == 0:\n", 131 | " evens.append(i)\n", 132 | "evens" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "id": "dbc31563", 138 | "metadata": {}, 139 | "source": [ 140 | "## With ternary operator" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 5, 146 | "id": "b6002c1c", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "x = 'a' if True else 'b'\n", 151 | "\n", 152 | "if True:\n", 153 | " x = 'a'\n", 154 | "else:\n", 155 | " x = 'b'" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 6, 161 | "id": "5b7fe22d", 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "[True, False, True, False, True, False, True, False, True, False]" 168 | ] 169 | }, 170 | "execution_count": 6, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "is_even = [True if i % 2 == 0 else False for i in range(10)]\n", 177 | "is_even" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 7, 183 | "id": "ba83fd83", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "[True, False, True, False, True, False, True, False, True, False]" 190 | ] 191 | }, 192 | "execution_count": 7, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "is_even = []\n", 199 | "for i in range(10):\n", 200 | " if i % 2 == 0:\n", 201 | " is_even.append(True)\n", 202 | " else:\n", 203 | " is_even.append(False)\n", 204 | "is_even" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "id": "4a4e28e1", 210 | "metadata": {}, 211 | "source": [ 212 | "## Nested comprehensions\n", 213 | "### Nested lists" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 8, 219 | "id": "c1bd1ec2", 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "data": { 224 | "text/plain": [ 225 | "[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]]" 226 | ] 227 | }, 228 | "execution_count": 8, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "coords = [\n", 235 | " [(x, y) for y in range(3)] \n", 236 | " for x in range(3)\n", 237 | "]\n", 238 | "coords" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "id": "38e1ae94", 244 | "metadata": {}, 245 | "source": [ 246 | "### Looping over coords" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 9, 252 | "id": "2dd071ae", 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "[(0, 0), (0, 1), (0, 2)]\n", 260 | "[(1, 0), (1, 1), (1, 2)]\n", 261 | "[(2, 0), (2, 1), (2, 2)]\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "for row in coords:\n", 267 | " print(row)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "id": "fd65c801", 273 | "metadata": {}, 274 | "source": [ 275 | "### As for-loop" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 10, 281 | "id": "99115c5c", 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "data": { 286 | "text/plain": [ 287 | "[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]]" 288 | ] 289 | }, 290 | "execution_count": 10, 291 | "metadata": {}, 292 | "output_type": "execute_result" 293 | } 294 | ], 295 | "source": [ 296 | "coords = []\n", 297 | "for x in range(3):\n", 298 | " row = []\n", 299 | " for y in range(3):\n", 300 | " row.append((x, y))\n", 301 | " coords.append(row)\n", 302 | "coords" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "id": "9f2a4f55", 308 | "metadata": {}, 309 | "source": [ 310 | "### Flattened list" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 11, 316 | "id": "d78b2ec4", 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]" 323 | ] 324 | }, 325 | "execution_count": 11, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "coords = [(x, y) for x in range(3) for y in range(3)]\n", 332 | "coords" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 12, 338 | "id": "90f8d9dc-0008-4bfd-a559-c458a2edb0d2", 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "data": { 343 | "text/plain": [ 344 | "[7, 2, 6, 1, 4, 5, -2, 8, 0]" 345 | ] 346 | }, 347 | "execution_count": 12, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "nums_list = [[7, 2], [6], [1, 4, 5], [-2, 8, 0]]\n", 354 | "nums = [n for li in nums_list for n in li]\n", 355 | "nums" 356 | ] 357 | }, 358 | { 359 | "cell_type": "markdown", 360 | "id": "f86e4c0f", 361 | "metadata": {}, 362 | "source": [ 363 | "### As for-loop" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 13, 369 | "id": "9cb3f840", 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "[7, 2, 6, 1, 4, 5, -2, 8, 0]" 376 | ] 377 | }, 378 | "execution_count": 13, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "nums = []\n", 385 | "for li in nums_list:\n", 386 | " for n in li:\n", 387 | " nums.append(n)\n", 388 | "nums" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 14, 394 | "id": "537afa17", 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]" 401 | ] 402 | }, 403 | "execution_count": 14, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "coords = []\n", 410 | "for x in range(3):\n", 411 | " for y in range(3):\n", 412 | " coords.append((x, y))\n", 413 | "coords" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "id": "3aa84d52", 419 | "metadata": {}, 420 | "source": [ 421 | "## Complex comprehension example" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 15, 427 | "id": "bf727a7f", 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [ 431 | "nums_list = [[7, 2], [6], [1, 4, 5], [-2, 8, 0]]" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 16, 437 | "id": "fc026a5b", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "[4, 1, 16, 4, 0]" 444 | ] 445 | }, 446 | "execution_count": 16, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "small_nums_squared = [n ** 2\n", 453 | " for li in nums_list\n", 454 | " for n in li\n", 455 | " if n < 5]\n", 456 | "small_nums_squared" 457 | ] 458 | }, 459 | { 460 | "cell_type": "markdown", 461 | "id": "f0cea854", 462 | "metadata": {}, 463 | "source": [ 464 | "### As for-loop" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 17, 470 | "id": "785f0f6b", 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "data": { 475 | "text/plain": [ 476 | "[2, 1, 4, -2, 0]" 477 | ] 478 | }, 479 | "execution_count": 17, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "small_nums = []\n", 486 | "for li in nums_list:\n", 487 | " for n in li:\n", 488 | " if n < 5:\n", 489 | " small_nums.append(n)\n", 490 | "small_nums" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "id": "b03b448a", 496 | "metadata": {}, 497 | "source": [ 498 | "## Very complex example" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 18, 504 | "id": "2eedf8cb", 505 | "metadata": {}, 506 | "outputs": [], 507 | "source": [ 508 | "nums_list = [[7, 2], [6], [1, 4, 5], [-2, 8, 0]]" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 19, 514 | "id": "de5f303a", 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "data": { 519 | "text/plain": [ 520 | "[7, -1, 5]" 521 | ] 522 | }, 523 | "execution_count": 19, 524 | "metadata": {}, 525 | "output_type": "execute_result" 526 | } 527 | ], 528 | "source": [ 529 | "a_list = [n if n > 3 else -n\n", 530 | " for li in nums_list\n", 531 | " if len(li) > 1\n", 532 | " for n in li\n", 533 | " if n % 2 == 1]\n", 534 | "a_list" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "id": "28e08d20", 540 | "metadata": {}, 541 | "source": [ 542 | "### As for-loop" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 20, 548 | "id": "5db47e00", 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "[7, -1, 5]" 555 | ] 556 | }, 557 | "execution_count": 20, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "a_list = []\n", 564 | "for li in nums_list:\n", 565 | " if len(li) > 1:\n", 566 | " for n in li:\n", 567 | " if n % 2 == 1:\n", 568 | " if n > 3:\n", 569 | " a_list.append(n)\n", 570 | " else:\n", 571 | " a_list.append(-n)\n", 572 | "a_list" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "id": "28aa0da2", 578 | "metadata": {}, 579 | "source": [ 580 | "### Functionally, with map/filter/sum" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 21, 586 | "id": "bb109db1", 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "data": { 591 | "text/plain": [ 592 | "[7, -1, 5]" 593 | ] 594 | }, 595 | "execution_count": 21, 596 | "metadata": {}, 597 | "output_type": "execute_result" 598 | } 599 | ], 600 | "source": [ 601 | "SMALL_NUM = 3\n", 602 | "multiple_nums = filter(lambda nums: len(nums) > 1, nums_list)\n", 603 | "flattened = sum(multiple_nums, [])\n", 604 | "odds = filter(lambda n: n % 2 == 1, flattened)\n", 605 | "small_becomes_negative = map(lambda n: -n if n < SMALL_NUM else n, odds)\n", 606 | "a_list = list(small_becomes_negative)\n", 607 | "a_list" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": null, 613 | "id": "3abfefc6", 614 | "metadata": {}, 615 | "outputs": [], 616 | "source": [] 617 | } 618 | ], 619 | "metadata": { 620 | "jupytext": { 621 | "cell_metadata_filter": "-all", 622 | "formats": "auto:light,ipynb", 623 | "main_language": "python", 624 | "notebook_metadata_filter": "-all" 625 | }, 626 | "kernelspec": { 627 | "display_name": "Python 3 (ipykernel)", 628 | "language": "python", 629 | "name": "python3" 630 | }, 631 | "language_info": { 632 | "codemirror_mode": { 633 | "name": "ipython", 634 | "version": 3 635 | }, 636 | "file_extension": ".py", 637 | "mimetype": "text/x-python", 638 | "name": "python", 639 | "nbconvert_exporter": "python", 640 | "pygments_lexer": "ipython3", 641 | "version": "3.11.4" 642 | } 643 | }, 644 | "nbformat": 4, 645 | "nbformat_minor": 5 646 | } 647 | -------------------------------------------------------------------------------- /content/examples/ex_06_dict_comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d7084a4d", 6 | "metadata": {}, 7 | "source": [ 8 | "# Dict Comprehensions\n", 9 | "Just like list comprehensions, but use a different syntax to create dicts.\n", 10 | "## Basic comprehensions\n", 11 | "Format: `{key_exp: value_exp for member in iterable}`" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "id": "86cd588f", 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "data": { 22 | "text/plain": [ 23 | "{2: 4, 4: 16, 6: 64, 8: 256}" 24 | ] 25 | }, 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "output_type": "execute_result" 29 | } 30 | ], 31 | "source": [ 32 | "powers_of_2 = {i: 2**i for i in range(2, 10, 2)}\n", 33 | "powers_of_2" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "4042c8e1", 39 | "metadata": {}, 40 | "source": [ 41 | "### As for-loop" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "id": "16341d9b", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "{2: 4, 4: 16, 6: 64, 8: 256}" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "powers_of_2 = {}\n", 63 | "for i in range(2, 10, 2):\n", 64 | " powers_of_2[i] = 2**i\n", 65 | "\n", 66 | "powers_of_2" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "866853e3", 72 | "metadata": {}, 73 | "source": [ 74 | "## Using conditionals" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "id": "f6f40261", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "{'i': 3,\n", 87 | " 'l': 1,\n", 88 | " 'f': 1,\n", 89 | " 'r': 3,\n", 90 | " 'a': 1,\n", 91 | " 'd': 2,\n", 92 | " 'o': 1,\n", 93 | " 's': 1,\n", 94 | " 't': 1,\n", 95 | " 'p': 9,\n", 96 | " 'k': 3,\n", 97 | " 'c': 3,\n", 98 | " 'e': 8}" 99 | ] 100 | }, 101 | "execution_count": 3, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "phrase = \"peter piper picked a peck of pickled peppers\"\n", 108 | "letter_set = set(phrase)\n", 109 | "\n", 110 | "letter_counts = {char: phrase.count(char) for char in letter_set if char != ' '}\n", 111 | "letter_counts" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "id": "a4df46a1", 117 | "metadata": {}, 118 | "source": [ 119 | "### As for-loop" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 4, 125 | "id": "a3fd7f02", 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "{'i': 3,\n", 132 | " 'l': 1,\n", 133 | " 'f': 1,\n", 134 | " 'r': 3,\n", 135 | " 'a': 1,\n", 136 | " 'd': 2,\n", 137 | " 'o': 1,\n", 138 | " 's': 1,\n", 139 | " 't': 1,\n", 140 | " 'p': 9,\n", 141 | " 'k': 3,\n", 142 | " 'c': 3,\n", 143 | " 'e': 8}" 144 | ] 145 | }, 146 | "execution_count": 4, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "phrase = \"peter piper picked a peck of pickled peppers\"\n", 153 | "letter_set = set(phrase)\n", 154 | "\n", 155 | "letter_counts = {}\n", 156 | "for char in letter_set:\n", 157 | " if char != ' ':\n", 158 | " letter_counts[char] = phrase.count(char)\n", 159 | "\n", 160 | "letter_counts" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "id": "3cd6e8f8", 166 | "metadata": {}, 167 | "source": [ 168 | "## With ternary operator" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 5, 174 | "id": "c80a1684", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "{0: True,\n", 181 | " 1: False,\n", 182 | " 2: True,\n", 183 | " 3: False,\n", 184 | " 4: True,\n", 185 | " 5: False,\n", 186 | " 6: True,\n", 187 | " 7: False,\n", 188 | " 8: True,\n", 189 | " 9: False}" 190 | ] 191 | }, 192 | "execution_count": 5, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "is_even = {i: True if i % 2 == 0 else False for i in range(10)}\n", 199 | "is_even" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 6, 205 | "id": "64a4d677", 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "{0: True,\n", 212 | " 1: False,\n", 213 | " 2: True,\n", 214 | " 3: False,\n", 215 | " 4: True,\n", 216 | " 5: False,\n", 217 | " 6: True,\n", 218 | " 7: False,\n", 219 | " 8: True,\n", 220 | " 9: False}" 221 | ] 222 | }, 223 | "execution_count": 6, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "is_even = {}\n", 230 | "for i in range(10):\n", 231 | " if i % 2 == 0:\n", 232 | " is_even[i] = True\n", 233 | " else:\n", 234 | " is_even[i] = False\n", 235 | "is_even" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "id": "bd2e0da3", 241 | "metadata": {}, 242 | "source": [ 243 | "## Nested comprehensions\n", 244 | "### Dict wth list values" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 7, 250 | "id": "e323800b", 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "data": { 255 | "text/plain": [ 256 | "{0: [],\n", 257 | " 1: [0],\n", 258 | " 2: [0, 1],\n", 259 | " 3: [0, 1, 2],\n", 260 | " 4: [0, 1, 2, 3],\n", 261 | " 5: [0, 1, 2, 3, 4],\n", 262 | " 6: [0, 1, 2, 3, 4, 5],\n", 263 | " 7: [0, 1, 2, 3, 4, 5, 6],\n", 264 | " 8: [0, 1, 2, 3, 4, 5, 6, 7],\n", 265 | " 9: [0, 1, 2, 3, 4, 5, 6, 7, 8]}" 266 | ] 267 | }, 268 | "execution_count": 7, 269 | "metadata": {}, 270 | "output_type": "execute_result" 271 | } 272 | ], 273 | "source": [ 274 | "lesser_nums = {\n", 275 | " i: [j for j in range(i)] \n", 276 | " for i in range(10)\n", 277 | "}\n", 278 | "lesser_nums" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "id": "7bef5215", 284 | "metadata": {}, 285 | "source": [ 286 | "### As for-loop" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 8, 292 | "id": "8344a681", 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "{0: [],\n", 299 | " 1: [0],\n", 300 | " 2: [0, 1],\n", 301 | " 3: [0, 1, 2],\n", 302 | " 4: [0, 1, 2, 3],\n", 303 | " 5: [0, 1, 2, 3, 4],\n", 304 | " 6: [0, 1, 2, 3, 4, 5],\n", 305 | " 7: [0, 1, 2, 3, 4, 5, 6],\n", 306 | " 8: [0, 1, 2, 3, 4, 5, 6, 7],\n", 307 | " 9: [0, 1, 2, 3, 4, 5, 6, 7, 8]}" 308 | ] 309 | }, 310 | "execution_count": 8, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "lesser_nums = {}\n", 317 | "for i in range(10):\n", 318 | " nums = []\n", 319 | " for j in range(i):\n", 320 | " nums.append(j)\n", 321 | " lesser_nums[i] = nums\n", 322 | "lesser_nums" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "id": "63466106", 328 | "metadata": {}, 329 | "source": [ 330 | "### Flat nesting" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 9, 336 | "id": "fc88ba86", 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "data": { 341 | "text/plain": [ 342 | "{(0, 0): 0,\n", 343 | " (0, 1): 0,\n", 344 | " (0, 2): 0,\n", 345 | " (1, 0): 0,\n", 346 | " (1, 1): 1,\n", 347 | " (1, 2): 2,\n", 348 | " (2, 0): 0,\n", 349 | " (2, 1): 2,\n", 350 | " (2, 2): 4}" 351 | ] 352 | }, 353 | "execution_count": 9, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "products = {(x, y): x*y for x in range(3) for y in range(3)}\n", 360 | "products" 361 | ] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "id": "74ad3722", 366 | "metadata": {}, 367 | "source": [ 368 | "### As for-loop" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 10, 374 | "id": "88182197", 375 | "metadata": {}, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/plain": [ 380 | "{(0, 0): 0,\n", 381 | " (0, 1): 0,\n", 382 | " (0, 2): 0,\n", 383 | " (1, 0): 0,\n", 384 | " (1, 1): 1,\n", 385 | " (1, 2): 2,\n", 386 | " (2, 0): 0,\n", 387 | " (2, 1): 2,\n", 388 | " (2, 2): 4}" 389 | ] 390 | }, 391 | "execution_count": 10, 392 | "metadata": {}, 393 | "output_type": "execute_result" 394 | } 395 | ], 396 | "source": [ 397 | "products = {}\n", 398 | "for x in range(3):\n", 399 | " for y in range(3):\n", 400 | " products[(x, y)] = x * y\n", 401 | "products" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": null, 407 | "id": "e4665cd0", 408 | "metadata": {}, 409 | "outputs": [], 410 | "source": [] 411 | } 412 | ], 413 | "metadata": { 414 | "jupytext": { 415 | "cell_metadata_filter": "-all", 416 | "formats": "auto:light,ipynb", 417 | "main_language": "python", 418 | "notebook_metadata_filter": "-all" 419 | }, 420 | "kernelspec": { 421 | "display_name": "Python 3 (ipykernel)", 422 | "language": "python", 423 | "name": "python3" 424 | }, 425 | "language_info": { 426 | "codemirror_mode": { 427 | "name": "ipython", 428 | "version": 3 429 | }, 430 | "file_extension": ".py", 431 | "mimetype": "text/x-python", 432 | "name": "python", 433 | "nbconvert_exporter": "python", 434 | "pygments_lexer": "ipython3", 435 | "version": "3.11.4" 436 | } 437 | }, 438 | "nbformat": 4, 439 | "nbformat_minor": 5 440 | } 441 | -------------------------------------------------------------------------------- /content/examples/ex_07_set_comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "440f5c8f", 6 | "metadata": {}, 7 | "source": [ 8 | "# Set Comprehensions\n", 9 | "Just like `dict` comprehensions, but without a value expression (just key).\n", 10 | "\n", 11 | "Or just like `list` comprehensions, but using curly braces `{}`.\n", 12 | "## Basic comprehensions" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "id": "39d3fc29", 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "{1, 4, 9, 64}" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "num_list = [1, 2, 3, 3, 8]\n", 34 | "num_set = {num ** 2 for num in num_list}\n", 35 | "num_set" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "b782a490", 41 | "metadata": {}, 42 | "source": [ 43 | "## Nested comprehensions\n", 44 | "### Nested set\n", 45 | "Sets can only contain hashable items.\n", 46 | "\n", 47 | "Since sets are mutable, if you want to nest sets, you must make any inner set an immutable `frozenset`." 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "id": "2796ea3e", 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "{frozenset({'m', 'o'}), frozenset({'a', 'd'})}" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "words = ['mom', 'dad', 'add']\n", 69 | "num_set = {\n", 70 | " frozenset({char for char in word})\n", 71 | " for word in words\n", 72 | "}\n", 73 | "num_set" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "id": "9117d146", 79 | "metadata": {}, 80 | "source": [ 81 | "### Flattened set" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 4, 87 | "id": "1d6802d8", 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "{'a', 'd', 'h', 'm', 'o'}" 94 | ] 95 | }, 96 | "execution_count": 4, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "words = ['mom', 'dad', 'ham']\n", 103 | "chars = {char for word in words for char in word}\n", 104 | "chars" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "id": "0f99d278", 110 | "metadata": {}, 111 | "source": [ 112 | "### As for-loop" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 5, 118 | "id": "40c6d4c0", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "{'a', 'd', 'h', 'm', 'o'}" 125 | ] 126 | }, 127 | "execution_count": 5, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "chars = set()\n", 134 | "for word in words:\n", 135 | " for char in word:\n", 136 | " chars.add(char)\n", 137 | "chars" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "7c6c11c7", 143 | "metadata": {}, 144 | "source": [ 145 | "## Complex comprehension example" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "id": "4fde3d46", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "{'d', 'h', 'm'}" 158 | ] 159 | }, 160 | "execution_count": 6, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "words = ['Mom', 'Dad', 'Ham']\n", 167 | "consonants = {\n", 168 | " char\n", 169 | " for word in words\n", 170 | " for char in word.lower()\n", 171 | " if char not in 'aeiou'\n", 172 | "}\n", 173 | "consonants" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "id": "c2a04f7f", 179 | "metadata": {}, 180 | "source": [ 181 | "### As for-loop" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 7, 187 | "id": "f06725ca", 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "{'d', 'h', 'm'}" 194 | ] 195 | }, 196 | "execution_count": 7, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "consonants = set()\n", 203 | "for word in words:\n", 204 | " for char in word.lower():\n", 205 | " if char not in 'aeiou':\n", 206 | " consonants.add(char)\n", 207 | "consonants" 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "jupytext": { 213 | "cell_metadata_filter": "-all", 214 | "formats": "auto:light,ipynb", 215 | "main_language": "python", 216 | "notebook_metadata_filter": "-all" 217 | }, 218 | "kernelspec": { 219 | "display_name": "Python 3 (ipykernel)", 220 | "language": "python", 221 | "name": "python3" 222 | }, 223 | "language_info": { 224 | "codemirror_mode": { 225 | "name": "ipython", 226 | "version": 3 227 | }, 228 | "file_extension": ".py", 229 | "mimetype": "text/x-python", 230 | "name": "python", 231 | "nbconvert_exporter": "python", 232 | "pygments_lexer": "ipython3", 233 | "version": "3.11.4" 234 | } 235 | }, 236 | "nbformat": 4, 237 | "nbformat_minor": 5 238 | } 239 | -------------------------------------------------------------------------------- /content/examples/ex_08_generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6856b48d", 6 | "metadata": {}, 7 | "source": [ 8 | "# Generator comprehensions\n", 9 | "If your main purpose is looping over a sequence and you don't need a list object, using a generator will save memory and can be a lot faster to run." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "cc3fe503", 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | " at 0x104a6f6b0>\n", 23 | "0\n", 24 | "1\n", 25 | "4\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "squares = (i ** 2 for i in range(10*2))\n", 31 | "print(squares)\n", 32 | "print(next(squares))\n", 33 | "print(next(squares))\n", 34 | "print(next(squares))" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "id": "9a5b63f1", 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "9\n", 48 | "16\n", 49 | "25\n", 50 | "36\n", 51 | "49\n", 52 | "64\n", 53 | "81\n", 54 | "100\n", 55 | "121\n", 56 | "144\n", 57 | "169\n", 58 | "196\n", 59 | "225\n", 60 | "256\n", 61 | "289\n", 62 | "324\n", 63 | "361\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "for num in squares:\n", 69 | " print(num)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "7c6bacb0", 75 | "metadata": {}, 76 | "source": [ 77 | "Generators can only be looped over once" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "id": "0bea45b5", 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "for num in squares:\n", 88 | " print(num)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "id": "39bc0e12", 94 | "metadata": {}, 95 | "source": [ 96 | "## Comparing speed" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 4, 102 | "id": "3eedf5e2", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "from time import time" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 5, 112 | "id": "c704b3b5", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "'Took 0.0001s'" 119 | ] 120 | }, 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "start = time()\n", 128 | "big_gen = (i for i in range(10**10))\n", 129 | "end = time()\n", 130 | "f'Took {round(end - start, 4)}s'" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 6, 136 | "id": "e4b4e5e7", 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "'Took 1.2816s'" 143 | ] 144 | }, 145 | "execution_count": 6, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "start = time()\n", 152 | "big_list = [i for i in range(10**8)] # Initializing can take a long time and use a lot of memory\n", 153 | "end = time()\n", 154 | "f'Took {round(end - start, 4)}s'" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "id": "1d32ad19", 160 | "metadata": {}, 161 | "source": [ 162 | "They can be used in certain functions without creating a list.\n", 163 | "\n", 164 | "`any()`, `all()`, `min()`, `max()` and `sum()` can all accept generators." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 7, 170 | "id": "546033ae", 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "1.7427762621924359e-07\n", 178 | "0.9999999041851108\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "from random import random\n", 184 | "\n", 185 | "print(min((random() for i in range(10**7))))\n", 186 | "print(max((random() for i in range(10**7))))" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "id": "3b1c15fe", 192 | "metadata": {}, 193 | "source": [ 194 | "To create a tuple, cast a generator using the `tuple()`." 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "id": "6a96a5ac", 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "(3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27)" 207 | ] 208 | }, 209 | "execution_count": 8, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "tuple(i for i in range(1, 30) if i % 3 == 0 or i % 5 == 0)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "id": "ebeed49e", 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [] 225 | } 226 | ], 227 | "metadata": { 228 | "jupytext": { 229 | "cell_metadata_filter": "-all", 230 | "formats": "auto:light,ipynb", 231 | "main_language": "python", 232 | "notebook_metadata_filter": "-all" 233 | }, 234 | "kernelspec": { 235 | "display_name": "Python 3 (ipykernel)", 236 | "language": "python", 237 | "name": "python3" 238 | }, 239 | "language_info": { 240 | "codemirror_mode": { 241 | "name": "ipython", 242 | "version": 3 243 | }, 244 | "file_extension": ".py", 245 | "mimetype": "text/x-python", 246 | "name": "python", 247 | "nbconvert_exporter": "python", 248 | "pygments_lexer": "ipython3", 249 | "version": "3.11.4" 250 | } 251 | }, 252 | "nbformat": 4, 253 | "nbformat_minor": 5 254 | } 255 | -------------------------------------------------------------------------------- /content/examples/ex_09_array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c3a9c03a", 6 | "metadata": {}, 7 | "source": [ 8 | "# array.array\n", 9 | "[Documentation](https://docs.python.org/3/library/array.html)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "a8a19782", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import array" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "id": "a7006a70", 25 | "metadata": {}, 26 | "source": [ 27 | "Must declare the type of data it holds:\n", 28 | "- `'f'` - float\n", 29 | "- `'i'` - integer\n", 30 | "- `'u'` - Unicode character\n", 31 | "\n", 32 | "More types in [documentation](https://docs.python.org/3/library/array.html)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "id": "497aef61", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "array('f', [1.0, 1.5, 2.0, 2.5])" 45 | ] 46 | }, 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "arr_f = array.array(\"f\", (1.0, 1.5, 2.0, 2.5))\n", 54 | "arr_f" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "fb04a0c1", 60 | "metadata": {}, 61 | "source": [ 62 | "### Arrays have many similar list methods" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "id": "42baa2c8", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "array('f', [1.0, 1.5, 2.0, 2.5, 3.0])" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "arr_f.append(3.0)\n", 84 | "arr_f" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "id": "ed6e2359", 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "array('f', [0.5, 1.0, 1.5, 2.0, 2.5, 3.0])" 97 | ] 98 | }, 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "arr_f.insert(0, 0.5)\n", 106 | "arr_f" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 5, 112 | "id": "16e596a2", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "array('f', [3.0, 2.5, 2.0, 1.5, 1.0, 0.5])" 119 | ] 120 | }, 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "arr_f.reverse()\n", 128 | "arr_f" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "id": "7d1208b1", 134 | "metadata": {}, 135 | "source": [ 136 | "### And a few conversion methods" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 6, 142 | "id": "7e2e7d6f", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "[3.0, 2.5, 2.0, 1.5, 1.0, 0.5]" 149 | ] 150 | }, 151 | "execution_count": 6, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "arr_f.tolist()" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 7, 163 | "id": "ea27c0c5", 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "b'\\x00\\x00@@\\x00\\x00 @\\x00\\x00\\x00@\\x00\\x00\\xc0?\\x00\\x00\\x80?\\x00\\x00\\x00?'" 170 | ] 171 | }, 172 | "execution_count": 7, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "arr_f.tobytes()" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "id": "3e89fa56", 184 | "metadata": {}, 185 | "source": [ 186 | "### Working with files" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "id": "5abc6e79", 192 | "metadata": {}, 193 | "source": [ 194 | "You can save arrays to a file as bytes, so use binary `'b'` mode. " 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "id": "82cee028", 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "with open('array', 'wb') as file:\n", 205 | " arr_f.tofile(file)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 9, 211 | "id": "0761f889", 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "data": { 216 | "text/plain": [ 217 | "array('f', [-1.0, -1.5])" 218 | ] 219 | }, 220 | "execution_count": 9, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "arr_f2 = array.array(\"f\", (-1.0, -1.5))\n", 227 | "arr_f2" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "id": "ec2d55b0", 233 | "metadata": {}, 234 | "source": [ 235 | "`.fromfile(file, n)` appends `n` items from the file onto the existing array." 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 10, 241 | "id": "5a13241c", 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "array('f', [-1.0, -1.5, 3.0, 2.5, 2.0])" 248 | ] 249 | }, 250 | "execution_count": 10, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "with open('array', 'rb') as file:\n", 257 | " arr_f2.fromfile(file, 3)\n", 258 | "arr_f2" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "id": "7b7d0887", 264 | "metadata": {}, 265 | "source": [ 266 | "## Working with chars\n", 267 | "The `'u'` type stores Unicode characters and prints them like a string (instead of list)." 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 11, 273 | "id": "f5ca2c03", 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "array('u', 'I 💚 🐍')" 280 | ] 281 | }, 282 | "execution_count": 11, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "arr_u = array.array('u', 'I 💚 🐍')\n", 289 | "arr_u" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 12, 295 | "id": "88f8e6e1", 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "data": { 300 | "text/plain": [ 301 | "array('u', 'I 💚 🐍!')" 302 | ] 303 | }, 304 | "execution_count": 12, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "arr_u.append('!')\n", 311 | "arr_u" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 13, 317 | "id": "ccdd9ddb", 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "\n", 325 | "I 💚 🐍!\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "as_u = arr_u.tounicode() # Only works with 'u' type\n", 331 | "print(type(as_u))\n", 332 | "print(as_u)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "id": "cea7dd31", 338 | "metadata": {}, 339 | "source": [ 340 | "## Size specific integers\n", 341 | "In most other languages, integers have a minimum and maximum value.\n", 342 | "Python increases the memory for integers dynamically behind the scenes, so there is no minimum or maximum value.\n", 343 | "\n", 344 | "Since arrays assign a fixed amount of memory for each item, you must specify the number of bytes allocated for integers arrays and whether or not they can store negative numbers (signed vs unsigned).\n", 345 | "\n", 346 | "Type to byte list:\n", 347 | "- `'b'` and `'B'` (char): 1 byte\n", 348 | "- `'h'` and `'H'` (short): 2 bytes\n", 349 | "- `'i'` and `'I'` (int): 2 bytes\n", 350 | "- `'h'` and `'H'` (long): 4 bytes\n", 351 | "- `'l'` and `'L'` (long long): 8 bytes\n", 352 | "- `'d'` (double): 8 bytes\n", 353 | "\n", 354 | "### 'B': unsigned char\n", 355 | "This is an unintuitive name. It's basically 1 byte of data as an integer" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 14, 361 | "id": "a0ce64bc", 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "data": { 366 | "text/plain": [ 367 | "array('B', [0, 255])" 368 | ] 369 | }, 370 | "execution_count": 14, 371 | "metadata": {}, 372 | "output_type": "execute_result" 373 | } 374 | ], 375 | "source": [ 376 | "smallest = 0\n", 377 | "largest = 2**8 - 1\n", 378 | "arr_B = array.array(\"B\", (smallest, largest))\n", 379 | "arr_B" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 15, 385 | "id": "8577e16e", 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "OverflowError('unsigned byte integer is greater than maximum')\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "try:\n", 398 | " arr_B.append(largest + 1)\n", 399 | "except Exception as e:\n", 400 | " print(repr(e))" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 16, 406 | "id": "45d09d6c", 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "name": "stdout", 411 | "output_type": "stream", 412 | "text": [ 413 | "OverflowError('unsigned byte integer is less than minimum')\n" 414 | ] 415 | } 416 | ], 417 | "source": [ 418 | "try:\n", 419 | " arr_B.append(smallest - 1)\n", 420 | "except Exception as e:\n", 421 | " print(repr(e))" 422 | ] 423 | }, 424 | { 425 | "cell_type": "markdown", 426 | "id": "eab908b6", 427 | "metadata": {}, 428 | "source": [ 429 | "### 'b': signed char\n", 430 | "Like unsigned char but one bit is used to store the sign." 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 17, 436 | "id": "6a5cfd84", 437 | "metadata": {}, 438 | "outputs": [ 439 | { 440 | "data": { 441 | "text/plain": [ 442 | "array('b', [-128, 127])" 443 | ] 444 | }, 445 | "execution_count": 17, 446 | "metadata": {}, 447 | "output_type": "execute_result" 448 | } 449 | ], 450 | "source": [ 451 | "smallest = -(2**7)\n", 452 | "largest = 2**7 - 1\n", 453 | "arr_f2 = array.array(\"b\", (smallest, largest))\n", 454 | "arr_f2" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 18, 460 | "id": "26da8ba7", 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "name": "stdout", 465 | "output_type": "stream", 466 | "text": [ 467 | "OverflowError('signed char is greater than maximum')\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "try:\n", 473 | " arr_f2.append(largest + 1)\n", 474 | "except Exception as e:\n", 475 | " print(repr(e))" 476 | ] 477 | }, 478 | { 479 | "cell_type": "markdown", 480 | "id": "06d218eb", 481 | "metadata": {}, 482 | "source": [ 483 | "### 'I': unsigned int\n", 484 | "2 bytes" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 19, 490 | "id": "3fd7c516", 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "data": { 495 | "text/plain": [ 496 | "array('I', [0, 4294967295])" 497 | ] 498 | }, 499 | "execution_count": 19, 500 | "metadata": {}, 501 | "output_type": "execute_result" 502 | } 503 | ], 504 | "source": [ 505 | "smallest = 0\n", 506 | "largest = 2**32 - 1\n", 507 | "arr_I = array.array(\"I\", (smallest, largest))\n", 508 | "arr_I" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 20, 514 | "id": "42b7692e", 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "name": "stdout", 519 | "output_type": "stream", 520 | "text": [ 521 | "OverflowError('unsigned int is greater than maximum')\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "try:\n", 527 | " arr_I.append(largest + 1)\n", 528 | "except Exception as e:\n", 529 | " print(repr(e))" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": 21, 535 | "id": "7a80f287", 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "name": "stdout", 540 | "output_type": "stream", 541 | "text": [ 542 | "OverflowError(\"can't convert negative value to unsigned int\")\n" 543 | ] 544 | } 545 | ], 546 | "source": [ 547 | "try:\n", 548 | " arr_I.append(smallest - 1)\n", 549 | "except Exception as e:\n", 550 | " print(repr(e))" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "id": "19cd1f75", 556 | "metadata": {}, 557 | "source": [ 558 | "### 'i': signed integer\n", 559 | "2 bytes, with 1 bit to store the sign" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 22, 565 | "id": "a7551ab4", 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "array('i', [-2147483648, 2147483647])" 572 | ] 573 | }, 574 | "execution_count": 22, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "smallest = -(2**31)\n", 581 | "largest = 2**31 - 1\n", 582 | "arr_i = array.array(\"i\", (smallest, largest))\n", 583 | "arr_i" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 23, 589 | "id": "e191dbdd", 590 | "metadata": {}, 591 | "outputs": [ 592 | { 593 | "name": "stdout", 594 | "output_type": "stream", 595 | "text": [ 596 | "OverflowError('signed integer is greater than maximum')\n" 597 | ] 598 | } 599 | ], 600 | "source": [ 601 | "try:\n", 602 | " arr_i.append(largest + 1)\n", 603 | "except Exception as e:\n", 604 | " print(repr(e))" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": 24, 610 | "id": "749892ff", 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "OverflowError('signed integer is less than minimum')\n" 618 | ] 619 | } 620 | ], 621 | "source": [ 622 | "try:\n", 623 | " arr_i.append(smallest - 1)\n", 624 | "except Exception as e:\n", 625 | " print(repr(e))" 626 | ] 627 | } 628 | ], 629 | "metadata": { 630 | "jupytext": { 631 | "cell_metadata_filter": "-all", 632 | "formats": "auto:light,ipynb", 633 | "main_language": "python", 634 | "notebook_metadata_filter": "-all" 635 | }, 636 | "kernelspec": { 637 | "display_name": "Python 3 (ipykernel)", 638 | "language": "python", 639 | "name": "python3" 640 | }, 641 | "language_info": { 642 | "codemirror_mode": { 643 | "name": "ipython", 644 | "version": 3 645 | }, 646 | "file_extension": ".py", 647 | "mimetype": "text/x-python", 648 | "name": "python", 649 | "nbconvert_exporter": "python", 650 | "pygments_lexer": "ipython3", 651 | "version": "3.11.4" 652 | } 653 | }, 654 | "nbformat": 4, 655 | "nbformat_minor": 5 656 | } 657 | -------------------------------------------------------------------------------- /content/examples/ex_10_namedtuple.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b96222eb-c6d6-4a99-8345-71f324bfd283", 6 | "metadata": {}, 7 | "source": [ 8 | "# collections.namedtuple\n", 9 | "\n", 10 | "A lightweight class that acts like a tuple with a type name and attributes." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "bd3a527a-9438-47e8-ac0e-19a252a5e02a", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "from collections import namedtuple" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "bfc59467-b978-4da6-a719-69dd13624fbb", 26 | "metadata": {}, 27 | "source": [ 28 | "Define a \"class\" with a name and attributes." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "id": "81e30457-2fb8-4931-9155-7d4ef56e7c60", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "Coords = namedtuple(\"Location\", \"name lat lon\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "f85c368a-c984-414b-94d6-88a3ed512afd", 44 | "metadata": {}, 45 | "source": [ 46 | "Create an \"instance\" of the class." 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "id": "7725a970-2681-473b-a3a3-be6d871f0262", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "Location(name='London', lat=42.99, lon=-81.243)" 59 | ] 60 | }, 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "c = Coords('London', 42.99, -81.243)\n", 68 | "c" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "3c9ece39-b54c-4fc0-9f2e-6abd9b6ecec2", 74 | "metadata": {}, 75 | "source": [ 76 | "You can interact with the instance as if it were a tuple." 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "id": "3f451a6d-e07f-46b0-803c-d018259fdc52", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'London'" 89 | ] 90 | }, 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "c[0]" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "ce85f872-3615-4edf-9428-3478dd7c9931", 103 | "metadata": {}, 104 | "source": [ 105 | "You can also access the named attributes." 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "id": "783fb82b-3017-424d-bfa9-52ebc801888f", 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "data": { 116 | "text/plain": [ 117 | "'London'" 118 | ] 119 | }, 120 | "execution_count": 5, 121 | "metadata": {}, 122 | "output_type": "execute_result" 123 | } 124 | ], 125 | "source": [ 126 | "c.name" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 6, 132 | "id": "a7d94b61-2579-4d1b-80a7-fff1aac4b85d", 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "42.99" 139 | ] 140 | }, 141 | "execution_count": 6, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "c.lat" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "id": "93396285-b89b-4395-91aa-2680656111b2", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "__main__.Location" 160 | ] 161 | }, 162 | "execution_count": 7, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "type(c)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 8, 174 | "id": "d7375a53-0790-45c5-aa6f-31f2b15bd03c", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "TypeError(\"'Location' object does not support item assignment\")\n" 182 | ] 183 | } 184 | ], 185 | "source": [ 186 | "try:\n", 187 | " c[1] += 1\n", 188 | "except Exception as e:\n", 189 | " print(repr(e))" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "id": "69446e6b-79f9-45d7-bd81-0e1d1907bb5e", 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 3 (ipykernel)", 204 | "language": "python", 205 | "name": "python3" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 3 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython3", 217 | "version": "3.11.4" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 5 222 | } 223 | -------------------------------------------------------------------------------- /content/examples/ex_11_ordered_dict.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "69635744", 6 | "metadata": {}, 7 | "source": [ 8 | "# collections.OrderedDict" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "cc0e011f", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "from collections import OrderedDict" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "6b1a42bc", 24 | "metadata": {}, 25 | "source": [ 26 | "## Create\n", 27 | "### Empty dict" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "id": "d3e75ad1", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "OrderedDict()" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "a = OrderedDict()\n", 49 | "a" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "id": "646cba33", 55 | "metadata": {}, 56 | "source": [ 57 | "### From a dict" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "id": "69606673", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "OrderedDict([(1, 'a'), (2, 'b')])" 70 | ] 71 | }, 72 | "execution_count": 3, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "b = OrderedDict({1: 'a', 2: 'b'})\n", 79 | "b" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "2f4df4a4", 85 | "metadata": {}, 86 | "source": [ 87 | "### From a sequence\n", 88 | "Each item in the sequence must be another sequence with length 2" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "id": "f3ff07f5", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "OrderedDict([('a', 'apple'), ('banana', 'b'), ('c', 'cherry')])" 101 | ] 102 | }, 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "c = OrderedDict([['a', 'apple'], {'b', 'banana'}, ('c', 'cherry')])\n", 110 | "c" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "id": "d78ef9f2", 116 | "metadata": {}, 117 | "source": [ 118 | "## Use like a dictionary\n", 119 | "`OrderedDict` has all the same methods as `dict`." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 5, 125 | "id": "c0ce21e7", 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "OrderedDict([(1, 'a'), (2, 'b'), (0, 'z')])" 132 | ] 133 | }, 134 | "execution_count": 5, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "b[0] = 'z'\n", 141 | "b" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 6, 147 | "id": "b4cbac45", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "z\n", 155 | "a\n", 156 | "b\n", 157 | "Not found\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "for i in range(4):\n", 163 | " print(b.get(i, 'Not found'))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "e3b4d662", 169 | "metadata": {}, 170 | "source": [ 171 | "## Iteration order\n", 172 | "When iterating, it retains memory of insertion order" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 7, 178 | "id": "6b6d9cd3", 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "1: a\n", 186 | "2: b\n", 187 | "0: z\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "for key, value in b.items():\n", 193 | " print(f'{key}: {value}')" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "id": "d90cc992", 199 | "metadata": {}, 200 | "source": [ 201 | "## Pop item\n", 202 | "Additional method to remove the last key-value pair and return it." 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 8, 208 | "id": "d5b7e0e2", 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "(0, 'z')" 215 | ] 216 | }, 217 | "execution_count": 8, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "b.popitem()" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 9, 229 | "id": "19f7d533", 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "data": { 234 | "text/plain": [ 235 | "'b'" 236 | ] 237 | }, 238 | "execution_count": 9, 239 | "metadata": {}, 240 | "output_type": "execute_result" 241 | } 242 | ], 243 | "source": [ 244 | "b.pop(2)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "id": "ccf95467", 250 | "metadata": {}, 251 | "source": [ 252 | "## Reorder items\n", 253 | "`.move_to_end(key, last=True)` will move the item with `key` either to the end (`last=True`) or the front (`last=False`)." 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 10, 259 | "id": "5bbe12ca", 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "data": { 264 | "text/plain": [ 265 | "OrderedDict([(1, 'a')])" 266 | ] 267 | }, 268 | "execution_count": 10, 269 | "metadata": {}, 270 | "output_type": "execute_result" 271 | } 272 | ], 273 | "source": [ 274 | "b.move_to_end(1)\n", 275 | "b" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 11, 281 | "id": "95893dfb", 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "KeyError(0)\n" 289 | ] 290 | } 291 | ], 292 | "source": [ 293 | "try:\n", 294 | " b.move_to_end(0, last=False)\n", 295 | "except Exception as e:\n", 296 | " print(repr(e))" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 12, 302 | "id": "5aa712c2", 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "1: a\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "for key, value in b.items():\n", 315 | " print(f'{key}: {value}')" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "id": "effa03db", 321 | "metadata": {}, 322 | "source": [ 323 | "## When to use OrderedDict over dict\n", 324 | "`dict` is has been ordered since Python3.6.\n", 325 | "\n", 326 | "You should still use `OrderedDict` to:\n", 327 | "- Clearly state that the order is important\n", 328 | "- Use the `.move_to_end()` method\n", 329 | "- Test dictionary equality based on order\n", 330 | "- Maintain backwards compatibility with Python < 3.6" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "id": "444282ef", 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [] 340 | } 341 | ], 342 | "metadata": { 343 | "jupytext": { 344 | "cell_metadata_filter": "-all", 345 | "formats": "auto:light,ipynb", 346 | "main_language": "python", 347 | "notebook_metadata_filter": "-all" 348 | }, 349 | "kernelspec": { 350 | "display_name": "Python 3 (ipykernel)", 351 | "language": "python", 352 | "name": "python3" 353 | }, 354 | "language_info": { 355 | "codemirror_mode": { 356 | "name": "ipython", 357 | "version": 3 358 | }, 359 | "file_extension": ".py", 360 | "mimetype": "text/x-python", 361 | "name": "python", 362 | "nbconvert_exporter": "python", 363 | "pygments_lexer": "ipython3", 364 | "version": "3.11.4" 365 | } 366 | }, 367 | "nbformat": 4, 368 | "nbformat_minor": 5 369 | } 370 | -------------------------------------------------------------------------------- /content/examples/ex_12_default_dict.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c148a471", 6 | "metadata": {}, 7 | "source": [ 8 | "# collections.defaultdict\n", 9 | "A common pattern is to check if a key exists in a dictionary and initialize it if not.\n", 10 | "## Without defaultdict" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "a990d3e6", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "{'U': ['United States'],\n", 23 | " 'M': ['Mexico'],\n", 24 | " 'I': ['Iceland', 'India', 'Indonesia'],\n", 25 | " 'P': ['Philippines']}" 26 | ] 27 | }, 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "output_type": "execute_result" 31 | } 32 | ], 33 | "source": [ 34 | "countries = [\"United States\", \"Mexico\", \"Iceland\", \"India\", \"Philippines\", \"Indonesia\"]\n", 35 | "\n", 36 | "letter_countries = {}\n", 37 | "for country in countries:\n", 38 | " first_letter = country[0]\n", 39 | " if first_letter not in letter_countries:\n", 40 | " letter_countries[first_letter] = []\n", 41 | " letter_countries[first_letter].append(country)\n", 42 | "\n", 43 | "letter_countries" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "c39a1314", 49 | "metadata": {}, 50 | "source": [ 51 | "## With defaultdict" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "id": "ce0187c4", 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "defaultdict(list,\n", 64 | " {'U': ['United States'],\n", 65 | " 'M': ['Mexico'],\n", 66 | " 'I': ['Iceland', 'India', 'Indonesia'],\n", 67 | " 'P': ['Philippines']})" 68 | ] 69 | }, 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "from collections import defaultdict\n", 77 | "\n", 78 | "letter_countries = defaultdict(list)\n", 79 | "for country in countries:\n", 80 | " letter_countries[country[0]].append(country)\n", 81 | "\n", 82 | "letter_countries" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "abb978f9", 88 | "metadata": {}, 89 | "source": [ 90 | "## defaultdict\n", 91 | "Like regular dictionaries, but any missing keys have a default value if you try to access them.\n", 92 | "\n", 93 | "You can pass in any callable that doesn't have any required arguments.\n", 94 | "\n", 95 | "A callable can be:\n", 96 | "- a builtin function\n", 97 | "- a user-defined function\n", 98 | "- a lambda function\n", 99 | "- a class\n", 100 | "- a module function\n", 101 | "- an object's method" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 3, 107 | "id": "24878caf", 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "from collections import defaultdict" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "id": "61248aca", 117 | "metadata": {}, 118 | "source": [ 119 | "## Builtin functions\n", 120 | "The builtin types all have associated functions: `int`, `float`, `str`, `list`, `dict`, `tuple`, `set`.\n", 121 | "\n", 122 | "When you call them without arguments, they return an empty container or 0." 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 4, 128 | "id": "1f9b7a22", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "defaultdict(int, {})" 135 | ] 136 | }, 137 | "execution_count": 4, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "word_counts = defaultdict(int)\n", 144 | "word_counts" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "id": "bbabdaa1", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "0" 157 | ] 158 | }, 159 | "execution_count": 5, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "word_counts['the']" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 6, 171 | "id": "51b1369b", 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "defaultdict(int, {'the': 0})" 178 | ] 179 | }, 180 | "execution_count": 6, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "word_counts" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 7, 192 | "id": "b1677830", 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "defaultdict(int,\n", 199 | " {'the': 3,\n", 200 | " 'quick': 1,\n", 201 | " 'brown': 2,\n", 202 | " 'fox': 1,\n", 203 | " 'jumps': 1,\n", 204 | " 'over': 1,\n", 205 | " 'lazy': 1,\n", 206 | " 'dog': 1,\n", 207 | " 'and': 1,\n", 208 | " 'cat': 1})" 209 | ] 210 | }, 211 | "execution_count": 7, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "phrase = \"the quick brown fox jumps over the lazy dog and the brown cat\"\n", 218 | "for word in phrase.split():\n", 219 | " word_counts[word] += 1\n", 220 | "word_counts" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "id": "50ad93c2", 226 | "metadata": {}, 227 | "source": [ 228 | "## Custom functions\n", 229 | "You can define the function to call. It shouldn't take any arguments." 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 8, 235 | "id": "5a1028a6", 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "1\n", 243 | "2\n", 244 | "3\n", 245 | "4\n" 246 | ] 247 | }, 248 | { 249 | "data": { 250 | "text/plain": [ 251 | "defaultdict(, {'a': 1, 'b': 2, 'c': 3, 'd': 4})" 252 | ] 253 | }, 254 | "execution_count": 8, 255 | "metadata": {}, 256 | "output_type": "execute_result" 257 | } 258 | ], 259 | "source": [ 260 | "count = 0\n", 261 | "\n", 262 | "def get_count():\n", 263 | " global count\n", 264 | " count += 1\n", 265 | " return count\n", 266 | "\n", 267 | "a = defaultdict(get_count) # No brackets after function\n", 268 | "for i in 'abcd':\n", 269 | " print(a[i])\n", 270 | " \n", 271 | "a" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "id": "dc892d9c", 277 | "metadata": {}, 278 | "source": [ 279 | "## Lambda functions\n", 280 | "For simple one-line functions that you won't need again, you can use lambda (anonymous) functions." 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 9, 286 | "id": "3ed6e1d2", 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "data": { 291 | "text/plain": [ 292 | "defaultdict(()>, {'A': [-2, 0], 'B': [1, 1]})" 293 | ] 294 | }, 295 | "execution_count": 9, 296 | "metadata": {}, 297 | "output_type": "execute_result" 298 | } 299 | ], 300 | "source": [ 301 | "b = defaultdict(lambda: [0, 0])\n", 302 | "\n", 303 | "sprites = {'A': ['L', 'D', 'L', 'U'], 'B': ['R', 'U']}\n", 304 | "\n", 305 | "for sprite, moves in sprites.items():\n", 306 | " for move in moves:\n", 307 | " if move == 'L':\n", 308 | " b[sprite][0] -= 1\n", 309 | " elif move == 'R':\n", 310 | " b[sprite][0] += 1\n", 311 | " elif move == 'D':\n", 312 | " b[sprite][1] -= 1\n", 313 | " elif move == 'U':\n", 314 | " b[sprite][1] += 1\n", 315 | " else:\n", 316 | " print(f\"Invalid move {move}\")\n", 317 | "b" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "id": "f3a0b62b", 323 | "metadata": {}, 324 | "source": [ 325 | "## Class" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 10, 331 | "id": "f32ecfef", 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "data": { 336 | "text/plain": [ 337 | "defaultdict(__main__.Person,\n", 338 | " {0: Person(\"Aida\"),\n", 339 | " 1: Person(\"Jia\"),\n", 340 | " 2: Person(\"Lárus\"),\n", 341 | " 3: Person(\"Idrissa\")})" 342 | ] 343 | }, 344 | "execution_count": 10, 345 | "metadata": {}, 346 | "output_type": "execute_result" 347 | } 348 | ], 349 | "source": [ 350 | "class Person:\n", 351 | " def __init__(self, name=''):\n", 352 | " self.name = name\n", 353 | " \n", 354 | " def __repr__(self):\n", 355 | " return f'Person(\"{self.name}\")'\n", 356 | "\n", 357 | "c = defaultdict(Person)\n", 358 | "\n", 359 | "names = [\"Aida\", \"Jia\", \"Lárus\", \"Idrissa\"]\n", 360 | "for i, name in enumerate(names):\n", 361 | " c[i].name = name\n", 362 | "\n", 363 | "c" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "id": "1a39c655", 369 | "metadata": {}, 370 | "source": [ 371 | "## Module function" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 11, 377 | "id": "c0f7e126", 378 | "metadata": { 379 | "lines_to_next_cell": 2 380 | }, 381 | "outputs": [ 382 | { 383 | "name": "stdout", 384 | "output_type": "stream", 385 | "text": [ 386 | "1684356252.131718\n", 387 | "1684356253.1320431\n", 388 | "1684356254.1328871\n", 389 | "1684356255.13824\n" 390 | ] 391 | }, 392 | { 393 | "data": { 394 | "text/plain": [ 395 | "defaultdict(,\n", 396 | " {0: 1684356252.131718,\n", 397 | " 1: 1684356253.1320431,\n", 398 | " 2: 1684356254.1328871,\n", 399 | " 3: 1684356255.13824})" 400 | ] 401 | }, 402 | "execution_count": 11, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "from time import (\n", 409 | " time, # Gets current time in secs since the Epoch (00:00 Jan 1, 1970 UTC)\n", 410 | " sleep, # Wait n seconds\n", 411 | ")\n", 412 | "\n", 413 | "d = defaultdict(time)\n", 414 | "for i in range(4):\n", 415 | " print(d[i])\n", 416 | " sleep(1)\n", 417 | "d" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "id": "bdd10c06", 423 | "metadata": {}, 424 | "source": [ 425 | "## Method\n", 426 | "I don't know a good reason to do this, but it can be done!" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 12, 432 | "id": "7b049187", 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "data": { 437 | "text/plain": [ 438 | "'c'" 439 | ] 440 | }, 441 | "execution_count": 12, 442 | "metadata": {}, 443 | "output_type": "execute_result" 444 | } 445 | ], 446 | "source": [ 447 | "chars = \"a b c\".split()\n", 448 | "e = defaultdict(chars.pop)\n", 449 | "e[0]" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 13, 455 | "id": "6a781afe", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "data": { 460 | "text/plain": [ 461 | "['a', 'b', 'd']" 462 | ] 463 | }, 464 | "execution_count": 13, 465 | "metadata": {}, 466 | "output_type": "execute_result" 467 | } 468 | ], 469 | "source": [ 470 | "chars.append('d')\n", 471 | "chars" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 14, 477 | "id": "279dd29d", 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "c\n", 485 | "d\n", 486 | "b\n" 487 | ] 488 | }, 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "defaultdict(, {0: 'c', 1: 'd', 2: 'b'})" 493 | ] 494 | }, 495 | "execution_count": 14, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "for i in range(3):\n", 502 | " print(e[i])\n", 503 | "e" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 15, 509 | "id": "1ff6bbd2", 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "data": { 514 | "text/plain": [ 515 | "['a']" 516 | ] 517 | }, 518 | "execution_count": 15, 519 | "metadata": {}, 520 | "output_type": "execute_result" 521 | } 522 | ], 523 | "source": [ 524 | "chars" 525 | ] 526 | } 527 | ], 528 | "metadata": { 529 | "jupytext": { 530 | "cell_metadata_filter": "-all", 531 | "formats": "auto:light,ipynb", 532 | "main_language": "python", 533 | "notebook_metadata_filter": "-all" 534 | }, 535 | "kernelspec": { 536 | "display_name": "Python 3 (ipykernel)", 537 | "language": "python", 538 | "name": "python3" 539 | }, 540 | "language_info": { 541 | "codemirror_mode": { 542 | "name": "ipython", 543 | "version": 3 544 | }, 545 | "file_extension": ".py", 546 | "mimetype": "text/x-python", 547 | "name": "python", 548 | "nbconvert_exporter": "python", 549 | "pygments_lexer": "ipython3", 550 | "version": "3.11.4" 551 | } 552 | }, 553 | "nbformat": 4, 554 | "nbformat_minor": 5 555 | } 556 | -------------------------------------------------------------------------------- /content/examples/ex_13_chain_map.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "cd41e0da", 6 | "metadata": {}, 7 | "source": [ 8 | "# collections.ChainMap\n", 9 | "Search through multiple dictionaries at once." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "96763fa4", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "from collections import ChainMap" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "58904e3f", 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "dict1 = {\"one\": 1, \"two\": 2, \"three\": 33}\n", 30 | "dict2 = {\"three\": 3, \"four\": 4}" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "id": "cf08775f", 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "ChainMap({'one': 1, 'two': 2, 'three': 33}, {'three': 3, 'four': 4})" 43 | ] 44 | }, 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "chain = ChainMap(dict1, dict2)\n", 52 | "chain" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "49d3c3ba", 58 | "metadata": {}, 59 | "source": [ 60 | "## Key lookup\n", 61 | "Searches for keys in order." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "id": "b444d662", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "33" 74 | ] 75 | }, 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "chain[\"three\"]" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 5, 88 | "id": "da9ba89c", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "4" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "chain.get(\"four\")" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "id": "3a7af329", 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "'-'" 116 | ] 117 | }, 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "chain.get(\"five\", \"-\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "id": "cda35a48", 130 | "metadata": {}, 131 | "source": [ 132 | "## Updates\n", 133 | "Updating existing keys affects the first one found." 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 7, 139 | "id": "a70b17a8", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "ChainMap({'one': 1, 'two': 2, 'three': -3}, {'three': 3, 'four': 4})" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "chain[\"three\"] = -3\n", 155 | "chain" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 8, 161 | "id": "cdde2275", 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "ChainMap({'one': 1, 'two': 2}, {'three': 3, 'four': 4})" 168 | ] 169 | }, 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "del chain[\"three\"]\n", 177 | "chain" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "id": "f55464a0", 183 | "metadata": {}, 184 | "source": [ 185 | "Adding new items adds them to the first `dict`." 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 9, 191 | "id": "d63be647", 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "data": { 196 | "text/plain": [ 197 | "ChainMap({'one': 1, 'two': 2, 'six': 6}, {'three': 3, 'four': 4})" 198 | ] 199 | }, 200 | "execution_count": 9, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "chain[\"six\"] = 6\n", 207 | "chain" 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "jupytext": { 213 | "cell_metadata_filter": "-all", 214 | "formats": "auto:light,ipynb", 215 | "main_language": "python", 216 | "notebook_metadata_filter": "-all" 217 | }, 218 | "kernelspec": { 219 | "display_name": "Python 3 (ipykernel)", 220 | "language": "python", 221 | "name": "python3" 222 | }, 223 | "language_info": { 224 | "codemirror_mode": { 225 | "name": "ipython", 226 | "version": 3 227 | }, 228 | "file_extension": ".py", 229 | "mimetype": "text/x-python", 230 | "name": "python", 231 | "nbconvert_exporter": "python", 232 | "pygments_lexer": "ipython3", 233 | "version": "3.11.4" 234 | } 235 | }, 236 | "nbformat": 4, 237 | "nbformat_minor": 5 238 | } 239 | -------------------------------------------------------------------------------- /content/examples/ex_14_deque.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "1a5ce425", 6 | "metadata": {}, 7 | "source": [ 8 | "# Deque\n", 9 | "A builtin linked list that can be used for stacks and queues." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "fffc7c41", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "from collections import deque" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "a66b2e5e", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "deque([1, 2, 3])" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "a = deque([1, 2, 3])\n", 41 | "a" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "0202b10a", 47 | "metadata": {}, 48 | "source": [ 49 | "## Stack\n", 50 | "Last in, first out.\n", 51 | "### Using the tail as the top of the stack\n", 52 | "Use `.append()` and `.pop()` to add/remove from the end of the `list`/`deque`." 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "id": "451e7a56", 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "3" 65 | ] 66 | }, 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "last = a.pop()\n", 74 | "last" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "id": "eaf35e08", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "deque([1, 2, 4])" 87 | ] 88 | }, 89 | "execution_count": 4, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "a.append(4)\n", 96 | "a" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "id": "2efc5f23", 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "4\n", 110 | "2\n", 111 | "1\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "for _ in range(len(a)):\n", 117 | " print(a.pop())" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 6, 123 | "id": "5a79d624", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "IndexError('pop from an empty deque')\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "try:\n", 136 | " a.pop()\n", 137 | "except Exception as e:\n", 138 | " print(repr(e))" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "id": "655f5ce7", 144 | "metadata": {}, 145 | "source": [ 146 | "### Using the head as the top of the stack\n", 147 | "Use `.appendleft()` and `.popleft()` to add/remove from the front." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "id": "9339e086", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "deque([1, 2, 3])" 160 | ] 161 | }, 162 | "execution_count": 7, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "b = deque([1, 2, 3])\n", 169 | "b" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 8, 175 | "id": "cb10e674", 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "1" 182 | ] 183 | }, 184 | "execution_count": 8, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "first = b.popleft()\n", 191 | "first" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 9, 197 | "id": "d66710b3", 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "deque([4, 2, 3])" 204 | ] 205 | }, 206 | "execution_count": 9, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "b.appendleft(4)\n", 213 | "b" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 10, 219 | "id": "4e7f4e4f", 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "4\n", 227 | "2\n", 228 | "3\n" 229 | ] 230 | } 231 | ], 232 | "source": [ 233 | "for _ in range(len(b)):\n", 234 | " print(b.popleft())" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 11, 240 | "id": "63083de5", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "IndexError('pop from an empty deque')\n" 248 | ] 249 | } 250 | ], 251 | "source": [ 252 | "try:\n", 253 | " a.popleft()\n", 254 | "except Exception as e:\n", 255 | " print(repr(e))" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "id": "a452351d", 261 | "metadata": {}, 262 | "source": [ 263 | "## Create a custom stack or queue \n", 264 | "They generally use **push** and **pop** as method names.\n", 265 | "### Stack\n", 266 | "Last in, first out" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 12, 272 | "id": "c89e3bf6", 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "class Stack(deque):\n", 277 | " def push(self, val):\n", 278 | " self.appendleft(val)\n", 279 | " \n", 280 | " def pop(self):\n", 281 | " return self.popleft()\n", 282 | " \n", 283 | " # If you want to remove methods, override it and use pass or raise an Exception" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 13, 289 | "id": "24b63572", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "data": { 294 | "text/plain": [ 295 | "Stack([])" 296 | ] 297 | }, 298 | "execution_count": 13, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "my_stack = Stack()\n", 305 | "my_stack" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 14, 311 | "id": "e0504405", 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "data": { 316 | "text/plain": [ 317 | "Stack(['d', 'c', 'b', 'a'])" 318 | ] 319 | }, 320 | "execution_count": 14, 321 | "metadata": {}, 322 | "output_type": "execute_result" 323 | } 324 | ], 325 | "source": [ 326 | "for c in 'abcd':\n", 327 | " my_stack.push(c)\n", 328 | "my_stack" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 15, 334 | "id": "ab138440", 335 | "metadata": {}, 336 | "outputs": [ 337 | { 338 | "data": { 339 | "text/plain": [ 340 | "'d'" 341 | ] 342 | }, 343 | "execution_count": 15, 344 | "metadata": {}, 345 | "output_type": "execute_result" 346 | } 347 | ], 348 | "source": [ 349 | "my_stack.pop()" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 16, 355 | "id": "6fac654a", 356 | "metadata": {}, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "c\n", 363 | "b\n", 364 | "a\n" 365 | ] 366 | } 367 | ], 368 | "source": [ 369 | "for _ in range(len(my_stack)):\n", 370 | " print(my_stack.pop())" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "id": "7ad7ef8e", 376 | "metadata": {}, 377 | "source": [ 378 | "### Custom queue\n", 379 | "First in, first out" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 17, 385 | "id": "0e432803", 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "class Queue(deque):\n", 390 | " def push(self, val):\n", 391 | " self.appendleft(val)\n", 392 | " \n", 393 | " # .pop() is already implemented\n", 394 | " \n", 395 | " # If you want to remove methods, override it and use pass or raise an Exception" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 18, 401 | "id": "1650b1b1", 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "data": { 406 | "text/plain": [ 407 | "Queue([])" 408 | ] 409 | }, 410 | "execution_count": 18, 411 | "metadata": {}, 412 | "output_type": "execute_result" 413 | } 414 | ], 415 | "source": [ 416 | "my_q = Queue()\n", 417 | "my_q" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 19, 423 | "id": "cb225932", 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "data": { 428 | "text/plain": [ 429 | "Queue(['d', 'c', 'b', 'a'])" 430 | ] 431 | }, 432 | "execution_count": 19, 433 | "metadata": {}, 434 | "output_type": "execute_result" 435 | } 436 | ], 437 | "source": [ 438 | "for c in 'abcd':\n", 439 | " my_q.push(c)\n", 440 | "my_q" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 20, 446 | "id": "ce457d2c", 447 | "metadata": {}, 448 | "outputs": [ 449 | { 450 | "data": { 451 | "text/plain": [ 452 | "'a'" 453 | ] 454 | }, 455 | "execution_count": 20, 456 | "metadata": {}, 457 | "output_type": "execute_result" 458 | } 459 | ], 460 | "source": [ 461 | "my_q.pop()" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 21, 467 | "id": "94b23981", 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "name": "stdout", 472 | "output_type": "stream", 473 | "text": [ 474 | "b\n", 475 | "c\n", 476 | "d\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "for _ in range(len(my_q)):\n", 482 | " print(my_q.pop())" 483 | ] 484 | } 485 | ], 486 | "metadata": { 487 | "jupytext": { 488 | "cell_metadata_filter": "-all", 489 | "formats": "auto:light,ipynb", 490 | "main_language": "python", 491 | "notebook_metadata_filter": "-all" 492 | }, 493 | "kernelspec": { 494 | "display_name": "Python 3 (ipykernel)", 495 | "language": "python", 496 | "name": "python3" 497 | }, 498 | "language_info": { 499 | "codemirror_mode": { 500 | "name": "ipython", 501 | "version": 3 502 | }, 503 | "file_extension": ".py", 504 | "mimetype": "text/x-python", 505 | "name": "python", 506 | "nbconvert_exporter": "python", 507 | "pygments_lexer": "ipython3", 508 | "version": "3.11.4" 509 | } 510 | }, 511 | "nbformat": 4, 512 | "nbformat_minor": 5 513 | } 514 | -------------------------------------------------------------------------------- /content/examples/ex_15_counter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "46dad6c9", 6 | "metadata": {}, 7 | "source": [ 8 | "# collections.Counter\n", 9 | "Also called a multiset or bag, `Counter` keeps track of how many times an element is included in a set." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "8686ecc2", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "from collections import Counter" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "366d6f6d", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "Counter({'silver': 4, 'copper': 4, 'gold': 1})" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "coins = Counter()\n", 41 | "coins.update(['silver', 'silver', 'gold'])\n", 42 | "coins.update({'copper': 4, 'silver': 2})\n", 43 | "coins" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "778e4e86", 49 | "metadata": {}, 50 | "source": [ 51 | "It's like a specialized `dict`, where the **key** is an element and the **value** is an integer of counts.\n", 52 | "## Creating counters \n", 53 | "### From a sequence\n", 54 | "If the sequence can contain duplicates, like `list` or `tuple`, it will keep track of item counts" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "id": "7e1c9fc6", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "Counter({1: 4, 3: 2, 2: 1, 4: 1})" 67 | ] 68 | }, 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "a = Counter([1, 1, 3, 2, 1, 3, 4, 1])\n", 76 | "a" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "d3226a07", 82 | "metadata": {}, 83 | "source": [ 84 | "Sets remove duplicates when created, each count will be 1." 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "id": "718b5a0d", 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "Counter({1: 1, 2: 1, 3: 1, 4: 1})" 97 | ] 98 | }, 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "b = Counter({1, 1, 3, 2, 1, 3, 4, 1})\n", 106 | "b" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "83c0ebd6", 112 | "metadata": {}, 113 | "source": [ 114 | "### From a dictionary\n", 115 | "It will treat `{key: value}` as `{element: count}`" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "id": "314ed85b", 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "Counter({'b': 5, 'a': 2, 'c': 1})" 128 | ] 129 | }, 130 | "execution_count": 5, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "c = Counter({'a': 2, 'b': 5, 'c': 1})\n", 137 | "c" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "9e005640", 143 | "metadata": { 144 | "lines_to_next_cell": 2 145 | }, 146 | "source": [ 147 | "## Updating\n", 148 | "You can pass in a sequence or dict of counts." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 6, 154 | "id": "bccfa417", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "Counter()" 161 | ] 162 | }, 163 | "execution_count": 6, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "d = Counter()\n", 170 | "d" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "id": "090740cb", 176 | "metadata": {}, 177 | "source": [ 178 | "`.update()` to adds items (uses the `+` operator)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 7, 184 | "id": "4a9e8960", 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "data": { 189 | "text/plain": [ 190 | "Counter({'b': 3, 'a': 1})" 191 | ] 192 | }, 193 | "execution_count": 7, 194 | "metadata": {}, 195 | "output_type": "execute_result" 196 | } 197 | ], 198 | "source": [ 199 | "d.update('abbb')\n", 200 | "d" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 8, 206 | "id": "f14a4f33", 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "data": { 211 | "text/plain": [ 212 | "Counter({'b': 3, 'a': 2, 'c': 1})" 213 | ] 214 | }, 215 | "execution_count": 8, 216 | "metadata": {}, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "d.update({'c', 'c', 'a'}) # c is only counted once in a normal set\n", 222 | "d" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 9, 228 | "id": "b0036a6b", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "Counter({'b': 3, 'a': 2, 'd': 2, 'c': -2})" 235 | ] 236 | }, 237 | "execution_count": 9, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "d.update({'c':-3, 'd': 2})\n", 244 | "d" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "id": "2f14184b", 250 | "metadata": {}, 251 | "source": [ 252 | "`.subtract()` removes items (uses the `-` operator)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 10, 258 | "id": "4926d9dd", 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "Counter({'d': 2, 'a': 1, 'b': 0, 'c': -2})" 265 | ] 266 | }, 267 | "execution_count": 10, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "d.subtract('abbb')\n", 274 | "d" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 11, 280 | "id": "0c07d5ca", 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "Counter({'a': 4, 'd': 2, 'c': -2, 'b': -3})" 287 | ] 288 | }, 289 | "execution_count": 11, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "d.subtract({'a': -3, 'b': 3})\n", 296 | "d" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "id": "2c386ddf", 302 | "metadata": {}, 303 | "source": [ 304 | "## Counters are like dictionaries" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 12, 310 | "id": "79671997", 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/plain": [ 316 | "4" 317 | ] 318 | }, 319 | "execution_count": 12, 320 | "metadata": {}, 321 | "output_type": "execute_result" 322 | } 323 | ], 324 | "source": [ 325 | "d['a']" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 13, 331 | "id": "0585d1ab", 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "data": { 336 | "text/plain": [ 337 | "2" 338 | ] 339 | }, 340 | "execution_count": 13, 341 | "metadata": {}, 342 | "output_type": "execute_result" 343 | } 344 | ], 345 | "source": [ 346 | "d.get('d', 0)" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 14, 352 | "id": "7dcabdc4", 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "data": { 357 | "text/plain": [ 358 | "0" 359 | ] 360 | }, 361 | "execution_count": 14, 362 | "metadata": {}, 363 | "output_type": "execute_result" 364 | } 365 | ], 366 | "source": [ 367 | "d.get('e', 0)" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 15, 373 | "id": "81778038", 374 | "metadata": {}, 375 | "outputs": [], 376 | "source": [ 377 | "d['c'] = 1" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 16, 383 | "id": "bff12e0f", 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "a: 4\n", 391 | "b: -3\n", 392 | "c: 1\n", 393 | "d: 2\n" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "for key, value in d.items():\n", 399 | " print(f'{key}: {value}')" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "id": "54cafde5", 405 | "metadata": {}, 406 | "source": [ 407 | "## Special Counter methods\n", 408 | "`.elements()` is an iterator of all the elements, repeating each value as many times as its count" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 17, 414 | "id": "1dd42444", 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "name": "stdout", 419 | "output_type": "stream", 420 | "text": [ 421 | "a\n", 422 | "a\n", 423 | "a\n", 424 | "a\n", 425 | "c\n", 426 | "d\n", 427 | "d\n" 428 | ] 429 | } 430 | ], 431 | "source": [ 432 | "for element in d.elements(): # negative integer counts won't be included\n", 433 | " print(element)" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "id": "ac21db92", 439 | "metadata": {}, 440 | "source": [ 441 | "`.elements()` expects all counts to be integers" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 18, 447 | "id": "eee6540a", 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "data": { 452 | "text/plain": [ 453 | "Counter({'a': 4, 'd': 2, 'c': 1.0, 'b': -3})" 454 | ] 455 | }, 456 | "execution_count": 18, 457 | "metadata": {}, 458 | "output_type": "execute_result" 459 | } 460 | ], 461 | "source": [ 462 | "d['c'] = 1.0\n", 463 | "d" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 19, 469 | "id": "30150434", 470 | "metadata": {}, 471 | "outputs": [ 472 | { 473 | "name": "stdout", 474 | "output_type": "stream", 475 | "text": [ 476 | "a\n", 477 | "a\n", 478 | "a\n", 479 | "a\n", 480 | "TypeError(\"'float' object cannot be interpreted as an integer\")\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "try:\n", 486 | " for value in d.elements():\n", 487 | " print(value)\n", 488 | "except Exception as ex:\n", 489 | " print(repr(ex))" 490 | ] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "id": "7b52f5e8", 495 | "metadata": {}, 496 | "source": [ 497 | "`.most_common()` will return a list of the most common elements as `(element, count)`. If you pass an integer `n`, only that number are returned." 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "execution_count": 20, 503 | "id": "2e9955c0", 504 | "metadata": {}, 505 | "outputs": [ 506 | { 507 | "data": { 508 | "text/plain": [ 509 | "[('a', 4), ('d', 2), ('c', 1.0), ('b', -3)]" 510 | ] 511 | }, 512 | "execution_count": 20, 513 | "metadata": {}, 514 | "output_type": "execute_result" 515 | } 516 | ], 517 | "source": [ 518 | "d.most_common()" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 21, 524 | "id": "74c0faca", 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | "[('a', 4), ('d', 2)]" 531 | ] 532 | }, 533 | "execution_count": 21, 534 | "metadata": {}, 535 | "output_type": "execute_result" 536 | } 537 | ], 538 | "source": [ 539 | "d.most_common(2)" 540 | ] 541 | } 542 | ], 543 | "metadata": { 544 | "jupytext": { 545 | "cell_metadata_filter": "-all", 546 | "formats": "auto:light,ipynb", 547 | "main_language": "python", 548 | "notebook_metadata_filter": "-all" 549 | }, 550 | "kernelspec": { 551 | "display_name": "Python 3 (ipykernel)", 552 | "language": "python", 553 | "name": "python3" 554 | }, 555 | "language_info": { 556 | "codemirror_mode": { 557 | "name": "ipython", 558 | "version": 3 559 | }, 560 | "file_extension": ".py", 561 | "mimetype": "text/x-python", 562 | "name": "python", 563 | "nbconvert_exporter": "python", 564 | "pygments_lexer": "ipython3", 565 | "version": "3.11.4" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 5 570 | } 571 | -------------------------------------------------------------------------------- /content/examples/ex_16_custom_classes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "58d5cbde", 6 | "metadata": {}, 7 | "source": [ 8 | "# Creating custom classes\n", 9 | "Any custom class should:\n", 10 | "- Override the default `.__init__()`, `.__str__()` and `.__repr__()` methods\n", 11 | "- Use PascalCase for its name unless you have a good reason not to\n", 12 | "- Use the appropriate dunder methods, like `.__len__()`, instead of a custom `.get_length()` or `.length` attribute" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "id": "58af9ed3", 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "class MyClass:\n", 23 | " def __init__(self, data):\n", 24 | " self.data = data\n", 25 | " \n", 26 | " def __len__(self):\n", 27 | " if hasattr(self.data, '__len__'): # Does self.data.__len__() exist?\n", 28 | " return len(self.data)\n", 29 | " return 1\n", 30 | " \n", 31 | " def times_2(self):\n", 32 | " if hasattr(self.data, '__mul__'):\n", 33 | " return self.data * 2\n", 34 | " return self.data, self.data\n", 35 | " \n", 36 | " def __str__(self):\n", 37 | " return str(self.data)\n", 38 | " \n", 39 | " def __repr__(self):\n", 40 | " return f'MyClass({repr(self.data)})'" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "id": "a4821eb2", 47 | "metadata": { 48 | "lines_to_end_of_cell_marker": 0, 49 | "lines_to_next_cell": 1 50 | }, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "[1, 2]\n", 57 | "[MyClass([1, 2])]\n", 58 | "2\n", 59 | "[1, 2, 1, 2]\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "data = [1, 2] # Try different data \n", 65 | "\n", 66 | "mc = MyClass(data) # Calls __init__\n", 67 | "print(mc) # Calls __str__\n", 68 | "print([mc]) # Calls __repr__\n", 69 | "print(len(mc)) # Calls __len__\n", 70 | "print(mc.times_2())" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "id": "f9775a8f", 76 | "metadata": {}, 77 | "source": [ 78 | "### Example: Custom Linked List" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 3, 84 | "id": "eafaf9eb", 85 | "metadata": { 86 | "lines_to_end_of_cell_marker": 0, 87 | "lines_to_next_cell": 1 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "class LinkedList:\n", 92 | " def __init__(self, iterable=None):\n", 93 | " self.head = None\n", 94 | " if iterable:\n", 95 | " for i in iterable:\n", 96 | " self.append(i)\n", 97 | "\n", 98 | " def append(self, value):\n", 99 | " node = Node(value, self.head)\n", 100 | " self.head = node\n", 101 | "\n", 102 | " def pop(self):\n", 103 | " if self.head:\n", 104 | " node = self.head\n", 105 | " self.head = self.head.next\n", 106 | " return node.value\n", 107 | " return None\n", 108 | "\n", 109 | " def __iter__(self):\n", 110 | " node = self.head\n", 111 | " while node is not None:\n", 112 | " yield node\n", 113 | " node = node.next\n", 114 | " \n", 115 | " def __repr__(self):\n", 116 | " aslist = [val for val in self]\n", 117 | " return f\"LinkedList{repr(aslist)}\"\n", 118 | "\n", 119 | "\n", 120 | "class Node:\n", 121 | " def __init__(self, value, next_node):\n", 122 | " self.value = value\n", 123 | " self.next = next_node\n", 124 | " \n", 125 | " def __repr__(self):\n", 126 | " return repr(self.value)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "id": "140882a2", 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "LinkedList['c', 'b', 'a']" 139 | ] 140 | }, 141 | "execution_count": 4, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "ll = LinkedList('abc')\n", 148 | "ll" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 5, 154 | "id": "c7b4f822", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "LinkedList['d', 'c', 'b', 'a']" 161 | ] 162 | }, 163 | "execution_count": 5, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "ll.append('d')\n", 170 | "ll" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 6, 176 | "id": "2378ba34", 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "'d'" 183 | ] 184 | }, 185 | "execution_count": 6, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "ll.pop()" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 7, 197 | "id": "e6c5a6e9", 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "LinkedList['c', 'b', 'a']" 204 | ] 205 | }, 206 | "execution_count": 7, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "ll" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 8, 218 | "id": "e790ca86", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "'c'\n", 226 | "'b'\n", 227 | "'a'\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "for val in ll:\n", 233 | " print(val)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "id": "345b0c42", 239 | "metadata": {}, 240 | "source": [ 241 | "## Inheriting from a class\n", 242 | "Say we want to be able to subtract one string for another. The `-` operator currently throws `TypeError`." 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 9, 248 | "id": "85d4a007", 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "name": "stdout", 253 | "output_type": "stream", 254 | "text": [ 255 | "TypeError(\"unsupported operand type(s) for -: 'str' and 'str'\")\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "try:\n", 261 | " 'abc' - 'b'\n", 262 | "except Exception as e:\n", 263 | " print(repr(e))" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "id": "71d51d07", 269 | "metadata": {}, 270 | "source": [ 271 | "We can inherit from `str`, use `self` to access the string, and add/override any methods we want." 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 10, 277 | "id": "17fe527f", 278 | "metadata": {}, 279 | "outputs": [], 280 | "source": [ 281 | "class mystr(str):\n", 282 | " def __sub__(self, other):\n", 283 | " result_list = [char for char in self if char not in other]\n", 284 | " return ''.join(result_list)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "id": "a5831511", 290 | "metadata": {}, 291 | "source": [ 292 | "To use it, wrap your string in the new class (create an instance of it)." 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 11, 298 | "id": "1ff20a3d", 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "'abc'" 305 | ] 306 | }, 307 | "execution_count": 11, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "test = mystr('abc')\n", 314 | "test" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 12, 320 | "id": "a5a1936b", 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "'ac'" 327 | ] 328 | }, 329 | "execution_count": 12, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "test - 'b'" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 13, 341 | "id": "b6f67efb", 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "data": { 346 | "text/plain": [ 347 | "'ABC'" 348 | ] 349 | }, 350 | "execution_count": 13, 351 | "metadata": {}, 352 | "output_type": "execute_result" 353 | } 354 | ], 355 | "source": [ 356 | "test.upper()" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "id": "61cf51d6", 362 | "metadata": {}, 363 | "source": [ 364 | "## Inheriting from a collection class\n", 365 | "The `collections` module has `UserDict`, `UserList` and `UserString` to inherit from." 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 14, 371 | "id": "834448e6", 372 | "metadata": { 373 | "lines_to_end_of_cell_marker": 0, 374 | "lines_to_next_cell": 1 375 | }, 376 | "outputs": [], 377 | "source": [ 378 | "from collections import UserList\n", 379 | "\n", 380 | "class MyList(UserList):\n", 381 | " def upper(self):\n", 382 | " new_data = []\n", 383 | " for val in self.data:\n", 384 | " if isinstance(val, str):\n", 385 | " new_data.append(val.upper())\n", 386 | " else:\n", 387 | " new_data.append(val)\n", 388 | " return MyList(new_data)" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 15, 394 | "id": "3b30f1fd", 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "MyList([1, 'a', 2, 'b'])" 401 | ] 402 | }, 403 | "execution_count": 15, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "l = MyList([1, 'a', 2, 'b'])\n", 410 | "l" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 16, 416 | "id": "786221d8", 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "data": { 421 | "text/plain": [ 422 | "MyList([1, 'A', 2, 'B'])" 423 | ] 424 | }, 425 | "execution_count": 16, 426 | "metadata": {}, 427 | "output_type": "execute_result" 428 | } 429 | ], 430 | "source": [ 431 | "l.upper()" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 17, 437 | "id": "096ec1d5", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "'a'" 444 | ] 445 | }, 446 | "execution_count": 17, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "l[1]" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "id": "24d28910-e51d-4176-977b-bc0e7370c6c2", 458 | "metadata": {}, 459 | "source": [ 460 | "## Dataclasses\n", 461 | "\n", 462 | "You can use these for quick class creation that mostly stores data.\n", 463 | "\n", 464 | "Instead of defining instance attributes in `__init__()`, you define them on a class and provide the type." 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 18, 470 | "id": "3a2b9eb3-7f72-4486-a4ad-d5dc5717bf9c", 471 | "metadata": {}, 472 | "outputs": [], 473 | "source": [ 474 | "from dataclasses import dataclass\n", 475 | "\n", 476 | "@dataclass\n", 477 | "class Location:\n", 478 | " name: str\n", 479 | " latitude: float\n", 480 | " longitude: float" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 19, 486 | "id": "d115e1ba-ed5b-465c-b39c-7870e4572067", 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "'London'" 493 | ] 494 | }, 495 | "execution_count": 19, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "london = Location('London', 42.99, -81.243)\n", 502 | "london.name" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 20, 508 | "id": "34e56858-7b98-4d14-abe7-44c5cb674e24", 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "42.99" 515 | ] 516 | }, 517 | "execution_count": 20, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "london.latitude" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 21, 529 | "id": "acb00994-e146-40e5-a8df-b65a0c086e9e", 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | "-81.243" 536 | ] 537 | }, 538 | "execution_count": 21, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "london.longitude" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "id": "2ce9a200-cf89-4721-8b4d-de9615e0c2a0", 550 | "metadata": {}, 551 | "source": [ 552 | "Dataclasses provide some default behaviour for `__str__()`, `__repr__()` and `__eq__()`." 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 22, 558 | "id": "4a621ef9-16c1-456a-916d-52cda9c9b0a6", 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "data": { 563 | "text/plain": [ 564 | "Location(name='London', latitude=42.99, longitude=-81.243)" 565 | ] 566 | }, 567 | "execution_count": 22, 568 | "metadata": {}, 569 | "output_type": "execute_result" 570 | } 571 | ], 572 | "source": [ 573 | "london" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": 23, 579 | "id": "da8c93d1-2a74-437b-bbe0-31b0d3eabbbb", 580 | "metadata": {}, 581 | "outputs": [ 582 | { 583 | "name": "stdout", 584 | "output_type": "stream", 585 | "text": [ 586 | "Location(name='London', latitude=42.99, longitude=-81.243)\n" 587 | ] 588 | } 589 | ], 590 | "source": [ 591 | "print(london)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 24, 597 | "id": "41fec277-835e-44db-9226-87b5aada662c", 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "data": { 602 | "text/plain": [ 603 | "True" 604 | ] 605 | }, 606 | "execution_count": 24, 607 | "metadata": {}, 608 | "output_type": "execute_result" 609 | } 610 | ], 611 | "source": [ 612 | "london == Location('London', 42.99, -81.243)" 613 | ] 614 | } 615 | ], 616 | "metadata": { 617 | "jupytext": { 618 | "cell_metadata_filter": "-all", 619 | "formats": "auto:light,ipynb", 620 | "main_language": "python", 621 | "notebook_metadata_filter": "-all" 622 | }, 623 | "kernelspec": { 624 | "display_name": "Python 3 (ipykernel)", 625 | "language": "python", 626 | "name": "python3" 627 | }, 628 | "language_info": { 629 | "codemirror_mode": { 630 | "name": "ipython", 631 | "version": 3 632 | }, 633 | "file_extension": ".py", 634 | "mimetype": "text/x-python", 635 | "name": "python", 636 | "nbconvert_exporter": "python", 637 | "pygments_lexer": "ipython3", 638 | "version": "3.11.4" 639 | } 640 | }, 641 | "nbformat": 4, 642 | "nbformat_minor": 5 643 | } 644 | -------------------------------------------------------------------------------- /content/examples/ex_18_numpy_example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "82cf0ca8-40e6-403e-9cb5-85ef7ac68b26", 6 | "metadata": {}, 7 | "source": [ 8 | "# Curving grades\n", 9 | "\n", 10 | "Example from https://realpython.com/numpy-tutorial/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "e15a2563-6a6d-4d37-8064-53ef3e338f83", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import numpy as np" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "1060171b-ba8c-49f8-8795-9cef00585f2e", 26 | "metadata": {}, 27 | "source": [ 28 | "Define list of grades" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "id": "d4a1872f", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "grades = np.array([72, 35, 64, 88, 51, 90, 74, 12])" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "18353a75-ccc0-43a1-9ffb-52c88b7b63d4", 44 | "metadata": {}, 45 | "source": [ 46 | "Curve the grades so the average is 80" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "id": "03017d26", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "array([ 91.25, 54.25, 83.25, 100. , 70.25, 100. , 93.25, 31.25])" 59 | ] 60 | }, 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "CURVE_CENTER = 80\n", 68 | "\n", 69 | "def curve(grades):\n", 70 | " average = grades.mean() # 60.75\n", 71 | " change = CURVE_CENTER - average # 19.25\n", 72 | " new_grades = grades + change # [91.25, 54.25, 83.25, 107.25, 70.25, 109.25, 93.25, 31.25]\n", 73 | " return np.clip(new_grades, grades, 100) # [91.25, 54.25, 83.25, 100.0, 70.25, 100.0, 93.25, 31.25]\n", 74 | "\n", 75 | "curve(grades)" 76 | ] 77 | } 78 | ], 79 | "metadata": { 80 | "jupytext": { 81 | "cell_metadata_filter": "-all", 82 | "main_language": "python", 83 | "notebook_metadata_filter": "-all" 84 | }, 85 | "kernelspec": { 86 | "display_name": "Python 3 (ipykernel)", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.11.4" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 5 105 | } 106 | -------------------------------------------------------------------------------- /content/examples/ex_19_numpy_example_timed.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "88677cab-e5a3-4dd8-b852-ec1d8c37e9e4", 6 | "metadata": {}, 7 | "source": [ 8 | "# Compare grades curving\n", 9 | "Compare NumPy ndarrays and Python lists/loops" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 11, 15 | "id": "3868584e-8e61-403f-8856-a372e683c931", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import random\n", 20 | "import time\n", 21 | "import numpy as np" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "id": "ac35ed0b", 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "CURVE_CENTER = 80" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "86649951-4edb-4a3d-8263-2196bd315810", 37 | "metadata": {}, 38 | "source": [ 39 | "`timer()` is a decorator to log function execution times\n", 40 | "\n", 41 | "See Practical Decorators PyCon 2019 talk by Reuven M. Lerner to learn about decorators\n", 42 | "https://youtu.be/MjHpMCIvwsY?t=405" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "id": "eb9d3c30", 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "def timer(func):\n", 53 | " def inner(*args, **kwargs):\n", 54 | " start = time.time()\n", 55 | " returned = func(*args, **kwargs)\n", 56 | " end = time.time()\n", 57 | " print(f'{func.__name__.upper():<20} {end - start:f}s')\n", 58 | " return returned\n", 59 | " return inner" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "id": "a76a233b-a63a-4321-a48c-3ec6a3cb804f", 65 | "metadata": {}, 66 | "source": [ 67 | "## Numpy functions\n", 68 | "Generate random grades" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 4, 74 | "id": "18aec345", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "@timer\n", 79 | "def np_grades(num):\n", 80 | " rg = np.random.default_rng(0)\n", 81 | " grades = rg.integers(20, 100, num)\n", 82 | " return grades" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "452d8ab1-1c4c-4f8e-b199-07ae5bd4f413", 88 | "metadata": {}, 89 | "source": [ 90 | "Curve grades" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "id": "34ed2f83", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "@timer\n", 101 | "def np_curve(grades):\n", 102 | " average = grades.mean()\n", 103 | " change = CURVE_CENTER - average\n", 104 | " new_grades = grades + change\n", 105 | " return np.clip(new_grades, grades, 100)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "36ae2c40-6183-46ca-9f50-7f98c841c3c9", 111 | "metadata": {}, 112 | "source": [ 113 | "## Looping functions\n", 114 | "Generate random grades" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 6, 120 | "id": "ae160e5e", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "@timer\n", 125 | "def loop_grades(num):\n", 126 | " grades = [random.randint(20, 100) for _ in range(num)]\n", 127 | " return grades" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "3a0a2aaf-d41b-4020-9cbc-75f9c6197cb3", 133 | "metadata": {}, 134 | "source": [ 135 | "Curve grades" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 12, 141 | "id": "4f0f93c5", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "@timer\n", 146 | "def loop_curve(grades):\n", 147 | " average = sum(grades) / len(grades)\n", 148 | " change = CURVE_CENTER - average\n", 149 | " new_grades = [max(min(grade + change, 100), grade) for grade in grades]\n", 150 | " return new_grades" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "id": "a07fa7db-27ef-4a75-9868-b2f1105075d6", 156 | "metadata": {}, 157 | "source": [ 158 | "## Time comparison of numpy vs loops\n", 159 | "Time how long it takes to curve 10 up to 10,000,000 grades" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 13, 165 | "id": "621568fd", 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "\n", 173 | "--- 100 GRADES ---\n", 174 | "NP_GRADES 0.001391s\n", 175 | "NP_CURVE 0.000297s\n", 176 | "LOOP_GRADES 0.000176s\n", 177 | "LOOP_CURVE 0.000049s\n", 178 | "\n", 179 | "--- 1,000 GRADES ---\n", 180 | "NP_GRADES 0.000141s\n", 181 | "NP_CURVE 0.000080s\n", 182 | "LOOP_GRADES 0.000648s\n", 183 | "LOOP_CURVE 0.000476s\n", 184 | "\n", 185 | "--- 10,000 GRADES ---\n", 186 | "NP_GRADES 0.000220s\n", 187 | "NP_CURVE 0.000557s\n", 188 | "LOOP_GRADES 0.005558s\n", 189 | "LOOP_CURVE 0.004576s\n", 190 | "\n", 191 | "--- 100,000 GRADES ---\n", 192 | "NP_GRADES 0.000693s\n", 193 | "NP_CURVE 0.000768s\n", 194 | "LOOP_GRADES 0.035679s\n", 195 | "LOOP_CURVE 0.017458s\n", 196 | "\n", 197 | "--- 1,000,000 GRADES ---\n", 198 | "NP_GRADES 0.003520s\n", 199 | "NP_CURVE 0.004345s\n", 200 | "LOOP_GRADES 0.224452s\n", 201 | "LOOP_CURVE 0.141708s\n", 202 | "\n", 203 | "--- 10,000,000 GRADES ---\n", 204 | "NP_GRADES 0.028744s\n", 205 | "NP_CURVE 0.058144s\n", 206 | "LOOP_GRADES 2.131262s\n", 207 | "LOOP_CURVE 1.384286s\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "for num_zeroes in range(2, 8):\n", 213 | " num_grades = 10 ** num_zeroes\n", 214 | " print(f'\\n--- {num_grades:,} GRADES ---')\n", 215 | " \n", 216 | " np_curve(np_grades(num_grades)).tolist() # Curve N grades with NumPy\n", 217 | " loop_curve(loop_grades(num_grades)) # Curve N grades with loops" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "id": "0a441aae-be08-406c-ba60-cc8242e6e2ee", 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [] 227 | } 228 | ], 229 | "metadata": { 230 | "jupytext": { 231 | "cell_metadata_filter": "-all", 232 | "main_language": "python", 233 | "notebook_metadata_filter": "-all" 234 | }, 235 | "kernelspec": { 236 | "display_name": "Python 3 (ipykernel)", 237 | "language": "python", 238 | "name": "python3" 239 | }, 240 | "language_info": { 241 | "codemirror_mode": { 242 | "name": "ipython", 243 | "version": 3 244 | }, 245 | "file_extension": ".py", 246 | "mimetype": "text/x-python", 247 | "name": "python", 248 | "nbconvert_exporter": "python", 249 | "pygments_lexer": "ipython3", 250 | "version": "3.11.4" 251 | } 252 | }, 253 | "nbformat": 4, 254 | "nbformat_minor": 5 255 | } 256 | -------------------------------------------------------------------------------- /content/examples/languages.csv: -------------------------------------------------------------------------------- 1 | Name,Percent used 2 | JavaScript,65.36 3 | HTML/CSS,55.08 4 | SQL,49.43 5 | Python,48.07 -------------------------------------------------------------------------------- /content/examples/numpy_file.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ariannedee/python-data-structs/e515ca196a98ea1179adf63b53eb5c9be9fd55d2/content/examples/numpy_file.npy -------------------------------------------------------------------------------- /content/project/data/Survey-2024-Jan.csv: -------------------------------------------------------------------------------- 1 | "Timestamp","What programming languages do you have experience in?","How many years have you been programming?" 2 | "2024/01/30 8:40:43 am GMT-8","Python;SQL","1 - 5" 3 | "2024/01/30 8:45:28 am GMT-8","Python","5 - 10" 4 | "2024/01/30 9:02:42 am GMT-8","Python","1 - 5" 5 | "2024/01/30 9:02:59 am GMT-8","Java;JavaScript;SQL;C;C++;Visual Basic;Perl;Cobol;Fortran;Assembly;Pascal","> 20" 6 | "2024/01/30 9:03:28 am GMT-8","Python;Perl","> 20" 7 | "2024/01/30 9:03:35 am GMT-8","JavaScript;TypeScript;PHP;SQL;C++;Visual Basic;Perl","> 20" 8 | "2024/01/30 9:03:37 am GMT-8","Python;Java;C++;Perl","1 - 5" 9 | "2024/01/30 9:03:49 am GMT-8","Python;Java;JavaScript;TypeScript;PHP;SQL;Ruby;R;Objective-C;Swift;Perl;Kotlin;Dart","> 20" 10 | "2024/01/30 9:06:50 am GMT-8","Python;Java;JavaScript;SQL;C;C++;Matlab;Visual Basic;Cobol;Fortran;Assembly","> 20" 11 | "2024/01/30 9:10:51 am GMT-8","Java;JavaScript","15 - 20" 12 | "2024/01/30 9:14:33 am GMT-8","Python;Java;JavaScript;SQL;C;C++","15 - 20" 13 | "2024/01/30 9:26:37 am GMT-8","Python;SQL;C;R","1 - 5" 14 | "2024/01/30 9:26:50 am GMT-8","Java;C++","15 - 20" 15 | "2024/01/30 9:27:18 am GMT-8","SQL;R","1 - 5" 16 | "2024/01/30 9:27:28 am GMT-8","Python;Java","" 17 | "2024/01/30 9:27:44 am GMT-8","Ruby;Fortran;Lisp","5 - 10" 18 | "2024/01/30 9:28:46 am GMT-8","Python;Java;C#;Ruby;Visual Basic;Cobol;Pascal","> 20" 19 | "2024/01/30 9:30:13 am GMT-8","SQL","10 - 15" 20 | "2024/01/30 9:36:15 am GMT-8","Python;Java;JavaScript;C;Rust","10 - 15" 21 | "2024/01/30 10:45:33 am GMT-8","Python;JavaScript;Perl","1 - 5" 22 | "2024/01/30 10:45:55 am GMT-8","Java;SQL;C#","10 - 15" 23 | "2024/01/30 10:46:07 am GMT-8","Python;Java","1 - 5" 24 | "2024/01/30 10:46:24 am GMT-8","Python;JavaScript;SQL;C","1 - 5" 25 | "2024/01/30 10:48:04 am GMT-8","Python;SQL;C;C++","1 - 5" 26 | "2024/01/30 10:48:28 am GMT-8","Python;Java;JavaScript","1 - 5" -------------------------------------------------------------------------------- /content/project/data/Survey-2024-Jul.csv: -------------------------------------------------------------------------------- 1 | "Timestamp","What programming languages do you have experience in?","How many years have you been programming?" 2 | "2024/06/28 2:57:21 pm GMT-6","Python;Java;JavaScript;SQL;C;Ruby;R;Objective-C","15 - 20" 3 | "2024/06/30 8:29:25 pm GMT-6","Python;JavaScript;SQL;R;Visual Basic","1 - 5" 4 | "2024/07/02 9:47:15 am GMT-6","Python;JavaScript;C;C++;Matlab","1 - 5" 5 | "2024/07/02 10:05:35 am GMT-6","Python","1 - 5" 6 | "2024/07/02 10:05:37 am GMT-6","Java","1 - 5" 7 | "2024/07/02 10:05:41 am GMT-6","Python;SQL","15 - 20" 8 | "2024/07/02 10:05:54 am GMT-6","Python;Java;Go;Perl;Elixir","> 20" 9 | "2024/07/02 10:06:02 am GMT-6","Python;Matlab;Perl;Lisp;Assembly","> 20" 10 | "2024/07/02 10:06:10 am GMT-6","Python;JavaScript;PHP;SQL;Go","5 - 10" 11 | "2024/07/02 10:06:13 am GMT-6","Python;Java;JavaScript;TypeScript","10 - 15" 12 | "2024/07/02 10:06:19 am GMT-6","Python","1 - 5" 13 | "2024/07/02 10:07:04 am GMT-6","Python;C;C++;Matlab","1 - 5" 14 | "2024/07/02 10:08:00 am GMT-6","Python;Java;JavaScript;C;C++;Ruby;R;Go;Perl;Fortran;Lisp;Assembly;Lua;Haskell;Pascal","> 20" 15 | "2024/07/02 10:08:53 am GMT-6","Python;Java;C++;C#;Go;Perl;Fortran;Assembly;Scala","10 - 15" 16 | "2024/07/02 10:12:08 am GMT-6","Python;Java;SQL;C;C++;C#;Visual Basic;Fortran;Lisp;Pascal","> 20" 17 | "2024/07/02 10:12:40 am GMT-6","SQL;Cobol","15 - 20" 18 | "2024/07/02 10:22:28 am GMT-6","Python;SQL;R","< 1" 19 | "2024/07/02 10:22:44 am GMT-6","Python;Java;C;Lua","< 1" 20 | "2024/07/02 11:28:53 am GMT-6","SQL","10 - 15" 21 | "2024/07/02 11:29:10 am GMT-6","JavaScript","1 - 5" 22 | "2024/07/02 11:29:50 am GMT-6","Python;Java","1 - 5" 23 | "2024/07/02 11:31:47 am GMT-6","Python;Java;JavaScript;SQL;C","1 - 5" -------------------------------------------------------------------------------- /content/project/data/Survey-2024-Mar.csv: -------------------------------------------------------------------------------- 1 | "Timestamp","What programming languages do you have experience in?","How many years have you been programming?" 2 | "2024/01/30 9:40:43 am GMT-7","Python;SQL","1 - 5" 3 | "2024/01/30 9:45:28 am GMT-7","Python","5 - 10" 4 | "2024/01/30 10:02:42 am GMT-7","Python","1 - 5" 5 | "2024/01/30 10:02:59 am GMT-7","Java;JavaScript;SQL;C;C++;Visual Basic;Perl;Cobol;Fortran;Assembly;Pascal","> 20" 6 | "2024/01/30 10:03:28 am GMT-7","Python;Perl","> 20" 7 | "2024/01/30 10:03:35 am GMT-7","JavaScript;TypeScript;PHP;SQL;C++;Visual Basic;Perl","> 20" 8 | "2024/01/30 10:03:37 am GMT-7","Python;Java;C++;Perl","1 - 5" 9 | "2024/01/30 10:03:49 am GMT-7","Python;Java;JavaScript;TypeScript;PHP;SQL;Ruby;R;Objective-C;Swift;Perl;Kotlin;Dart","> 20" 10 | "2024/01/30 10:06:50 am GMT-7","Python;Java;JavaScript;SQL;C;C++;Matlab;Visual Basic;Cobol;Fortran;Assembly","> 20" 11 | "2024/01/30 10:10:51 am GMT-7","Java;JavaScript","15 - 20" 12 | "2024/01/30 10:14:33 am GMT-7","Python;Java;JavaScript;SQL;C;C++","15 - 20" 13 | "2024/01/30 10:26:37 am GMT-7","Python;SQL;C;R","1 - 5" 14 | "2024/01/30 10:26:50 am GMT-7","Java;C++","15 - 20" 15 | "2024/01/30 10:27:18 am GMT-7","SQL;R","1 - 5" 16 | "2024/01/30 10:27:28 am GMT-7","Python;Java","" 17 | "2024/01/30 10:27:44 am GMT-7","Ruby;Fortran;Lisp","5 - 10" 18 | "2024/01/30 10:28:46 am GMT-7","Python;Java;C#;Ruby;Visual Basic;Cobol;Pascal","> 20" 19 | "2024/01/30 10:30:13 am GMT-7","SQL","10 - 15" 20 | "2024/01/30 10:36:15 am GMT-7","Python;Java;JavaScript;C;Rust","10 - 15" 21 | "2024/01/30 11:45:33 am GMT-7","Python;JavaScript;Perl","1 - 5" 22 | "2024/01/30 11:45:55 am GMT-7","Java;SQL;C#","10 - 15" 23 | "2024/01/30 11:46:07 am GMT-7","Python;Java","1 - 5" 24 | "2024/01/30 11:46:24 am GMT-7","Python;JavaScript;SQL;C","1 - 5" 25 | "2024/01/30 11:48:04 am GMT-7","Python;SQL;C;C++","1 - 5" 26 | "2024/01/30 11:48:28 am GMT-7","Python;Java;JavaScript","1 - 5" 27 | "2024/01/30 12:10:37 pm GMT-7","Python;Java;JavaScript;TypeScript;SQL;C;C++","1 - 5" 28 | "2024/01/30 12:24:02 pm GMT-7","JavaScript;Matlab;Cobol;Fortran;Assembly;Pascal","> 20" 29 | "2024/02/29 8:17:01 pm GMT-7","Python;C","5 - 10" 30 | "2024/03/25 7:35:39 pm GMT-6","Python;JavaScript;C;Perl","15 - 20" 31 | "2024/03/25 8:23:58 pm GMT-6","Python","< 1" 32 | "2024/03/26 3:13:46 am GMT-6","Python;SQL","1 - 5" 33 | "2024/03/26 7:40:45 am GMT-6","Python;Java;JavaScript;SQL;C;C++;Perl;Kotlin;Pascal","> 20" 34 | "2024/03/26 8:37:35 am GMT-6","Python;C","< 1" 35 | "2024/03/26 9:27:38 am GMT-6","C++","10 - 15" 36 | "2024/03/26 10:03:59 am GMT-6","Python;PHP;SQL;Visual Basic","1 - 5" 37 | "2024/03/26 10:04:01 am GMT-6","Python;JavaScript;SQL;R","1 - 5" 38 | "2024/03/26 10:04:02 am GMT-6","Python;Java;JavaScript;TypeScript;SQL;C;R;Perl;Lisp;Haskell","> 20" 39 | "2024/03/26 10:04:10 am GMT-6","Java;SQL;C;C#;Assembly","> 20" 40 | "2024/03/26 10:04:23 am GMT-6","Python;SQL","1 - 5" 41 | "2024/03/26 10:04:32 am GMT-6","Python;SQL;C;C++;Perl","5 - 10" 42 | "2024/03/26 10:04:37 am GMT-6","Python;SQL;C;C++;C#;Visual Basic;Pascal","> 20" 43 | "2024/03/26 10:04:37 am GMT-6","Java","1 - 5" 44 | "2024/03/26 10:04:38 am GMT-6","Python;Java;JavaScript;SQL;C","15 - 20" 45 | "2024/03/26 10:04:48 am GMT-6","Python;JavaScript;SQL","1 - 5" 46 | "2024/03/26 10:04:52 am GMT-6","C;C++","> 20" 47 | "2024/03/26 10:04:55 am GMT-6","Java;SQL","> 20" 48 | "2024/03/26 10:05:05 am GMT-6","Java;Kotlin","10 - 15" 49 | "2024/03/26 10:05:26 am GMT-6","Python;SQL;C++;Perl;Cobol;Pascal","> 20" 50 | "2024/03/26 10:05:32 am GMT-6","Python;SQL;C;C++;C#;Matlab;Pascal","15 - 20" 51 | "2024/03/26 10:05:48 am GMT-6","Python;SQL;Visual Basic","> 20" 52 | "2024/03/26 10:11:25 am GMT-6","Python;C;C++;C#;Matlab;Perl","5 - 10" 53 | "2024/03/26 10:32:14 am GMT-6","Python;PHP;SQL;Visual Basic","1 - 5" 54 | "2024/03/26 10:34:02 am GMT-6","Python;Java;C;C++;Perl","5 - 10" 55 | "2024/03/26 10:35:29 am GMT-6","Java;SQL;C++","10 - 15" -------------------------------------------------------------------------------- /content/project/data/Survey-2024-May.csv: -------------------------------------------------------------------------------- 1 | "Timestamp","What programming languages do you have experience in?","How many years have you been programming?" 2 | "2024/05/14 5:22:21 am GMT-6","Python;SQL","1 - 5" 3 | "2024/05/17 10:13:04 am GMT-6","Python;C++","1 - 5" 4 | "2024/05/19 11:55:24 pm GMT-6","Python;SQL;C++;R;Matlab","1 - 5" 5 | "2024/05/21 9:57:33 am GMT-6","Python;Java","1 - 5" 6 | "2024/05/21 10:01:48 am GMT-6","Python;SQL;Perl","15 - 20" 7 | "2024/05/21 10:15:30 am GMT-6","Python","< 1" 8 | "2024/05/21 10:20:01 am GMT-6","Java","5 - 10" 9 | "2024/05/21 10:20:11 am GMT-6","SQL","< 1" 10 | "2024/05/21 10:20:47 am GMT-6","Python;Java;C;C++;Go;Cobol","> 20" 11 | "2024/05/21 10:21:19 am GMT-6","JavaScript","< 1" 12 | "2024/05/21 10:21:52 am GMT-6","Python","< 1" 13 | "2024/05/21 10:21:56 am GMT-6","Python;Java;SQL;C;R","1 - 5" 14 | "2024/05/21 10:22:01 am GMT-6","Python;Java;JavaScript;TypeScript;PHP;SQL;C;C++;C#;R;Go;Objective-C;Swift;Perl;Cobol;Assembly;Kotlin","10 - 15" 15 | "2024/05/21 10:22:01 am GMT-6","Python;Java;JavaScript;PHP;SQL;C;C++;Cobol","5 - 10" 16 | "2024/05/21 10:22:09 am GMT-6","Python;Java;JavaScript;TypeScript;SQL","10 - 15" 17 | "2024/05/21 10:22:42 am GMT-6","Python","1 - 5" 18 | "2024/05/21 10:23:00 am GMT-6","Python;SQL","< 1" 19 | "2024/05/21 10:31:44 am GMT-6","Python;SQL","< 1" 20 | "2024/05/21 11:22:40 am GMT-6","Python","< 1" -------------------------------------------------------------------------------- /content/project/sample_solutions/proj_1_builtin.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "9d86ef30", 6 | "metadata": {}, 7 | "source": [ 8 | "# Survey analysis\n", 9 | "Analyse the results from the [languages survey](https://forms.gle/5b3mZRVcgAsoNG1FA)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "801ca19d", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "all_langs = ['Python', 'Java', 'JavaScript', 'TypeScript', 'PHP', 'SQL', 'C', 'C++', 'C#',\n", 20 | " 'Ruby', 'R', 'Matlab', 'Go', 'Rust', 'Objective-C', 'Swift', 'Visual Basic',\n", 21 | " 'Perl', 'Cobol', 'Fortran', 'Lisp', 'Assembly', 'Kotlin', 'Dart', 'Scala',\n", 22 | " 'Lua', 'Delphi', 'Haskell', 'Julia', 'Clojure', 'Elixir', 'Pascal']" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "868dd80b", 28 | "metadata": {}, 29 | "source": [ 30 | "## Load data" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "id": "4ad9c4b4", 37 | "metadata": { 38 | "lines_to_end_of_cell_marker": 2 39 | }, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "27\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "import csv\n", 51 | "from pprint import pprint\n", 52 | "\n", 53 | "list_of_languages = []\n", 54 | "with open('../data/Survey-2024-Mar.csv') as file:\n", 55 | " file.readline() # Ignore first line (header)\n", 56 | " reader = csv.DictReader(file, fieldnames=('timestamp', 'languages', 'years')) # list of dicts with keys passed into fieldnames\n", 57 | " for line in reader:\n", 58 | " list_of_languages.append(line['languages'].split(';'))\n", 59 | "\n", 60 | "num_responses = len(list_of_languages)\n", 61 | "print(num_responses)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "id": "fbd49e64", 67 | "metadata": {}, 68 | "source": [ 69 | "## Find number of languages known\n", 70 | "Print `\"{# known by class} / {# in list} languages known by this class (as %)\"`.\n", 71 | "\n", 72 | "E.g. **12/21 languages known by this class (57%)**" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 3, 78 | "id": "5734bef0", 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "19 / 32 languages known by this class (59%)\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "languages_known = set()\n", 91 | "for langs in list_of_languages:\n", 92 | " languages_known.update(langs)\n", 93 | "\n", 94 | "num_known = len(languages_known)\n", 95 | "num_all = len(all_langs)\n", 96 | "print(f\"{num_known} / {num_all} languages known by this class ({round(num_known / num_all * 100)}%)\")" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "id": "b9824b6a", 102 | "metadata": {}, 103 | "source": [ 104 | "## List languages not known by anyone in the class" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "id": "d423f3bc", 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "Clojure\n", 118 | "Dart\n", 119 | "Delphi\n", 120 | "Elixir\n", 121 | "Fortran\n", 122 | "Go\n", 123 | "Julia\n", 124 | "Lua\n", 125 | "Objective-C\n", 126 | "Ruby\n", 127 | "Rust\n", 128 | "Scala\n", 129 | "Swift\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "not_known = set(all_langs) - languages_known\n", 135 | "for lang in sorted(not_known):\n", 136 | " print(lang)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "id": "39f83d3b", 142 | "metadata": {}, 143 | "source": [ 144 | "## Rank languages by most commonly known\n", 145 | "Print each language as `\"{position}: {language} ({percent_known}%)\"`, in order from most to least known\n", 146 | "\n", 147 | "e.g. **1: Python (93%)**" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 5, 153 | "id": "e433e4bd", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "{'Assembly': 1,\n", 161 | " 'C': 13,\n", 162 | " 'C#': 4,\n", 163 | " 'C++': 10,\n", 164 | " 'Cobol': 1,\n", 165 | " 'Haskell': 1,\n", 166 | " 'Java': 9,\n", 167 | " 'JavaScript': 6,\n", 168 | " 'Kotlin': 2,\n", 169 | " 'Lisp': 1,\n", 170 | " 'Matlab': 2,\n", 171 | " 'PHP': 2,\n", 172 | " 'Pascal': 4,\n", 173 | " 'Perl': 7,\n", 174 | " 'Python': 20,\n", 175 | " 'R': 2,\n", 176 | " 'SQL': 17,\n", 177 | " 'TypeScript': 1,\n", 178 | " 'Visual Basic': 4}\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "lang_counts = {}\n", 184 | "for langs in list_of_languages:\n", 185 | " for lang in langs:\n", 186 | " lang_counts[lang] = lang_counts.get(lang, 0) + 1\n", 187 | "\n", 188 | "pprint(lang_counts)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 6, 194 | "id": "4a331dfe", 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "1: Python (74%)\n", 202 | "2: SQL (63%)\n", 203 | "3: C (48%)\n", 204 | "4: C++ (37%)\n", 205 | "5: Java (33%)\n", 206 | "6: Perl (26%)\n", 207 | "7: JavaScript (22%)\n", 208 | "8: Pascal (15%)\n", 209 | "9: Visual Basic (15%)\n", 210 | "10: C# (15%)\n", 211 | "11: Kotlin (7%)\n", 212 | "12: PHP (7%)\n", 213 | "13: R (7%)\n", 214 | "14: Matlab (7%)\n", 215 | "15: TypeScript (4%)\n", 216 | "16: Lisp (4%)\n", 217 | "17: Haskell (4%)\n", 218 | "18: Assembly (4%)\n", 219 | "19: Cobol (4%)\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "lang_counts_list = list(lang_counts.items())\n", 225 | "lang_counts_list.sort(key=lambda t: t[1], reverse=True)\n", 226 | "for position, (lang, count) in enumerate(lang_counts_list, start=1):\n", 227 | " print(f\"{position}: {lang} ({round(count / num_responses * 100)}%)\")" 228 | ] 229 | } 230 | ], 231 | "metadata": { 232 | "jupytext": { 233 | "cell_metadata_filter": "-all", 234 | "formats": "auto:light,ipynb", 235 | "main_language": "python", 236 | "notebook_metadata_filter": "-all" 237 | }, 238 | "kernelspec": { 239 | "display_name": "Python 3 (ipykernel)", 240 | "language": "python", 241 | "name": "python3" 242 | }, 243 | "language_info": { 244 | "codemirror_mode": { 245 | "name": "ipython", 246 | "version": 3 247 | }, 248 | "file_extension": ".py", 249 | "mimetype": "text/x-python", 250 | "name": "python", 251 | "nbconvert_exporter": "python", 252 | "pygments_lexer": "ipython3", 253 | "version": "3.11.4" 254 | } 255 | }, 256 | "nbformat": 4, 257 | "nbformat_minor": 5 258 | } 259 | -------------------------------------------------------------------------------- /content/project/sample_solutions/proj_2_comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "055c860b", 6 | "metadata": {}, 7 | "source": [ 8 | "# Survey analysis\n", 9 | "Analyse the results from the [languages survey](https://forms.gle/5b3mZRVcgAsoNG1FA)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "id": "9d0908cf", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "all_langs = ['Python', 'Java', 'JavaScript', 'TypeScript', 'PHP', 'SQL', 'C', 'C++', 'C#',\n", 20 | " 'Ruby', 'R', 'Matlab', 'Go', 'Rust', 'Objective-C', 'Swift', 'Visual Basic',\n", 21 | " 'Perl', 'Cobol', 'Fortran', 'Lisp', 'Assembly', 'Kotlin', 'Dart', 'Scala',\n", 22 | " 'Lua', 'Delphi', 'Haskell', 'Julia', 'Clojure', 'Elixir', 'Pascal']" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "b6e4473c", 28 | "metadata": {}, 29 | "source": [ 30 | "## Load data" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "id": "7175f462", 37 | "metadata": { 38 | "lines_to_end_of_cell_marker": 2 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "import csv\n", 43 | "from pprint import pprint\n", 44 | "\n", 45 | "with open('../data/Survey-2024-Mar.csv') as file:\n", 46 | " file.readline() # Ignore first line (header)\n", 47 | " reader = csv.DictReader(file, fieldnames=('timestamp', 'languages', 'years')) # list of dicts with keys passed into fieldnames\n", 48 | " list_of_languages = [line['languages'].split(';') for line in reader]\n", 49 | "\n", 50 | "num_responses = len(list_of_languages)\n", 51 | "print(num_responses)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "id": "58290b58", 57 | "metadata": {}, 58 | "source": [ 59 | "## Find number of languages known\n", 60 | "Print `\"{# known by class} / {# in list} languages known by this class (as %)\"`.\n", 61 | "\n", 62 | "E.g. **12/21 languages known by this class (57%)**" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "id": "33126e76", 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "languages_known = {lang for langs in list_of_languages for lang in langs}\n", 73 | "\n", 74 | "num_known = len(languages_known)\n", 75 | "num_all = len(all_langs)\n", 76 | "print(f\"{num_known} / {num_all} languages known by this class ({round(num_known / num_all * 100)}%)\")" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "55fc2064", 82 | "metadata": {}, 83 | "source": [ 84 | "## List languages not known by anyone in the class" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "026956bf", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "not_known = set(all_langs) - languages_known\n", 95 | "for lang in sorted(not_known):\n", 96 | " print(lang)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "id": "2db4a188", 102 | "metadata": {}, 103 | "source": [ 104 | "## Rank languages by most commonly known\n", 105 | "Print each language as `\"{position}: {language} ({percent_known}%)\"`, in order from most to least known\n", 106 | "\n", 107 | "e.g. **1: Python (93%)**" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "id": "90695a5c", 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "lang_counts = {lang: 0 for lang in all_langs}\n", 118 | "for langs in list_of_languages:\n", 119 | " for lang in langs:\n", 120 | " lang_counts[lang] += 1\n", 121 | "\n", 122 | "pprint(lang_counts)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "id": "25a6fa96", 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "lang_counts_list = [(lang, lang_counts[lang]) for lang in lang_counts]\n", 133 | "\n", 134 | "lang_counts_list.sort(key=lambda t: t[1], reverse=True)\n", 135 | "for position, (lang, count) in enumerate(lang_counts_list, start=1):\n", 136 | " print(f\"{position}: {lang} ({round(count / num_responses * 100)}%)\")" 137 | ] 138 | } 139 | ], 140 | "metadata": { 141 | "jupytext": { 142 | "cell_metadata_filter": "-all", 143 | "formats": "auto:light,ipynb", 144 | "main_language": "python", 145 | "notebook_metadata_filter": "-all" 146 | }, 147 | "kernelspec": { 148 | "display_name": "Python 3 (ipykernel)", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.11.4" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 5 167 | } 168 | -------------------------------------------------------------------------------- /content/project/sample_solutions/proj_3_collections.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c4ce8d5d", 6 | "metadata": {}, 7 | "source": [ 8 | "# Survey analysis\n", 9 | "Analyse the results from the [languages survey](https://forms.gle/5b3mZRVcgAsoNG1FA)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "dbdcda8a", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "all_langs = ['Python', 'Java', 'JavaScript', 'TypeScript', 'PHP', 'SQL', 'C', 'C++', 'C#',\n", 20 | " 'Ruby', 'R', 'Matlab', 'Go', 'Rust', 'Objective-C', 'Swift', 'Visual Basic',\n", 21 | " 'Perl', 'Cobol', 'Fortran', 'Lisp', 'Assembly', 'Kotlin', 'Dart', 'Scala',\n", 22 | " 'Lua', 'Delphi', 'Haskell', 'Julia', 'Clojure', 'Elixir', 'Pascal']" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "4cd6ff36", 28 | "metadata": {}, 29 | "source": [ 30 | "## Load data" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "id": "3777a962", 37 | "metadata": { 38 | "lines_to_end_of_cell_marker": 2 39 | }, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "22\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "import csv\n", 51 | "from pprint import pprint\n", 52 | "from collections import Counter\n", 53 | "\n", 54 | "langs_known_counter = Counter()\n", 55 | "num_responses = 0\n", 56 | "with open('../data/Survey-2024-Mar.csv') as file:\n", 57 | " file.readline() # Ignore first line (header)\n", 58 | " reader = csv.DictReader(file, fieldnames=('timestamp', 'languages', 'years')) # list of dicts with keys passed into fieldnames\n", 59 | " for line in reader:\n", 60 | " langs_known_counter.update(line['languages'].split(';'))\n", 61 | " num_responses += 1\n", 62 | "\n", 63 | "print(num_responses)\n", 64 | "print(langs_known_counter)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "359f16df", 70 | "metadata": {}, 71 | "source": [ 72 | "## Find number of languages known\n", 73 | "Print `\"{# known by class} / {# in list} languages known by this class (as %)\"`.\n", 74 | "\n", 75 | "E.g. **12/21 languages known by this class (57%)**" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "id": "e6f6ac48", 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "25 / 32 languages known by this class (78%)\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "languages_known = langs_known_counter.keys()\n", 94 | "\n", 95 | "num_known = len(languages_known)\n", 96 | "num_all = len(all_langs)\n", 97 | "print(f\"{num_known} / {num_all} languages known by this class ({round(num_known / num_all * 100)}%)\")" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "1337ca41", 103 | "metadata": {}, 104 | "source": [ 105 | "## List languages not known by anyone in the class" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 4, 111 | "id": "33f65e70", 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "{'Julia', 'Rust', 'Dart', 'Swift', 'Kotlin', 'Delphi', 'Clojure'}\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "not_known = set(all_langs) - languages_known\n", 124 | "for lang in sorted(not_known):\n", 125 | " print(lang)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "id": "a503589b", 131 | "metadata": {}, 132 | "source": [ 133 | "## Rank languages by most commonly known\n", 134 | "Print each language as `\"{position}: {language} ({percent_known}%)\"`, in order from most to least known\n", 135 | "\n", 136 | "e.g. **1: Python (93%)**" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "id": "5d8d72a7", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "[('Python', 18),\n", 150 | " ('Java', 10),\n", 151 | " ('SQL', 9),\n", 152 | " ('JavaScript', 8),\n", 153 | " ('C', 7),\n", 154 | " ('C++', 5),\n", 155 | " ('R', 4),\n", 156 | " ('Go', 4),\n", 157 | " ('Perl', 4),\n", 158 | " ('Matlab', 3),\n", 159 | " ('Lisp', 3),\n", 160 | " ('Assembly', 3),\n", 161 | " ('Fortran', 3),\n", 162 | " ('Ruby', 2),\n", 163 | " ('Visual Basic', 2),\n", 164 | " ('Lua', 2),\n", 165 | " ('Pascal', 2),\n", 166 | " ('C#', 2),\n", 167 | " ('Objective-C', 1),\n", 168 | " ('Elixir', 1),\n", 169 | " ('PHP', 1),\n", 170 | " ('TypeScript', 1),\n", 171 | " ('Haskell', 1),\n", 172 | " ('Scala', 1),\n", 173 | " ('Cobol', 1)]\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "for i, (lang, count) in enumerate(langs_known_counter.most_common()):\n", 179 | " print(f\"{i + 1}: {lang} ({round(count / num_responses * 100)}%)\")" 180 | ] 181 | } 182 | ], 183 | "metadata": { 184 | "jupytext": { 185 | "cell_metadata_filter": "-all", 186 | "formats": "auto:light,ipynb", 187 | "main_language": "python", 188 | "notebook_metadata_filter": "-all" 189 | }, 190 | "kernelspec": { 191 | "display_name": "Python 3 (ipykernel)", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.11.4" 206 | } 207 | }, 208 | "nbformat": 4, 209 | "nbformat_minor": 5 210 | } 211 | -------------------------------------------------------------------------------- /content/project/survey_analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "10ab2efb", 6 | "metadata": {}, 7 | "source": [ 8 | "# Survey analysis\n", 9 | "Analyse the results from the [languages survey](https://forms.gle/5b3mZRVcgAsoNG1FA)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "id": "415ca06e", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "all_langs = ['Python', 'Java', 'JavaScript', 'TypeScript', 'PHP', 'SQL', 'C', 'C++', 'C#',\n", 20 | " 'Ruby', 'R', 'Matlab', 'Go', 'Rust', 'Objective-C', 'Swift', 'Visual Basic',\n", 21 | " 'Perl', 'Cobol', 'Fortran', 'Lisp', 'Assembly', 'Kotlin', 'Dart', 'Scala',\n", 22 | " 'Lua', 'Delphi', 'Haskell', 'Julia', 'Clojure', 'Elixir', 'Pascal']" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "0ea92773", 28 | "metadata": {}, 29 | "source": [ 30 | "## Load data" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "id": "4e5b4f4d", 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import csv\n", 41 | "\n", 42 | "with open('data/Survey-2024-Mar.csv') as file:\n", 43 | " file.readline() # Ignore first line (header)\n", 44 | " reader = csv.DictReader(file, fieldnames=('timestamp', 'languages', 'years')) # list of dicts with keys passed into fieldnames\n", 45 | " for line in reader:\n", 46 | " print(line)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "2b06bc8f", 52 | "metadata": {}, 53 | "source": [ 54 | "## Find number of languages known\n", 55 | "Print `\"{# known by class} / {# in list} languages known by this class (as %)\"`.\n", 56 | "\n", 57 | "E.g. **12/21 languages known by this class (57%)**" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "id": "ce9b883a", 64 | "metadata": { 65 | "lines_to_next_cell": 2 66 | }, 67 | "outputs": [], 68 | "source": [] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "id": "98a40025", 73 | "metadata": {}, 74 | "source": [ 75 | "## List languages not known by anyone in the class" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "id": "0ea84dac", 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "id": "a6e1d57b", 89 | "metadata": {}, 90 | "source": [ 91 | "## Rank languages by most commonly known\n", 92 | "Print each language as `\"{position}: {language} ({percent_known}%)\"`, in order from most to least known\n", 93 | "\n", 94 | "e.g. **1: Python (93%)**" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "eed70ade", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [] 104 | } 105 | ], 106 | "metadata": { 107 | "jupytext": { 108 | "cell_metadata_filter": "-all", 109 | "formats": "auto:light,ipynb", 110 | "main_language": "python", 111 | "notebook_metadata_filter": "-all" 112 | }, 113 | "kernelspec": { 114 | "display_name": "Python 3 (ipykernel)", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.11.4" 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 5 133 | } 134 | -------------------------------------------------------------------------------- /repl/jupyter-lite.json: -------------------------------------------------------------------------------- 1 | { 2 | "jupyter-lite-schema-version": 0, 3 | "jupyter-config-data": { 4 | "disabledExtensions": [ 5 | "@jupyterlab/drawio-extension", 6 | "jupyterlab-kernel-spy", 7 | "jupyterlab-tour" 8 | ] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Core modules (mandatory) 2 | jupyterlite-core==0.1.2 3 | jupyterlab~=3.5.1 4 | 5 | # Python kernel (optional) 6 | jupyterlite-pyodide-kernel==0.1.2 7 | 8 | # Language support (optional) 9 | jupyterlab-language-pack-fr-FR 10 | jupyterlab-language-pack-zh-CN 11 | 12 | # JupyterLab: Fasta file renderer (optional) 13 | jupyterlab-fasta>=3,<4 14 | # JupyterLab: dark theme 15 | jupyterlab-night 16 | # JupyterLab: Miami nights theme (optional) 17 | jupyterlab_miami_nights 18 | 19 | numpy 20 | pandas --------------------------------------------------------------------------------