├── .gitignore ├── Complete-Python_Intro_2022.ipynb ├── HandsOn-Python-Intro-Workshop-2022 ├── Python_Intro_Workshop_2023.ipynb ├── README.md ├── python_intro_2019.ipynb ├── python_intro_code.py └── python_resources.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /HandsOn-Python-Intro-Workshop-2022: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.3" 21 | }, 22 | "colab": { 23 | "name": "PythonIntroWorkshop2022.ipynb", 24 | "provenance": [] 25 | } 26 | }, 27 | "cells": [ 28 | { 29 | "cell_type": "markdown", 30 | "metadata": { 31 | "id": "-nKUfS2pQhvG" 32 | }, 33 | "source": [ 34 | "# Python 3 Introductory Workshop" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": { 40 | "id": "e_5aDfbIQhvM" 41 | }, 42 | "source": [ 43 | "### Topics\n", 44 | " - Why Python?\n", 45 | " - Variables\n", 46 | " - Operators\n", 47 | " - Collections\n", 48 | " - Conditionals\n", 49 | " - Loops" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "id": "crEu5G8XQhvO" 56 | }, 57 | "source": [ 58 | "### Why Python?\n", 59 | "Python is known for being one of the most versitile and easy to use languages\n", 60 | " \n", 61 | " - Simple syntax makes it easy to learn\n", 62 | " - Easy and intuitive to read, even with minimal programming knowledge\n", 63 | " - Ex. string.lower(); string.upper();\n", 64 | " - Great for prototyping and quickly writing working code\n", 65 | " - Portable across systems; As long as Python is installed it will run on any operting system\n", 66 | " \n", 67 | "Popular uses for Python include\n", 68 | "\n", 69 | " - Mathematics and Big Data\n", 70 | " Packages used for reading, manipulating, and analyzing data quickly(Numpy, Pandas)\n", 71 | " - Data Visualization\n", 72 | " Packages used for visualizing data on plots, graphs (Matplotlib, Plotly)\n", 73 | " - Simple Scripting\n", 74 | " Writing a functional program that performs an individual task (rasberry pi)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "source": [ 80 | "# Anything that comes after a # on the same line is a comment\n", 81 | "# Python ignores it, but it lets us humans take notes on what we did" 82 | ], 83 | "metadata": { 84 | "id": "Y-nYIwr1q-KC" 85 | }, 86 | "execution_count": null, 87 | "outputs": [] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "id": "qO9iINdYQhvQ" 93 | }, 94 | "source": [ 95 | "### Variables" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": { 101 | "id": "PyFrcZ8PQhvR" 102 | }, 103 | "source": [ 104 | "Variables are the building blocks in Python. We use variables to store information that we want to use later. \n", 105 | "\n", 106 | "To create a variable and assign it to a value/piece of information, we use =. \n", 107 | "\n", 108 | "Here, we're going to look at 4 primitive (basic) variable types:\n", 109 | " \n", 110 | " -integers\n", 111 | " -floats\n", 112 | " -strings\n", 113 | " -booleans" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": { 119 | "id": "h4kwW9yVQhvS" 120 | }, 121 | "source": [ 122 | "##### Integers\n", 123 | "\n", 124 | "Integers: positive or negative numbers WITHOUT decimal places - the same as in math." 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "metadata": { 130 | "id": "w9iQToKJQhvU", 131 | "colab": { 132 | "base_uri": "https://localhost:8080/" 133 | }, 134 | "outputId": "b6d79227-f04f-419f-fcb3-01e0e0fa0c0b" 135 | }, 136 | "source": [ 137 | "myInt = 5\n", 138 | "print(myInt)" 139 | ], 140 | "execution_count": null, 141 | "outputs": [ 142 | { 143 | "output_type": "stream", 144 | "name": "stdout", 145 | "text": [ 146 | "5\n" 147 | ] 148 | } 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": { 154 | "id": "mqjLSsMdd7X6" 155 | }, 156 | "source": [ 157 | "If we want to see the value stored in a variable, we can use the built-in print( ) function.\n", 158 | "\n", 159 | "The print() function will show us whatever is inside the ( ) to our console, for Colab, the console will appear below our code blocks" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": { 165 | "id": "HsdHDijQQhvY" 166 | }, 167 | "source": [ 168 | "#### Floats\n", 169 | "Floating point numbers: numbers WITH decimal places. " 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "metadata": { 175 | "id": "drSDl5nZQhvb", 176 | "colab": { 177 | "base_uri": "https://localhost:8080/" 178 | }, 179 | "outputId": "c68d21f4-5adb-492c-b686-9b0bc08e848f" 180 | }, 181 | "source": [ 182 | "floatingPointNumber = 5.5\n", 183 | "print(floatingPointNumber)" 184 | ], 185 | "execution_count": null, 186 | "outputs": [ 187 | { 188 | "output_type": "stream", 189 | "name": "stdout", 190 | "text": [ 191 | "5.5\n" 192 | ] 193 | } 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": { 199 | "id": "X0PyOXFgQhvd" 200 | }, 201 | "source": [ 202 | "##### Strings\n", 203 | "Sequence of characters surrounded by \"\" or ''(double quotes or single quotes)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "metadata": { 209 | "id": "Jwq4SmevQhve", 210 | "colab": { 211 | "base_uri": "https://localhost:8080/" 212 | }, 213 | "outputId": "7b40ed10-1155-42d3-c29f-1417aadbd617" 214 | }, 215 | "source": [ 216 | "#String examples:\n", 217 | "string1 = \"hello\"\n", 218 | "string2 = '3'\n", 219 | "string3 = \"Hi 'Pam'\"\n", 220 | "print(string1)\n", 221 | "print(string2)\n", 222 | "print(string3)\n", 223 | "# Double quoted strings (\"\") can have single quotes ('') in them\n", 224 | "# Single quoted strings can have double quotes in them\n", 225 | "\n" 226 | ], 227 | "execution_count": 10, 228 | "outputs": [ 229 | { 230 | "output_type": "stream", 231 | "name": "stdout", 232 | "text": [ 233 | "hello\n", 234 | "3\n", 235 | "Hi 'Pam'\n" 236 | ] 237 | } 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": { 243 | "id": "Aa11RTN0Qhvf" 244 | }, 245 | "source": [ 246 | "We can access strings through indexing. Indexing a string will allow us to access specific letters in the string. In python, we use [] to access specific letters with 0 being the first character and -1 being the last." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "metadata": { 252 | "id": "8e5SU4eAQhvg", 253 | "colab": { 254 | "base_uri": "https://localhost:8080/" 255 | }, 256 | "outputId": "2b09d1f2-a034-4897-c46a-f2f3dd62ec8a" 257 | }, 258 | "source": [ 259 | "a_string = \"hello\"\n", 260 | "# print h:\n", 261 | "print(a_string[0])\n", 262 | "# print o:\n", 263 | "print(a_string[4])" 264 | ], 265 | "execution_count": null, 266 | "outputs": [ 267 | { 268 | "output_type": "stream", 269 | "name": "stdout", 270 | "text": [ 271 | "h\n", 272 | "o\n" 273 | ] 274 | } 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": { 280 | "id": "knzZi2UlQhvh" 281 | }, 282 | "source": [ 283 | "##### Booleans\n", 284 | "Boolean variables either have the value True or False. " 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "metadata": { 290 | "id": "t5VEm7daQhvi" 291 | }, 292 | "source": [ 293 | "#used in IF-conditionals such as checking if a variable is a specific datatype, if an integer is a specific number, etc\n", 294 | "\n", 295 | "a_bool = True # any non-zero number as a boolean will be cast to True\n", 296 | "b_bool = False # 0 will be cast to False\n" 297 | ], 298 | "execution_count": null, 299 | "outputs": [] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": { 304 | "id": "VkhJjsgSQhvj" 305 | }, 306 | "source": [ 307 | "If we want to make a variable but have nothing in it, not even the value 0, we use None. This gets helpful in some more advanced programs, but for the basics, just know it exists. \n", 308 | "It is a useful thing to check when passing variables to different functions to ensure that input is correct. You always want to check that your input is not None and that it is not outside the bounds of what you need it to be for that function to ensure that someone can't break your code. " 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "metadata": { 314 | "id": "MXQW9VnOQhvj", 315 | "colab": { 316 | "base_uri": "https://localhost:8080/" 317 | }, 318 | "outputId": "4e0a298b-7667-49fa-8dcd-587f8ddc4664" 319 | }, 320 | "source": [ 321 | "myVar = None\n", 322 | "print(myVar)" 323 | ], 324 | "execution_count": null, 325 | "outputs": [ 326 | { 327 | "output_type": "stream", 328 | "name": "stdout", 329 | "text": [ 330 | "None\n" 331 | ] 332 | } 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": { 338 | "id": "EDtnPTv0Qhvk" 339 | }, 340 | "source": [ 341 | "If we want to know the type of a variable, we can use the type( ) function" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "metadata": { 347 | "id": "6yRYuTpWQhvk", 348 | "colab": { 349 | "base_uri": "https://localhost:8080/" 350 | }, 351 | "outputId": "3bcc5f31-8bc8-4a50-8561-7b380b119d19" 352 | }, 353 | "source": [ 354 | "a_string = \"hello\"\n", 355 | "print(type(a_string))" 356 | ], 357 | "execution_count": null, 358 | "outputs": [ 359 | { 360 | "output_type": "stream", 361 | "name": "stdout", 362 | "text": [ 363 | "\n" 364 | ] 365 | } 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": { 371 | "id": "Cd4rulE5Qhvl" 372 | }, 373 | "source": [ 374 | "You can name a variable almost anything you want with just a few rules\n", 375 | "\n", 376 | " - can't start a variable name with numbers\n", 377 | " - can't have a space in a variable name\n", 378 | " \n", 379 | "You should generally follow a few guidelines as well\n", 380 | "\n", 381 | " - start a variable name with a lowercase letter\n", 382 | " - keep them descriptive" 383 | ] 384 | }, 385 | { 386 | "cell_type": "markdown", 387 | "metadata": { 388 | "id": "4A6Qqs69Qhvm" 389 | }, 390 | "source": [ 391 | "### Operators" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": { 397 | "id": "DuHwgupYQhvm" 398 | }, 399 | "source": [ 400 | "Operators let us do things with variables and values, such as add, subtract, or compare them. \n", 401 | "\n", 402 | "We have already seen one operator: the assigment operator (=) lets us assign values to variables. \n", 403 | "\n", 404 | "The basic operators to know are\n", 405 | " \n", 406 | " assignment: =\n", 407 | " addition/concatenation: +\n", 408 | " subtraction: -\n", 409 | " multiplication: *\n", 410 | " floating point division: /\n", 411 | " integer division: //\n", 412 | " modulo: %\n", 413 | " exponentiation: **\n", 414 | " comparisons: <, <=, >, >=, !=, ==\n", 415 | " Boolean Operations: or, and, not" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": { 421 | "id": "OUNYaR5JQhvn" 422 | }, 423 | "source": [ 424 | "When we use + on two numbers, it works as addition just like in math. We can add different types of numbers together, such as floats and integers. " 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "metadata": { 430 | "id": "8LB0Y1FRQhvo", 431 | "colab": { 432 | "base_uri": "https://localhost:8080/" 433 | }, 434 | "outputId": "d7ae05f2-2023-4f32-f4e9-0667ca1dde39" 435 | }, 436 | "source": [ 437 | "beam = 9\n", 438 | "balance = 9.9\n", 439 | "bars = 7.5\n", 440 | "\n", 441 | "#find total score:\n", 442 | "print(beam+balance+bars)" 443 | ], 444 | "execution_count": null, 445 | "outputs": [ 446 | { 447 | "output_type": "stream", 448 | "name": "stdout", 449 | "text": [ 450 | "12.6\n" 451 | ] 452 | } 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": { 458 | "id": "VsbVITCRQhvp" 459 | }, 460 | "source": [ 461 | "When we use + on two strings, it concatenates them, we can use them with variables, strings, or both. + won't add spaces between strings though, so we have to do it manually." 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "metadata": { 467 | "id": "T2NGFwIVQhvp", 468 | "colab": { 469 | "base_uri": "https://localhost:8080/" 470 | }, 471 | "outputId": "ced73ac8-98b1-4ec5-96cb-0323197da56e" 472 | }, 473 | "source": [ 474 | "a_string = \"hello\"\n", 475 | "b_string = \"17\"\n", 476 | "\n", 477 | "print(a_string + b_string)\n", 478 | "print(a_string + \" \" + b_string)" 479 | ], 480 | "execution_count": null, 481 | "outputs": [ 482 | { 483 | "output_type": "stream", 484 | "name": "stdout", 485 | "text": [ 486 | "hello17\n", 487 | "hello 17\n" 488 | ] 489 | } 490 | ] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "metadata": { 495 | "id": "M3rF0ckIQhvq" 496 | }, 497 | "source": [ 498 | "We can't mix and match strings and numbers though, if we want to, we have to \"cast\" them with Pythons built-in casting system." 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "metadata": { 504 | "scrolled": true, 505 | "id": "I6wjbG5HQhvr", 506 | "colab": { 507 | "base_uri": "https://localhost:8080/" 508 | }, 509 | "outputId": "a1d0d13d-ccf8-4b03-9700-ef7c522eb9a9" 510 | }, 511 | "source": [ 512 | "#checking for types is useful for debugging\n", 513 | "print(4 + int('3')) #causes an error\n" 514 | ], 515 | "execution_count": null, 516 | "outputs": [ 517 | { 518 | "output_type": "stream", 519 | "name": "stdout", 520 | "text": [ 521 | "7\n" 522 | ] 523 | } 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": { 529 | "id": "SFCuOCROQhvt" 530 | }, 531 | "source": [ 532 | "Subtraction (-) works the same way as addition, but only works with numerical vaules\n" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "metadata": { 538 | "id": "Xg7U_8uxQhvt", 539 | "colab": { 540 | "base_uri": "https://localhost:8080/" 541 | }, 542 | "outputId": "6aee4584-1876-46ef-e135-9292b43ededa" 543 | }, 544 | "source": [ 545 | "a_float = 6.8 \n", 546 | "a_int = 2\n", 547 | "\n", 548 | "print(a_float - a_int) # a_int = 6" 549 | ], 550 | "execution_count": null, 551 | "outputs": [ 552 | { 553 | "output_type": "stream", 554 | "name": "stdout", 555 | "text": [ 556 | "4.8\n" 557 | ] 558 | } 559 | ] 560 | }, 561 | { 562 | "cell_type": "markdown", 563 | "metadata": { 564 | "id": "caALCCbhnqFb" 565 | }, 566 | "source": [ 567 | "Multiplication operator (*)" 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "metadata": { 573 | "id": "nome67GZQhvu", 574 | "colab": { 575 | "base_uri": "https://localhost:8080/" 576 | }, 577 | "outputId": "888be2a1-023a-4000-b4b2-c1d83c25bab8" 578 | }, 579 | "source": [ 580 | "# multiplication: \n", 581 | "print(3*4)\n", 582 | "\n", 583 | "# with strings:\n", 584 | "# what would you do to get the string \"hello hello hello\"\n", 585 | "print(\"hello \"*3)\n", 586 | "\n", 587 | "#exponents: \n", 588 | "print(2**4)" 589 | ], 590 | "execution_count": null, 591 | "outputs": [ 592 | { 593 | "output_type": "stream", 594 | "name": "stdout", 595 | "text": [ 596 | "12\n", 597 | "hello hello hello \n", 598 | "16\n" 599 | ] 600 | } 601 | ] 602 | }, 603 | { 604 | "cell_type": "markdown", 605 | "metadata": { 606 | "id": "SrgLf9q7nvtN" 607 | }, 608 | "source": [ 609 | "Division \n", 610 | "- floating point division: /\n", 611 | "- integer division: //\n", 612 | "\n", 613 | "Modulo (%)" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "metadata": { 619 | "id": "iy_aweTpQhvu", 620 | "colab": { 621 | "base_uri": "https://localhost:8080/" 622 | }, 623 | "outputId": "59dbad94-cc05-4397-9725-a7417781c7ff" 624 | }, 625 | "source": [ 626 | "# floating point division: \n", 627 | "print(5/3)\n", 628 | "\n", 629 | "# floor division (this will drop the decimal part and floor it): \n", 630 | "print(5//3)\n", 631 | "\n", 632 | "# modulo (this gives us the remainder):\n", 633 | "print(5 % 3)" 634 | ], 635 | "execution_count": null, 636 | "outputs": [ 637 | { 638 | "output_type": "stream", 639 | "name": "stdout", 640 | "text": [ 641 | "1.6666666666666667\n", 642 | "1\n", 643 | "2\n" 644 | ] 645 | } 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": { 651 | "id": "AknyNibxpL0g" 652 | }, 653 | "source": [ 654 | "Comparison operators:\n", 655 | "\n", 656 | "\n", 657 | "- less than \n", 658 | "- greater than \n", 659 | "- less than or equal to \n", 660 | "- greater than or equal to\n", 661 | "- not equals \n", 662 | "- equality \n", 663 | "\n", 664 | "\n", 665 | "\n", 666 | "\n", 667 | "\n" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "metadata": { 673 | "id": "l47wmTXnQhvv", 674 | "colab": { 675 | "base_uri": "https://localhost:8080/" 676 | }, 677 | "outputId": "755407cf-81ff-4a96-97d2-4dd1d8e12253" 678 | }, 679 | "source": [ 680 | "# Comparison operators return boolean values (True or False)\n", 681 | "# its worth taking extra care: == is equals. = is assignment.\n", 682 | "\n", 683 | "# testing comparison operators:\n", 684 | "beam = 9\n", 685 | "balance = 9.9\n", 686 | "\n", 687 | "print(beam < balance)\n", 688 | "\n" 689 | ], 690 | "execution_count": null, 691 | "outputs": [ 692 | { 693 | "output_type": "stream", 694 | "name": "stdout", 695 | "text": [ 696 | "True\n" 697 | ] 698 | } 699 | ] 700 | }, 701 | { 702 | "cell_type": "code", 703 | "metadata": { 704 | "id": "8r0SGZGYQhvv", 705 | "colab": { 706 | "base_uri": "https://localhost:8080/" 707 | }, 708 | "outputId": "00672f3f-c755-4865-d4ef-4e23897e91e5" 709 | }, 710 | "source": [ 711 | "# Boolean operators: or, and, not \n", 712 | "# Use these operators to combine boolean values (true and false)\n", 713 | "\n", 714 | "# Ex. Weather!\n", 715 | "\n", 716 | "cold = True \n", 717 | "windy = False\n", 718 | "\n", 719 | "print(cold and windy)" 720 | ], 721 | "execution_count": null, 722 | "outputs": [ 723 | { 724 | "output_type": "stream", 725 | "name": "stdout", 726 | "text": [ 727 | "False\n" 728 | ] 729 | } 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "source": [ 735 | "# OR returns True if either one is True\n", 736 | "print(True or True)\n", 737 | "print(True or False)\n", 738 | "print(False or False)\n", 739 | "\n", 740 | "# AND returns True only if both are True\n", 741 | "# Something cannot be be True AND False at the same time\n", 742 | "\n", 743 | "# NOT returns the opposite value (flip a boolean value)\n", 744 | "print(not True)" 745 | ], 746 | "metadata": { 747 | "colab": { 748 | "base_uri": "https://localhost:8080/" 749 | }, 750 | "id": "YQm78MC9pqJR", 751 | "outputId": "1de10f01-7c11-4095-cb16-84128de252e7" 752 | }, 753 | "execution_count": null, 754 | "outputs": [ 755 | { 756 | "output_type": "stream", 757 | "name": "stdout", 758 | "text": [ 759 | "True\n", 760 | "True\n", 761 | "False\n", 762 | "False\n" 763 | ] 764 | } 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": { 770 | "id": "IcCs380EQhvw" 771 | }, 772 | "source": [ 773 | "### Collections" 774 | ] 775 | }, 776 | { 777 | "cell_type": "markdown", 778 | "metadata": { 779 | "id": "tas7527sQhvw" 780 | }, 781 | "source": [ 782 | "A lot of the time, we will be working with a lot of numbers, and we need some way to arrange those numbers that make sense. Let's say we want a list of all the buildings in Atlanta. Making 100 or 200 variables for each building would be tedious! \n", 783 | "\n", 784 | "Python has a good range of collections to help us with this, the four most useful being:\n", 785 | "\n", 786 | " - Lists\n", 787 | " - Sets\n", 788 | " - Dictionaries\n", 789 | " - Tuples" 790 | ] 791 | }, 792 | { 793 | "cell_type": "markdown", 794 | "metadata": { 795 | "id": "B5MlBoMaQhvx" 796 | }, 797 | "source": [ 798 | "###### Lists\n", 799 | "\n", 800 | "Now we'll talk about lists.\n", 801 | "Lists are great when we have some ORDERDED DATA and WE HAVE DUPLICATES." 802 | ] 803 | }, 804 | { 805 | "cell_type": "code", 806 | "metadata": { 807 | "id": "xgDVKNk8Qhvy", 808 | "colab": { 809 | "base_uri": "https://localhost:8080/" 810 | }, 811 | "outputId": "124d04ed-1812-46c1-f089-095c637c7048" 812 | }, 813 | "source": [ 814 | "# There are two ways to make an empty list\n", 815 | "atl_buildings = list()\n", 816 | "atl_buildings = []\n", 817 | "\n", 818 | "# We can make one with information already in it if we want. We can also have a mix of strings and numbers in a list. \n", 819 | "atl_buildings = [\"Accenture\", \"Coca-cola\", \"NCR\", \"Pencil Building\", \"UHouse\"]\n", 820 | "print(atl_buildings)\n", 821 | "\n", 822 | "# We can access a list just with []. The index 0 is the first element in the list. \n", 823 | "atl_buildings[1] = \"Coke\"\n", 824 | "print(atl_buildings)\n", 825 | "# We can also change an element in the list using indices. \n" 826 | ], 827 | "execution_count": null, 828 | "outputs": [ 829 | { 830 | "output_type": "stream", 831 | "name": "stdout", 832 | "text": [ 833 | "['Accenture', 'Coca-cola', 'NCR', 'Pencil Building', 'UHouse']\n", 834 | "['Accenture', 'Coke', 'NCR', 'Pencil Building', 'UHouse']\n" 835 | ] 836 | } 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "metadata": { 842 | "id": "lsAIZke74Lha", 843 | "colab": { 844 | "base_uri": "https://localhost:8080/" 845 | }, 846 | "outputId": "8a398a4b-1b1c-4f66-8065-2ecf73ce83b2" 847 | }, 848 | "source": [ 849 | "atl_buildings = [\"Accenture\", \"Coca-cola\", \"NCR\", \"Pencil Building\", \"UHouse\"]\n", 850 | "\n", 851 | "# We can add to the end of a list with List.append(value)\n", 852 | "atl_buildings.append(\"Coda\")\n", 853 | "print(atl_buildings)\n", 854 | "\n", 855 | "# We can add an element at a certain index in a list using List.insert(index, value). \n", 856 | "# This replaces whatever is currently at index 3. \n", 857 | "atl_buildings.insert(2, \"Coda\")\n", 858 | "print(atl_buildings)\n", 859 | "\n", 860 | "\n" 861 | ], 862 | "execution_count": null, 863 | "outputs": [ 864 | { 865 | "output_type": "stream", 866 | "name": "stdout", 867 | "text": [ 868 | "['Accenture', 'Coca-cola', 'NCR', 'Pencil Building', 'UHouse', 'Coda']\n", 869 | "['Accenture', 'Coca-cola', 'Coda', 'NCR', 'Pencil Building', 'UHouse', 'Coda']\n" 870 | ] 871 | } 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "metadata": { 877 | "id": "_oLmy29PQhvy", 878 | "colab": { 879 | "base_uri": "https://localhost:8080/" 880 | }, 881 | "outputId": "3982647c-cad9-4f46-d278-977d51b02162" 882 | }, 883 | "source": [ 884 | "# There are a few ways to remove from a list\n", 885 | "\n", 886 | "# We can remove an item using List.remove(value) - only removes the FIRST OCCURRENCE in the list\n", 887 | "atl_buildings.remove(\"NCR\")\n", 888 | "print(atl_buildings)\n", 889 | "\n", 890 | "# List.pop() will remove and return the value at the LOCATION we want\n", 891 | "# we can save it to a variable for later! List.pop(index)\n", 892 | "\n", 893 | "# if we dont specify an index, the LAST element will be removed\n", 894 | "\n" 895 | ], 896 | "execution_count": null, 897 | "outputs": [ 898 | { 899 | "output_type": "stream", 900 | "name": "stdout", 901 | "text": [ 902 | "['Accenture', 'Coca-cola', 'Coda', 'Pencil Building', 'UHouse']\n" 903 | ] 904 | } 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "metadata": { 910 | "id": "oYf8Db8k2re7", 911 | "colab": { 912 | "base_uri": "https://localhost:8080/" 913 | }, 914 | "outputId": "12c156dd-5204-450b-b821-ab93a46dc6b6" 915 | }, 916 | "source": [ 917 | "# Time for a new operator, \"in\"\n", 918 | "# We can check to see if something is in our list (or any other collection!) using the \"in\" operator:\n", 919 | "print(\"NCR\" in atl_buildings)\n", 920 | "print(\"Google\" in atl_buildings)" 921 | ], 922 | "execution_count": null, 923 | "outputs": [ 924 | { 925 | "output_type": "stream", 926 | "name": "stdout", 927 | "text": [ 928 | "True\n", 929 | "False\n" 930 | ] 931 | } 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": { 937 | "id": "M5eCwz5RQhvx" 938 | }, 939 | "source": [ 940 | "###### Sets\n", 941 | "\n", 942 | "Sets are just like sets in math, they are a grouping of information that is NON-ORDERED and DO NOT CONTAIN DUPLICATES. We can store multiple types of data in a set - so a set can include strings, integers, floating point values, etc." 943 | ] 944 | }, 945 | { 946 | "cell_type": "code", 947 | "metadata": { 948 | "id": "U6HKIKNIQhvx", 949 | "colab": { 950 | "base_uri": "https://localhost:8080/" 951 | }, 952 | "outputId": "28d11647-c446-49e9-e6c8-c60650e06a47" 953 | }, 954 | "source": [ 955 | "# There is only one way to make an empty set\n", 956 | "buildings_set = {}\n", 957 | "\n", 958 | "# We can make one with information already in it if we want\n", 959 | "buildings_set = {\"Coca-cola\", 711}\n", 960 | "\n", 961 | "\n", 962 | "# to add to a set we use the Set.add() function\n", 963 | "buildings_set.add(\"NCR\")\n", 964 | "print(buildings_set)\n", 965 | "\n", 966 | "# we can remove from the set with Set.remove()\n", 967 | "buildings_set.remove(711)\n", 968 | "print(buildings_set)\n", 969 | "\n" 970 | ], 971 | "execution_count": null, 972 | "outputs": [ 973 | { 974 | "output_type": "stream", 975 | "name": "stdout", 976 | "text": [ 977 | "{'Coca-cola', 'NCR', 711}\n", 978 | "{'Coca-cola', 'NCR'}\n" 979 | ] 980 | } 981 | ] 982 | }, 983 | { 984 | "cell_type": "markdown", 985 | "metadata": { 986 | "id": "BTm5VelBQhv1" 987 | }, 988 | "source": [ 989 | "###### Dictionaries\n", 990 | "\n", 991 | "Dictionaries have information stored in Key:Value pairs, it lets us associate data with other data rather than just an index (or location)" 992 | ] 993 | }, 994 | { 995 | "cell_type": "code", 996 | "metadata": { 997 | "id": "fifUmarFQhv1", 998 | "colab": { 999 | "base_uri": "https://localhost:8080/" 1000 | }, 1001 | "outputId": "565f01d5-aa0c-426d-fa8b-8dd6aebcb8cd" 1002 | }, 1003 | "source": [ 1004 | "# There are two ways to make an empty dictionary\n", 1005 | "\n", 1006 | "# If we want to make one with information already in it\n", 1007 | "a_dict = {\"StudentNo\":5555, \"major\":\"Computer Science\"}\n", 1008 | "print(a_dict)\n", 1009 | "\n", 1010 | "# We can add to the dictionary by defining a NEW KEY\n", 1011 | "a_dict[\"Student Name\"] = \"John\"\n", 1012 | "print(a_dict)\n", 1013 | "\n", 1014 | "# If the key is already in the dictionary, we OVERWRITE the old data, its gone forever\n", 1015 | "a_dict[\"major\"] = \"Biology\"\n", 1016 | "print(a_dict)\n" 1017 | ], 1018 | "execution_count": null, 1019 | "outputs": [ 1020 | { 1021 | "output_type": "stream", 1022 | "name": "stdout", 1023 | "text": [ 1024 | "{'StudentNo': 5555, 'major': 'Computer Science'}\n", 1025 | "{'StudentNo': 5555, 'major': 'Computer Science', 'Student Name': 'John'}\n", 1026 | "{'StudentNo': 5555, 'major': 'Biology', 'Student Name': 'John'}\n" 1027 | ] 1028 | } 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "metadata": { 1034 | "id": "6OiZU3vcQhv2", 1035 | "colab": { 1036 | "base_uri": "https://localhost:8080/" 1037 | }, 1038 | "outputId": "c6f01a21-b6df-412c-fdca-5a73d735390f" 1039 | }, 1040 | "source": [ 1041 | "# If we don't know a key, we can get a list of keys back! \n", 1042 | "print(a_dict.keys())\n", 1043 | "\n", 1044 | "# We can also get a list of values back:\n", 1045 | "print(a_dict.values())\n", 1046 | "\n", 1047 | "# If we want to remove something from the dictionary we use Dict.pop(key)\n", 1048 | "# we need to tell it what key we want removed. It gives us (or \"returns\") the value back to save\n", 1049 | "popped = a_dict.pop(\"major\")\n", 1050 | "print(a_dict)\n", 1051 | "print(popped)\n" 1052 | ], 1053 | "execution_count": null, 1054 | "outputs": [ 1055 | { 1056 | "output_type": "stream", 1057 | "name": "stdout", 1058 | "text": [ 1059 | "dict_keys(['StudentNo', 'major', 'Student Name'])\n", 1060 | "dict_values([5555, 'Biology', 'John'])\n", 1061 | "{'StudentNo': 5555, 'Student Name': 'John'}\n", 1062 | "Biology\n" 1063 | ] 1064 | } 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "markdown", 1069 | "metadata": { 1070 | "id": "VmJMqG19Qhvz" 1071 | }, 1072 | "source": [ 1073 | "###### Tuples\n", 1074 | "\n", 1075 | "Next we'll touch on Tuples.\n", 1076 | "We use tuples when we have a small amount of ordered data we want to save." 1077 | ] 1078 | }, 1079 | { 1080 | "cell_type": "code", 1081 | "metadata": { 1082 | "id": "qngWvtWqQhvz", 1083 | "colab": { 1084 | "base_uri": "https://localhost:8080/" 1085 | }, 1086 | "outputId": "b050d3c6-aafc-4676-a29d-f73daeb06e32" 1087 | }, 1088 | "source": [ 1089 | "# Theres only one way to make tuple\n", 1090 | "a_tup = (1, 2, 3, \"dog\")\n", 1091 | "\n", 1092 | "# just like with a string and a list, we can access a tuple using []\n", 1093 | "\n", 1094 | "print(a_tup[1]) # remember we count from 0 in Python programming!" 1095 | ], 1096 | "execution_count": null, 1097 | "outputs": [ 1098 | { 1099 | "output_type": "stream", 1100 | "name": "stdout", 1101 | "text": [ 1102 | "2\n" 1103 | ] 1104 | } 1105 | ] 1106 | }, 1107 | { 1108 | "cell_type": "markdown", 1109 | "metadata": { 1110 | "id": "2DGSevqEQhv0" 1111 | }, 1112 | "source": [ 1113 | "Once we make a tuple, we can't change it. This is the reason we would pick a tuple over a List.\n", 1114 | "If we want to change it, we have to make a new tuple\n", 1115 | "we can however, assign that tuple to the same variable as the old one" 1116 | ] 1117 | }, 1118 | { 1119 | "cell_type": "code", 1120 | "metadata": { 1121 | "id": "FIpXk6tOQhv0", 1122 | "colab": { 1123 | "base_uri": "https://localhost:8080/" 1124 | }, 1125 | "outputId": "2510b8c6-f4d1-4f98-cc8b-60fb647241b3" 1126 | }, 1127 | "source": [ 1128 | "a_tup = (1,2,3,\"dog\") # so things don't break if you re-run\n", 1129 | "\n", 1130 | "# a_tup[1] = 1 # Python yells at us! We can't use indices to change a value in a tuple. \n", 1131 | "\n", 1132 | "b_tup = (a_tup[0], \"cat\", a_tup[2], a_tup[3], \"dog\")\n", 1133 | "\n", 1134 | "print(a_tup)\n", 1135 | "print(b_tup)" 1136 | ], 1137 | "execution_count": null, 1138 | "outputs": [ 1139 | { 1140 | "output_type": "stream", 1141 | "name": "stdout", 1142 | "text": [ 1143 | "(1, 2, 3, 'dog')\n", 1144 | "(1, 'cat', 3, 'dog', 'dog')\n" 1145 | ] 1146 | } 1147 | ] 1148 | }, 1149 | { 1150 | "cell_type": "markdown", 1151 | "metadata": { 1152 | "id": "3HkP0dw6Qhv2" 1153 | }, 1154 | "source": [ 1155 | "Tuples, Lists, Sets, and Dictionaries can hold any and all kinds of data we want. So we can put a List inside a List, a Dictionary inside a Dictionary, or a List inside a Set. Though its best to avoid doing this unless you need to." 1156 | ] 1157 | }, 1158 | { 1159 | "cell_type": "code", 1160 | "metadata": { 1161 | "colab": { 1162 | "base_uri": "https://localhost:8080/" 1163 | }, 1164 | "id": "-41ppVvtAr0v", 1165 | "outputId": "5e6de9ee-465e-4337-95b6-5f9c223c1a55" 1166 | }, 1167 | "source": [ 1168 | "a_dict = {\"StudentNo\":5555, \"major\":\"Computer Science\", \"name\": \"John\"}\n", 1169 | "b_dict = {\"StudentNo\":3333, \"major\":\"Computer Engineering\"}\n", 1170 | "list_student = [a_dict, b_dict]\n", 1171 | "print(list_student)" 1172 | ], 1173 | "execution_count": null, 1174 | "outputs": [ 1175 | { 1176 | "output_type": "stream", 1177 | "name": "stdout", 1178 | "text": [ 1179 | "[{'StudentNo': 5555, 'major': 'Computer Science', 'name': 'John'}, {'StudentNo': 3333, 'major': 'Computer Engineering'}]\n" 1180 | ] 1181 | } 1182 | ] 1183 | }, 1184 | { 1185 | "cell_type": "markdown", 1186 | "metadata": { 1187 | "id": "IdEe-cu8Qhv3" 1188 | }, 1189 | "source": [ 1190 | "### Conditionals" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "markdown", 1195 | "metadata": { 1196 | "id": "6ATPVhyNQhv3" 1197 | }, 1198 | "source": [ 1199 | "Conditional statements are used to execute a certain command only if certain requirements are met.\n", 1200 | "\n", 1201 | "The basic conditionals are if, elif, and else.\n", 1202 | "\n", 1203 | "Conditional statements are interpreted from start to end. Python will first interpret the if statement, then move to elif in the order they were written, then finally the else (note: elif and else ARE NOT required)\n", 1204 | "\n", 1205 | "Ex. Income taxes/tax brackets" 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "markdown", 1210 | "metadata": { 1211 | "id": "FqwYZeiGQhv5" 1212 | }, 1213 | "source": [ 1214 | "We use a colon (:) to end the line that contains the conditional\n", 1215 | "The : tells python that the next line should be a nested block of code; this is the block of code that runs if the conditional is true. \n", 1216 | "\n", 1217 | "The elif conditional is only evaluated if the original if statement fails.\n", 1218 | "the else conditional is only evaluated if both the original if statement and all elifs between fail.\n", 1219 | "\n", 1220 | "Because the blocks of code associated with the elif conditional and the else conditional are not executed, we skip them as well.\n", 1221 | "\n" 1222 | ] 1223 | }, 1224 | { 1225 | "cell_type": "code", 1226 | "metadata": { 1227 | "id": "u_J4id4DQhv4", 1228 | "colab": { 1229 | "base_uri": "https://localhost:8080/" 1230 | }, 1231 | "outputId": "50115274-3474-4cba-fc4d-c87cd1882852" 1232 | }, 1233 | "source": [ 1234 | "a_tup = (\"dog\", \"cat\", \"horse\")\n", 1235 | "\n", 1236 | "if (\"dog\" in a_tup):\n", 1237 | " print(\"bark\")\n", 1238 | "elif \"cat\" in a_tup:\n", 1239 | " print(\"meow\")\n", 1240 | "else:\n", 1241 | " print(\"I am not a dog or cat\")" 1242 | ], 1243 | "execution_count": null, 1244 | "outputs": [ 1245 | { 1246 | "output_type": "stream", 1247 | "name": "stdout", 1248 | "text": [ 1249 | "bark\n" 1250 | ] 1251 | } 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "code", 1256 | "metadata": { 1257 | "id": "SU_0AoNaQhv5", 1258 | "colab": { 1259 | "base_uri": "https://localhost:8080/" 1260 | }, 1261 | "outputId": "542db412-1c20-4150-ac93-f71487e401d3" 1262 | }, 1263 | "source": [ 1264 | "if \"dog\" in a_tup:\n", 1265 | " # since there is an if directly after this, this if statement stands alone\n", 1266 | " print(\"bark\")\n", 1267 | "if \"mouse\" in a_tup:\n", 1268 | " print(\"squeak\")\n", 1269 | "else:\n", 1270 | " # This else statement belongs to the If Directly above it. The first if is independent\n", 1271 | " print(\"I am not a dog or a mouse\")\n", 1272 | "# therefore both the first if, and the else statement are executed." 1273 | ], 1274 | "execution_count": null, 1275 | "outputs": [ 1276 | { 1277 | "output_type": "stream", 1278 | "name": "stdout", 1279 | "text": [ 1280 | "bark\n", 1281 | "I am not a dog or a mouse\n" 1282 | ] 1283 | } 1284 | ] 1285 | }, 1286 | { 1287 | "cell_type": "markdown", 1288 | "metadata": { 1289 | "id": "mUF6ir_NQhv5" 1290 | }, 1291 | "source": [ 1292 | "### For loops:\n", 1293 | "Probably the most powerful tool in a programming language is the ability to interate over every item with one statement this is done using for loops.\n", 1294 | "\n", 1295 | "Ex. Income statements: if we had the incomes of 1000 people in a list and we wanted to determine their income tax, it would be tedious to do this manually for every single person. Instead, we can loop through every element in the list using a for loop. \n", 1296 | "\n", 1297 | "Python has a very simple method for writing for loops" 1298 | ] 1299 | }, 1300 | { 1301 | "cell_type": "code", 1302 | "metadata": { 1303 | "id": "GL_k3a77Qhv6", 1304 | "colab": { 1305 | "base_uri": "https://localhost:8080/" 1306 | }, 1307 | "outputId": "a72bf750-1c78-417a-caea-c366e6b2dd78" 1308 | }, 1309 | "source": [ 1310 | "# x is representing an item in the list\n", 1311 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1312 | "\n", 1313 | "# write a for loop that iterates through the list: \n", 1314 | "for item in a_list:\n", 1315 | " print(item)" 1316 | ], 1317 | "execution_count": null, 1318 | "outputs": [ 1319 | { 1320 | "output_type": "stream", 1321 | "name": "stdout", 1322 | "text": [ 1323 | "1\n", 1324 | "2\n", 1325 | "3\n", 1326 | "dog\n", 1327 | "cat\n", 1328 | "fish\n", 1329 | "dog\n" 1330 | ] 1331 | } 1332 | ] 1333 | }, 1334 | { 1335 | "cell_type": "code", 1336 | "metadata": { 1337 | "id": "95k1FPoKQhv6", 1338 | "colab": { 1339 | "base_uri": "https://localhost:8080/" 1340 | }, 1341 | "outputId": "744317ed-c2b2-406b-96c7-d45e606cb625" 1342 | }, 1343 | "source": [ 1344 | "# Two functions useful for going through a list are len() and range(). \n", 1345 | "\n", 1346 | "# len() will return an integer that is the length of a collection\n", 1347 | "print(len(a_list))" 1348 | ], 1349 | "execution_count": null, 1350 | "outputs": [ 1351 | { 1352 | "output_type": "stream", 1353 | "name": "stdout", 1354 | "text": [ 1355 | "7\n" 1356 | ] 1357 | } 1358 | ] 1359 | }, 1360 | { 1361 | "cell_type": "code", 1362 | "source": [ 1363 | "# range() will generate a list of numbers we can use, usually in a loop we can call range with 1, 2, or 3 arguments\n", 1364 | "\n", 1365 | "# if we pass one input to range(x), it generates a list from 0 (inclusive) to x (exclusive)\n", 1366 | "for i in range(10):\n", 1367 | " print(i)" 1368 | ], 1369 | "metadata": { 1370 | "colab": { 1371 | "base_uri": "https://localhost:8080/" 1372 | }, 1373 | "id": "Q0kci46oA90e", 1374 | "outputId": "bcddf49c-9dd0-4818-f9eb-8fca6ef276e6" 1375 | }, 1376 | "execution_count": null, 1377 | "outputs": [ 1378 | { 1379 | "output_type": "stream", 1380 | "name": "stdout", 1381 | "text": [ 1382 | "0\n", 1383 | "1\n", 1384 | "2\n", 1385 | "3\n", 1386 | "4\n", 1387 | "5\n", 1388 | "6\n", 1389 | "7\n", 1390 | "8\n", 1391 | "9\n" 1392 | ] 1393 | } 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "source": [ 1399 | "# if we pass two inputs to range(x,y) it generates a list from x (inclusive) to y (exclusive)\n", 1400 | "for i in range(5,10):\n", 1401 | " print(i)" 1402 | ], 1403 | "metadata": { 1404 | "colab": { 1405 | "base_uri": "https://localhost:8080/" 1406 | }, 1407 | "id": "QDpcToZUBAQF", 1408 | "outputId": "c88d8aeb-110d-4ff0-d7b3-61392727279f" 1409 | }, 1410 | "execution_count": null, 1411 | "outputs": [ 1412 | { 1413 | "output_type": "stream", 1414 | "name": "stdout", 1415 | "text": [ 1416 | "5\n", 1417 | "6\n", 1418 | "7\n", 1419 | "8\n", 1420 | "9\n" 1421 | ] 1422 | } 1423 | ] 1424 | }, 1425 | { 1426 | "cell_type": "code", 1427 | "source": [ 1428 | "# if we pass three inputs to range(x,y,z) it generates a list from x to y using a step or increment of size z. z can be negative or positive.\n", 1429 | "for i in range(5,10, 2):\n", 1430 | " print(i)" 1431 | ], 1432 | "metadata": { 1433 | "colab": { 1434 | "base_uri": "https://localhost:8080/" 1435 | }, 1436 | "id": "cr4_QvhMBAap", 1437 | "outputId": "d2019a53-fa30-4c50-fbb3-e702bbffd602" 1438 | }, 1439 | "execution_count": null, 1440 | "outputs": [ 1441 | { 1442 | "output_type": "stream", 1443 | "name": "stdout", 1444 | "text": [ 1445 | "5\n", 1446 | "7\n", 1447 | "9\n" 1448 | ] 1449 | } 1450 | ] 1451 | }, 1452 | { 1453 | "cell_type": "code", 1454 | "metadata": { 1455 | "colab": { 1456 | "base_uri": "https://localhost:8080/" 1457 | }, 1458 | "id": "dFt1JaQpHn3l", 1459 | "outputId": "fc076edc-b710-43d0-9f40-d4095dc7cad2" 1460 | }, 1461 | "source": [ 1462 | "# Finally, we can use this to access elements of a list.\n", 1463 | "a_list = [1,2,3,\"dog\",\"cat\"]\n", 1464 | "for i in range(len(a_list)):\n", 1465 | " print(a_list[i])\n", 1466 | "print(\"end of list (indexing)\")\n", 1467 | "print()\n", 1468 | "\n", 1469 | "for item in a_list:\n", 1470 | " print(item)\n", 1471 | "print(\"end of list (iterate over elements)\")" 1472 | ], 1473 | "execution_count": null, 1474 | "outputs": [ 1475 | { 1476 | "output_type": "stream", 1477 | "name": "stdout", 1478 | "text": [ 1479 | "1\n", 1480 | "2\n", 1481 | "3\n", 1482 | "dog\n", 1483 | "cat\n", 1484 | "end of list (indexing)\n", 1485 | "\n", 1486 | "1\n", 1487 | "2\n", 1488 | "3\n", 1489 | "dog\n", 1490 | "cat\n", 1491 | "end of list (iterate over elements)\n" 1492 | ] 1493 | } 1494 | ] 1495 | }, 1496 | { 1497 | "cell_type": "markdown", 1498 | "metadata": { 1499 | "id": "mJHTprLKQhv7" 1500 | }, 1501 | "source": [ 1502 | "### While Loops:\n", 1503 | "On the other hand while loops will allow you to keep running a block of code until a condition is met. While a for loop can do the same thing as a while loop, they have different strengths for different use cases. \n", 1504 | "\n", 1505 | "Typically, use a while loop when you don't know how many times you will iterate and use a for-loop when you have a set numbers of times to iterate.\n", 1506 | "\n", 1507 | "Ex. Guessing a password" 1508 | ] 1509 | }, 1510 | { 1511 | "cell_type": "code", 1512 | "metadata": { 1513 | "id": "mw5YW8hgQhv7", 1514 | "colab": { 1515 | "base_uri": "https://localhost:8080/" 1516 | }, 1517 | "outputId": "77d6adbe-0e13-4900-98c8-c41f66a3f4c4" 1518 | }, 1519 | "source": [ 1520 | "# This accomplishes the same thing as the above for loop\n", 1521 | "# We would generally use the for loop instead of while loop in this case\n", 1522 | "i = 0\n", 1523 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1524 | "while i < len(a_list):\n", 1525 | " print(a_list[i])\n", 1526 | " i = i + 1\n", 1527 | " \n", 1528 | "# Unlike the for loop, you need to manually increment your index variable so that the while loop doesn't run forever" 1529 | ], 1530 | "execution_count": null, 1531 | "outputs": [ 1532 | { 1533 | "output_type": "stream", 1534 | "name": "stdout", 1535 | "text": [ 1536 | "1\n", 1537 | "2\n", 1538 | "3\n", 1539 | "dog\n", 1540 | "cat\n", 1541 | "fish\n", 1542 | "dog\n" 1543 | ] 1544 | } 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "markdown", 1549 | "metadata": { 1550 | "id": "PMhOzWt3Qhv8" 1551 | }, 1552 | "source": [ 1553 | "In the above example, theres not really a reason to use a while loop instead of a for loop. But a while loop's condition can be anything, so it can be really powerful." 1554 | ] 1555 | }, 1556 | { 1557 | "cell_type": "code", 1558 | "metadata": { 1559 | "id": "NO3-5sAqQhv8", 1560 | "colab": { 1561 | "base_uri": "https://localhost:8080/" 1562 | }, 1563 | "outputId": "720d98dc-e8e9-48b0-fe62-50464c4b8328" 1564 | }, 1565 | "source": [ 1566 | "# In this instance, the condition to loop is whether the item at the index in the list is not a string\n", 1567 | "# As soon as the item at the current index is a string, we exit loop\n", 1568 | "i = 0\n", 1569 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1570 | "while type(a_list[i]) is not str:\n", 1571 | " print(a_list[i])\n", 1572 | " i = i + 1" 1573 | ], 1574 | "execution_count": null, 1575 | "outputs": [ 1576 | { 1577 | "output_type": "stream", 1578 | "name": "stdout", 1579 | "text": [ 1580 | "1\n", 1581 | "2\n", 1582 | "3\n" 1583 | ] 1584 | } 1585 | ] 1586 | }, 1587 | { 1588 | "cell_type": "markdown", 1589 | "metadata": { 1590 | "id": "PFLJDGqDQhv9" 1591 | }, 1592 | "source": [ 1593 | "Because anything that evaluates to a boolean can be used as a condition, we can use True and have a while loop run forever.\n", 1594 | "\n", 1595 | "If we need to get out early, we can use the command break, which will always get you out of the most recent loop.\n" 1596 | ] 1597 | }, 1598 | { 1599 | "cell_type": "code", 1600 | "metadata": { 1601 | "id": "K7PzacIqQhv9", 1602 | "colab": { 1603 | "base_uri": "https://localhost:8080/" 1604 | }, 1605 | "outputId": "cf5f46c5-2cee-41f6-a6c0-2652309836ac" 1606 | }, 1607 | "source": [ 1608 | "i = 0\n", 1609 | "while True:\n", 1610 | " print(\"Hello! \" + str(i))\n", 1611 | " if i >= 5:\n", 1612 | " print(\"Breaking out of while loop\" + \"\\n\")\n", 1613 | " break\n", 1614 | " i += 1" 1615 | ], 1616 | "execution_count": null, 1617 | "outputs": [ 1618 | { 1619 | "output_type": "stream", 1620 | "name": "stdout", 1621 | "text": [ 1622 | "Hello! 0\n", 1623 | "Hello! 1\n", 1624 | "Hello! 2\n", 1625 | "Hello! 3\n", 1626 | "Hello! 4\n", 1627 | "Hello! 5\n", 1628 | "Breaking out of while loop\n", 1629 | "\n" 1630 | ] 1631 | } 1632 | ] 1633 | }, 1634 | { 1635 | "cell_type": "code", 1636 | "metadata": { 1637 | "colab": { 1638 | "base_uri": "https://localhost:8080/" 1639 | }, 1640 | "id": "TKvtKLy1MnoF", 1641 | "outputId": "7eb89ca8-ca1c-4ebe-b74b-50004b823f2b" 1642 | }, 1643 | "source": [ 1644 | "# break command also works with for loops\n", 1645 | "a_list = [\"snake\", \"elephant\", \"mouse\", \"dog\", \"cat\", \"fish\", \"dog\"]\n", 1646 | "\n", 1647 | "for animal in a_list:\n", 1648 | " print(\"Current animal: \" + str(animal))\n", 1649 | " if animal == \"dog\":\n", 1650 | " print(\"Breaking out of while loop.\")\n", 1651 | " break\n", 1652 | " else:\n", 1653 | " print(\"Not a dog.\")" 1654 | ], 1655 | "execution_count": null, 1656 | "outputs": [ 1657 | { 1658 | "output_type": "stream", 1659 | "name": "stdout", 1660 | "text": [ 1661 | "Current animal: snake\n", 1662 | "Not a dog.\n", 1663 | "Current animal: elephant\n", 1664 | "Not a dog.\n", 1665 | "Current animal: mouse\n", 1666 | "Not a dog.\n", 1667 | "Current animal: dog\n", 1668 | "Breaking out of while loop.\n" 1669 | ] 1670 | } 1671 | ] 1672 | }, 1673 | { 1674 | "cell_type": "markdown", 1675 | "metadata": { 1676 | "id": "6eNLEo1NQhv9" 1677 | }, 1678 | "source": [ 1679 | "### Functions:\n", 1680 | "\n", 1681 | "Now that we have learned the basics, we are ready for functions, We've already been using them. When we want to use a function we use the f_name() format. So print(), range(), len(), type() are all functions python already gives to us!\n", 1682 | "\n", 1683 | "Functions are sections of code that are CALLABLE and perform a specific task. \n", 1684 | "\n", 1685 | "Callable means that throughout our code we can \"summon\" that code we created once to run with a function call. \n", 1686 | "\n", 1687 | "A function WILL NOT run unless called.\n", 1688 | "\n", 1689 | "A Parameter is a variable we can pass into a function in order to change the function's behavior" 1690 | ] 1691 | }, 1692 | { 1693 | "cell_type": "code", 1694 | "metadata": { 1695 | "id": "epPwbXEaQhv-" 1696 | }, 1697 | "source": [ 1698 | "# We use def to define a function, then all parameters are inside the parenthesis\n", 1699 | "# This function multiplies 2 numbers together and returns the result\n", 1700 | "\n", 1701 | "def my_func(x, y): \n", 1702 | " z = x * y\n", 1703 | " if z > 0:\n", 1704 | " print(z)\n", 1705 | " return z # return statements are optional.\n", 1706 | "\n", 1707 | "# Functions with a return statement can pass its operation result into another variable" 1708 | ], 1709 | "execution_count": null, 1710 | "outputs": [] 1711 | }, 1712 | { 1713 | "cell_type": "code", 1714 | "metadata": { 1715 | "id": "W7OBhYx9Qhv-", 1716 | "colab": { 1717 | "base_uri": "https://localhost:8080/" 1718 | }, 1719 | "outputId": "e269d055-7998-4450-b06e-2515a7da329a" 1720 | }, 1721 | "source": [ 1722 | "# Now we will test our function\n", 1723 | "my_func(2, 3)\n", 1724 | "return_val = my_func(-2, 4)\n", 1725 | "print(return_val)\n", 1726 | "# Notice how these variables now contain what we specified by the return statement" 1727 | ], 1728 | "execution_count": null, 1729 | "outputs": [ 1730 | { 1731 | "output_type": "stream", 1732 | "name": "stdout", 1733 | "text": [ 1734 | "6\n", 1735 | "-8\n" 1736 | ] 1737 | } 1738 | ] 1739 | }, 1740 | { 1741 | "cell_type": "markdown", 1742 | "metadata": { 1743 | "id": "RCordUCbQhv-" 1744 | }, 1745 | "source": [ 1746 | "Today we have learned everything from the basic Python types, all the way up to crafting simple functions. This is as far as this lesson will go. If anyone has any questions, I would be happy to clarify now!" 1747 | ] 1748 | } 1749 | ] 1750 | } -------------------------------------------------------------------------------- /Python_Intro_Workshop_2023.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "-nKUfS2pQhvG" 17 | }, 18 | "source": [ 19 | "# Python 3 Introductory Workshop" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": { 25 | "id": "e_5aDfbIQhvM" 26 | }, 27 | "source": [ 28 | "### Topics\n", 29 | " - Why Python?\n", 30 | " - Variables\n", 31 | " - Operators\n", 32 | " - Collections\n", 33 | " - Conditionals\n", 34 | " - Loops" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": { 40 | "id": "crEu5G8XQhvO" 41 | }, 42 | "source": [ 43 | "### Why Python?\n", 44 | "Python is known for being one of the most versatile and easy to use languages\n", 45 | " \n", 46 | " - Simple syntax makes it easy to learn\n", 47 | " - Easy and intuitive to read, even with minimal programming knowledge\n", 48 | " - Ex. string.lower(); string.upper();\n", 49 | " - Great for prototyping and quickly writing working code\n", 50 | " - Portable across systems; As long as Python is installed it will run on any operating system\n", 51 | " \n", 52 | "Popular uses for Python include\n", 53 | "\n", 54 | " - Mathematics and Big Data\n", 55 | " Packages used for reading, manipulating, and analyzing data quickly(Numpy, Pandas)\n", 56 | " - Data Visualization\n", 57 | " Packages used for visualizing data on plots, graphs (Matplotlib, Plotly)\n", 58 | " - Webb Development\n", 59 | " Python offers many framework, which have been used to power some of the most popular sites such as Instagram, Spotify, Dropbox, Reddit, Netflix and Yelp.\n", 60 | " - Simple Scripting\n", 61 | " Writing a functional program that performs an individual task (rasberry pi)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 1, 67 | "metadata": { 68 | "id": "Y-nYIwr1q-KC" 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "# Anything that comes after a # on the same line is a comment\n", 73 | "# Python ignores it, but it lets us humans take notes on what we did" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": { 79 | "id": "qO9iINdYQhvQ" 80 | }, 81 | "source": [ 82 | "### Variables" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": { 88 | "id": "PyFrcZ8PQhvR" 89 | }, 90 | "source": [ 91 | "Variables are the building blocks in Python. We use variables to store information that we want to use later. \n", 92 | "\n", 93 | "To create a variable and assign it to a value/piece of information, we use =. \n", 94 | "\n", 95 | "Here, we're going to look at 4 primitive (basic) variable types:\n", 96 | " \n", 97 | " -integers\n", 98 | " -floats\n", 99 | " -strings\n", 100 | " -booleans" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": { 106 | "id": "h4kwW9yVQhvS" 107 | }, 108 | "source": [ 109 | "##### Integers\n", 110 | "\n", 111 | "Integers: positive or negative numbers WITHOUT decimal places - the same as in math." 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": { 118 | "colab": { 119 | "base_uri": "https://localhost:8080/" 120 | }, 121 | "id": "w9iQToKJQhvU", 122 | "outputId": "580fe190-becc-4064-a70d-ad7ddbac16d4" 123 | }, 124 | "outputs": [ 125 | { 126 | "output_type": "stream", 127 | "name": "stdout", 128 | "text": [ 129 | "5\n", 130 | "\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "myInt = 5\n", 136 | "print(myInt)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": { 142 | "id": "mqjLSsMdd7X6" 143 | }, 144 | "source": [ 145 | "If we want to see the value stored in a variable, we can use the built-in print( ) function.\n", 146 | "\n", 147 | "The print() function will show us whatever is inside the ( ) to our console, for Colab, the console will appear below our code blocks" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": { 153 | "id": "HsdHDijQQhvY" 154 | }, 155 | "source": [ 156 | "#### Floats\n", 157 | "Floating point numbers: numbers WITH decimal places. " 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": { 164 | "colab": { 165 | "base_uri": "https://localhost:8080/" 166 | }, 167 | "id": "drSDl5nZQhvb", 168 | "outputId": "c39df3ec-b973-408c-cfdb-0aac7a3af547" 169 | }, 170 | "outputs": [ 171 | { 172 | "output_type": "stream", 173 | "name": "stdout", 174 | "text": [ 175 | "5.5\n", 176 | "\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "floatingPointNumber = 5.5\n", 182 | "print(floatingPointNumber)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": { 188 | "id": "X0PyOXFgQhvd" 189 | }, 190 | "source": [ 191 | "##### Strings\n", 192 | "Sequence of characters surrounded by \"\" or ''(double quotes or single quotes)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "colab": { 200 | "base_uri": "https://localhost:8080/" 201 | }, 202 | "id": "Jwq4SmevQhve", 203 | "outputId": "72b8e25c-8389-4878-f0d6-dacb266359f1" 204 | }, 205 | "outputs": [ 206 | { 207 | "output_type": "stream", 208 | "name": "stdout", 209 | "text": [ 210 | "hello\n", 211 | "3\n", 212 | "Hi 'Pam'\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "#String examples:\n", 218 | "string1 = \"hello\"\n", 219 | "string2 = '3'\n", 220 | "string3 = \"Hi 'Pam'\"\n", 221 | "print(string1)\n", 222 | "print(string2)\n", 223 | "print(string3)\n", 224 | "# Double quoted strings (\"\") can have single quotes ('') in them\n", 225 | "# Single quoted strings can have double quotes in them" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": { 231 | "id": "Aa11RTN0Qhvf" 232 | }, 233 | "source": [] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": { 239 | "colab": { 240 | "base_uri": "https://localhost:8080/" 241 | }, 242 | "id": "8e5SU4eAQhvg", 243 | "outputId": "2def92b5-a033-4528-cefc-c5bc262d2a3b" 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "h\n", 251 | "o\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "a_string = \"hello\"\n", 257 | "# print h:\n", 258 | "print(a_string[0])\n", 259 | "# print o:\n", 260 | "print()" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": { 266 | "id": "knzZi2UlQhvh" 267 | }, 268 | "source": [ 269 | "##### Booleans\n", 270 | "Boolean variables either have the value True or False. " 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": { 277 | "id": "t5VEm7daQhvi" 278 | }, 279 | "outputs": [], 280 | "source": [ 281 | "#used in IF-conditionals such as checking if a variable is a specific datatype, if an integer is a specific number, etc\n", 282 | "\n", 283 | "a_bool = True # any non-zero number as a boolean will be cast to True\n", 284 | "b_bool = False # 0 will be cast to False\n" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "source": [ 290 | "##### None" 291 | ], 292 | "metadata": { 293 | "collapsed": false, 294 | "pycharm": { 295 | "name": "#%% md\n" 296 | } 297 | } 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "source": [ 302 | "If we want to make a variable but have nothing in it, not even the value 0, we use None. This gets helpful in some more advanced programs, but for the basics, just know it exists.\n", 303 | "It is a useful thing to check when passing variables to different functions to ensure that input is correct. You always want to check that your input is not None and that it is not outside the bounds of what you need it to be for that function to ensure that someone can't break your code.\n", 304 | "Ex. Temperature" 305 | ], 306 | "metadata": { 307 | "collapsed": false 308 | } 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 3, 313 | "metadata": { 314 | "colab": { 315 | "base_uri": "https://localhost:8080/" 316 | }, 317 | "id": "MXQW9VnOQhvj", 318 | "outputId": "4e0a298b-7667-49fa-8dcd-587f8ddc4664" 319 | }, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "None\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "myVar = None\n", 331 | "print(myVar)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": { 337 | "id": "EDtnPTv0Qhvk" 338 | }, 339 | "source": [ 340 | "If we want to know the type of a variable, we can use the type( ) function" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 4, 346 | "metadata": { 347 | "colab": { 348 | "base_uri": "https://localhost:8080/" 349 | }, 350 | "id": "6yRYuTpWQhvk", 351 | "outputId": "3bcc5f31-8bc8-4a50-8561-7b380b119d19" 352 | }, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "\n", 359 | "\n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "a_string = \"hello\"\n", 365 | "print(type(a_string))\n", 366 | "print(type(myVar))" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": { 372 | "id": "Cd4rulE5Qhvl" 373 | }, 374 | "source": [ 375 | "You can name a variable almost anything you want with just a few rules\n", 376 | "\n", 377 | " - can't start a variable name with numbers\n", 378 | " - can't have a space in a variable name\n", 379 | " \n", 380 | "You should generally follow a few guidelines as well\n", 381 | "\n", 382 | " - start a variable name with a lowercase letter\n", 383 | " - keep them descriptive" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": { 389 | "id": "4A6Qqs69Qhvm" 390 | }, 391 | "source": [ 392 | "### Operators" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": { 398 | "id": "DuHwgupYQhvm" 399 | }, 400 | "source": [ 401 | "Operators let us do things with variables and values, such as add, subtract, or compare them. \n", 402 | "\n", 403 | "We have already seen one operator: the assigment operator (=) lets us assign values to variables. \n", 404 | "\n", 405 | "The basic operators to know are\n", 406 | " \n", 407 | " assignment: =\n", 408 | " addition/concatenation: +\n", 409 | " subtraction: -\n", 410 | " multiplication: *\n", 411 | " floating point division: /\n", 412 | " integer division: //\n", 413 | " modulo: %\n", 414 | " exponentiation: **\n", 415 | " comparisons: <, <=, >, >=, !=, ==\n", 416 | " Boolean Operations: or, and, not" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": { 422 | "id": "OUNYaR5JQhvn" 423 | }, 424 | "source": [ 425 | "When we use + on two numbers, it works as addition just like in math. We can add different types of numbers together, such as floats and integers. " 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 5, 431 | "metadata": { 432 | "colab": { 433 | "base_uri": "https://localhost:8080/" 434 | }, 435 | "id": "8LB0Y1FRQhvo", 436 | "outputId": "2cbb7137-5d9c-48fa-e632-26fd4a6f35d0" 437 | }, 438 | "outputs": [ 439 | { 440 | "name": "stdout", 441 | "output_type": "stream", 442 | "text": [ 443 | "26.4\n", 444 | "\n" 445 | ] 446 | } 447 | ], 448 | "source": [ 449 | "# get three names and give a score\n", 450 | "beam = 9\n", 451 | "balance = 9.9\n", 452 | "bars = 7.5\n", 453 | "\n", 454 | "#find total score:\n", 455 | "print(beam+balance+bars)\n", 456 | "print(type(beam+balance+bars))" 457 | ] 458 | }, 459 | { 460 | "cell_type": "markdown", 461 | "metadata": { 462 | "id": "VsbVITCRQhvp" 463 | }, 464 | "source": [ 465 | "When we use + on two strings, it concatenates them, we can use them with variables, strings, or both. + won't add spaces between strings though, so we have to do it manually." 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": null, 471 | "metadata": { 472 | "colab": { 473 | "base_uri": "https://localhost:8080/" 474 | }, 475 | "id": "T2NGFwIVQhvp", 476 | "outputId": "65a1652b-1712-4403-ddc6-d6c3447c0f48" 477 | }, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "hello17\n", 484 | "hello 17\n" 485 | ] 486 | } 487 | ], 488 | "source": [ 489 | "a_string = \"hello\"\n", 490 | "b_string = \"17\"\n", 491 | "\n", 492 | "print(a_string + b_string)\n", 493 | "print(a_string + \" \" + b_string)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": { 499 | "id": "M3rF0ckIQhvq" 500 | }, 501 | "source": [ 502 | "We can't mix and match strings and numbers though, if we want to, we have to \"cast\" them with Pythons built-in casting system." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": { 509 | "colab": { 510 | "base_uri": "https://localhost:8080/" 511 | }, 512 | "id": "I6wjbG5HQhvr", 513 | "outputId": "a1d0d13d-ccf8-4b03-9700-ef7c522eb9a9", 514 | "scrolled": true 515 | }, 516 | "outputs": [ 517 | { 518 | "name": "stdout", 519 | "output_type": "stream", 520 | "text": [ 521 | "7\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "#checking for types is useful for debugging\n", 527 | "print(4 + '3') #causes an error\n", 528 | "print(4 + int('3')) # cast string into an int\n" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": { 534 | "id": "SFCuOCROQhvt" 535 | }, 536 | "source": [ 537 | "Subtraction (-) works the same way as addition, but only works with numerical vaules\n" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 6, 543 | "metadata": { 544 | "colab": { 545 | "base_uri": "https://localhost:8080/" 546 | }, 547 | "id": "Xg7U_8uxQhvt", 548 | "outputId": "6aee4584-1876-46ef-e135-9292b43ededa" 549 | }, 550 | "outputs": [ 551 | { 552 | "name": "stdout", 553 | "output_type": "stream", 554 | "text": [ 555 | "4.8\n" 556 | ] 557 | } 558 | ], 559 | "source": [ 560 | "a_float = 6.8 \n", 561 | "a_int = 2\n", 562 | "\n", 563 | "print(a_float - a_int) # a_int = 6" 564 | ] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "metadata": { 569 | "id": "caALCCbhnqFb" 570 | }, 571 | "source": [ 572 | "Multiplication operator (*)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "metadata": { 579 | "colab": { 580 | "base_uri": "https://localhost:8080/" 581 | }, 582 | "id": "nome67GZQhvu", 583 | "outputId": "888be2a1-023a-4000-b4b2-c1d83c25bab8" 584 | }, 585 | "outputs": [ 586 | { 587 | "name": "stdout", 588 | "output_type": "stream", 589 | "text": [ 590 | "12\n", 591 | "hello hello hello \n", 592 | "16\n" 593 | ] 594 | } 595 | ], 596 | "source": [ 597 | "# multiplication: \n", 598 | "print(3*4)\n", 599 | "\n", 600 | "# with strings:\n", 601 | "# what would you do to get the string \"hello hello hello\"\n", 602 | "print(\"hello \"*3)\n", 603 | "\n", 604 | "#exponents: \n", 605 | "print(2**4)" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": { 611 | "id": "SrgLf9q7nvtN" 612 | }, 613 | "source": [ 614 | "Division \n", 615 | "- floating point division: /\n", 616 | "- integer division: //" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": 7, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "1.6666666666666667\n", 628 | "1\n" 629 | ] 630 | } 631 | ], 632 | "source": [ 633 | "# floating point division:\n", 634 | "print(5/3)\n", 635 | "\n", 636 | "# floor division (this will drop the decimal part and floor it):\n", 637 | "print(5//3)" 638 | ], 639 | "metadata": { 640 | "collapsed": false, 641 | "pycharm": { 642 | "name": "#%%\n" 643 | } 644 | } 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "source": [ 649 | "Modulo (%)" 650 | ], 651 | "metadata": { 652 | "collapsed": false, 653 | "pycharm": { 654 | "name": "#%% md\n" 655 | } 656 | } 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 8, 661 | "metadata": { 662 | "colab": { 663 | "base_uri": "https://localhost:8080/" 664 | }, 665 | "id": "iy_aweTpQhvu", 666 | "outputId": "59dbad94-cc05-4397-9725-a7417781c7ff" 667 | }, 668 | "outputs": [ 669 | { 670 | "name": "stdout", 671 | "output_type": "stream", 672 | "text": [ 673 | "2\n" 674 | ] 675 | } 676 | ], 677 | "source": [ 678 | "# modulo (this gives us the remainder):\n", 679 | "print(5 % 3)" 680 | ] 681 | }, 682 | { 683 | "cell_type": "markdown", 684 | "metadata": { 685 | "id": "AknyNibxpL0g" 686 | }, 687 | "source": [ 688 | "Comparison operators:\n", 689 | "\n", 690 | "- less than \n", 691 | "- greater than \n", 692 | "- less than or equal to \n", 693 | "- greater than or equal to\n", 694 | "- not equals \n", 695 | "- equality" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": null, 701 | "metadata": { 702 | "colab": { 703 | "base_uri": "https://localhost:8080/" 704 | }, 705 | "id": "l47wmTXnQhvv", 706 | "outputId": "755407cf-81ff-4a96-97d2-4dd1d8e12253" 707 | }, 708 | "outputs": [ 709 | { 710 | "name": "stdout", 711 | "output_type": "stream", 712 | "text": [ 713 | "True\n" 714 | ] 715 | } 716 | ], 717 | "source": [ 718 | "# Comparison operators return boolean values (True or False)\n", 719 | "# its worth taking extra care: == is equals. = is assignment.\n", 720 | "\n", 721 | "# testing comparison operators:\n", 722 | "beam = 9\n", 723 | "balance = 9.9\n", 724 | "\n", 725 | "print(beam < balance)\n" 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": null, 731 | "metadata": { 732 | "colab": { 733 | "base_uri": "https://localhost:8080/" 734 | }, 735 | "id": "8r0SGZGYQhvv", 736 | "outputId": "00672f3f-c755-4865-d4ef-4e23897e91e5" 737 | }, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "False\n" 744 | ] 745 | } 746 | ], 747 | "source": [ 748 | "# Boolean operators: or, and, not \n", 749 | "# Use these operators to combine boolean values (true and false)\n", 750 | "\n", 751 | "# Ex. Weather!\n", 752 | "\n", 753 | "cold = True \n", 754 | "windy = False\n", 755 | "\n", 756 | "print(cold and windy)\n", 757 | "## try some more?" 758 | ] 759 | }, 760 | { 761 | "cell_type": "code", 762 | "execution_count": null, 763 | "metadata": { 764 | "colab": { 765 | "base_uri": "https://localhost:8080/" 766 | }, 767 | "id": "YQm78MC9pqJR", 768 | "outputId": "1de10f01-7c11-4095-cb16-84128de252e7" 769 | }, 770 | "outputs": [ 771 | { 772 | "name": "stdout", 773 | "output_type": "stream", 774 | "text": [ 775 | "True\n", 776 | "True\n", 777 | "False\n", 778 | "False\n" 779 | ] 780 | } 781 | ], 782 | "source": [ 783 | "# OR returns True if either one is True\n", 784 | "print(True or True)\n", 785 | "print(True or False)\n", 786 | "print(False or False)\n", 787 | "\n", 788 | "# AND returns True only if both are True\n", 789 | "# Something cannot be True AND False at the same time\n", 790 | "\n", 791 | "# NOT returns the opposite value (flip a boolean value)\n", 792 | "print(not True)" 793 | ] 794 | }, 795 | { 796 | "cell_type": "markdown", 797 | "metadata": { 798 | "id": "IcCs380EQhvw" 799 | }, 800 | "source": [ 801 | "### Collections" 802 | ] 803 | }, 804 | { 805 | "cell_type": "markdown", 806 | "metadata": { 807 | "id": "tas7527sQhvw" 808 | }, 809 | "source": [ 810 | "A lot of the time, we will be working with a lot of numbers, and we need some way to arrange those numbers that make sense. Let's say we want a list of all the buildings in Atlanta. Making 100 or 200 variables for each building would be tedious! \n", 811 | "\n", 812 | "There are four collection data types in the Python programming language:\n", 813 | "\n", 814 | "- List\n", 815 | "- Tuple\n", 816 | "- Set\n", 817 | "- Dictionary" 818 | ] 819 | }, 820 | { 821 | "cell_type": "markdown", 822 | "metadata": { 823 | "id": "B5MlBoMaQhvx" 824 | }, 825 | "source": [ 826 | "###### Lists\n", 827 | "\n", 828 | "Now we'll talk about lists.\n", 829 | "Lists are great when we have some ORDERDED DATA and WE HAVE DUPLICATES." 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": null, 835 | "metadata": { 836 | "colab": { 837 | "base_uri": "https://localhost:8080/" 838 | }, 839 | "id": "xgDVKNk8Qhvy", 840 | "outputId": "a61d4d72-e6e6-4d78-f9df-8ba7bcb3d179" 841 | }, 842 | "outputs": [ 843 | { 844 | "name": "stdout", 845 | "output_type": "stream", 846 | "text": [ 847 | "['Accenture', 'Coca-cola', 'NCR', 'Pencil Building', 'UHouse']\n", 848 | "['Accenture', 'Coke', 'NCR', 'Pencil Building', 'UHouse']\n" 849 | ] 850 | } 851 | ], 852 | "source": [ 853 | "# There are two ways to make an empty list\n", 854 | "atl_buildings = list()\n", 855 | "atl_buildings = []\n", 856 | "\n", 857 | "# We can make one with information already in it if we want. We can also have a mix of strings and numbers in a list. \n", 858 | "atl_buildings = [\"Accenture\", \"Coca-cola\", \"NCR\", \"Pencil Building\", \"UHouse\"]\n", 859 | "print(atl_buildings)" 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": null, 865 | "outputs": [], 866 | "source": [ 867 | "# We can access a list just with []. The index 0 is the first element in the list.\n", 868 | "atl_buildings[1] = \"Coke\"\n", 869 | "print(atl_buildings)\n", 870 | "# We can also change an element in the list using indices." 871 | ], 872 | "metadata": { 873 | "collapsed": false, 874 | "pycharm": { 875 | "name": "#%%\n" 876 | } 877 | } 878 | }, 879 | { 880 | "cell_type": "code", 881 | "execution_count": null, 882 | "metadata": { 883 | "colab": { 884 | "base_uri": "https://localhost:8080/" 885 | }, 886 | "id": "lsAIZke74Lha", 887 | "outputId": "2fcd998d-331a-4a68-9925-a0fca3f32aed" 888 | }, 889 | "outputs": [ 890 | { 891 | "name": "stdout", 892 | "output_type": "stream", 893 | "text": [ 894 | "['Accenture', 'Coca-cola', 'NCR', 'Pencil Building', 'UHouse', 'Coda']\n", 895 | "['Accenture', 'Coca-cola', 'Coda', 'NCR', 'Pencil Building', 'UHouse', 'Coda']\n" 896 | ] 897 | } 898 | ], 899 | "source": [ 900 | "atl_buildings = [\"Accenture\", \"Coca-cola\", \"NCR\", \"Pencil Building\", \"UHouse\"]\n", 901 | "\n", 902 | "# We can add to the end of a list with List.append(value)\n", 903 | "atl_buildings.append(\"Coda\")\n", 904 | "print(atl_buildings)\n", 905 | "\n", 906 | "# We can add an element at a certain index in a list using List.insert(index, value). \n", 907 | "atl_buildings.insert(2, \"Coda\")\n", 908 | "print(atl_buildings)\n" 909 | ] 910 | }, 911 | { 912 | "cell_type": "code", 913 | "execution_count": null, 914 | "metadata": { 915 | "colab": { 916 | "base_uri": "https://localhost:8080/" 917 | }, 918 | "id": "_oLmy29PQhvy", 919 | "outputId": "ac08b366-265c-412c-88cb-2852b1a1305b" 920 | }, 921 | "outputs": [ 922 | { 923 | "name": "stdout", 924 | "output_type": "stream", 925 | "text": [ 926 | "['Accenture', 'Coca-cola', 'Coda', 'Pencil Building', 'UHouse', 'Coda']\n", 927 | "['Accenture', 'Coca-cola', 'Coda', 'UHouse', 'Coda']\n", 928 | "Pencil Building\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "# There are a few ways to remove from a list\n", 934 | "\n", 935 | "# We can remove an item using List.remove(value) - only removes the FIRST OCCURRENCE in the list\n", 936 | "atl_buildings.remove(\"Coda\")\n", 937 | "print(atl_buildings)" 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "execution_count": null, 943 | "outputs": [], 944 | "source": [ 945 | "# List.pop() will remove and return the value at the LOCATION we want\n", 946 | "# we can save it to a variable for later! List.pop(index)\n", 947 | "\n", 948 | "# if we do not specify an index, the LAST element will be removed\n", 949 | "popped_element = atl_buildings.pop(3)\n", 950 | "print(atl_buildings)\n", 951 | "print(popped_element)" 952 | ], 953 | "metadata": { 954 | "collapsed": false, 955 | "pycharm": { 956 | "name": "#%%\n" 957 | } 958 | } 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": null, 963 | "metadata": { 964 | "colab": { 965 | "base_uri": "https://localhost:8080/" 966 | }, 967 | "id": "oYf8Db8k2re7", 968 | "outputId": "9b9ec760-4478-4eb7-d794-9a3d0b8ecf97" 969 | }, 970 | "outputs": [ 971 | { 972 | "name": "stdout", 973 | "output_type": "stream", 974 | "text": [ 975 | "True\n", 976 | "False\n" 977 | ] 978 | } 979 | ], 980 | "source": [ 981 | "# Time for a new operator, \"in\"\n", 982 | "# We can check to see if something is in our list (or any other collection!) using the \"in\" operator:\n", 983 | "print(\"UHouse\" in atl_buildings)\n", 984 | "print(\"Google\" in atl_buildings)" 985 | ] 986 | }, 987 | { 988 | "cell_type": "markdown", 989 | "metadata": { 990 | "id": "M5eCwz5RQhvx" 991 | }, 992 | "source": [ 993 | "###### Sets\n", 994 | "\n", 995 | "Sets are just like sets in math, they are a grouping of information that is NON-ORDERED and DO NOT CONTAIN DUPLICATES. We can store multiple types of data in a set - so a set can include strings, integers, floating point values, etc." 996 | ] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "execution_count": null, 1001 | "metadata": { 1002 | "colab": { 1003 | "base_uri": "https://localhost:8080/" 1004 | }, 1005 | "id": "U6HKIKNIQhvx", 1006 | "outputId": "c87fb57d-f7ea-475c-b9a7-14ef4f8704cb" 1007 | }, 1008 | "outputs": [ 1009 | { 1010 | "name": "stdout", 1011 | "output_type": "stream", 1012 | "text": [ 1013 | "{711, 'NCR', 'Coca-cola'}\n", 1014 | "{'NCR', 'Coca-cola'}\n" 1015 | ] 1016 | } 1017 | ], 1018 | "source": [ 1019 | "# There is only one way to make an empty set\n", 1020 | "buildings_set = {}\n", 1021 | "\n", 1022 | "# We can make one with information already in it if we want\n", 1023 | "buildings_set = {\"Coca-cola\", 711}\n", 1024 | "\n", 1025 | "\n", 1026 | "# to add to a set we use the Set.add() function\n", 1027 | "buildings_set.add(\"NCR\")\n", 1028 | "print(buildings_set)\n", 1029 | "\n", 1030 | "# adding multiple elements\n", 1031 | "buildings_set.update([\"Coda\", \"616 13th street\", \"Tech Square\"])\n", 1032 | "\n", 1033 | "# we can remove from the set with Set.remove()\n", 1034 | "buildings_set.remove(711)\n", 1035 | "print(buildings_set)" 1036 | ] 1037 | }, 1038 | { 1039 | "cell_type": "markdown", 1040 | "metadata": { 1041 | "id": "BTm5VelBQhv1" 1042 | }, 1043 | "source": [ 1044 | "###### Dictionaries\n", 1045 | "\n", 1046 | "Dictionaries have information stored in Key:Value pairs, it lets us associate data with other data rather than just an index (or location)\n", 1047 | "A dictionary is a collection which is ordered, changeable and do not allow duplicates." 1048 | ] 1049 | }, 1050 | { 1051 | "cell_type": "code", 1052 | "execution_count": null, 1053 | "metadata": { 1054 | "colab": { 1055 | "base_uri": "https://localhost:8080/" 1056 | }, 1057 | "id": "fifUmarFQhv1", 1058 | "outputId": "c54002a9-8e4c-43c5-a693-e7b8947c594f" 1059 | }, 1060 | "outputs": [ 1061 | { 1062 | "name": "stdout", 1063 | "output_type": "stream", 1064 | "text": [ 1065 | "{'StudentNo': 5555, 'major': 'Computer Science'}\n", 1066 | "{'StudentNo': 5555, 'major': 'Computer Science', 'Student Name': 'John'}\n", 1067 | "{'StudentNo': 5555, 'major': 'Biology', 'Student Name': 'John'}\n" 1068 | ] 1069 | } 1070 | ], 1071 | "source": [ 1072 | "# There are two ways to make an empty dictionary\n", 1073 | "\n", 1074 | "# If we want to make one with information already in it\n", 1075 | "a_dict = {\"StudentNo\":5555, \"major\":\"Computer Science\"}\n", 1076 | "print(a_dict)\n", 1077 | "\n", 1078 | "# We can add to the dictionary by defining a NEW KEY\n", 1079 | "a_dict[\"Student Name\"] = \"John\"\n", 1080 | "print(a_dict)\n", 1081 | "\n", 1082 | "# If the key is already in the dictionary, we OVERWRITE the old data, its gone forever\n", 1083 | "a_dict[\"major\"] = \"Biology\"\n", 1084 | "print(a_dict)\n" 1085 | ] 1086 | }, 1087 | { 1088 | "cell_type": "code", 1089 | "execution_count": null, 1090 | "metadata": { 1091 | "colab": { 1092 | "base_uri": "https://localhost:8080/" 1093 | }, 1094 | "id": "6OiZU3vcQhv2", 1095 | "outputId": "2d61a618-44e4-4add-8c9c-d0d3ae35bf68" 1096 | }, 1097 | "outputs": [ 1098 | { 1099 | "name": "stdout", 1100 | "output_type": "stream", 1101 | "text": [ 1102 | "dict_keys(['StudentNo', 'major', 'Student Name'])\n", 1103 | "dict_values([5555, 'Biology', 'John'])\n", 1104 | "{'StudentNo': 5555, 'Student Name': 'John'}\n", 1105 | "Biology\n" 1106 | ] 1107 | } 1108 | ], 1109 | "source": [ 1110 | "# If we don't know a key, we can get a list of keys back! \n", 1111 | "print(a_dict.keys())\n", 1112 | "\n", 1113 | "# We can also get a list of values back:\n", 1114 | "print(a_dict.values())" 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "code", 1119 | "execution_count": null, 1120 | "outputs": [], 1121 | "source": [ 1122 | "# If we want to remove something from the dictionary we use Dict.pop(key)\n", 1123 | "# we need to tell it what key we want removed. It gives us (or \"returns\") the value back to save\n", 1124 | "popped = a_dict.pop(\"major\")\n", 1125 | "print(a_dict)\n", 1126 | "print(popped)" 1127 | ], 1128 | "metadata": { 1129 | "collapsed": false, 1130 | "pycharm": { 1131 | "name": "#%%\n" 1132 | } 1133 | } 1134 | }, 1135 | { 1136 | "cell_type": "markdown", 1137 | "metadata": { 1138 | "id": "VmJMqG19Qhvz" 1139 | }, 1140 | "source": [ 1141 | "###### Tuples\n", 1142 | "\n", 1143 | "Next we'll touch on Tuples.\n", 1144 | "We use tuples when we have a small amount of ordered data we want to save." 1145 | ] 1146 | }, 1147 | { 1148 | "cell_type": "code", 1149 | "execution_count": null, 1150 | "metadata": { 1151 | "colab": { 1152 | "base_uri": "https://localhost:8080/" 1153 | }, 1154 | "id": "qngWvtWqQhvz", 1155 | "outputId": "b050d3c6-aafc-4676-a29d-f73daeb06e32" 1156 | }, 1157 | "outputs": [ 1158 | { 1159 | "name": "stdout", 1160 | "output_type": "stream", 1161 | "text": [ 1162 | "2\n" 1163 | ] 1164 | } 1165 | ], 1166 | "source": [ 1167 | "# Theres only one way to make tuple\n", 1168 | "a_tup = (1, 2, 3, \"dog\")\n", 1169 | "\n", 1170 | "# just like with a string and a list, we can access a tuple using []\n", 1171 | "\n", 1172 | "print(a_tup[1]) # remember we count from 0 in Python programming!" 1173 | ] 1174 | }, 1175 | { 1176 | "cell_type": "markdown", 1177 | "metadata": { 1178 | "id": "2DGSevqEQhv0" 1179 | }, 1180 | "source": [ 1181 | "Once we make a tuple, we can't change it. This is the reason we would pick a tuple over a List.\n", 1182 | "If we want to change it, we have to make a new tuple\n", 1183 | "we can however, assign that tuple to the same variable as the old one" 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "execution_count": null, 1189 | "metadata": { 1190 | "colab": { 1191 | "base_uri": "https://localhost:8080/" 1192 | }, 1193 | "id": "FIpXk6tOQhv0", 1194 | "outputId": "9f50a361-4784-495b-e7a5-a2b2def00c30" 1195 | }, 1196 | "outputs": [ 1197 | { 1198 | "name": "stdout", 1199 | "output_type": "stream", 1200 | "text": [ 1201 | "(1, 2, 3, 'dog')\n", 1202 | "(1, 'cat', 3, 'dog')\n" 1203 | ] 1204 | } 1205 | ], 1206 | "source": [ 1207 | "a_tup = (1,2,3,\"dog\") # so things don't break if you re-run\n", 1208 | "\n", 1209 | "a_tup[1] = \"cat\" # Python yells at us! We can't use indices to change a value in a tuple.\n", 1210 | "\n", 1211 | "# if I want to change 2 to \"cat\":\n", 1212 | "\n", 1213 | "b_tup = (a_tup[0], \"cat\", a_tup[2], a_tup[3])\n", 1214 | "\n", 1215 | "print(a_tup)\n", 1216 | "print(b_tup)" 1217 | ] 1218 | }, 1219 | { 1220 | "cell_type": "markdown", 1221 | "metadata": { 1222 | "id": "3HkP0dw6Qhv2" 1223 | }, 1224 | "source": [ 1225 | "Tuples, Lists, Sets, and Dictionaries can hold any and all kinds of data we want. So we can put a List inside a List, a Dictionary inside a Dictionary, or a List inside a Set." 1226 | ] 1227 | }, 1228 | { 1229 | "cell_type": "code", 1230 | "execution_count": null, 1231 | "metadata": { 1232 | "colab": { 1233 | "base_uri": "https://localhost:8080/" 1234 | }, 1235 | "id": "-41ppVvtAr0v", 1236 | "outputId": "5e6de9ee-465e-4337-95b6-5f9c223c1a55" 1237 | }, 1238 | "outputs": [ 1239 | { 1240 | "name": "stdout", 1241 | "output_type": "stream", 1242 | "text": [ 1243 | "[{'StudentNo': 5555, 'major': 'Computer Science', 'name': 'John'}, {'StudentNo': 3333, 'major': 'Computer Engineering'}]\n" 1244 | ] 1245 | } 1246 | ], 1247 | "source": [ 1248 | "a_dict = {\"StudentNo\":5555, \"major\":\"Computer Science\", \"name\": \"John\"}\n", 1249 | "b_dict = {\"StudentNo\":3333, \"major\":\"Computer Engineering\"}\n", 1250 | "list_student = [a_dict, b_dict]\n", 1251 | "print(list_student)" 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "markdown", 1256 | "metadata": { 1257 | "id": "IdEe-cu8Qhv3" 1258 | }, 1259 | "source": [ 1260 | "### Conditionals" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "markdown", 1265 | "metadata": { 1266 | "id": "6ATPVhyNQhv3" 1267 | }, 1268 | "source": [ 1269 | "Conditional statements are used to execute a certain command only if certain requirements are met.\n", 1270 | "\n", 1271 | "The basic conditionals are if, elif, and else.\n", 1272 | "\n", 1273 | "Conditional statements are interpreted from start to end. Python will first interpret the if statement, then move to elif in the order they were written, then finally the else (note: elif and else ARE NOT required)\n", 1274 | "\n", 1275 | "Ex. Income taxes/tax brackets" 1276 | ] 1277 | }, 1278 | { 1279 | "cell_type": "markdown", 1280 | "metadata": { 1281 | "id": "FqwYZeiGQhv5" 1282 | }, 1283 | "source": [ 1284 | "We use a colon (:) to end the line that contains the conditional\n", 1285 | "The : tells python that the next line should be a nested block of code; this is the block of code that runs if the conditional is true. \n", 1286 | "\n", 1287 | "The elif conditional is only evaluated if the original if statement fails.\n", 1288 | "the else conditional is only evaluated if both the original if statement and all elifs between fail.\n", 1289 | "\n", 1290 | "Because the blocks of code associated with the elif conditional and the else conditional are not executed, we skip them as well.\n", 1291 | "\n" 1292 | ] 1293 | }, 1294 | { 1295 | "cell_type": "code", 1296 | "execution_count": 9, 1297 | "metadata": { 1298 | "colab": { 1299 | "base_uri": "https://localhost:8080/" 1300 | }, 1301 | "id": "u_J4id4DQhv4", 1302 | "outputId": "348bcf78-e78a-4731-9ded-7a2ee917affa" 1303 | }, 1304 | "outputs": [ 1305 | { 1306 | "name": "stdout", 1307 | "output_type": "stream", 1308 | "text": [ 1309 | "bark\n" 1310 | ] 1311 | } 1312 | ], 1313 | "source": [ 1314 | "a_tup = (\"dog\", \"cat\", \"horse\")\n", 1315 | "\n", 1316 | "if (\"dog\" in a_tup):\n", 1317 | " print(\"bark\")\n", 1318 | "elif \"cat\" in a_tup:\n", 1319 | " print(\"meow\")\n", 1320 | "else:\n", 1321 | " print(\"I am not a dog or cat\")" 1322 | ] 1323 | }, 1324 | { 1325 | "cell_type": "code", 1326 | "execution_count": 10, 1327 | "metadata": { 1328 | "colab": { 1329 | "base_uri": "https://localhost:8080/" 1330 | }, 1331 | "id": "SU_0AoNaQhv5", 1332 | "outputId": "31d399d4-c02b-43dd-98c5-95a8476aef0f" 1333 | }, 1334 | "outputs": [ 1335 | { 1336 | "name": "stdout", 1337 | "output_type": "stream", 1338 | "text": [ 1339 | "bark\n", 1340 | "meow\n" 1341 | ] 1342 | } 1343 | ], 1344 | "source": [ 1345 | "if \"dog\" in a_tup:\n", 1346 | " # since there is an if directly after this, this if statement stands alone\n", 1347 | " print(\"bark\")\n", 1348 | "if \"cat\" in a_tup:\n", 1349 | " print(\"meow\")\n", 1350 | "else:\n", 1351 | " # This else statement belongs to the If Directly above it. The first if is independent\n", 1352 | " print(\"I am not a dog or a mouse\")\n", 1353 | "# therefore both the first if, and the else statement are executed." 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "markdown", 1358 | "metadata": { 1359 | "id": "mUF6ir_NQhv5" 1360 | }, 1361 | "source": [ 1362 | "### For loops:\n", 1363 | "Probably the most powerful tool in a programming language is the ability to iterate over every item with one statement this is done using for loops.\n", 1364 | "\n", 1365 | "Ex. Income statements: if we had the incomes of 1000 people in a list and we wanted to determine their income tax, it would be tedious to do this manually for every single person. Instead, we can loop through every element in the list using a for loop. \n", 1366 | "\n", 1367 | "Python has a very simple method for writing for loops" 1368 | ] 1369 | }, 1370 | { 1371 | "cell_type": "code", 1372 | "execution_count": null, 1373 | "metadata": { 1374 | "colab": { 1375 | "base_uri": "https://localhost:8080/" 1376 | }, 1377 | "id": "GL_k3a77Qhv6", 1378 | "outputId": "d618a50c-7891-4580-e6d4-db32f252c641" 1379 | }, 1380 | "outputs": [ 1381 | { 1382 | "name": "stdout", 1383 | "output_type": "stream", 1384 | "text": [ 1385 | "1\n", 1386 | "2\n", 1387 | "3\n", 1388 | "dog\n", 1389 | "cat\n", 1390 | "fish\n", 1391 | "dog\n" 1392 | ] 1393 | } 1394 | ], 1395 | "source": [ 1396 | "# x is representing an item in the list\n", 1397 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1398 | "\n", 1399 | "# write a for loop that iterates through the list: \n", 1400 | "for item in a_list:\n", 1401 | " print(item)" 1402 | ] 1403 | }, 1404 | { 1405 | "cell_type": "code", 1406 | "execution_count": null, 1407 | "metadata": { 1408 | "colab": { 1409 | "base_uri": "https://localhost:8080/" 1410 | }, 1411 | "id": "95k1FPoKQhv6", 1412 | "outputId": "785c4a91-2587-4fbe-9b64-2ab2c73313dc" 1413 | }, 1414 | "outputs": [ 1415 | { 1416 | "name": "stdout", 1417 | "output_type": "stream", 1418 | "text": [ 1419 | "7\n" 1420 | ] 1421 | } 1422 | ], 1423 | "source": [ 1424 | "# Two functions useful for going through a list are len() and range(). \n", 1425 | "\n", 1426 | "# len() will return an integer that is the length of a collection\n", 1427 | "print(len(a_list))" 1428 | ] 1429 | }, 1430 | { 1431 | "cell_type": "code", 1432 | "execution_count": null, 1433 | "metadata": { 1434 | "colab": { 1435 | "base_uri": "https://localhost:8080/" 1436 | }, 1437 | "id": "Q0kci46oA90e", 1438 | "outputId": "a3dd73ab-80f9-47c8-a919-cf7c2c1ac4f0" 1439 | }, 1440 | "outputs": [ 1441 | { 1442 | "name": "stdout", 1443 | "output_type": "stream", 1444 | "text": [ 1445 | "0\n", 1446 | "1\n", 1447 | "2\n", 1448 | "3\n", 1449 | "4\n", 1450 | "5\n", 1451 | "6\n", 1452 | "7\n", 1453 | "8\n", 1454 | "9\n" 1455 | ] 1456 | } 1457 | ], 1458 | "source": [ 1459 | "# range() will generate a list of numbers we can use, usually in a loop we can call range with 1, 2, or 3 arguments\n", 1460 | "\n", 1461 | "# if we pass one input to range(x), it generates a list from 0 (inclusive) to x (exclusive)\n", 1462 | "for i in range(10):\n", 1463 | " print(i)" 1464 | ] 1465 | }, 1466 | { 1467 | "cell_type": "code", 1468 | "execution_count": null, 1469 | "metadata": { 1470 | "colab": { 1471 | "base_uri": "https://localhost:8080/" 1472 | }, 1473 | "id": "dFt1JaQpHn3l", 1474 | "outputId": "fc076edc-b710-43d0-9f40-d4095dc7cad2" 1475 | }, 1476 | "outputs": [ 1477 | { 1478 | "name": "stdout", 1479 | "output_type": "stream", 1480 | "text": [ 1481 | "1\n", 1482 | "2\n", 1483 | "3\n", 1484 | "dog\n", 1485 | "cat\n", 1486 | "end of list (indexing)\n", 1487 | "\n", 1488 | "1\n", 1489 | "2\n", 1490 | "3\n", 1491 | "dog\n", 1492 | "cat\n", 1493 | "end of list (iterate over elements)\n" 1494 | ] 1495 | } 1496 | ], 1497 | "source": [ 1498 | "# Finally, we can use this to access elements of a list.\n", 1499 | "a_list = [1,2,3,\"dog\",\"cat\"]\n", 1500 | "for i in range(len(a_list)):\n", 1501 | " print(a_list[i])\n", 1502 | "print(\"end of list (indexing)\")\n", 1503 | "print()" 1504 | ] 1505 | }, 1506 | { 1507 | "cell_type": "markdown", 1508 | "metadata": { 1509 | "id": "mJHTprLKQhv7" 1510 | }, 1511 | "source": [ 1512 | "### While Loops:\n", 1513 | "On the other hand while loops will allow you to keep running a block of code until a condition is met. While a for loop can do the same thing as a while loop, they have different strengths for different use cases. \n", 1514 | "\n", 1515 | "Typically, use a while loop when you don't know how many times you will iterate and use a for-loop when you have a set numbers of times to iterate.\n", 1516 | "\n", 1517 | "Ex. Guessing a password" 1518 | ] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "execution_count": null, 1523 | "metadata": { 1524 | "colab": { 1525 | "base_uri": "https://localhost:8080/" 1526 | }, 1527 | "id": "mw5YW8hgQhv7", 1528 | "outputId": "8b3d5e8a-1521-4fb4-97d3-ec8781260cbc" 1529 | }, 1530 | "outputs": [ 1531 | { 1532 | "name": "stdout", 1533 | "output_type": "stream", 1534 | "text": [ 1535 | "1\n", 1536 | "2\n", 1537 | "3\n", 1538 | "dog\n", 1539 | "cat\n", 1540 | "fish\n", 1541 | "dog\n" 1542 | ] 1543 | } 1544 | ], 1545 | "source": [ 1546 | "# This accomplishes the same thing as the above for loop\n", 1547 | "# We would generally use the for loop instead of while loop in this case\n", 1548 | "\n", 1549 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1550 | "i = 0\n", 1551 | "while i < len(a_list):\n", 1552 | " print(a_list[i])\n", 1553 | " i = i + 1\n", 1554 | " \n", 1555 | "# Unlike the for loop, you need to manually increment your index variable so that the while loop doesn't run forever" 1556 | ] 1557 | }, 1558 | { 1559 | "cell_type": "markdown", 1560 | "metadata": { 1561 | "id": "PMhOzWt3Qhv8" 1562 | }, 1563 | "source": [ 1564 | "In the above example, theres not really a reason to use a while loop instead of a for loop. But a while loop's condition can be anything, so it can be really powerful." 1565 | ] 1566 | }, 1567 | { 1568 | "cell_type": "code", 1569 | "execution_count": null, 1570 | "metadata": { 1571 | "colab": { 1572 | "base_uri": "https://localhost:8080/" 1573 | }, 1574 | "id": "NO3-5sAqQhv8", 1575 | "outputId": "6af5316e-538a-48db-c193-43ecf38988a6" 1576 | }, 1577 | "outputs": [ 1578 | { 1579 | "name": "stdout", 1580 | "output_type": "stream", 1581 | "text": [ 1582 | "1\n", 1583 | "2\n", 1584 | "3\n" 1585 | ] 1586 | } 1587 | ], 1588 | "source": [ 1589 | "# In this instance, the condition to loop is whether the item at the index in the list is not a string\n", 1590 | "# As soon as the item at the current index is a string, we exit loop\n", 1591 | "\n", 1592 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1593 | "i = 0\n", 1594 | "while type(a_list[i]) is not str:\n", 1595 | " print(a_list[i])\n", 1596 | " i = i + 1" 1597 | ] 1598 | }, 1599 | { 1600 | "cell_type": "markdown", 1601 | "metadata": { 1602 | "id": "PFLJDGqDQhv9" 1603 | }, 1604 | "source": [ 1605 | "Because anything that evaluates to a boolean can be used as a condition, we can use True and have a while loop run forever.\n", 1606 | "\n", 1607 | "If we need to get out early, we can use the command break, which will always get you out of the most recent loop.\n" 1608 | ] 1609 | }, 1610 | { 1611 | "cell_type": "code", 1612 | "execution_count": null, 1613 | "metadata": { 1614 | "colab": { 1615 | "base_uri": "https://localhost:8080/" 1616 | }, 1617 | "id": "K7PzacIqQhv9", 1618 | "outputId": "12b54ae7-c234-4b28-9202-cbb8085ab712" 1619 | }, 1620 | "outputs": [ 1621 | { 1622 | "name": "stdout", 1623 | "output_type": "stream", 1624 | "text": [ 1625 | "Hello! 0\n", 1626 | "Hello! 1\n", 1627 | "Hello! 2\n", 1628 | "Hello! 3\n", 1629 | "Hello! 4\n", 1630 | "Hello! 5\n", 1631 | "Breaking out of while loop\n", 1632 | "\n" 1633 | ] 1634 | } 1635 | ], 1636 | "source": [ 1637 | "i = 0\n", 1638 | "while True:\n", 1639 | " print(\"Hello! \" + str(i))\n", 1640 | " if i >= 5:\n", 1641 | " print(\"Breaking out of while loop\" + \"\\n\")\n", 1642 | " break\n", 1643 | " i += 1" 1644 | ] 1645 | }, 1646 | { 1647 | "cell_type": "code", 1648 | "execution_count": null, 1649 | "metadata": { 1650 | "colab": { 1651 | "base_uri": "https://localhost:8080/" 1652 | }, 1653 | "id": "TKvtKLy1MnoF", 1654 | "outputId": "cdf36b34-09ed-4f4c-bab5-b54019e54760" 1655 | }, 1656 | "outputs": [ 1657 | { 1658 | "name": "stdout", 1659 | "output_type": "stream", 1660 | "text": [ 1661 | "Current animal: snake\n", 1662 | "Not a dog.\n", 1663 | "Current animal: elephant\n", 1664 | "Not a dog.\n", 1665 | "Current animal: mouse\n", 1666 | "Not a dog.\n", 1667 | "Current animal: dog\n", 1668 | "Breaking out of while loop.\n" 1669 | ] 1670 | } 1671 | ], 1672 | "source": [ 1673 | "# break command also works with for loops\n", 1674 | "a_list = [\"snake\", \"elephant\", \"mouse\", \"dog\", \"cat\", \"fish\", \"dog\"]\n", 1675 | "\n", 1676 | "for animal in a_list:\n", 1677 | " print(\"Current animal: \" + str(animal))\n", 1678 | " if animal == \"dog\":\n", 1679 | " print(\"Breaking out of while loop.\")\n", 1680 | " break\n", 1681 | " else:\n", 1682 | " print(\"Not a dog.\")" 1683 | ] 1684 | }, 1685 | { 1686 | "cell_type": "markdown", 1687 | "metadata": { 1688 | "id": "6eNLEo1NQhv9" 1689 | }, 1690 | "source": [ 1691 | "### Functions:\n", 1692 | "\n", 1693 | "Now that we have learned the basics, we are ready for functions, We've already been using them. When we want to use a function we use the f_name() format. So print(), range(), len(), type() are all functions python already gives to us!\n", 1694 | "\n", 1695 | "Functions are sections of code that are CALLABLE and perform a specific task. \n", 1696 | "\n", 1697 | "Callable means that throughout our code we can \"summon\" that code we created once to run with a function call. \n", 1698 | "\n", 1699 | "A function WILL NOT run unless called.\n", 1700 | "\n", 1701 | "A Parameter is a variable we can pass into a function in order to change the function's behavior" 1702 | ] 1703 | }, 1704 | { 1705 | "cell_type": "code", 1706 | "execution_count": null, 1707 | "metadata": { 1708 | "id": "epPwbXEaQhv-" 1709 | }, 1710 | "outputs": [], 1711 | "source": [ 1712 | "# We use def to define a function, then all parameters are inside the parenthesis\n", 1713 | "# This function multiplies 2 numbers together and returns the result\n", 1714 | "\n", 1715 | "def my_func(x, y): \n", 1716 | " z = x * y\n", 1717 | " return z # return statements are optional.\n", 1718 | "\n", 1719 | "# Functions with a return statement can pass its operation result into another variable" 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "code", 1724 | "execution_count": null, 1725 | "metadata": { 1726 | "colab": { 1727 | "base_uri": "https://localhost:8080/" 1728 | }, 1729 | "id": "W7OBhYx9Qhv-", 1730 | "outputId": "e269d055-7998-4450-b06e-2515a7da329a" 1731 | }, 1732 | "outputs": [ 1733 | { 1734 | "name": "stdout", 1735 | "output_type": "stream", 1736 | "text": [ 1737 | "6\n", 1738 | "-8\n" 1739 | ] 1740 | } 1741 | ], 1742 | "source": [ 1743 | "# Now we will test our function by calling it\n", 1744 | "my_func(2, 3)" 1745 | ] 1746 | }, 1747 | { 1748 | "cell_type": "code", 1749 | "execution_count": null, 1750 | "outputs": [], 1751 | "source": [ 1752 | "return_val = my_func(-2, 4)\n", 1753 | "print(return_val)\n", 1754 | "# Notice how these variables now contain what we specified by the return statement" 1755 | ], 1756 | "metadata": { 1757 | "collapsed": false, 1758 | "pycharm": { 1759 | "name": "#%%\n" 1760 | } 1761 | } 1762 | }, 1763 | { 1764 | "cell_type": "markdown", 1765 | "metadata": { 1766 | "id": "RCordUCbQhv-" 1767 | }, 1768 | "source": [ 1769 | "Today we have learned everything from the basic Python types, all the way up to crafting simple functions. This is as far as this lesson will go. If anyone has any questions, I would be happy to clarify now!" 1770 | ] 1771 | }, 1772 | { 1773 | "cell_type": "code", 1774 | "execution_count": null, 1775 | "outputs": [], 1776 | "source": [ 1777 | "# Exercise:\n", 1778 | "#Type here. Assign a number to the variable: glass_of_water\n", 1779 | "\n", 1780 | "glass_of_water = 0\n", 1781 | "glass_of_water = glass_of_water + 1\n", 1782 | "\n", 1783 | "print(\"I drank\", glass_of_water, \"glasses of water today.\")\n", 1784 | "\n", 1785 | "#my_grade variable is a string (because it's in quotes). Convert it to an integer.\n", 1786 | "my_grade = \"10\"\n", 1787 | "\n", 1788 | "# my_grade_int =\n", 1789 | "#\n", 1790 | "# print(my_grade_int)\n", 1791 | "\n", 1792 | "#my_temp variable is a float (because it has decimals). Convert it to an integer.\n", 1793 | "my_temp = 97.70\n", 1794 | "\n", 1795 | "# my_temp_int =\n", 1796 | "#\n", 1797 | "# print(my_temp_int)" 1798 | ], 1799 | "metadata": { 1800 | "collapsed": false, 1801 | "pycharm": { 1802 | "name": "#%%\n" 1803 | } 1804 | } 1805 | } 1806 | ], 1807 | "metadata": { 1808 | "colab": { 1809 | "provenance": [], 1810 | "include_colab_link": true 1811 | }, 1812 | "kernelspec": { 1813 | "display_name": "myenv", 1814 | "language": "python", 1815 | "name": "myenv" 1816 | }, 1817 | "language_info": { 1818 | "codemirror_mode": { 1819 | "name": "ipython", 1820 | "version": 3 1821 | }, 1822 | "file_extension": ".py", 1823 | "mimetype": "text/x-python", 1824 | "name": "python", 1825 | "nbconvert_exporter": "python", 1826 | "pygments_lexer": "ipython3", 1827 | "version": "3.9.12" 1828 | }, 1829 | "vscode": { 1830 | "interpreter": { 1831 | "hash": "ec097131899e94b66af6538ba0a72a31cc5f4683721812f596a030ea0bd94a20" 1832 | } 1833 | } 1834 | }, 1835 | "nbformat": 4, 1836 | "nbformat_minor": 0 1837 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Intro-Workshop-2021 2 | 3 | This workshop aims to teach the basic fundamentals of python. We will be coding using python 3 specifically, but most of the concepts apply to python 2 as well. We will go over variables, types, conditionals, loops, and basic functions in the course of the workshop. This will give you a good base to figure out how to start making your own simple functions and programs going forward as well as understanding other people's code you may encounter. 4 | 5 | 6 | Main content is inside of python_intro_2019.ipynb file, you can view it in the browser on github or at this link: 7 | https://nbviewer.jupyter.org/github/GTLibraryDataVisualization/Python-Intro-Workshop-2019/blob/master/python_intro_2019.ipynb# 8 | 9 | FOR THIS WORKSHOP YOU NEED: 10 | -Jupyter Notebook 11 | -Python 3.0 and above. (3.7 recommended) 12 | 13 | Download Anaconda (inlcuding Python and Juputer Notebook): https://docs.anaconda.com/anaconda/install/ 14 | 15 | OR 16 | 17 | Download Python: https://www.python.org/downloads/ 18 | 19 | Tutorial for installing Python (Windows): 20 | -https://www.youtube.com/watch?v=rVb1TqqbPj0 21 | 22 | Tutorial for intalling on Mac: 23 | -https://www.youtube.com/watch?v=8BiYGIDCvvA 24 | 25 | Once you have python installed: 26 | -open command prompt 27 | -type: pip3 install jupyter 28 | 29 | If you run into an error message "pip not recognized", it means you do not have pip, which is what we use to install jupyter notebook here. Then run the commands below: 30 | 31 | curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 32 | 33 | python3 get-pip.py 34 | 35 | These two lines of code will help you install pip. And then go back to the section above and run the command: 36 | 37 | pip3 install jupyter 38 | 39 | 40 | Workshop survey: https://docs.google.com/forms/d/e/1FAIpQLScpN3Leq765IihESOpakWJgj4MqdtR_jf9GXtlyqrMGA7nYqg/viewform?usp=sf_link 41 | 42 | -------------------------------------------------------------------------------- /python_intro_2019.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python 3 Introductory Workshop" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Topics\n", 15 | " - Why Python?\n", 16 | " - Variables\n", 17 | " - Operators\n", 18 | " - Collections\n", 19 | " - Conditionals\n", 20 | " - Loops" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "### Why Python?\n", 28 | "Python is known for being one of the most versitile and easy to use languages\n", 29 | " \n", 30 | " - Simple syntax makes it easy to learn\n", 31 | " - Easy and intuitive to read, even with minimal programming knowledge\n", 32 | " - Great for prototyping and quickly writing working code\n", 33 | " - Portable across systems; As long as Python is installed it will run on any operting system\n", 34 | " \n", 35 | "Popular uses for Python include\n", 36 | "\n", 37 | " - Mathematics and big data\n", 38 | " Packagages used for reading in, manipulating, and analyzing data quickly(Numpy, Pandas)\n", 39 | " - Simple Scripting\n", 40 | " Writing a functional program that performs an individual task (rasberry pi)\n", 41 | " - Server side web development (django, flask)\n", 42 | " Server side scripting means hosting a web server that can handle requests\n", 43 | " - Software development\n", 44 | " Python is robust enough to handle creating almost anything you want to do" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### Variables" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "We need to be able to remember information for later, to do this we use variables.\n", 59 | "we can assign a variable a value with =\n", 60 | "\n", 61 | "Here, we're going to look at 4 variable types:\n", 62 | " \n", 63 | " -integers\n", 64 | " -floats\n", 65 | " -strings\n", 66 | " -booleans\n", 67 | "\n", 68 | "Python is a \"dynamically typed language\"\n", 69 | "\n", 70 | "This means that we do not need to declare variable types, or tell the computer what type they are, we just trust Python to keep track for us." 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "##### Integers\n", 78 | "\n", 79 | "integers; positive or negative numbers WITHOUT decimal places - the same as in math" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 38, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "6\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "a_int = 6\n", 97 | "print(a_int)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "#### Floats\n", 105 | "floating point numbers; numbers WITH decimal places\n", 106 | "in actuality, floats are only approximations of numbers, so sometimes (as we'll see later) they can get a little weird\n", 107 | "If you're interested in more info on why this occurs, go to this link: https://docs.python.org/3/tutorial/floatingpoint.html" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 39, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "6.6\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "a_float = 6.6\n", 125 | "print(a_float)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "##### Strings\n", 133 | "sequence of characters surrounded by \"\" or ''" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 40, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "a_string = \"hello\"\n", 143 | "b_string = \"17\"\n", 144 | "# Double quoted strings (\"\") can have single quotes ('') in them\n", 145 | "# Single quoted strings can have double quotes in them\n", 146 | "c_string = 'This is a pair of of \"double quotes\" in a string.'" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "we can access strings with [ ] with 0 being the first character and -1 being the last.\n", 154 | "[:] will also give us a range of characters " 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 41, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "h\n", 167 | "o\n", 168 | "ell\n" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "a_string = \"hello\"\n", 174 | "print(a_string[0])\n", 175 | "print(a_string[-1])\n", 176 | "print(a_string[1:4]) #this will return the 3 characters including character 1 but not character 4 " 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "##### Booleans\n", 184 | "True / False values with the first letter capitalized" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 42, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "my name is David Neil\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "#used in if-conditionals such as checking if a variable is a specific datatype, if an integer is a specific number, etc\n", 202 | "\n", 203 | "a_bool = True # any non-zero number as a boolean will be cast to True\n", 204 | "b_bool = False # 0 will be cast to False\n", 205 | "\n", 206 | "a_string = \"hello\"\n", 207 | "if (a_string == \"hello\"):\n", 208 | " print(\"my name is David Neil\")" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "If we want to make a variable but have nothing in it, not even the value 0, we use None. This gets helpful in some more advanced programs, but for the basics, just know it exists. \n", 216 | "It is a useful thing to check when passing variables to different functions to ensure that input is correct. You always want to check that your input is not None and that it is not outside the bounds of what you need it to be for that function to ensure that someone can't break your code. " 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 43, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [ 225 | "a_var = None" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "If we want to see the value stored in a variable, we can use the built-in print( ) function.\n", 233 | "Print will show us whatever is inside the ( ) to our console, for Jupyter, the console will appear below our code blocks\n", 234 | "\n", 235 | "and if we want to know the type of a variable, we can use the type( ) function" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 44, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "\n", 248 | "\n", 249 | "\n", 250 | "\n", 251 | "\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "print(type(a_int))\n", 257 | "print(type(a_float))\n", 258 | "print(type(a_string))\n", 259 | "print(type(a_bool))\n", 260 | "print(type(a_var))" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "You can name a variable almost anything you want with just a few rules\n", 268 | "\n", 269 | " - can't start a variable name with numbers\n", 270 | " - can't have a space in a variable name\n", 271 | " \n", 272 | "You should generally follow a few guidelines as well\n", 273 | "\n", 274 | " - start a variable name with a lowercase letter\n", 275 | " - keep them descriptive" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### Operators" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "Operators let us do things with variables and values, we've already seen one: =\n", 290 | "\n", 291 | "The basic operators to know are\n", 292 | " \n", 293 | " assignment: =\n", 294 | " addition/concatenation: +\n", 295 | " subtraction: -\n", 296 | " multiplication: *\n", 297 | " floating point division: /\n", 298 | " integer division: //\n", 299 | " modulo: %\n", 300 | " exponentiation: **\n", 301 | " comparisons: <, <=, >, >=, !=, ==\n", 302 | " Boolean Operations: or, and, not" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "When we use + on two numbers, it works as addition just like in math, we can even use variables or even mix and match" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 45, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "12.6\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "#print(4 + 2)\n", 327 | "print(a_int + a_float) # a_int = 6, a_float = 6.6\n", 328 | "#print(3 + a_int) # a_int = 6\n", 329 | "#print(str(3) + \"hi\")\n" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 46, 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [ 338 | "# Anything that comes after a # on the same line is a comment\n", 339 | "# Python ignores it, but it lets us humans take notes on what we did" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "When we use + on two strings, it concatenates them, we can use them with variables, strings, or both. + won't add spaces between strings though, so we have to do it manually." 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 47, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "hello17\n", 359 | "hello\n", 360 | "17\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "print(a_string + b_string)\n", 366 | "print(a_string + \"\\n\" + b_string)" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": {}, 372 | "source": [ 373 | "We can't mix and match strings and numbers though, if we want to, we have to \"cast\" them with Pythons quilt in casting system." 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 48, 379 | "metadata": { 380 | "scrolled": true 381 | }, 382 | "outputs": [ 383 | { 384 | "name": "stdout", 385 | "output_type": "stream", 386 | "text": [ 387 | "4\n", 388 | "\n", 389 | "4.0\n", 390 | "\n", 391 | "4.0\n", 392 | "\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "#checking for types is useful for debugging\n", 398 | "\n", 399 | "value = 4\n", 400 | "print(value)\n", 401 | "print(type(value))\n", 402 | "value = float(value)\n", 403 | "print(value)\n", 404 | "print(type(value))\n", 405 | "value = str(value)\n", 406 | "print(value)\n", 407 | "print(type(value))" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": {}, 413 | "source": [ 414 | "now that value is a string, I can concatenate it with other strings" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 49, 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "hello 4.0 17\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "print(a_string + \" \" + value + \" \" + b_string)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "assignment: =\n", 439 | "addition/concatenation: +\n", 440 | "subtraction: -\n", 441 | "multiplication: *\n", 442 | "floating point division: /\n", 443 | "integer division: //\n", 444 | "modulo: %\n", 445 | "exponentiation: **\n", 446 | "comparisons: <, <=, >, >=, !=, ==\n", 447 | "Boolean Operations: or, and, not\n", 448 | " \n", 449 | "subtraction works the same way as addition, but only works with numerical vaules\n" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 50, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "6.6\n", 462 | "0.5999999999999996\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "print(a_float) # a_float = 6.6\n", 468 | "print(a_float - a_int) # a_int = 6\n", 469 | "# remember when I warned things would get weird?\n", 470 | "# why this happens is a bit technical\n", 471 | "# but its such a small amount you can effectivly ignore it" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 51, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "15\n", 484 | "hellohellohello\n", 485 | "16\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "print(5 * 3)\n", 491 | "print(a_string * 3) # a_string = \"hello\"\n", 492 | "# what would you do to get the string \"hello hello hello\"\n", 493 | "\n", 494 | "print(2**4)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 52, 500 | "metadata": {}, 501 | "outputs": [ 502 | { 503 | "name": "stdout", 504 | "output_type": "stream", 505 | "text": [ 506 | "1.6666666666666667\n", 507 | "1\n", 508 | "2\n" 509 | ] 510 | } 511 | ], 512 | "source": [ 513 | "print(5 / 3)\n", 514 | "print(5 // 3) # this will drop the decimal part and floor it\n", 515 | "print(5 % 3) # this gives us the remainder" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 53, 521 | "metadata": {}, 522 | "outputs": [ 523 | { 524 | "name": "stdout", 525 | "output_type": "stream", 526 | "text": [ 527 | "False\n", 528 | "True\n", 529 | "True\n", 530 | "False\n", 531 | "True\n", 532 | "True\n", 533 | "False\n", 534 | "True\n", 535 | "success!\n" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "# comparison operators evaluate to bools\n", 541 | "# its worth taking extra care: == is equals. = is assignment.\n", 542 | "print(5 < 3)\n", 543 | "print(5 > 3)\n", 544 | "print(5 >= 5) # greater than or equal to\n", 545 | "print(5 <= 4) # less than or equal to\n", 546 | "print(5 != 4) # not equals\n", 547 | "print(5 == 5) # equality!\n", 548 | "\n", 549 | "# can be used to test the identity of OBJECTS\n", 550 | "# this is different from equality because it will check if its the exact same object\n", 551 | "int_a = 1\n", 552 | "string_a = \"1\"\n", 553 | "int_b = 1\n", 554 | "print (int_a is string_a)\n", 555 | "if int_a is int_b:\n", 556 | " print(int_a is int_b)\n", 557 | " print(\"success!\")" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 54, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "name": "stdout", 567 | "output_type": "stream", 568 | "text": [ 569 | "True\n", 570 | "False\n", 571 | "False\n", 572 | "False\n", 573 | "True\n", 574 | "False\n", 575 | "True\n" 576 | ] 577 | } 578 | ], 579 | "source": [ 580 | "# boolean values can be combined using or, and, & not\n", 581 | "# evalutates to true if either one is true\n", 582 | "\n", 583 | "print(True or False)\n", 584 | "print(False or False)\n", 585 | "# evaluates to true only if both are True\n", 586 | "# Something cannot be be true AND false at the same time\n", 587 | "print(True and False)\n", 588 | "print(False and False)\n", 589 | "print(True and True)\n", 590 | "# evaluates to the opposite value\n", 591 | "print(not True)\n", 592 | "print(not False)" 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "### Collections" 600 | ] 601 | }, 602 | { 603 | "cell_type": "markdown", 604 | "metadata": {}, 605 | "source": [ 606 | "A lot of the time, we will be working with a lot of numbers, and we need some way to arrange those numbers that makes sense. Python has a good range of collections to help us with this, the four most useful being:\n", 607 | "\n", 608 | " - Sets\n", 609 | " - Lists\n", 610 | " - Tuples\n", 611 | " - Dictionaries" 612 | ] 613 | }, 614 | { 615 | "cell_type": "markdown", 616 | "metadata": {}, 617 | "source": [ 618 | "###### Sets\n", 619 | "\n", 620 | "Sets are just like sets in math, they are a grouping of information that is NON ORDERED and DO NOT CONTAIN DUPLICATES" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 55, 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "name": "stdout", 630 | "output_type": "stream", 631 | "text": [ 632 | "{42, 'dog', 'fish', '3'}\n", 633 | "True\n", 634 | "False\n", 635 | "{42, 'fish', '3'}\n" 636 | ] 637 | } 638 | ], 639 | "source": [ 640 | "# There is only one way to make an empty set\n", 641 | "a_set = set()\n", 642 | "# We can make one with information already in it if we want\n", 643 | "a_set = {\"dog\", \"fish\", 42}\n", 644 | "b_set = set()\n", 645 | "\n", 646 | "# to add to a set we use the Set.add() function\n", 647 | "a_set.add(\"3\")\n", 648 | "a_set.add(\"dog\")\n", 649 | "# how would we add the number 3 to the set?\n", 650 | "print(a_set)\n", 651 | "# Time for a new operator, \"in\"\n", 652 | "# We can check to see if something is in our set using the \"in\" operator\n", 653 | "print(\"dog\" in a_set)\n", 654 | "\n", 655 | "#we can remove from the set with Set.remove()\n", 656 | "a_set.remove(\"dog\")\n", 657 | "\n", 658 | "print(\"dog\" in a_set)\n", 659 | "print(a_set)" 660 | ] 661 | }, 662 | { 663 | "cell_type": "markdown", 664 | "metadata": {}, 665 | "source": [ 666 | "###### Lists\n", 667 | "\n", 668 | "Now we'll talk about lists\n", 669 | "Lists are great when we have some ordered data we want to save" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": 56, 675 | "metadata": {}, 676 | "outputs": [ 677 | { 678 | "name": "stdout", 679 | "output_type": "stream", 680 | "text": [ 681 | "3\n", 682 | "[1, 2, 'kitten', 'dog', 'cat', 'fish', 'dog']\n", 683 | "[1, 2, 'kitten', 'hello', 'dog', 'cat', 'fish', 'dog', 'mouse']\n", 684 | "[1, 2, 'hello', 'dog', 'cat', 'fish', 'dog', 'mouse']\n" 685 | ] 686 | } 687 | ], 688 | "source": [ 689 | "# There are two ways to make an empty list (I recommend the first)\n", 690 | "a_list = list()\n", 691 | "a_list = []\n", 692 | "# We can make one with information already in it if we want\n", 693 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 694 | "\n", 695 | "# We can access a list just with []\n", 696 | "print(a_list[2])\n", 697 | "a_list[2] = \"kitten\"\n", 698 | "print(a_list)\n", 699 | "\n", 700 | "# We can add to the end of a list with List.append()\n", 701 | "a_list.append(\"mouse\")\n", 702 | "a_list.insert(3, \"hello\") # add hello to the location of index 3\n", 703 | "print(a_list)\n", 704 | "a_list.remove(\"kitten\")\n", 705 | "\n", 706 | "\n", 707 | "print(a_list)" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 57, 713 | "metadata": {}, 714 | "outputs": [ 715 | { 716 | "name": "stdout", 717 | "output_type": "stream", 718 | "text": [ 719 | "[1, 2, 'dog', 'cat', 'fish', 'dog']\n", 720 | "[1, 2, 'cat', 'fish', 'dog']\n", 721 | "hello\n" 722 | ] 723 | } 724 | ], 725 | "source": [ 726 | "# There are a few ways to remove from a list\n", 727 | "# List.pop() will remove and return the value at the LOCATION we want\n", 728 | "# we can save it to a variable for later!\n", 729 | "popped = a_list.pop(2)\n", 730 | "\n", 731 | "# if we dont specify, the last element will be removed\n", 732 | "a_list.pop()\n", 733 | "print(a_list)\n", 734 | "\n", 735 | "# if we know a value and want to remove THE FIRST TIME it occurs in our data\n", 736 | "# we can use List.remove()\n", 737 | "# since we already know what we want to get rid of, List.remove() doesnt return anything\n", 738 | "a_list.remove(\"dog\")\n", 739 | "print(a_list)\n", 740 | "print(popped)" 741 | ] 742 | }, 743 | { 744 | "cell_type": "markdown", 745 | "metadata": {}, 746 | "source": [ 747 | "###### Tuples\n", 748 | "\n", 749 | "Next we'll touch on Tuples\n", 750 | "We use tuples when we have a small amount of ordered data we want to save" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 58, 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "name": "stdout", 760 | "output_type": "stream", 761 | "text": [ 762 | "2\n" 763 | ] 764 | } 765 | ], 766 | "source": [ 767 | "# Theres only one way to make tuple\n", 768 | "a_tup = (1,2,3,\"dog\")\n", 769 | "\n", 770 | "# just like with a string, we can access a tuple using []\n", 771 | "\n", 772 | "print(a_tup[1]) # remember we count from 0 in programming!" 773 | ] 774 | }, 775 | { 776 | "cell_type": "markdown", 777 | "metadata": {}, 778 | "source": [ 779 | "Once we make a tuple, we can't change it. This is the reason we would pick a tuple over a List.\n", 780 | "If we want to change it, we have to make a new tuple\n", 781 | "we can however, assign that tuple to the same variable as the old one" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": 59, 787 | "metadata": {}, 788 | "outputs": [ 789 | { 790 | "name": "stdout", 791 | "output_type": "stream", 792 | "text": [ 793 | "(1, 2, 3, 'dog')\n", 794 | "(1, 'cat', 3, 'dog', 'dog')\n", 795 | "(1, 'cat', 3, 'dog', 'dog')\n" 796 | ] 797 | } 798 | ], 799 | "source": [ 800 | "a_tup = (1,2,3,\"dog\") # so things don't break if you re-run\n", 801 | "\n", 802 | "# a_tup[1] = 1 # python yells at us!\n", 803 | "\n", 804 | "b_tup = (a_tup[0], \"cat\", a_tup[2], a_tup[3], \"dog\")\n", 805 | "\n", 806 | "print(a_tup)\n", 807 | "print(b_tup)\n", 808 | "a_tup = (a_tup[0], \"cat\", a_tup[2], a_tup[3], \"dog\")\n", 809 | "print(a_tup)" 810 | ] 811 | }, 812 | { 813 | "cell_type": "markdown", 814 | "metadata": {}, 815 | "source": [ 816 | "###### Dictionaries\n", 817 | "\n", 818 | "Dictionaries have information stored in Key:Value pairs, it lets us associate data with other data rather than just an index (or location)" 819 | ] 820 | }, 821 | { 822 | "cell_type": "code", 823 | "execution_count": 60, 824 | "metadata": {}, 825 | "outputs": [ 826 | { 827 | "name": "stdout", 828 | "output_type": "stream", 829 | "text": [ 830 | "5555\n", 831 | "{'StudentNo': 4444, 'major': 'Computer Science', 'zip': 30313}\n" 832 | ] 833 | } 834 | ], 835 | "source": [ 836 | "# There are two ways to make an empty dictionary (I recommend the first)\n", 837 | "a_dict = dict()\n", 838 | "a_dict = {}\n", 839 | "\n", 840 | "# if we want to make one with information already in it\n", 841 | "a_dict = {\"StudentNo\":5555, \"major\":\"Computer Science\"}\n", 842 | "print(a_dict[\"StudentNo\"])\n", 843 | "\n", 844 | "# we can add to the dictionary by defining a new key\n", 845 | "# if the key is already in the dictionary, we overwrite the old data, its gone forever\n", 846 | "a_dict[\"zip\"] = 30313\n", 847 | "a_dict[\"StudentNo\"] = 4444\n", 848 | "print(a_dict)" 849 | ] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": 61, 854 | "metadata": {}, 855 | "outputs": [ 856 | { 857 | "name": "stdout", 858 | "output_type": "stream", 859 | "text": [ 860 | "Computer Science\n", 861 | "dict_keys(['StudentNo', 'major', 'zip'])\n", 862 | "dict_values([4444, 'Computer Science', 30313])\n", 863 | "{'StudentNo': 4444, 'zip': 30313}\n" 864 | ] 865 | } 866 | ], 867 | "source": [ 868 | "# If we know a key, we can go ahead and get the value from it\n", 869 | "# If we want to save it for later we can store in in a variable\n", 870 | "\n", 871 | "print(a_dict[\"major\"])\n", 872 | "\n", 873 | "# If we don't know a key, we can get a list of keys back!\n", 874 | "\n", 875 | "print(a_dict.keys())\n", 876 | "print(a_dict.values())\n", 877 | "\n", 878 | "# if we want to remove something from the dictionary we use Dict.pop()\n", 879 | "# we need to tell it what key we want removed\n", 880 | "# it gives us the databack to save\n", 881 | "\n", 882 | "popped = a_dict.pop(\"major\")\n", 883 | "print(a_dict)" 884 | ] 885 | }, 886 | { 887 | "cell_type": "markdown", 888 | "metadata": {}, 889 | "source": [ 890 | "Tuples, Lists, Sets, and Dictionaries can hold any and all kinds of data we want. So we can put a List inside a List, a Dictionary inside a Dictionary, or a List inside a Set. Though its best to avoid doing this unless you need to." 891 | ] 892 | }, 893 | { 894 | "cell_type": "markdown", 895 | "metadata": {}, 896 | "source": [ 897 | "### Aside: Code Blocks and Control Flow\n", 898 | "Python seperates code into (possibly nested) blocks through indentation / whitespace. These blocks get executed or skipped as a single unit.\n", 899 | "\n", 900 | "Georgia Tech's CS Classes prefer (and I recommend) an intentation level of 4 spaces as the standard indentation level.\n", 901 | "\n", 902 | "This is a complex topic; We'll explore and visualize code blocks and their execution as we discuss conditionals and loops in the following section." 903 | ] 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | "### Conditionals" 910 | ] 911 | }, 912 | { 913 | "cell_type": "markdown", 914 | "metadata": {}, 915 | "source": [ 916 | "Conditional statements are used to execute a certain command only if certain requirements are met\n", 917 | "the basic conditionals are if, elif, and else.\n", 918 | "\n", 919 | "ifs are interpreted from start to end, so it will first interpret the if statement, then move to elif in the order they were written, then finally the else (note: elif and else ARE NOT required)" 920 | ] 921 | }, 922 | { 923 | "cell_type": "code", 924 | "execution_count": 62, 925 | "metadata": { 926 | "scrolled": true 927 | }, 928 | "outputs": [ 929 | { 930 | "name": "stdout", 931 | "output_type": "stream", 932 | "text": [ 933 | "(1, 'cat', 3, 'dog', 'dog')\n" 934 | ] 935 | } 936 | ], 937 | "source": [ 938 | "print(a_tup)" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 63, 944 | "metadata": {}, 945 | "outputs": [ 946 | { 947 | "name": "stdout", 948 | "output_type": "stream", 949 | "text": [ 950 | "bark\n" 951 | ] 952 | } 953 | ], 954 | "source": [ 955 | "#allows you to layer out your logic\n", 956 | "#allows you do different things with different parts of your data\n", 957 | "if \"dog\" in a_tup:\n", 958 | " print(\"bark\")\n", 959 | "elif \"cat\" in a_tup:\n", 960 | " print(\"meow\")\n", 961 | "else:\n", 962 | " print(\"I am not a dog or cat\")" 963 | ] 964 | }, 965 | { 966 | "cell_type": "markdown", 967 | "metadata": {}, 968 | "source": [ 969 | "We end the line containing the conditional with a :\n", 970 | "The : tells python that the next line should be a nested block of code; our print() statement in this case.\n", 971 | "\n", 972 | "The elif conditional is only evaluated if the original if statement fails.\n", 973 | "the else conditional is only evaluated if both the original if statement and all elifs between fail.\n", 974 | "\n", 975 | "Because the blocks of code associated with the elif conditional and the else conditional, we skip them as well.\n", 976 | "\n" 977 | ] 978 | }, 979 | { 980 | "cell_type": "code", 981 | "execution_count": 64, 982 | "metadata": {}, 983 | "outputs": [ 984 | { 985 | "name": "stdout", 986 | "output_type": "stream", 987 | "text": [ 988 | "bark\n", 989 | "I am not a dog or mouse\n" 990 | ] 991 | } 992 | ], 993 | "source": [ 994 | "if \"dog\" in a_tup:\n", 995 | " # since there is an if directly after this, this if statement stands alone\n", 996 | " print(\"bark\")\n", 997 | "if \"mouse\" in a_tup:\n", 998 | " print(\"squeak\")\n", 999 | "else:\n", 1000 | " # This else statement belongs to the If Directly above it. The first if is independent\n", 1001 | " print(\"I am not a dog or mouse\")\n", 1002 | "# therefore both the first if, and the else statement are executed." 1003 | ] 1004 | }, 1005 | { 1006 | "cell_type": "markdown", 1007 | "metadata": {}, 1008 | "source": [ 1009 | "### For loops:\n", 1010 | "Probably the most powerful tool in a programming language is the ability to interate over every item with one statement this is done using for loops Python has a very simple method for writing for loops" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": 65, 1016 | "metadata": {}, 1017 | "outputs": [ 1018 | { 1019 | "name": "stdout", 1020 | "output_type": "stream", 1021 | "text": [ 1022 | "1\n", 1023 | "2\n", 1024 | "3\n", 1025 | "dog\n", 1026 | "cat\n", 1027 | "fish\n", 1028 | "dog\n" 1029 | ] 1030 | } 1031 | ], 1032 | "source": [ 1033 | "# x is representing an item in the list\n", 1034 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1035 | "for item in a_list:\n", 1036 | " print(item)" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "code", 1041 | "execution_count": 66, 1042 | "metadata": {}, 1043 | "outputs": [], 1044 | "source": [ 1045 | "#Two functions useful for going through a list are len() and range() len() will return an integer that is the length of a collection\n", 1046 | "\n", 1047 | "#range() will generate a list of numbers we can use, usually in a loop we can call range with 1, 2, or 3 arguments\n", 1048 | "\n", 1049 | "#if we pass one input to range(x), it generates a list from 0 to x (exclusive)\n", 1050 | "#if we pass two inputs to range(x,y) it generates a list from x to y (exclusive)\n", 1051 | "#if we pass three inputs to range(x,y,z) it generates a list from x to y using a step or increment of size z. z can be negative or positive." 1052 | ] 1053 | }, 1054 | { 1055 | "cell_type": "code", 1056 | "execution_count": 67, 1057 | "metadata": {}, 1058 | "outputs": [ 1059 | { 1060 | "name": "stdout", 1061 | "output_type": "stream", 1062 | "text": [ 1063 | "0\n", 1064 | "1\n", 1065 | "2\n", 1066 | "3\n", 1067 | "4\n", 1068 | "end of loop.\n", 1069 | "\n", 1070 | "1\n", 1071 | "2\n", 1072 | "3\n", 1073 | "4\n", 1074 | "end of loop 2\n", 1075 | "\n", 1076 | "1\n", 1077 | "3\n", 1078 | "end of loop 3\n", 1079 | "\n", 1080 | "1\n", 1081 | "2\n", 1082 | "3\n", 1083 | "dog\n", 1084 | "cat\n", 1085 | "fish\n", 1086 | "dog\n", 1087 | "end of list\n" 1088 | ] 1089 | } 1090 | ], 1091 | "source": [ 1092 | "# Here is a more complex for loop using the function range\n", 1093 | "#I will first display how the indexing works\n", 1094 | "\n", 1095 | "for i in range(5):\n", 1096 | " print(i)\n", 1097 | "print(\"end of loop.\" + \"\\n\")\n", 1098 | "#see how if we input five, it x starts at 0 and increments until 4.\n", 1099 | "#it doesn't increment to 5 because the range parameter is exclusive.\n", 1100 | "\n", 1101 | "\n", 1102 | "#let's showcase the start and stop parameter\n", 1103 | "start = 1\n", 1104 | "stop = 5\n", 1105 | "for j in range(start, stop):\n", 1106 | " print(j)\n", 1107 | "print(\"end of loop 2\" + \"\\n\")\n", 1108 | "\n", 1109 | "\n", 1110 | "#one more with the step parameter\n", 1111 | "start = 1\n", 1112 | "stop = 5\n", 1113 | "step = 2\n", 1114 | "for k in range(start, stop, step):\n", 1115 | " print(k)\n", 1116 | "print(\"end of loop 3\" + \"\\n\")\n", 1117 | "\n", 1118 | "\n", 1119 | "#finally we can use this to index into a list!\n", 1120 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1121 | "for l in range(len(a_list)):\n", 1122 | " print(a_list[l])\n", 1123 | "print(\"end of list\")" 1124 | ] 1125 | }, 1126 | { 1127 | "cell_type": "markdown", 1128 | "metadata": {}, 1129 | "source": [ 1130 | "### While Loops:\n", 1131 | "On the other hand while loops will allow you to keep running a block of code until a condition is met. While a for loop can do the same thing as a while loop, they have different strengths for different use cases. A while loop to do the same thing as the for loop above is more complex" 1132 | ] 1133 | }, 1134 | { 1135 | "cell_type": "code", 1136 | "execution_count": 68, 1137 | "metadata": {}, 1138 | "outputs": [ 1139 | { 1140 | "name": "stdout", 1141 | "output_type": "stream", 1142 | "text": [ 1143 | "1\n", 1144 | "2\n", 1145 | "3\n", 1146 | "dog\n", 1147 | "cat\n", 1148 | "fish\n", 1149 | "dog\n" 1150 | ] 1151 | } 1152 | ], 1153 | "source": [ 1154 | "# this accomplishes the same thing as the above for loop\n", 1155 | "# would generally use the for loop instead of while loop in this case\n", 1156 | "i = 0\n", 1157 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1158 | "while i < len(a_list):\n", 1159 | " print(a_list[i])\n", 1160 | " i = i + 1\n", 1161 | " \n", 1162 | "#unlike the for loop, you need to manually increment your index variable so that the while loop doesn't run forever" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "In the above example, theres not really a reason to use a while loop instead of a for loop. But a while loop's condition can be anything, so it can be really powerful." 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": 69, 1175 | "metadata": {}, 1176 | "outputs": [ 1177 | { 1178 | "name": "stdout", 1179 | "output_type": "stream", 1180 | "text": [ 1181 | "1\n", 1182 | "2\n", 1183 | "3\n" 1184 | ] 1185 | } 1186 | ], 1187 | "source": [ 1188 | "# in this instance, the condition to loop is whether the item at the index in the list is not a string\n", 1189 | "# as soon as the item at the current index is a string we exit loop\n", 1190 | "i = 0\n", 1191 | "a_list = [1,2,3,\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1192 | "while type(a_list[i]) is not str:\n", 1193 | " print(a_list[i])\n", 1194 | " i = i + 1" 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "markdown", 1199 | "metadata": {}, 1200 | "source": [ 1201 | "because anything that evaluates to a boolean can be used as a condition, we can use True and have a while loop run forever.\n", 1202 | "\n", 1203 | "We're not running a web server though so we dont want that.\n", 1204 | "\n", 1205 | "If we need to get out early, we can use the word break, which will always get you out of the most recent loop.\n" 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "code", 1210 | "execution_count": 70, 1211 | "metadata": {}, 1212 | "outputs": [ 1213 | { 1214 | "name": "stdout", 1215 | "output_type": "stream", 1216 | "text": [ 1217 | "Hello! 0\n", 1218 | "Hello! 1\n", 1219 | "Hello! 2\n", 1220 | "Hello! 3\n", 1221 | "Hello! 4\n", 1222 | "Hello! 5\n", 1223 | "Breaking out of while loop\n", 1224 | "\n", 1225 | "Current animal: snake\n", 1226 | "Not a dog.\n", 1227 | "Current animal: elephant\n", 1228 | "Not a dog.\n", 1229 | "Current animal: mouse\n", 1230 | "Not a dog.\n", 1231 | "Current animal: dog\n", 1232 | "Found the correct animal! Breaking out of while loop.\n" 1233 | ] 1234 | } 1235 | ], 1236 | "source": [ 1237 | "i = 0\n", 1238 | "while True:\n", 1239 | " print(\"Hello! \" + str(i))\n", 1240 | " if i >= 5:\n", 1241 | " print(\"Breaking out of while loop\" + \"\\n\")\n", 1242 | " break\n", 1243 | " i += 1\n", 1244 | " \n", 1245 | "#also works with for loops\n", 1246 | "a_list = [\"snake\", \"elephant\", \"mouse\",\"dog\",\"cat\",\"fish\",\"dog\"]\n", 1247 | "for animal in a_list:\n", 1248 | " print(\"Current animal: \" + str(animal))\n", 1249 | " if animal == \"dog\":\n", 1250 | " print(\"Found the correct animal! Breaking out of while loop.\")\n", 1251 | " break\n", 1252 | " else:\n", 1253 | " print(\"Not a dog.\")" 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "markdown", 1258 | "metadata": {}, 1259 | "source": [ 1260 | "### Functions:\n", 1261 | "\n", 1262 | "Now that we have learned the basics, we are ready for functions, We've already been using them. When we want to use a function we use the f_name() format. So print(), range(), len(), type() are all functions python already gives to us!\n", 1263 | "\n", 1264 | "functions are sections of code that are CALLABLE\n", 1265 | "\n", 1266 | "that means throughout our code we can \"summon\" that code we created once to run\n", 1267 | "\n", 1268 | "a function WILL NOT run unless called\n", 1269 | "\n", 1270 | "A Parameter is a variable we can pass into a function in order to change the function's behavior" 1271 | ] 1272 | }, 1273 | { 1274 | "cell_type": "code", 1275 | "execution_count": 71, 1276 | "metadata": {}, 1277 | "outputs": [], 1278 | "source": [ 1279 | "def my_func(x, y): # we use def to define a function, then all parameters are inside the parentesis\n", 1280 | " # this function multiplies 2 numbers together and returns that number\n", 1281 | " z = x * y\n", 1282 | " if z > 0:\n", 1283 | " print(z)\n", 1284 | " return z # functions with a return statement put the information after the return statement into \n", 1285 | "# the variable defined when we call our function. return statements are optional.\n" 1286 | ] 1287 | }, 1288 | { 1289 | "cell_type": "code", 1290 | "execution_count": 72, 1291 | "metadata": {}, 1292 | "outputs": [ 1293 | { 1294 | "name": "stdout", 1295 | "output_type": "stream", 1296 | "text": [ 1297 | "6\n", 1298 | "6\n", 1299 | "8\n", 1300 | "8\n" 1301 | ] 1302 | } 1303 | ], 1304 | "source": [ 1305 | "# Now we will test our function\n", 1306 | "print(my_func(2, 3))\n", 1307 | "return_val = my_func(2, 4)\n", 1308 | "print(return_val)\n", 1309 | "# notice how these variables now contain what we specified by the return statement" 1310 | ] 1311 | }, 1312 | { 1313 | "cell_type": "markdown", 1314 | "metadata": {}, 1315 | "source": [ 1316 | "Today we have learned everything from the basic Python types, all the way up to crafting simple functions. This is as far as this lesson will go. If anyone has any questions, I would be happy to clarify now!" 1317 | ] 1318 | } 1319 | ], 1320 | "metadata": { 1321 | "kernelspec": { 1322 | "display_name": "Python 3", 1323 | "language": "python", 1324 | "name": "python3" 1325 | }, 1326 | "language_info": { 1327 | "codemirror_mode": { 1328 | "name": "ipython", 1329 | "version": 3 1330 | }, 1331 | "file_extension": ".py", 1332 | "mimetype": "text/x-python", 1333 | "name": "python", 1334 | "nbconvert_exporter": "python", 1335 | "pygments_lexer": "ipython3", 1336 | "version": "3.7.3" 1337 | } 1338 | }, 1339 | "nbformat": 4, 1340 | "nbformat_minor": 2 1341 | } 1342 | -------------------------------------------------------------------------------- /python_intro_code.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # # Python 3 Introductory Workshop 5 | 6 | # ### Topics 7 | # - Why Python? 8 | # - Variables 9 | # - Operators 10 | # - Collections 11 | # - Conditionals 12 | # - Loops 13 | 14 | # ### Why Python? 15 | # Python is known for being one of the most versitile and easy to use languages 16 | # 17 | # - Simple syntax makes it easy to learn 18 | # - Easy and intuitive to read, even with minimal programming knowledge 19 | # - Great for prototyping and quickly writing working code 20 | # - Portable across systems; As long as Python is installed it will run on any operting system 21 | # 22 | # Popular uses for Python include 23 | # 24 | # - Mathematics and big data 25 | # Packagages used for reading in, manipulating, and analyzing data quickly(Numpy, Pandas) 26 | # - Simple Scripting 27 | # Writing a functional program that performs an individual task (rasberry pi) 28 | # - Server side web development (django, flask) 29 | # Server side scripting means hosting a web server that can handle requests 30 | # - Software development 31 | # Python is robust enough to handle creating almost anything you want to do 32 | 33 | # ### Variables 34 | 35 | # We need to be able to remember information for later, to do this we use variables. 36 | # we can assign a variable a value with = 37 | # 38 | # Here, we're going to look at 4 variable types: 39 | # 40 | # -integers 41 | # -floats 42 | # -strings 43 | # -booleans 44 | # 45 | # Python is a "dynamically typed language" 46 | # 47 | # This means that we do not need to declare variable types, or tell the computer what type they are, we just trust Python to keep track for us. 48 | 49 | # ##### Integers 50 | # 51 | # integers; positive or negative numbers WITHOUT decimal places - the same as in math 52 | 53 | # In[5]: 54 | 55 | 56 | a_int = 6 57 | print(a_int) 58 | 59 | 60 | # #### Floats 61 | # floating point numbers; numbers WITH decimal places 62 | # in actuality, floats are only approximations of numbers, so sometimes (as we'll see later) they can get a little weird 63 | # If you're interested in more info on why this occurs, go to this link: https://docs.python.org/3/tutorial/floatingpoint.html 64 | 65 | # In[6]: 66 | 67 | 68 | a_float = 6.6 69 | print(a_float) 70 | 71 | 72 | # ##### Strings 73 | # sequence of characters surrounded by "" or '' 74 | 75 | # In[7]: 76 | 77 | 78 | a_string = "hello" 79 | b_string = "17" 80 | # Double quoted strings ("") can have single quotes ('') in them 81 | # Single quoted strings can have double quotes in them 82 | c_string = 'This is a pair of of "double quotes" in a string.' 83 | 84 | 85 | # we can access strings with [ ] with 0 being the first character and -1 being the last. 86 | # [:] will also give us a range of characters 87 | 88 | # In[8]: 89 | 90 | 91 | print(a_string[0]) 92 | print(a_string[-1]) 93 | print(a_string[1:4]) #this will return the 3 characters including character 1 but not character 4 94 | 95 | 96 | # ##### Booleans 97 | # True / False values with the first letter capitalized 98 | 99 | # In[9]: 100 | 101 | 102 | a_bool = True # any non-zero number as a boolean will be cast to True 103 | b_bool = False # 0 will be cast to False 104 | 105 | 106 | # If we want to make a variable but have nothing in it, not even the value 0, we use None. This gets helpful in some more advanced programs, but for the basics, just know it exists. 107 | # It is a useful thing to check when passing variables to different functions to ensure that input is correct. You always want to check that your input is not None and that it is not outside the bounds of what you need it to be for that function to ensure that someone can't break your code. 108 | 109 | # In[10]: 110 | 111 | 112 | a_var = None 113 | 114 | 115 | # If we want to see the value stored in a variable, we can use the built-in print( ) function. 116 | # Print will show us whatever is inside the ( ) to our console, for Jupyter, the console will appear below our code blocks 117 | # 118 | # and if we want to know the type of a variable, we can use the type( ) function 119 | 120 | # In[11]: 121 | 122 | 123 | print(type(a_int)) 124 | print(type(a_float)) 125 | print(type(a_string)) 126 | print(type(a_bool)) 127 | print(type(a_var)) 128 | 129 | 130 | # You can name a variable almost anything you want with just a few rules 131 | # 132 | # - can't start a variable name with numbers 133 | # - can't have a space in a variable name 134 | # 135 | # You should generally follow a few guidelines as well 136 | # 137 | # - start a variable name with a lowercase letter 138 | # - keep them descriptive 139 | 140 | # ### Operators 141 | 142 | # Operators let us do things with variables and values, we've already seen one: = 143 | # 144 | # The basic operators to know are 145 | # 146 | # assignment: = 147 | # addition/concatenation: + 148 | # subtraction: - 149 | # multiplication: * 150 | # floating point division: / 151 | # integer division: // 152 | # modulo: % 153 | # exponentiation: ** 154 | # comparisons: <, <=, >, >=, !=, == 155 | # Boolean Operations: or, and, not 156 | 157 | # When we use + on two numbers, it works as addition just like in math, we can even use variables or even mix and match 158 | 159 | # In[12]: 160 | 161 | 162 | print(4 + 2) 163 | print(a_int + a_float) # a_int = 6, a_float = 6.6 164 | print(3 + a_int) # a_int = 6 165 | 166 | 167 | # In[13]: 168 | 169 | 170 | # Anything that comes after a # on the same line is a comment 171 | # Python ignores it, but it lets us humans take notes on what we did 172 | 173 | 174 | # When we use + on two strings, it concatenates them, we can use them with variables, strings, or both. + won't add spaces between strings though, so we have to do it manually. 175 | 176 | # In[14]: 177 | 178 | 179 | print(a_string + b_string) 180 | print(a_string + " " + b_string) 181 | 182 | 183 | # We can't mix and match strings and numbers though, if we want to, we have to "cast" them with Pythons quilt in casting system. 184 | 185 | # In[15]: 186 | 187 | 188 | value = 4 189 | print(value) 190 | print(type(value)) 191 | value = float(value) 192 | print(value) 193 | print(type(value)) 194 | value = str(value) 195 | print(value) 196 | print(type(value)) 197 | 198 | 199 | # now that value is a string, I can concatenate it with other strings 200 | 201 | # In[16]: 202 | 203 | 204 | print(a_string + " " + value + " " + b_string) 205 | 206 | 207 | # assignment: = 208 | # addition/concatenation: + 209 | # subtraction: - 210 | # multiplication: * 211 | # floating point division: / 212 | # integer division: // 213 | # modulo: % 214 | # exponentiation: ** 215 | # comparisons: <, <=, >, >=, !=, == 216 | # Boolean Operations: or, and, not 217 | # 218 | # subtraction works the same way as addition, but only works with numerical vaules 219 | # 220 | 221 | # In[17]: 222 | 223 | 224 | print(a_float) # a_float = 6.6 225 | print(a_float - a_int) # a_int = 6 226 | # remember when I warned things would get weird? 227 | # why this happens is a bit technical 228 | # but its such a small amount you can effectivly ignore it 229 | 230 | 231 | # In[18]: 232 | 233 | 234 | print(5 * 3) 235 | print(a_string * 3) # a_string = "hello" 236 | # what would you do to get the string "hello hello hello" 237 | 238 | print(2**4) 239 | 240 | 241 | # In[19]: 242 | 243 | 244 | print(5 / 3) 245 | print(5 // 3) # this will drop the decimal part and floor it 246 | print(5 % 2) # this gives us the remainder 247 | 248 | 249 | # In[20]: 250 | 251 | 252 | # comparison operators evaluate to bools 253 | # its worth taking extra care: == is equals. = is assignment. 254 | print(5 < 3) 255 | print(5 > 3) 256 | print(5 >= 5) # greater than or equal to 257 | print(5 <= 4) # less than or equal to 258 | print(5 != 4) # not equals 259 | print(5 == 5) # equality! 260 | print ("g" is "g") # is can be used to test equality of OBJECTS, will return different values than == for some things 261 | # here is a reference with examples https://www.geeksforgeeks.org/difference-operator-python/ 262 | 263 | 264 | # In[21]: 265 | 266 | 267 | # boolean values can be combined using or, and, & not 268 | # evalutates to true if either one is true 269 | print(True or False) 270 | print(False or False) 271 | # evaluates to true only if both are True 272 | print(True and False) 273 | print(False and False) 274 | print(True and True) 275 | # evaluates to the opposite value 276 | print(not True) 277 | print(not False) 278 | 279 | 280 | # ### Collections 281 | 282 | # A lot of the time, we will be working with a lot of numbers, and we need some way to arrange those numbers that makes sense. Python has a good range of collections to help us with this, the four most useful being: 283 | # 284 | # - Sets 285 | # - Lists 286 | # - Tuples 287 | # - Dictionaries 288 | 289 | # ###### Sets 290 | # 291 | # Sets are just like sets in math, they are a grouping of information that is NON ORDERED and DO NOT CONTAIN DUPLICATES 292 | 293 | # In[22]: 294 | 295 | 296 | # There is only one way to make an empty set 297 | a_set = set() 298 | # We can make one with information already in it if we want 299 | a_set = {"dog", "fish", 42} 300 | 301 | # to add to a set we use the Set.add() function 302 | a_set.add("3") 303 | a_set.add("dog") 304 | # how would we add the number 3 to the set? 305 | print(a_set) 306 | # time for a new operator! in 307 | # we can check to see if something is in our set using this 308 | 309 | print("dog" in a_set) 310 | 311 | #we can remove from the set with Set.remove() 312 | a_set.remove("dog") 313 | 314 | print("dog" in a_set) 315 | print(a_set) 316 | 317 | 318 | # ###### Lists 319 | # 320 | # Now we'll talk about lists 321 | # Lists are great when we have some ordered data we want to save 322 | 323 | # In[23]: 324 | 325 | 326 | # There are two ways to make an empty list (I recommend the first) 327 | a_list = list() 328 | a_list = [] 329 | # We can make one with information already in it if we want 330 | a_list = [1,2,3,"dog","cat","fish","dog"] 331 | 332 | # We can access a list just with [] 333 | print(a_list[2]) 334 | a_list[2] = "kitten" 335 | print(a_list) 336 | 337 | # We can add to the end of a list with List.append() 338 | a_list.append("mouse") 339 | a_list.insert(3, "hello") # add hello to the location of index 3 340 | 341 | print(a_list) 342 | 343 | 344 | # In[24]: 345 | 346 | 347 | # There are a few ways to remove from a list 348 | # List.pop() will remove and return the value at the LOCATION we want 349 | # we can save it to a variable for later! 350 | popped = a_list.pop(2) 351 | # if we dont specify, the last element will be removed 352 | a_list.pop() 353 | print(a_list) 354 | # if we know a value and want to remove THE FIRST TIME it occurs in our data 355 | # we can use List.remove() 356 | # since we already know what we want to get rid of, List.remove() doesnt return anything 357 | a_list.remove("dog") 358 | 359 | print(a_list) 360 | print(popped) 361 | 362 | 363 | # ###### Tuples 364 | # 365 | # Next we'll touch on Tuples 366 | # We use tuples when we have a small amount of ordered data we want to save 367 | 368 | # In[25]: 369 | 370 | 371 | # Theres only one way to make tuple 372 | a_tup = (1,2,3,"dog") 373 | # just like with a string, we can access a tuple using [] 374 | print(a_tup[1]) # remember we count from 0 in programming! 375 | 376 | 377 | # Once we make a tuple, we can't change it. This is the reason we would pick a tuple over a List. 378 | # If we want to change it, we have to make a new tuple 379 | # we can however, assign that tuple to the same variable as the old one 380 | 381 | # In[26]: 382 | 383 | 384 | a_tup = (1,2,3,"dog") # so things don't break if you re-run 385 | # a_tup[1] = 1 # python yells at us! 386 | b_tup = (a_tup[0], "cat", a_tup[2], a_tup[3], "dog") 387 | print(a_tup) 388 | print(b_tup) 389 | a_tup = (a_tup[0], "cat", a_tup[2], a_tup[3], "dog") 390 | print(a_tup) 391 | 392 | 393 | # ###### Dictionaries 394 | # 395 | # Dictionaries have information stored in Key:Value pairs, it lets us associate data with other data rather than just an index (or location) 396 | 397 | # In[27]: 398 | 399 | 400 | # There are two ways to make an empty dictionary (I recommend the first) 401 | a_dict = dict() 402 | a_dict = {} 403 | # if we want to make one with information already in it 404 | a_dict = {"StudentNo":5555, "major":"Computer Science"} 405 | 406 | # we can add to the dictionary by defining a new key 407 | # if the key is already in the dictionary, we overwrite the old data, its gone forever 408 | a_dict["zip"] = 30313 409 | a_dict["StudentNo"] = 4444 410 | print(a_dict) 411 | 412 | 413 | # In[28]: 414 | 415 | 416 | # If we know a key, we can go ahead and get the value from it 417 | # If we want to save it for later we can store in in a variable 418 | print(a_dict["major"]) 419 | # If we don't know a key, we can get a list of keys back! 420 | print(a_dict.keys()) 421 | print(a_dict.values()) 422 | 423 | # if we want to remove something from the dictionary we use Dict.pop() 424 | # we need to tell it what key we want removed 425 | # it gives us the databack to save 426 | popped = a_dict.pop("major") 427 | print(a_dict) 428 | 429 | 430 | # Tuples, Lists, Sets, and Dictionaries can hold any and all kinds of data we want. So we can put a List inside a List, a Dictionary inside a Dictionary, or a List inside a Set. Though its best to avoid doing this unless you need to. 431 | 432 | # ### Aside: Code Blocks and Control Flow 433 | # Python seperates code into (possibly nested) blocks through indentation / whitespace. These blocks get executed or skipped as a single unit. 434 | # 435 | # Georgia Tech's CS Classes prefer (and I recommend) an intentation level of 4 spaces as the standard indentation level. 436 | # 437 | # This is a complex topic; We'll explore and visualize code blocks and their execution as we discuss conditionals and loops in the following section. 438 | 439 | # ### Conditionals 440 | 441 | # Conditional statements are used to execute a certain command only if certain requirements are met 442 | # the basic conditionals are if, elif, and else. 443 | # 444 | # ifs are interpreted from start to end, so it will first interpret the if statement, then move to elif in the order they were written, then finally the else (note: elif and else ARE NOT required) 445 | 446 | # In[29]: 447 | 448 | 449 | print(a_tup) 450 | 451 | 452 | # In[30]: 453 | 454 | 455 | if "dog" in a_tup: 456 | print("bark") 457 | elif "cat" in a_tup: 458 | print("meow") 459 | else: 460 | print("I am not a dog or cat") 461 | 462 | 463 | # We end the line containing the conditional with a : 464 | # The : tells python that the next line should be a nested block of code; our print() statement in this case. 465 | # 466 | # The elif conditional is only evaluated if the original if statement fails. 467 | # the else conditional is only evaluated if both the original if statement and all elifs between fail. 468 | # 469 | # Because the blocks of code associated with the elif conditional and the else conditional, we skip them as well. 470 | # 471 | # 472 | 473 | # In[31]: 474 | 475 | 476 | if "dog" in a_tup: 477 | # since there is an if directly after this, this if statement stands alone 478 | print("bark") 479 | if "mouse" in a_tup: 480 | print("squeak") 481 | else: 482 | # This else statement belongs to the If Directly above it. The first if is independent 483 | print("I am not a dog or mouse") 484 | # therefore both the first if, and the else statement are executed. 485 | 486 | 487 | # ### For loops: 488 | # Probably the most powerful tool in a programming language is the ability to interate over every item with one statement this is done using for loops Python has a very simple method for writing for loops 489 | 490 | # In[32]: 491 | 492 | 493 | # x is representing an item in the list 494 | for x in a_list: 495 | print(x) 496 | 497 | 498 | # In[41]: 499 | 500 | 501 | #Two functions useful for going through a list are len() and range() len() will return an integer that is the length of a collection 502 | 503 | #range() will generate a list of numbers we can use, usually in a loop we can call range with 1, 2, or 3 arguments 504 | 505 | #if we pass one input to range(x), it generates a list from 0 to x 506 | #if we pass two inputs to range(x,y) it generates a list from x to y 507 | #if we pass three inputs to range(x,y,z) it generates a list from x to y using a step size of z. z can be negative 508 | 509 | 510 | # In[35]: 511 | 512 | 513 | # Here is a more complex for loop, utilizing if statements also 514 | # this for look will first look for the length of list 1 (5 items), then will make x each number 515 | # in that range (so will be 0,1,2,3,4) this is how you create that "classic" java or c++ loop 516 | for x in range(len(a_list)): 517 | if x > 2: 518 | print("I must be pretty big I'm " + str(x)) 519 | else: 520 | print("I am still small. I am only " + str(x)) 521 | 522 | 523 | # ### While Loops: 524 | # On the other hand while loops will allow you to keep running a block of code until a condition is met. While a for loop can do the same thing as a while loop, they have different strengths for different use cases. A while loop to do the same thing as the for loop above is more complex 525 | 526 | # In[36]: 527 | 528 | 529 | # this accomplishes teh same thing as the above for loop 530 | # would generally use the for loop instead of while loop in this case 531 | i = 0 532 | while i < len(a_list): 533 | print(a_list[i]) 534 | i += 1 535 | 536 | 537 | # In the above example, theres not really a reason to use a while loop instead of a for loop. But a while loop's condition can be anything, so it can be really powerful. 538 | 539 | # In[37]: 540 | 541 | 542 | # in this instance, the condition to loop is whether the item at the index in the list is not a string 543 | # as soon as the item at the current index is a string we exit loop 544 | i = 0 545 | while type(a_list[i]) is not str: 546 | print(a_list[i]) 547 | i += 1 548 | 549 | 550 | # because anything that evaluates to a boolean can be used as a condition, we can use True and have a while loop run forever. 551 | # 552 | # We're not running a web server though so we dont want that. 553 | # 554 | # If we need to get out early, we can use the word break, which will always get you out of the most recent loop. 555 | # 556 | 557 | # In[38]: 558 | 559 | 560 | i = 0 561 | while True: 562 | print("Hello! " + str(i)) 563 | if i >= 5: 564 | break 565 | i += 1 566 | 567 | 568 | # ### Functions: 569 | # 570 | # Now that we have learned the basics, we are ready for functions, We've already been using them. When we want to use a function we use the f_name() format. So print(), range(), len(), type() are all functions python already gives to us! 571 | # 572 | # functions are sections of code that are CALLABLE 573 | # 574 | # that means throughout our code we can "summon" that code we created once to run 575 | # 576 | # a function WILL NOT run unless called 577 | # 578 | # A Parameter is a variable we can pass into a function in order to change the function's behavior 579 | 580 | # In[39]: 581 | 582 | 583 | def my_func(x, y): # we use def to define a function, then all parameters are inside the parentesis 584 | # this function multiplies 2 numbers together and returns that number 585 | z = x * y 586 | if z > 0: 587 | print(z) 588 | return z # functions with a return statement put the information after the return statement into 589 | # the variable defined when we call our function. return statements are optional. 590 | 591 | 592 | # In[40]: 593 | 594 | 595 | # Now we will test our function 596 | print(my_func(2, 3)) 597 | return_val = my_func(2, 4) 598 | print(return_val) 599 | # notice how these variables now contain what we specified by the return statement 600 | 601 | 602 | # Today we have learned everything from the basic Python types, all the way up to crafting simple functions. This is as far as this lesson will go. If anyone has any questions, I would be happy to clarify now! -------------------------------------------------------------------------------- /python_resources.txt: -------------------------------------------------------------------------------- 1 | Lilly Merck 2 | lmerck3@gatech.edu 3 | 4 | Feel free to send me an email for any questions you have on this or anything else, I'll try and get back by the end of the next weekday at the latest. 5 | 6 | installing python: 7 | official website: https://www.python.org 8 | anaconda: https://www.anaconda.com 9 | 10 | python3 documentation: 11 | https://docs.python.org/3/ 12 | 13 | quick reference: 14 | python 2: https://learnxinyminutes.com/docs/python/ 15 | python 3: https://learnxinyminutes.com/docs/python3/ 16 | 17 | Light Weight Text editors (I recommend VSCode): 18 | https://code.visualstudio.com 19 | https://www.sublimetext.com 20 | https://atom.io 21 | 22 | Integrated Development Environment (Warning: overkill for most projects) 23 | https://www.jetbrains.com/pycharm/ 24 | https://visualstudio.microsoft.com (windows only, not the same thing as visual studio code) 25 | https://developer.apple.com/xcode/ (Mac only) 26 | 27 | recommended websites for python practice: 28 | beginner: offical beginners guide: https://wiki.python.org/moin/BeginnersGuide 29 | beginner: codecademy: https://www.codecademy.com/learn/learn-python 30 | beginner - intermediate: geeksforgeeks: https://www.geeksforgeeks.org/python-programming-language/ 31 | intermediate - advanced: leetcode:https://leetcode.com 32 | 33 | Other links: 34 | https://www.library.gatech.edu/events 35 | https://github.com/GTLibraryDataVisualization 36 | 37 | Note: if you installed both python2 and python3 replace python below with python3 and pip with pip3 38 | 39 | To run the Jupiter notebook: 40 | python.org installation + windows: 41 | 1) open the command prompt - windows key (search), type in cmd, press enter 42 | 2) check to make sure python is properly installed by typing "python --verison" without the quotes in the command prompt 43 | you could get a version back ex: python 3.7.5 44 | 3) type "pip install jupyter" without quotes in the command prompt 45 | 4) type "jupyter notebook" without the quotes in the command prompt 46 | 5) navigate to where you downloaded the file in the opened window 47 | 48 | Anaconda installation + windows: 49 | 1) open the command prompt - windows key (search), type in cmd, press enter 50 | 2) check to make sure python is properly installed by typing "python --verison" without the quotes in the command prompt 51 | you could get a version back ex: python 3.7.5 (anaconda) 52 | 3) type "jupyter notebook" without the quotes in the command prompt 53 | 4) navigate to where you downloaded the file in the opened window 54 | 55 | python.org installation + mac/linux: 56 | 1) open the command prompt - cmd/windows key + space (search), type in terminal, press enter 57 | 2) check to make sure python is properly installed by typing "python --verison" without the quotes in the command prompt 58 | you could get a version back ex: python 3.7.5 59 | 3) type "pip install jupyter" without quotes in the command prompt 60 | 4) type "jupyter notebook" without the quotes in the command prompt 61 | 5) navigate to where you downloaded the file in the opened window 62 | 63 | Anaconda installation + Mac/linux: 64 | 1) open the command prompt - cmd/windows key + space (search), type in terminal press enter 65 | 2) check to make sure python is properly installed by typing "python --verison" without the quotes in the command prompt 66 | you could get a version back ex: python 3.7.5 (anaconda) 67 | 3) type "jupyter notebook" without the quotes in the command prompt 68 | 4) navigate to where you downloaded the file in the opened window 69 | 70 | 71 | 72 | to run python using a text editor: 73 | save your file as file.py 74 | navigate to your file in the command prompt 75 | windows: 76 | cd folder -> goes into the folder 77 | cd .. -> goes up a folder 78 | dir -> list everything in current folder 79 | python file.py -> runs file.py 80 | Mac/Linux: 81 | cd folder -> goes into the folder 82 | cd .. -> goes up a folder 83 | ls -> list everything in current folder 84 | python file.py -> runs file.py 85 | 86 | --------------------------------------------------------------------------------