├── .DS_Store └── Introduction-to-ML ├── .ipynb_checkpoints ├── 0 # Python Crash Course-checkpoint.ipynb ├── 1 # Variable, Operators and Built-in Functions-checkpoint.ipynb ├── 2 # Print Statement, Precision and FieldWidth-checkpoint.ipynb ├── 3 # Lists, Tuples and Sets-checkpoint.ipynb ├── 6 # Functions-checkpoint.ipynb ├── 7 # Python Crash Course Exercises-checkpoint.ipynb └── Untitled-checkpoint.ipynb ├── 0 # Python Crash Course.ipynb ├── 1 # Variable, Operators and Built-in Functions.ipynb ├── 2 # Print Statement, Precision and FieldWidth.ipynb ├── 3 # Lists, Tuples and Sets.ipynb ├── 4 # Strings and Dictionaries.ipynb ├── 5 # Control Flow Statements.ipynb ├── 6 # Functions.ipynb ├── 7 # Python Crash Course Exercises.ipynb └── Untitled.ipynb /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pksvv/MachineLearningSL/295f3f37a194089b716912b5d9d466f13ce46145/.DS_Store -------------------------------------------------------------------------------- /Introduction-to-ML/.ipynb_checkpoints/1 # Variable, Operators and Built-in Functions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Easter egg\n", 15 | " - an unexpected or undocumented feature in a piece of computer software or on a DVD, included as a joke or a bonus.\n", 16 | "\n", 17 | "#### The Zen Of Python\n", 18 | "\n", 19 | "\n", 20 | "Let's start with some fun facts about python" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "The Zen of Python, by Tim Peters\n", 33 | "\n", 34 | "Beautiful is better than ugly.\n", 35 | "Explicit is better than implicit.\n", 36 | "Simple is better than complex.\n", 37 | "Complex is better than complicated.\n", 38 | "Flat is better than nested.\n", 39 | "Sparse is better than dense.\n", 40 | "Readability counts.\n", 41 | "Special cases aren't special enough to break the rules.\n", 42 | "Although practicality beats purity.\n", 43 | "Errors should never pass silently.\n", 44 | "Unless explicitly silenced.\n", 45 | "In the face of ambiguity, refuse the temptation to guess.\n", 46 | "There should be one-- and preferably only one --obvious way to do it.\n", 47 | "Although that way may not be obvious at first unless you're Dutch.\n", 48 | "Now is better than never.\n", 49 | "Although never is often better than *right* now.\n", 50 | "If the implementation is hard to explain, it's a bad idea.\n", 51 | "If the implementation is easy to explain, it may be a good idea.\n", 52 | "Namespaces are one honking great idea -- let's do more of those!\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "import this\n", 58 | "\n", 59 | "# try import antigravity" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "# Variables" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows," 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 2, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "x = 2\n", 83 | "y = 5\n", 84 | "xy = 'Hey'" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "7 Hey\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "print (x+y, xy)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "Multiple variables can be assigned with the same value." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 5, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "x = y = 1" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 6, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "1 1\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print (x,y)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "# Operators" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "##Arithmetic Operators" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "| Symbol | Task Performed |\n", 156 | "|----|---|\n", 157 | "| + | Addition |\n", 158 | "| - | Subtraction |\n", 159 | "| / | division |\n", 160 | "| % | mod |\n", 161 | "| * | multiplication |\n", 162 | "| // | floor division |\n", 163 | "| ** | to the power of |" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 7, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "3" 175 | ] 176 | }, 177 | "execution_count": 7, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "1+2" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 8, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "1" 195 | ] 196 | }, 197 | "execution_count": 8, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "source": [ 203 | "2-1" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 9, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "2" 215 | ] 216 | }, 217 | "execution_count": 9, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "1*2" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 10, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "0.5" 235 | ] 236 | }, 237 | "execution_count": 10, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "1/2" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained." 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 11, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "0.5" 262 | ] 263 | }, 264 | "execution_count": 11, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "1/2.0" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 12, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "data": { 280 | "text/plain": [ 281 | "5" 282 | ] 283 | }, 284 | "execution_count": 12, 285 | "metadata": {}, 286 | "output_type": "execute_result" 287 | } 288 | ], 289 | "source": [ 290 | "15%10" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "Floor division is nothing but converting the result so obtained to the nearest integer." 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 13, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "data": { 307 | "text/plain": [ 308 | "1.0" 309 | ] 310 | }, 311 | "execution_count": 13, 312 | "metadata": {}, 313 | "output_type": "execute_result" 314 | } 315 | ], 316 | "source": [ 317 | "2.8//2.0" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "##Relational Operators" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "| Symbol | Task Performed |\n", 332 | "|----|---|\n", 333 | "| == | True, if it is equal |\n", 334 | "| != | True, if not equal to |\n", 335 | "| < | less than |\n", 336 | "| > | greater than |\n", 337 | "| <= | less than or equal to |\n", 338 | "| >= | greater than or equal to |" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 14, 344 | "metadata": {}, 345 | "outputs": [], 346 | "source": [ 347 | "z = 1" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 15, 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "data": { 357 | "text/plain": [ 358 | "True" 359 | ] 360 | }, 361 | "execution_count": 15, 362 | "metadata": {}, 363 | "output_type": "execute_result" 364 | } 365 | ], 366 | "source": [ 367 | "z == 1" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 16, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "False" 379 | ] 380 | }, 381 | "execution_count": 16, 382 | "metadata": {}, 383 | "output_type": "execute_result" 384 | } 385 | ], 386 | "source": [ 387 | "z > 1" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": {}, 393 | "source": [ 394 | "##Bitwise Operators" 395 | ] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "metadata": {}, 400 | "source": [ 401 | "| Symbol | Task Performed |\n", 402 | "|----|---|\n", 403 | "| & | Logical And |\n", 404 | "| l | Logical OR |\n", 405 | "| ^ | XOR |\n", 406 | "| ~ | Negate |\n", 407 | "| >> | Right shift |\n", 408 | "| << | Left shift |" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 17, 414 | "metadata": {}, 415 | "outputs": [], 416 | "source": [ 417 | "a = 2 #10\n", 418 | "b = 3 #11" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 19, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "2\n", 431 | "0b10\n" 432 | ] 433 | } 434 | ], 435 | "source": [ 436 | "print (a & b)\n", 437 | "print (bin(a&b))" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 20, 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "data": { 447 | "text/plain": [ 448 | "2" 449 | ] 450 | }, 451 | "execution_count": 20, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "5 >> 1" 458 | ] 459 | }, 460 | { 461 | "cell_type": "markdown", 462 | "metadata": {}, 463 | "source": [ 464 | "0000 0101 -> 5 \n", 465 | "\n", 466 | "Shifting the digits by 1 to the right and zero padding\n", 467 | "\n", 468 | "0000 0010 -> 2" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 21, 474 | "metadata": {}, 475 | "outputs": [ 476 | { 477 | "data": { 478 | "text/plain": [ 479 | "10" 480 | ] 481 | }, 482 | "execution_count": 21, 483 | "metadata": {}, 484 | "output_type": "execute_result" 485 | } 486 | ], 487 | "source": [ 488 | "5 << 1" 489 | ] 490 | }, 491 | { 492 | "cell_type": "markdown", 493 | "metadata": {}, 494 | "source": [ 495 | "0000 0101 -> 5 \n", 496 | "\n", 497 | "Shifting the digits by 1 to the left and zero padding\n", 498 | "\n", 499 | "0000 1010 -> 10" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "# Built-in Functions" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": {}, 512 | "source": [ 513 | "Python comes loaded with pre-built functions" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "## Conversion from one system to another" 521 | ] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "metadata": {}, 526 | "source": [ 527 | "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**." 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 22, 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "data": { 537 | "text/plain": [ 538 | "'0xaa'" 539 | ] 540 | }, 541 | "execution_count": 22, 542 | "metadata": {}, 543 | "output_type": "execute_result" 544 | } 545 | ], 546 | "source": [ 547 | "hex(170)" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 23, 553 | "metadata": {}, 554 | "outputs": [ 555 | { 556 | "data": { 557 | "text/plain": [ 558 | "170" 559 | ] 560 | }, 561 | "execution_count": 23, 562 | "metadata": {}, 563 | "output_type": "execute_result" 564 | } 565 | ], 566 | "source": [ 567 | "0xAA" 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "execution_count": 24, 573 | "metadata": {}, 574 | "outputs": [ 575 | { 576 | "data": { 577 | "text/plain": [ 578 | "'0o10'" 579 | ] 580 | }, 581 | "execution_count": 24, 582 | "metadata": {}, 583 | "output_type": "execute_result" 584 | } 585 | ], 586 | "source": [ 587 | "oct(8)" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 26, 600 | "metadata": {}, 601 | "outputs": [ 602 | { 603 | "name": "stdout", 604 | "output_type": "stream", 605 | "text": [ 606 | "8\n", 607 | "170\n", 608 | "10\n" 609 | ] 610 | } 611 | ], 612 | "source": [ 613 | "print (int('010',8))\n", 614 | "print (int('0xaa',16))\n", 615 | "print (int('1010',2))" 616 | ] 617 | }, 618 | { 619 | "cell_type": "markdown", 620 | "metadata": {}, 621 | "source": [ 622 | "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 28, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "name": "stdout", 632 | "output_type": "stream", 633 | "text": [ 634 | "7\n", 635 | "7\n" 636 | ] 637 | } 638 | ], 639 | "source": [ 640 | "print (int(7.7))\n", 641 | "print (int('7'))" 642 | ] 643 | }, 644 | { 645 | "cell_type": "markdown", 646 | "metadata": {}, 647 | "source": [ 648 | "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round." 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": 29, 654 | "metadata": {}, 655 | "outputs": [ 656 | { 657 | "data": { 658 | "text/plain": [ 659 | "'b'" 660 | ] 661 | }, 662 | "execution_count": 29, 663 | "metadata": {}, 664 | "output_type": "execute_result" 665 | } 666 | ], 667 | "source": [ 668 | "chr(98)" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": 30, 674 | "metadata": {}, 675 | "outputs": [ 676 | { 677 | "data": { 678 | "text/plain": [ 679 | "98" 680 | ] 681 | }, 682 | "execution_count": 30, 683 | "metadata": {}, 684 | "output_type": "execute_result" 685 | } 686 | ], 687 | "source": [ 688 | "ord('b')" 689 | ] 690 | }, 691 | { 692 | "cell_type": "markdown", 693 | "metadata": {}, 694 | "source": [ 695 | "## Simplifying Arithmetic Operations" 696 | ] 697 | }, 698 | { 699 | "cell_type": "markdown", 700 | "metadata": {}, 701 | "source": [ 702 | "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": 32, 708 | "metadata": { 709 | "scrolled": false 710 | }, 711 | "outputs": [ 712 | { 713 | "name": "stdout", 714 | "output_type": "stream", 715 | "text": [ 716 | "6\n", 717 | "4.56\n" 718 | ] 719 | } 720 | ], 721 | "source": [ 722 | "print (round(5.6231))\n", 723 | "print (round(4.55892, 2))" 724 | ] 725 | }, 726 | { 727 | "cell_type": "markdown", 728 | "metadata": {}, 729 | "source": [ 730 | "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 33, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "name": "stdout", 740 | "output_type": "stream", 741 | "text": [ 742 | "5.385164807134504\n" 743 | ] 744 | } 745 | ], 746 | "source": [ 747 | "c =complex('5+2j')\n", 748 | "print (abs(c))" 749 | ] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "metadata": {}, 754 | "source": [ 755 | "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": 34, 761 | "metadata": {}, 762 | "outputs": [ 763 | { 764 | "data": { 765 | "text/plain": [ 766 | "(4, 1)" 767 | ] 768 | }, 769 | "execution_count": 34, 770 | "metadata": {}, 771 | "output_type": "execute_result" 772 | } 773 | ], 774 | "source": [ 775 | "divmod(9,2)" 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 35, 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "name": "stdout", 792 | "output_type": "stream", 793 | "text": [ 794 | "True\n", 795 | "False\n", 796 | "True\n" 797 | ] 798 | } 799 | ], 800 | "source": [ 801 | "print (isinstance(1, int))\n", 802 | "print (isinstance(1.0,int))\n", 803 | "print (isinstance(1.0,(int,float)))" 804 | ] 805 | }, 806 | { 807 | "cell_type": "markdown", 808 | "metadata": {}, 809 | "source": [ 810 | "**cmp(x,y)**\n", 811 | "\n", 812 | "|x ? y|Output|\n", 813 | "|---|---|\n", 814 | "| x < y | -1 |\n", 815 | "| x == y | 0 |\n", 816 | "| x > y | 1 |" 817 | ] 818 | }, 819 | { 820 | "cell_type": "markdown", 821 | "metadata": {}, 822 | "source": [ 823 | "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." 824 | ] 825 | }, 826 | { 827 | "cell_type": "code", 828 | "execution_count": 37, 829 | "metadata": {}, 830 | "outputs": [ 831 | { 832 | "name": "stdout", 833 | "output_type": "stream", 834 | "text": [ 835 | "27\n", 836 | "2\n" 837 | ] 838 | } 839 | ], 840 | "source": [ 841 | "print (pow(3,3))\n", 842 | "print (pow(3,3,5))" 843 | ] 844 | }, 845 | { 846 | "cell_type": "markdown", 847 | "metadata": {}, 848 | "source": [ 849 | "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)" 850 | ] 851 | }, 852 | { 853 | "cell_type": "code", 854 | "execution_count": 42, 855 | "metadata": {}, 856 | "outputs": [ 857 | { 858 | "name": "stdout", 859 | "output_type": "stream", 860 | "text": [ 861 | "range(0, 3)\n", 862 | "range(2, 9)\n", 863 | "range(2, 27, 8)\n", 864 | "\n" 865 | ] 866 | } 867 | ], 868 | "source": [ 869 | "print (range(3))\n", 870 | "print (range(2,9))\n", 871 | "print (range(2,27,8))\n", 872 | "\n", 873 | "print(type(range(3)))" 874 | ] 875 | }, 876 | { 877 | "cell_type": "markdown", 878 | "metadata": {}, 879 | "source": [ 880 | "##Accepting User Inputs" 881 | ] 882 | }, 883 | { 884 | "cell_type": "markdown", 885 | "metadata": {}, 886 | "source": [ 887 | "**raw_input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." 888 | ] 889 | }, 890 | { 891 | "cell_type": "code", 892 | "execution_count": 44, 893 | "metadata": {}, 894 | "outputs": [ 895 | { 896 | "name": "stdout", 897 | "output_type": "stream", 898 | "text": [ 899 | "Type something here and it will be stored in variable abc \t567890\n" 900 | ] 901 | } 902 | ], 903 | "source": [ 904 | "abc = input(\"Type something here and it will be stored in variable abc \\t\")" 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "execution_count": 45, 910 | "metadata": {}, 911 | "outputs": [ 912 | { 913 | "data": { 914 | "text/plain": [ 915 | "str" 916 | ] 917 | }, 918 | "execution_count": 45, 919 | "metadata": {}, 920 | "output_type": "execute_result" 921 | } 922 | ], 923 | "source": [ 924 | "type(abc)" 925 | ] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "**input( )**, this is used only for accepting only integer inputs." 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": 46, 937 | "metadata": {}, 938 | "outputs": [ 939 | { 940 | "name": "stdout", 941 | "output_type": "stream", 942 | "text": [ 943 | "Only integer can be stored in variable abc \t1234\n" 944 | ] 945 | } 946 | ], 947 | "source": [ 948 | "abc1 = input(\"Only integer can be stored in variable abc \\t\")" 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": 47, 954 | "metadata": {}, 955 | "outputs": [ 956 | { 957 | "data": { 958 | "text/plain": [ 959 | "str" 960 | ] 961 | }, 962 | "execution_count": 47, 963 | "metadata": {}, 964 | "output_type": "execute_result" 965 | } 966 | ], 967 | "source": [ 968 | "type(abc1)" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "Note that **type( )** returns the format or the type of a variable or a number" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": null, 981 | "metadata": {}, 982 | "outputs": [], 983 | "source": [] 984 | } 985 | ], 986 | "metadata": { 987 | "kernelspec": { 988 | "display_name": "Python 3", 989 | "language": "python", 990 | "name": "python3" 991 | }, 992 | "language_info": { 993 | "codemirror_mode": { 994 | "name": "ipython", 995 | "version": 3 996 | }, 997 | "file_extension": ".py", 998 | "mimetype": "text/x-python", 999 | "name": "python", 1000 | "nbconvert_exporter": "python", 1001 | "pygments_lexer": "ipython3", 1002 | "version": "3.7.3" 1003 | } 1004 | }, 1005 | "nbformat": 4, 1006 | "nbformat_minor": 1 1007 | } 1008 | -------------------------------------------------------------------------------- /Introduction-to-ML/.ipynb_checkpoints/2 # Print Statement, Precision and FieldWidth-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Print Statement" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "The **print** statement can be used in the following different ways :\n", 22 | "\n", 23 | " - print (\"Hello World\")\n", 24 | " - print (\"Hello\", )\n", 25 | " - print (\"Hello\" + )\n", 26 | " - print (\"Hello %s\" % )" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "Hello World\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "print (\"Hello World\"" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "In Python, single, double and triple quotes are used to denote a string.\n", 51 | "Most use single quotes when declaring a single character. \n", 52 | "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "Hey\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "print ('Hey')" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 1, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "My name is Vipul Gaur\n", 82 | "\n", 83 | "I love Python.\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "print (\"\"\"My name is Vipul Gaur\n", 89 | "\n", 90 | "I love Python.\"\"\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 2, 103 | "metadata": { 104 | "scrolled": true 105 | }, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Hello World\n", 112 | "Hello World !\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "string1 = 'World'\n", 118 | "print ('Hello', string1)\n", 119 | "\n", 120 | "string2 = '!'\n", 121 | "print ('Hello', string1, string2)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 3, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "HelloWorld!\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "print ('Hello' + string1 + string2)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "**%s** is used to refer to a variable which contains a string." 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 4, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "Hello World\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "print (\"Hello %s\" % string1)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Similarly, when using other data types\n", 177 | "\n", 178 | " - %s -> string\n", 179 | " - %d -> Integer\n", 180 | " - %f -> Float\n", 181 | " - %o -> Octal\n", 182 | " - %x -> Hexadecimal\n", 183 | " - %e -> exponential\n", 184 | " \n", 185 | "This can be used for conversions inside the print statement itself." 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 5, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "Actual Number = 18\n", 198 | "Float of the number = 18.000000\n", 199 | "Octal equivalent of the number = 22\n", 200 | "Hexadecimal equivalent of the number = 12\n", 201 | "Exponential equivalent of the number = 1.800000e+01\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "print (\"Actual Number = %d\" %18)\n", 207 | "print (\"Float of the number = %f\" %18)\n", 208 | "print (\"Octal equivalent of the number = %o\" %18)\n", 209 | "print (\"Hexadecimal equivalent of the number = %x\" %18)\n", 210 | "print (\"Exponential equivalent of the number = %e\" %18)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "When referring to multiple variables parenthesis is used." 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 6, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "Hello World !\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "print (\"Hello %s %s\" %(string1,string2))" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "#### Other Examples" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "The following are other different ways the print statement can be put to use." 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 7, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "name": "stdout", 258 | "output_type": "stream", 259 | "text": [ 260 | "I want %d to be printed here\n" 261 | ] 262 | } 263 | ], 264 | "source": [ 265 | "print (\"I want %%d to be printed %s\" %'here')" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 8, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "_A_A_A_A_A_A_A_A_A_A\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "print ('_A'*10)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 9, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "name": "stdout", 292 | "output_type": "stream", 293 | "text": [ 294 | "Jan\n", 295 | "Feb\n", 296 | "Mar\n", 297 | "Apr\n", 298 | "May\n", 299 | "Jun\n", 300 | "Jul\n", 301 | "Aug\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "print (\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 10, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "I want \\n to be printed.\n" 319 | ] 320 | } 321 | ], 322 | "source": [ 323 | "print (\"I want \\\\n to be printed.\")" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 11, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "name": "stdout", 333 | "output_type": "stream", 334 | "text": [ 335 | "\n", 336 | "Routine:\n", 337 | "\t- Eat\n", 338 | "\t- Sleep\n", 339 | "\t- Repeat\n", 340 | "\n" 341 | ] 342 | } 343 | ], 344 | "source": [ 345 | "print (\"\"\"\n", 346 | "Routine:\n", 347 | "\\t- Eat\n", 348 | "\\t- Sleep\\n\\t- Repeat\n", 349 | "\"\"\")" 350 | ] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "#### PrecisionWidth and FieldWidth" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": {}, 362 | "source": [ 363 | "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", 364 | "\n", 365 | "The default Precision Width is set to 6." 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 12, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "data": { 375 | "text/plain": [ 376 | "'3.121312'" 377 | ] 378 | }, 379 | "execution_count": 12, 380 | "metadata": {}, 381 | "output_type": "execute_result" 382 | } 383 | ], 384 | "source": [ 385 | "\"%f\" % 3.121312312312" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 13, 398 | "metadata": {}, 399 | "outputs": [ 400 | { 401 | "data": { 402 | "text/plain": [ 403 | "'3.12131'" 404 | ] 405 | }, 406 | "execution_count": 13, 407 | "metadata": {}, 408 | "output_type": "execute_result" 409 | } 410 | ], 411 | "source": [ 412 | "\"%.5f\" % 3.121312312312" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 14, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "data": { 429 | "text/plain": [ 430 | "' 3.12131'" 431 | ] 432 | }, 433 | "execution_count": 14, 434 | "metadata": {}, 435 | "output_type": "execute_result" 436 | } 437 | ], 438 | "source": [ 439 | "\"%9.5f\" % 3.121312312312" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "Zero padding is done by adding a 0 at the start of fieldwidth." 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 15, 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "'00000000000003.12131'" 458 | ] 459 | }, 460 | "execution_count": 15, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "\"%020.5f\" % 3.121312312312" 467 | ] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "metadata": {}, 472 | "source": [ 473 | "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 16, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | " 3.121312\n", 486 | "-3.121312\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "print (\"% 9f\" % 3.121312312312)\n", 492 | "print (\"% 9f\" % -3.121312312312)" 493 | ] 494 | }, 495 | { 496 | "cell_type": "markdown", 497 | "metadata": {}, 498 | "source": [ 499 | "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 17, 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "+3.121312\n", 512 | "-3.121312\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "print (\"%+9f\" % 3.121312312312)\n", 518 | "print (\"% 9f\" % -3.121312312312)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actually field width. But left alignment can be done by specifying a negative symbol in the field width." 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 18, 531 | "metadata": {}, 532 | "outputs": [ 533 | { 534 | "data": { 535 | "text/plain": [ 536 | "'3.121 '" 537 | ] 538 | }, 539 | "execution_count": 18, 540 | "metadata": {}, 541 | "output_type": "execute_result" 542 | } 543 | ], 544 | "source": [ 545 | "\"%-9.3f\" % 3.121312312312" 546 | ] 547 | } 548 | ], 549 | "metadata": { 550 | "kernelspec": { 551 | "display_name": "Python 3", 552 | "language": "python", 553 | "name": "python3" 554 | }, 555 | "language_info": { 556 | "codemirror_mode": { 557 | "name": "ipython", 558 | "version": 3 559 | }, 560 | "file_extension": ".py", 561 | "mimetype": "text/x-python", 562 | "name": "python", 563 | "nbconvert_exporter": "python", 564 | "pygments_lexer": "ipython3", 565 | "version": "3.7.3" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 1 570 | } 571 | -------------------------------------------------------------------------------- /Introduction-to-ML/.ipynb_checkpoints/6 # Functions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Functions" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Most of the times, In a algorithm the statements keep repeating and it will be a tedious job to execute the same statements again and again and will consume a lot of memory and is not efficient. Enter Functions." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "This is the basic syntax of a function" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "def funcname(arg1, arg2,... argN):\n", 36 | " \n", 37 | " ''' Document String'''\n", 38 | "\n", 39 | " statements\n", 40 | "\n", 41 | "\n", 42 | " return " 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Read the above syntax as, A function by name \"funcname\" is defined, which accepts arguements \"arg1,arg2,....argN\". The function is documented and it is '''Document String'''. The function after executing the statements returns a \"value\"." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Hey Vipul!\n", 62 | "Vipul, How do you do?\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "print (\"Hey Vipul!\")\n", 68 | "print (\"Vipul, How do you do?\")" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Instead of writing the above two statements every single time it can be replaced by defining a function which would do the job in just one line. \n", 76 | "\n", 77 | "Defining a function firstfunc()." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "def firstfunc():\n", 87 | " print (\"Hey Vipul!\")\n", 88 | " print (\"Vipul, How do you do?\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Hey Vipul!\n", 101 | "Vipul, How do you do?\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "firstfunc()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "def firstfunc(username):\n", 123 | " print (\"Hey\", username + '!')\n", 124 | " print (username + ',' ,\"How do you do?\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 9, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Please enter your name : Vipul\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "name1 = input('Please enter your name : ')" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "The name \"Vipul\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 11, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "Hey Vipul!\n", 161 | "Vipul, How do you do?\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "firstfunc(name1)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself." 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 12, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "def firstfunc(username):\n", 183 | " print (\"Hey\", username + '!')\n", 184 | " print (username + ',' ,\"How do you do?\")\n", 185 | "def secondfunc():\n", 186 | " name = input(\"Please enter your name : \")\n", 187 | " firstfunc(name)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 13, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "Please enter your name : V Gaur\n", 200 | "Hey V Gaur!\n", 201 | "V Gaur, How do you do?\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "secondfunc()" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "## Return Statement" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "When the function results in some value and that value has to be stored in a variable or needs to be sent back or returned for further operation to the main algorithm, return statement is used." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 14, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "def times(x,y):\n", 230 | " z = x*y\n", 231 | " return z" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 15, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "20\n" 251 | ] 252 | } 253 | ], 254 | "source": [ 255 | "c = times(4,5)\n", 256 | "print (c)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "The z value is stored in variable c and can be used for further operations." 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "Instead of declaring another variable the entire statement itself can be used in the return statement as shown." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 16, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "def times(x,y):\n", 280 | " '''This multiplies the two input arguments'''\n", 281 | " return x*y" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 17, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "20\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "c = times(4,5)\n", 299 | "print (c)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 18, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "Help on function times in module __main__:\n", 319 | "\n", 320 | "times(x, y)\n", 321 | " This multiplies the two input arguments\n", 322 | "\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "help(times)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "Multiple variable can also be returned, But keep in mind the order." 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 19, 340 | "metadata": {}, 341 | "outputs": [], 342 | "source": [ 343 | "eglist = [10,50,30,12,6,8,100]" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 20, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "def egfunc(eglist):\n", 353 | " highest = max(eglist)\n", 354 | " lowest = min(eglist)\n", 355 | " first = eglist[0]\n", 356 | " last = eglist[-1]\n", 357 | " return highest,lowest,first,last" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "If the function is just called without any variable for it to be assigned to, the result is returned inside a tuple. But if the variables are mentioned then the result is assigned to the variable in a particular order which is declared in the return statement." 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 21, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "(100, 6, 10, 100)" 376 | ] 377 | }, 378 | "execution_count": 21, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "egfunc(eglist)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 23, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | " a = 100 \n", 397 | " b = 6 \n", 398 | " c = 10 \n", 399 | " d = 100\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "a,b,c,d = egfunc(eglist)\n", 405 | "print (' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "## Implicit arguments" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 24, 425 | "metadata": {}, 426 | "outputs": [], 427 | "source": [ 428 | "def implicitadd(x,y=3):\n", 429 | " return x+y" 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 25, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "7" 455 | ] 456 | }, 457 | "execution_count": 25, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "implicitadd(4)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "metadata": {}, 469 | "source": [ 470 | "But if the second argument is specified then this value overrides the implicit value assigned to the argument " 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 26, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "8" 482 | ] 483 | }, 484 | "execution_count": 26, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "implicitadd(4,4)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "metadata": {}, 496 | "source": [ 497 | "## Any number of arguments" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "If the number of arguments that is to be accepted by a function is not known then a asterisk symbol is used before the argument." 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 29, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [ 513 | "def add_n(*args):\n", 514 | " res = 0\n", 515 | " reslist = []\n", 516 | " for i in args:\n", 517 | " reslist.append(i)\n", 518 | " print (reslist)\n", 519 | " return sum(reslist)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "The above function accepts any number of arguments, defines a list and appends all the arguments into that list and return the sum of all the arguments." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 30, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "[1, 2, 3, 4, 5]\n" 539 | ] 540 | }, 541 | { 542 | "data": { 543 | "text/plain": [ 544 | "15" 545 | ] 546 | }, 547 | "execution_count": 30, 548 | "metadata": {}, 549 | "output_type": "execute_result" 550 | } 551 | ], 552 | "source": [ 553 | "add_n(1,2,3,4,5)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 31, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "[1, 2, 3]\n" 566 | ] 567 | }, 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "6" 572 | ] 573 | }, 574 | "execution_count": 31, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "add_n(1,2,3)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "## Global and Local Variables" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "Whatever variable is declared inside a function is local variable and outside the function in global variable." 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 32, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "eg1 = [1,2,3,4,5]" 604 | ] 605 | }, 606 | { 607 | "cell_type": "markdown", 608 | "metadata": {}, 609 | "source": [ 610 | "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable." 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 35, 616 | "metadata": {}, 617 | "outputs": [], 618 | "source": [ 619 | "def egfunc1():\n", 620 | " def thirdfunc(arg1):\n", 621 | " eg2 = arg1[:]\n", 622 | " eg2.append(6)\n", 623 | " print (\"This is happening inside the function :\", eg2)\n", 624 | " print (\"This is happening before the function is called : \", eg1)\n", 625 | " thirdfunc(eg1)\n", 626 | " print (\"This is happening outside the function :\", eg1)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 36, 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 639 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 640 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "egfunc1()" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": {}, 651 | "source": [ 652 | "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 37, 658 | "metadata": {}, 659 | "outputs": [], 660 | "source": [ 661 | "eg3 = [1,2,3,4,5]" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 38, 667 | "metadata": {}, 668 | "outputs": [ 669 | { 670 | "ename": "SyntaxError", 671 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)? (, line 6)", 672 | "output_type": "error", 673 | "traceback": [ 674 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m print \"This is happening inside the function :\", eg2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)?\n" 675 | ] 676 | } 677 | ], 678 | "source": [ 679 | "def egfunc1():\n", 680 | " def thirdfunc(arg1):\n", 681 | " global eg2\n", 682 | " eg2 = arg1[:]\n", 683 | " eg2.append(6)\n", 684 | " print \"This is happening inside the function :\", eg2 \n", 685 | " print \"This is happening before the function is called : \", eg1\n", 686 | " thirdfunc(eg1)\n", 687 | " print \"This is happening outside the function :\", eg1 \n", 688 | " print \"Accessing a variable declared inside the function from outside :\" , eg2" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 39, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 701 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 702 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 703 | ] 704 | } 705 | ], 706 | "source": [ 707 | "egfunc1()" 708 | ] 709 | }, 710 | { 711 | "cell_type": "markdown", 712 | "metadata": {}, 713 | "source": [ 714 | "## Lambda Functions" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression." 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 40, 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [ 730 | "z = lambda x: x * x" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 41, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "data": { 740 | "text/plain": [ 741 | "64" 742 | ] 743 | }, 744 | "execution_count": 41, 745 | "metadata": {}, 746 | "output_type": "execute_result" 747 | } 748 | ], 749 | "source": [ 750 | "z(8)" 751 | ] 752 | }, 753 | { 754 | "cell_type": "markdown", 755 | "metadata": {}, 756 | "source": [ 757 | "###map" 758 | ] 759 | }, 760 | { 761 | "cell_type": "markdown", 762 | "metadata": {}, 763 | "source": [ 764 | "**map( )** function basically executes the function that is defined to each of the list's element separately." 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": 42, 770 | "metadata": {}, 771 | "outputs": [], 772 | "source": [ 773 | "list1 = [1,2,3,4,5,6,7,8,9]" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": 44, 779 | "metadata": {}, 780 | "outputs": [ 781 | { 782 | "name": "stdout", 783 | "output_type": "stream", 784 | "text": [ 785 | "\n" 786 | ] 787 | } 788 | ], 789 | "source": [ 790 | "eg = map(lambda x:x+2, list1)\n", 791 | "print (eg)" 792 | ] 793 | }, 794 | { 795 | "cell_type": "markdown", 796 | "metadata": {}, 797 | "source": [ 798 | "You can also add two lists." 799 | ] 800 | }, 801 | { 802 | "cell_type": "code", 803 | "execution_count": 45, 804 | "metadata": {}, 805 | "outputs": [], 806 | "source": [ 807 | "list2 = [9,8,7,6,5,4,3,2,1]" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": 47, 813 | "metadata": {}, 814 | "outputs": [ 815 | { 816 | "name": "stdout", 817 | "output_type": "stream", 818 | "text": [ 819 | "\n" 820 | ] 821 | } 822 | ], 823 | "source": [ 824 | "eg2 = map(lambda x,y:x+y, list1,list2)\n", 825 | "print (eg2)" 826 | ] 827 | }, 828 | { 829 | "cell_type": "markdown", 830 | "metadata": {}, 831 | "source": [ 832 | "Not only lambda function but also other built in functions can also be used." 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": 48, 838 | "metadata": {}, 839 | "outputs": [ 840 | { 841 | "name": "stdout", 842 | "output_type": "stream", 843 | "text": [ 844 | "\n" 845 | ] 846 | } 847 | ], 848 | "source": [ 849 | "eg3 = map(str,eg2)\n", 850 | "print (eg3)" 851 | ] 852 | }, 853 | { 854 | "cell_type": "markdown", 855 | "metadata": {}, 856 | "source": [ 857 | "### filter" 858 | ] 859 | }, 860 | { 861 | "cell_type": "markdown", 862 | "metadata": {}, 863 | "source": [ 864 | "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": 49, 870 | "metadata": {}, 871 | "outputs": [], 872 | "source": [ 873 | "list1 = [1,2,3,4,5,6,7,8,9]" 874 | ] 875 | }, 876 | { 877 | "cell_type": "markdown", 878 | "metadata": {}, 879 | "source": [ 880 | "To get the elements which are less than 5," 881 | ] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "execution_count": 50, 886 | "metadata": {}, 887 | "outputs": [ 888 | { 889 | "data": { 890 | "text/plain": [ 891 | "" 892 | ] 893 | }, 894 | "execution_count": 50, 895 | "metadata": {}, 896 | "output_type": "execute_result" 897 | } 898 | ], 899 | "source": [ 900 | "filter(lambda x:x<5,list1)" 901 | ] 902 | }, 903 | { 904 | "cell_type": "markdown", 905 | "metadata": {}, 906 | "source": [ 907 | "Notice what happens when **map()** is used." 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": 51, 913 | "metadata": {}, 914 | "outputs": [ 915 | { 916 | "data": { 917 | "text/plain": [ 918 | "" 919 | ] 920 | }, 921 | "execution_count": 51, 922 | "metadata": {}, 923 | "output_type": "execute_result" 924 | } 925 | ], 926 | "source": [ 927 | "map(lambda x:x<5, list1)" 928 | ] 929 | }, 930 | { 931 | "cell_type": "markdown", 932 | "metadata": {}, 933 | "source": [ 934 | "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": 52, 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "data": { 944 | "text/plain": [ 945 | "" 946 | ] 947 | }, 948 | "execution_count": 52, 949 | "metadata": {}, 950 | "output_type": "execute_result" 951 | } 952 | ], 953 | "source": [ 954 | "filter(lambda x:x%4==0,list1)" 955 | ] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": null, 960 | "metadata": {}, 961 | "outputs": [], 962 | "source": [] 963 | } 964 | ], 965 | "metadata": { 966 | "kernelspec": { 967 | "display_name": "Python 3", 968 | "language": "python", 969 | "name": "python3" 970 | }, 971 | "language_info": { 972 | "codemirror_mode": { 973 | "name": "ipython", 974 | "version": 3 975 | }, 976 | "file_extension": ".py", 977 | "mimetype": "text/x-python", 978 | "name": "python", 979 | "nbconvert_exporter": "python", 980 | "pygments_lexer": "ipython3", 981 | "version": "3.7.3" 982 | } 983 | }, 984 | "nbformat": 4, 985 | "nbformat_minor": 1 986 | } 987 | -------------------------------------------------------------------------------- /Introduction-to-ML/.ipynb_checkpoints/7 # Python Crash Course Exercises-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Crash Course Exercise" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Exercises\n", 15 | "\n", 16 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "** What is 7 to the power of 4?**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "2401\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "#7**4\n", 41 | "\n", 42 | "print(pow(7,4))" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "** Split this string:**\n", 50 | "\n", 51 | " s = \"Hi there Sam!\"\n", 52 | " \n", 53 | "**into a list. **" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 6, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "list" 65 | ] 66 | }, 67 | "execution_count": 6, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "s = 'Hi there Sam!'\n", 74 | "\n", 75 | "s2 = \"There are a lot's of something\"\n", 76 | "\n", 77 | "type(s.split())" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "['Hi', 'there', 'dad!']" 89 | ] 90 | }, 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "** Given the variables:**\n", 103 | "\n", 104 | " planet = \"Earth\"\n", 105 | " diameter = 12742\n", 106 | "\n", 107 | "** Use .format() to print the following string: **\n", 108 | "\n", 109 | " The diameter of Earth is 12742 kilometers." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 8, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "planet = \"Earth\"\n", 119 | "diameter = 12742" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 9, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "The diameter of Earth is 12742 kilometers\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "print('The diameter of {one} is {two} kilometers'.format(one=planet,two=diameter))" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "** Given this nested list, use indexing to grab the word \"hello\" **" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 11, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 18, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "'hello'" 164 | ] 165 | }, 166 | "execution_count": 18, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "lst[3][1][2][0]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 20, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 26, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "'hello'" 200 | ] 201 | }, 202 | "execution_count": 26, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "d['k1'][3]['tricky'][3]['target'][3]" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "** What is the main difference between a tuple and a list? **" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 23, 221 | "metadata": { 222 | "collapsed": true 223 | }, 224 | "outputs": [], 225 | "source": [ 226 | "# Tuple is immutable" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "** Create a function that grabs the email website domain from a string in the form: **\n", 234 | "\n", 235 | " user@domain.com\n", 236 | " \n", 237 | "**So for example, passing \"user@domain.com\" would return: domain.com**" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 29, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "def domainGet(email):\n", 247 | " return email.split('@')[1]" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 30, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "'domain.com'" 259 | ] 260 | }, 261 | "execution_count": 30, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "domainGet('user@domain.com')" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 33, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "False" 286 | ] 287 | }, 288 | "execution_count": 33, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "'dog' in 'Is there a DOG here?'" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 43, 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [ 303 | "def findDog(string):\n", 304 | " if 'dog' in string.lower():\n", 305 | " print(string.lower())\n", 306 | " return True\n", 307 | " else:\n", 308 | " return False" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 47, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "True" 320 | ] 321 | }, 322 | "execution_count": 47, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "findDog2('Is there a dog here?')" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 48, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "True" 340 | ] 341 | }, 342 | "execution_count": 48, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "findDog2('Is there a DOG here')" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 46, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "def findDog2(string):\n", 358 | " return 'dog' in string.lower()" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "** Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases. **" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 30, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 31, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "2" 384 | ] 385 | }, 386 | "execution_count": 31, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "countDog('This dog runs faster than the other dog dude!')" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", 400 | "\n", 401 | " seq = ['soup','dog','salad','cat','great']\n", 402 | "\n", 403 | "**should be filtered down to:**\n", 404 | "\n", 405 | " ['soup','salad']" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 55, 411 | "metadata": {}, 412 | "outputs": [], 413 | "source": [ 414 | "seq = ['soup','dog','salad','cat','great']" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 57, 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "['soup', 'salad']" 426 | ] 427 | }, 428 | "execution_count": 57, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "# the letter should start with s\n", 435 | "# in python the strings can be treated as list as well\n", 436 | "\n", 437 | "#word = 'cat'\n", 438 | "lambda word: word[0] == 's'\n", 439 | "\n", 440 | "list(filter(lambda word: word[0] == 's',seq))" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "### Final Problem\n", 448 | "**You are driving a little too fast, and a police officer stops you. Write a function\n", 449 | " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", 450 | " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", 451 | " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", 452 | " cases. **" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 59, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "def caught_speeding(speed, is_birthday):\n", 462 | " if is_birthday:\n", 463 | " speed -= 5\n", 464 | " \n", 465 | " if speed > 80:\n", 466 | " return 'Big Ticket'\n", 467 | " elif speed > 60:\n", 468 | " return 'Small Ticket'\n", 469 | " else:\n", 470 | " return 'No Ticket'" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 60, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "'Small Ticket'" 482 | ] 483 | }, 484 | "execution_count": 60, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "caught_speeding(81,True)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 61, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "'Big Ticket'" 502 | ] 503 | }, 504 | "execution_count": 61, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "caught_speeding(81,False)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "# Great job!" 518 | ] 519 | } 520 | ], 521 | "metadata": { 522 | "kernelspec": { 523 | "display_name": "Python 3", 524 | "language": "python", 525 | "name": "python3" 526 | }, 527 | "language_info": { 528 | "codemirror_mode": { 529 | "name": "ipython", 530 | "version": 3 531 | }, 532 | "file_extension": ".py", 533 | "mimetype": "text/x-python", 534 | "name": "python", 535 | "nbconvert_exporter": "python", 536 | "pygments_lexer": "ipython3", 537 | "version": "3.7.4" 538 | } 539 | }, 540 | "nbformat": 4, 541 | "nbformat_minor": 1 542 | } 543 | -------------------------------------------------------------------------------- /Introduction-to-ML/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Introduction-to-ML/1 # Variable, Operators and Built-in Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Easter egg\n", 15 | " - an unexpected or undocumented feature in a piece of computer software or on a DVD, included as a joke or a bonus.\n", 16 | "\n", 17 | "#### The Zen Of Python\n", 18 | "\n", 19 | "\n", 20 | "Let's start with some fun facts about python" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "import this\n", 30 | "\n", 31 | "# try import antigravity" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "# Variables" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows," 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "str" 57 | ] 58 | }, 59 | "execution_count": 5, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "x = 2\n", 66 | "y = 5\n", 67 | "xy = 'Hey'\n", 68 | "\n", 69 | "type(xy)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "7 Hey\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "print (x+y, xy)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Multiple variables can be assigned with the same value." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "x = y = 1" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 6, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "1 1\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "print (x,y)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "# Operators" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "##Arithmetic Operators" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "| Symbol | Task Performed |\n", 141 | "|----|---|\n", 142 | "| + | Addition |\n", 143 | "| - | Subtraction |\n", 144 | "| / | division |\n", 145 | "| % | mod |\n", 146 | "| * | multiplication |\n", 147 | "| // | floor division |\n", 148 | "| ** | to the power of |" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "3" 160 | ] 161 | }, 162 | "execution_count": 7, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "1+2" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 8, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "1" 180 | ] 181 | }, 182 | "execution_count": 8, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "2-1" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "2" 200 | ] 201 | }, 202 | "execution_count": 9, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "1*2" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 10, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "0.5" 220 | ] 221 | }, 222 | "execution_count": 10, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "1/2" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained." 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 11, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "0.5" 247 | ] 248 | }, 249 | "execution_count": 11, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "1/2.0" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 12, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "5" 267 | ] 268 | }, 269 | "execution_count": 12, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "15%10" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "Floor division is nothing but converting the result so obtained to the nearest integer." 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 13, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "1.0" 294 | ] 295 | }, 296 | "execution_count": 13, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "2.8//2.0" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "##Relational Operators" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "| Symbol | Task Performed |\n", 317 | "|----|---|\n", 318 | "| == | True, if it is equal |\n", 319 | "| != | True, if not equal to |\n", 320 | "| < | less than |\n", 321 | "| > | greater than |\n", 322 | "| <= | less than or equal to |\n", 323 | "| >= | greater than or equal to |" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 14, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "z = 1" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 15, 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "True" 344 | ] 345 | }, 346 | "execution_count": 15, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "z == 1" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 16, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "False" 364 | ] 365 | }, 366 | "execution_count": 16, 367 | "metadata": {}, 368 | "output_type": "execute_result" 369 | } 370 | ], 371 | "source": [ 372 | "z > 1" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "##Bitwise Operators" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "| Symbol | Task Performed |\n", 387 | "|----|---|\n", 388 | "| & | Logical And |\n", 389 | "| l | Logical OR |\n", 390 | "| ^ | XOR |\n", 391 | "| ~ | Negate |\n", 392 | "| >> | Right shift |\n", 393 | "| << | Left shift |" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 17, 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [ 402 | "a = 2 #10\n", 403 | "b = 3 #11" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 19, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "2\n", 416 | "0b10\n" 417 | ] 418 | } 419 | ], 420 | "source": [ 421 | "print (a & b)\n", 422 | "print (bin(a&b))" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 20, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "2" 434 | ] 435 | }, 436 | "execution_count": 20, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "5 >> 1" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "0000 0101 -> 5 \n", 450 | "\n", 451 | "Shifting the digits by 1 to the right and zero padding\n", 452 | "\n", 453 | "0000 0010 -> 2" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 21, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "10" 465 | ] 466 | }, 467 | "execution_count": 21, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "5 << 1" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": {}, 479 | "source": [ 480 | "0000 0101 -> 5 \n", 481 | "\n", 482 | "Shifting the digits by 1 to the left and zero padding\n", 483 | "\n", 484 | "0000 1010 -> 10" 485 | ] 486 | }, 487 | { 488 | "cell_type": "markdown", 489 | "metadata": {}, 490 | "source": [ 491 | "# Built-in Functions" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": {}, 497 | "source": [ 498 | "Python comes loaded with pre-built functions" 499 | ] 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "metadata": {}, 504 | "source": [ 505 | "## Conversion from one system to another" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": {}, 511 | "source": [ 512 | "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**." 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 22, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "data": { 522 | "text/plain": [ 523 | "'0xaa'" 524 | ] 525 | }, 526 | "execution_count": 22, 527 | "metadata": {}, 528 | "output_type": "execute_result" 529 | } 530 | ], 531 | "source": [ 532 | "hex(170)" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 23, 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "170" 544 | ] 545 | }, 546 | "execution_count": 23, 547 | "metadata": {}, 548 | "output_type": "execute_result" 549 | } 550 | ], 551 | "source": [ 552 | "0xAA" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 24, 558 | "metadata": {}, 559 | "outputs": [ 560 | { 561 | "data": { 562 | "text/plain": [ 563 | "'0o10'" 564 | ] 565 | }, 566 | "execution_count": 24, 567 | "metadata": {}, 568 | "output_type": "execute_result" 569 | } 570 | ], 571 | "source": [ 572 | "oct(8)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 26, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "name": "stdout", 589 | "output_type": "stream", 590 | "text": [ 591 | "8\n", 592 | "170\n", 593 | "10\n" 594 | ] 595 | } 596 | ], 597 | "source": [ 598 | "print (int('010',8))\n", 599 | "print (int('0xaa',16))\n", 600 | "print (int('1010',2))" 601 | ] 602 | }, 603 | { 604 | "cell_type": "markdown", 605 | "metadata": {}, 606 | "source": [ 607 | "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 28, 613 | "metadata": {}, 614 | "outputs": [ 615 | { 616 | "name": "stdout", 617 | "output_type": "stream", 618 | "text": [ 619 | "7\n", 620 | "7\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "print (int(7.7))\n", 626 | "print (int('7'))" 627 | ] 628 | }, 629 | { 630 | "cell_type": "markdown", 631 | "metadata": {}, 632 | "source": [ 633 | "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round." 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": 29, 639 | "metadata": {}, 640 | "outputs": [ 641 | { 642 | "data": { 643 | "text/plain": [ 644 | "'b'" 645 | ] 646 | }, 647 | "execution_count": 29, 648 | "metadata": {}, 649 | "output_type": "execute_result" 650 | } 651 | ], 652 | "source": [ 653 | "chr(98)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 30, 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": [ 664 | "98" 665 | ] 666 | }, 667 | "execution_count": 30, 668 | "metadata": {}, 669 | "output_type": "execute_result" 670 | } 671 | ], 672 | "source": [ 673 | "ord('b')" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "## Simplifying Arithmetic Operations" 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 32, 693 | "metadata": { 694 | "scrolled": false 695 | }, 696 | "outputs": [ 697 | { 698 | "name": "stdout", 699 | "output_type": "stream", 700 | "text": [ 701 | "6\n", 702 | "4.56\n" 703 | ] 704 | } 705 | ], 706 | "source": [ 707 | "print (round(5.6231))\n", 708 | "print (round(4.55892, 2))" 709 | ] 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "metadata": {}, 714 | "source": [ 715 | "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." 716 | ] 717 | }, 718 | { 719 | "cell_type": "code", 720 | "execution_count": 33, 721 | "metadata": {}, 722 | "outputs": [ 723 | { 724 | "name": "stdout", 725 | "output_type": "stream", 726 | "text": [ 727 | "5.385164807134504\n" 728 | ] 729 | } 730 | ], 731 | "source": [ 732 | "c = complex('5+2j')\n", 733 | "print (abs(c))" 734 | ] 735 | }, 736 | { 737 | "cell_type": "markdown", 738 | "metadata": {}, 739 | "source": [ 740 | "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " 741 | ] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": 34, 746 | "metadata": {}, 747 | "outputs": [ 748 | { 749 | "data": { 750 | "text/plain": [ 751 | "(4, 1)" 752 | ] 753 | }, 754 | "execution_count": 34, 755 | "metadata": {}, 756 | "output_type": "execute_result" 757 | } 758 | ], 759 | "source": [ 760 | "divmod(9,2)" 761 | ] 762 | }, 763 | { 764 | "cell_type": "markdown", 765 | "metadata": {}, 766 | "source": [ 767 | "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 35, 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "True\n", 780 | "False\n", 781 | "True\n" 782 | ] 783 | } 784 | ], 785 | "source": [ 786 | "print (isinstance(1, int))\n", 787 | "print (isinstance(1.0,int))\n", 788 | "print (isinstance(1.0,(int,float)))" 789 | ] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "**cmp(x,y)**\n", 796 | "\n", 797 | "|x ? y|Output|\n", 798 | "|---|---|\n", 799 | "| x < y | -1 |\n", 800 | "| x == y | 0 |\n", 801 | "| x > y | 1 |" 802 | ] 803 | }, 804 | { 805 | "cell_type": "markdown", 806 | "metadata": {}, 807 | "source": [ 808 | "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 37, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "name": "stdout", 818 | "output_type": "stream", 819 | "text": [ 820 | "27\n", 821 | "2\n" 822 | ] 823 | } 824 | ], 825 | "source": [ 826 | "print (pow(3,3))\n", 827 | "print (pow(3,3,5))" 828 | ] 829 | }, 830 | { 831 | "cell_type": "markdown", 832 | "metadata": {}, 833 | "source": [ 834 | "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 42, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "name": "stdout", 844 | "output_type": "stream", 845 | "text": [ 846 | "range(0, 3)\n", 847 | "range(2, 9)\n", 848 | "range(2, 27, 8)\n", 849 | "\n" 850 | ] 851 | } 852 | ], 853 | "source": [ 854 | "print (range(3))\n", 855 | "print (range(2,9))\n", 856 | "print (range(2,27,8))\n", 857 | "\n", 858 | "print(type(range(3)))" 859 | ] 860 | }, 861 | { 862 | "cell_type": "markdown", 863 | "metadata": {}, 864 | "source": [ 865 | "##Accepting User Inputs" 866 | ] 867 | }, 868 | { 869 | "cell_type": "markdown", 870 | "metadata": {}, 871 | "source": [ 872 | "**raw_input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 44, 878 | "metadata": {}, 879 | "outputs": [ 880 | { 881 | "name": "stdout", 882 | "output_type": "stream", 883 | "text": [ 884 | "Type something here and it will be stored in variable abc \t567890\n" 885 | ] 886 | } 887 | ], 888 | "source": [ 889 | "abc = input(\"Type something here and it will be stored in variable abc \\t\")" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": 45, 895 | "metadata": {}, 896 | "outputs": [ 897 | { 898 | "data": { 899 | "text/plain": [ 900 | "str" 901 | ] 902 | }, 903 | "execution_count": 45, 904 | "metadata": {}, 905 | "output_type": "execute_result" 906 | } 907 | ], 908 | "source": [ 909 | "type(abc)" 910 | ] 911 | }, 912 | { 913 | "cell_type": "markdown", 914 | "metadata": {}, 915 | "source": [ 916 | "**input( )**, this is used only for accepting only integer inputs." 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 46, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "Only integer can be stored in variable abc \t1234\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "abc1 = input(\"Only integer can be stored in variable abc \\t\")" 934 | ] 935 | }, 936 | { 937 | "cell_type": "code", 938 | "execution_count": 47, 939 | "metadata": {}, 940 | "outputs": [ 941 | { 942 | "data": { 943 | "text/plain": [ 944 | "str" 945 | ] 946 | }, 947 | "execution_count": 47, 948 | "metadata": {}, 949 | "output_type": "execute_result" 950 | } 951 | ], 952 | "source": [ 953 | "type(abc1)" 954 | ] 955 | }, 956 | { 957 | "cell_type": "markdown", 958 | "metadata": {}, 959 | "source": [ 960 | "Note that **type( )** returns the format or the type of a variable or a number" 961 | ] 962 | }, 963 | { 964 | "cell_type": "code", 965 | "execution_count": null, 966 | "metadata": {}, 967 | "outputs": [], 968 | "source": [] 969 | } 970 | ], 971 | "metadata": { 972 | "kernelspec": { 973 | "display_name": "Python 3", 974 | "language": "python", 975 | "name": "python3" 976 | }, 977 | "language_info": { 978 | "codemirror_mode": { 979 | "name": "ipython", 980 | "version": 3 981 | }, 982 | "file_extension": ".py", 983 | "mimetype": "text/x-python", 984 | "name": "python", 985 | "nbconvert_exporter": "python", 986 | "pygments_lexer": "ipython3", 987 | "version": "3.7.3" 988 | } 989 | }, 990 | "nbformat": 4, 991 | "nbformat_minor": 1 992 | } 993 | -------------------------------------------------------------------------------- /Introduction-to-ML/2 # Print Statement, Precision and FieldWidth.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Print Statement" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "The **print** statement can be used in the following different ways :\n", 22 | "\n", 23 | " - print (\"Hello World\")\n", 24 | " - print (\"Hello\", )\n", 25 | " - print (\"Hello\" + )\n", 26 | " - print (\"Hello %s\" % )" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "ename": "SyntaxError", 36 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"Hello World\")? (, line 1)", 37 | "output_type": "error", 38 | "traceback": [ 39 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print \"Hello World\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"Hello World\")?\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "print (\"Hello World\")" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "In Python, single, double and triple quotes are used to denote a string.\n", 52 | "Most use single quotes when declaring a single character. \n", 53 | "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "Hey\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "print ('Hey')" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 1, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "My name is Vipul Gaur\n", 83 | "\n", 84 | "I love Python.\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "print (\"\"\"My name is Vipul Gaur\n", 90 | "\n", 91 | "I love Python.\"\"\")" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "metadata": { 105 | "scrolled": true 106 | }, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "Hello World\n", 113 | "Hello World !\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "string1 = 'World'\n", 119 | "print ('Hello', string1)\n", 120 | "\n", 121 | "string2 = '!'\n", 122 | "print ('Hello', string1, string2)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 3, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "HelloWorld!\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print ('Hello' + string1 + string2)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "**%s** is used to refer to a variable which contains a string." 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 4, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "Hello World\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "print (\"Hello %s\" % string1)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "Similarly, when using other data types\n", 178 | "\n", 179 | " - %s -> string\n", 180 | " - %d -> Integer\n", 181 | " - %f -> Float\n", 182 | " - %o -> Octal\n", 183 | " - %x -> Hexadecimal\n", 184 | " - %e -> exponential\n", 185 | " \n", 186 | "This can be used for conversions inside the print statement itself." 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 5, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "Actual Number = 18\n", 199 | "Float of the number = 18.000000\n", 200 | "Octal equivalent of the number = 22\n", 201 | "Hexadecimal equivalent of the number = 12\n", 202 | "Exponential equivalent of the number = 1.800000e+01\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print (\"Actual Number = %d\" %18)\n", 208 | "print (\"Float of the number = %f\" %18)\n", 209 | "print (\"Octal equivalent of the number = %o\" %18)\n", 210 | "print (\"Hexadecimal equivalent of the number = %x\" %18)\n", 211 | "print (\"Exponential equivalent of the number = %e\" %18)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "When referring to multiple variables parenthesis is used." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 6, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "Hello World !\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "print (\"Hello %s %s\" %(string1,string2))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "#### Other Examples" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "The following are other different ways the print statement can be put to use." 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 7, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "I want %d to be printed here\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "print (\"I want %%d to be printed %s\" %'here')" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 8, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "_A_A_A_A_A_A_A_A_A_A\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "print ('_A'*10)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 9, 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "Jan\n", 296 | "Feb\n", 297 | "Mar\n", 298 | "Apr\n", 299 | "May\n", 300 | "Jun\n", 301 | "Jul\n", 302 | "Aug\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "print (\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 10, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "I want \\n to be printed.\n" 320 | ] 321 | } 322 | ], 323 | "source": [ 324 | "print (\"I want \\\\n to be printed.\")" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 11, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "\n", 337 | "Routine:\n", 338 | "\t- Eat\n", 339 | "\t- Sleep\n", 340 | "\t- Repeat\n", 341 | "\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "print (\"\"\"\n", 347 | "Routine:\n", 348 | "\\t- Eat\n", 349 | "\\t- Sleep\\n\\t- Repeat\n", 350 | "\"\"\")" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": {}, 356 | "source": [ 357 | "#### PrecisionWidth and FieldWidth" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", 365 | "\n", 366 | "The default Precision Width is set to 6." 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 12, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "'3.121312'" 378 | ] 379 | }, 380 | "execution_count": 12, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "\"%f\" % 3.121312312312" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": {}, 392 | "source": [ 393 | "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 13, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "'3.12131'" 405 | ] 406 | }, 407 | "execution_count": 13, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "\"%.5f\" % 3.121312312312" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 14, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "' 3.12131'" 432 | ] 433 | }, 434 | "execution_count": 14, 435 | "metadata": {}, 436 | "output_type": "execute_result" 437 | } 438 | ], 439 | "source": [ 440 | "\"%9.5f\" % 3.121312312312" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "Zero padding is done by adding a 0 at the start of fieldwidth." 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 15, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "'00000000000003.12131'" 459 | ] 460 | }, 461 | "execution_count": 15, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "\"%020.5f\" % 3.121312312312" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": {}, 473 | "source": [ 474 | "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 16, 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "name": "stdout", 484 | "output_type": "stream", 485 | "text": [ 486 | " 3.121312\n", 487 | "-3.121312\n" 488 | ] 489 | } 490 | ], 491 | "source": [ 492 | "print (\"% 9f\" % 3.121312312312)\n", 493 | "print (\"% 9f\" % -3.121312312312)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 17, 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "+3.121312\n", 513 | "-3.121312\n" 514 | ] 515 | } 516 | ], 517 | "source": [ 518 | "print (\"%+9f\" % 3.121312312312)\n", 519 | "print (\"% 9f\" % -3.121312312312)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actually field width. But left alignment can be done by specifying a negative symbol in the field width." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 18, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "data": { 536 | "text/plain": [ 537 | "'3.121 '" 538 | ] 539 | }, 540 | "execution_count": 18, 541 | "metadata": {}, 542 | "output_type": "execute_result" 543 | } 544 | ], 545 | "source": [ 546 | "\"%-9.3f\" % 3.121312312312" 547 | ] 548 | } 549 | ], 550 | "metadata": { 551 | "kernelspec": { 552 | "display_name": "Python 3", 553 | "language": "python", 554 | "name": "python3" 555 | }, 556 | "language_info": { 557 | "codemirror_mode": { 558 | "name": "ipython", 559 | "version": 3 560 | }, 561 | "file_extension": ".py", 562 | "mimetype": "text/x-python", 563 | "name": "python", 564 | "nbconvert_exporter": "python", 565 | "pygments_lexer": "ipython3", 566 | "version": "3.7.3" 567 | } 568 | }, 569 | "nbformat": 4, 570 | "nbformat_minor": 1 571 | } 572 | -------------------------------------------------------------------------------- /Introduction-to-ML/4 # Strings and Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Strings" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "String0 = 'Taj Mahal is beautiful'\n", 31 | "String1 = \"Taj Mahal is beautiful\"\n", 32 | "String2 = '''Taj Mahal\n", 33 | "is\n", 34 | "beautiful'''" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "Taj Mahal is beautiful \n", 47 | "Taj Mahal is beautiful \n", 48 | "Taj Mahal\n", 49 | "is\n", 50 | "beautiful \n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print (String0 , type(String0))\n", 56 | "print (String1 , type(String1))\n", 57 | "print (String2 , type(String2))" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "String Indexing and Slicing are similar to Lists which was explained in detail earlier." 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 4, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "M\n", 77 | "Mahal is beautiful\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "print (String0[4])\n", 83 | "print (String0[4:])" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Built-in Functions" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "7\n", 110 | "-1\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "print (String0.find('al'))\n", 116 | "print (String0.find('am'))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "The index value returned is the index of the first element in the input data." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 10, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "a\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print (String0[7])" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "One can also input **find( )** function between which index values it has to search." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 11, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "2\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "print (String0.find('j',1,3))" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "**capitalize( )** is used to capitalize the first element in the string." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 12, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Observe the first letter in this sentence.\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "String3 = 'observe the first letter in this sentence.'\n", 189 | "print (String3.capitalize())" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "**center( )** is used to center align the string by specifying the field width." 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 13, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "' Taj Mahal is beautiful '" 208 | ] 209 | }, 210 | "execution_count": 13, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "String0.center(70)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "One can also fill the left out spaces with any other character." 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 14, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "'------------------------Taj Mahal is beautiful------------------------'" 235 | ] 236 | }, 237 | "execution_count": 14, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "String0.center(70,'-')" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "**zfill( )** is used for zero padding by specifying the field width." 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 15, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "'00000000Taj Mahal is beautiful'" 262 | ] 263 | }, 264 | "execution_count": 15, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "String0.zfill(30)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 20, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "h\te\tl\tl\to\n", 290 | "h e l l o\n", 291 | "h e l l o\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "s = 'h\\te\\tl\\tl\\to'\n", 297 | "print (s)\n", 298 | "print (s.expandtabs(2))\n", 299 | "print (s.expandtabs())" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": { 305 | "collapsed": true 306 | }, 307 | "source": [ 308 | "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 21, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "name": "stdout", 318 | "output_type": "stream", 319 | "text": [ 320 | "0\n", 321 | "4\n" 322 | ] 323 | }, 324 | { 325 | "ename": "ValueError", 326 | "evalue": "substring not found", 327 | "output_type": "error", 328 | "traceback": [ 329 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 330 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 331 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Taj'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Mahal'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Mahal'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 332 | "\u001b[1;31mValueError\u001b[0m: substring not found" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "print (String0.index('Taj'))\n", 338 | "print (String0.index('Mahal',0))\n", 339 | "print (String0.index('Mahal',10,20))" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 22, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "False\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "print (String0.endswith('y'))" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "The start and stop index values can also be specified." 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 24, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "True\n", 383 | "True\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "print (String0.endswith('l',0))\n", 389 | "print (String0.endswith('M',0,5))" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 25, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "4\n", 409 | "2\n" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "print (String0.count('a',0))\n", 415 | "print (String0.count('a',5,10))" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": {}, 421 | "source": [ 422 | "**join( )** function is used add a char in between the elements of the input string." 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 26, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "'*a_a-'" 434 | ] 435 | }, 436 | "execution_count": 26, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "'a'.join('*_-')" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "'*_-' is the input string and char 'a' is added in between each element" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "metadata": {}, 455 | "source": [ 456 | "**join( )** function can also be used to convert a list into a string." 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 28, 462 | "metadata": {}, 463 | "outputs": [ 464 | { 465 | "name": "stdout", 466 | "output_type": "stream", 467 | "text": [ 468 | "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", 469 | "Taj Mahal is beautiful\n" 470 | ] 471 | } 472 | ], 473 | "source": [ 474 | "a = list(String0)\n", 475 | "print (a)\n", 476 | "b = ''.join(a)\n", 477 | "print (b)" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 29, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | " /i/s/ /b/e/a/u/t/i/f/u/l\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "c = '/'.join(a)[18:]\n", 502 | "print (c)" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "metadata": {}, 508 | "source": [ 509 | "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 30, 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "name": "stdout", 519 | "output_type": "stream", 520 | "text": [ 521 | "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "d = c.split('/')\n", 527 | "print (d)" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 31, 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "name": "stdout", 544 | "output_type": "stream", 545 | "text": [ 546 | "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", 547 | "4\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "e = c.split('/',3)\n", 553 | "print (e)\n", 554 | "print (len(e))" 555 | ] 556 | }, 557 | { 558 | "cell_type": "markdown", 559 | "metadata": {}, 560 | "source": [ 561 | "**lower( )** converts any capital letter to small letter." 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 32, 567 | "metadata": {}, 568 | "outputs": [ 569 | { 570 | "name": "stdout", 571 | "output_type": "stream", 572 | "text": [ 573 | "Taj Mahal is beautiful\n", 574 | "taj mahal is beautiful\n" 575 | ] 576 | } 577 | ], 578 | "source": [ 579 | "print (String0)\n", 580 | "print (String0.lower())" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "**upper( )** converts any small letter to capital letter." 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 33, 593 | "metadata": {}, 594 | "outputs": [ 595 | { 596 | "data": { 597 | "text/plain": [ 598 | "'TAJ MAHAL IS BEAUTIFUL'" 599 | ] 600 | }, 601 | "execution_count": 33, 602 | "metadata": {}, 603 | "output_type": "execute_result" 604 | } 605 | ], 606 | "source": [ 607 | "String0.upper()" 608 | ] 609 | }, 610 | { 611 | "cell_type": "markdown", 612 | "metadata": {}, 613 | "source": [ 614 | "**replace( )** function replaces the element with another element." 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 35, 620 | "metadata": {}, 621 | "outputs": [ 622 | { 623 | "data": { 624 | "text/plain": [ 625 | "'Chandigarh is beautiful'" 626 | ] 627 | }, 628 | "execution_count": 35, 629 | "metadata": {}, 630 | "output_type": "execute_result" 631 | } 632 | ], 633 | "source": [ 634 | "String0.replace('Taj Mahal','Chandigarh')" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": {}, 640 | "source": [ 641 | "**strip( )** function is used to delete elements from the right end and the left end which is not required." 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": 36, 647 | "metadata": {}, 648 | "outputs": [], 649 | "source": [ 650 | "f = ' hello '" 651 | ] 652 | }, 653 | { 654 | "cell_type": "markdown", 655 | "metadata": {}, 656 | "source": [ 657 | "If no char is specified then it will delete all the spaces that is present in the right and left hand side of the data." 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 37, 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "'hello'" 669 | ] 670 | }, 671 | "execution_count": 37, 672 | "metadata": {}, 673 | "output_type": "execute_result" 674 | } 675 | ], 676 | "source": [ 677 | "f.strip()" 678 | ] 679 | }, 680 | { 681 | "cell_type": "markdown", 682 | "metadata": {}, 683 | "source": [ 684 | "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 38, 690 | "metadata": {}, 691 | "outputs": [], 692 | "source": [ 693 | "f = ' ***----hello---******* '" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": 40, 699 | "metadata": {}, 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/plain": [ 704 | "' ***----hello---******* '" 705 | ] 706 | }, 707 | "execution_count": 40, 708 | "metadata": {}, 709 | "output_type": "execute_result" 710 | } 711 | ], 712 | "source": [ 713 | "f.strip('*')" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "metadata": {}, 719 | "source": [ 720 | "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present." 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": 42, 726 | "metadata": {}, 727 | "outputs": [ 728 | { 729 | "name": "stdout", 730 | "output_type": "stream", 731 | "text": [ 732 | "----hello---\n", 733 | "hello\n" 734 | ] 735 | } 736 | ], 737 | "source": [ 738 | "print (f.strip(' *'))\n", 739 | "print (f.strip(' *-'))" 740 | ] 741 | }, 742 | { 743 | "cell_type": "markdown", 744 | "metadata": {}, 745 | "source": [ 746 | "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right." 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 43, 752 | "metadata": {}, 753 | "outputs": [ 754 | { 755 | "name": "stdout", 756 | "output_type": "stream", 757 | "text": [ 758 | "----hello---******* \n", 759 | " ***----hello---\n" 760 | ] 761 | } 762 | ], 763 | "source": [ 764 | "print (f.lstrip(' *'))\n", 765 | "print (f.rstrip(' *'))" 766 | ] 767 | }, 768 | { 769 | "cell_type": "markdown", 770 | "metadata": {}, 771 | "source": [ 772 | "## Dictionaries" 773 | ] 774 | }, 775 | { 776 | "cell_type": "markdown", 777 | "metadata": {}, 778 | "source": [ 779 | "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." 780 | ] 781 | }, 782 | { 783 | "cell_type": "markdown", 784 | "metadata": {}, 785 | "source": [ 786 | "To define a dictionary, equate a variable to { } or dict()" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": 44, 792 | "metadata": {}, 793 | "outputs": [ 794 | { 795 | "name": "stdout", 796 | "output_type": "stream", 797 | "text": [ 798 | " \n" 799 | ] 800 | } 801 | ], 802 | "source": [ 803 | "d0 = {}\n", 804 | "d1 = dict()\n", 805 | "print (type(d0), type(d1))" 806 | ] 807 | }, 808 | { 809 | "cell_type": "markdown", 810 | "metadata": {}, 811 | "source": [ 812 | "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": 45, 818 | "metadata": {}, 819 | "outputs": [ 820 | { 821 | "name": "stdout", 822 | "output_type": "stream", 823 | "text": [ 824 | "{'One': 1, 'OneTwo': 12}\n" 825 | ] 826 | } 827 | ], 828 | "source": [ 829 | "d0['One'] = 1\n", 830 | "d0['OneTwo'] = 12 \n", 831 | "print (d0)" 832 | ] 833 | }, 834 | { 835 | "cell_type": "markdown", 836 | "metadata": {}, 837 | "source": [ 838 | "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" 839 | ] 840 | }, 841 | { 842 | "cell_type": "code", 843 | "execution_count": 46, 844 | "metadata": {}, 845 | "outputs": [ 846 | { 847 | "name": "stdout", 848 | "output_type": "stream", 849 | "text": [ 850 | "1\n" 851 | ] 852 | } 853 | ], 854 | "source": [ 855 | "print (d0['One'])" 856 | ] 857 | }, 858 | { 859 | "cell_type": "markdown", 860 | "metadata": {}, 861 | "source": [ 862 | "Two lists which are related can be merged to form a dictionary." 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 47, 868 | "metadata": {}, 869 | "outputs": [], 870 | "source": [ 871 | "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", 872 | "numbers = [1, 2, 3, 4, 5]" 873 | ] 874 | }, 875 | { 876 | "cell_type": "markdown", 877 | "metadata": {}, 878 | "source": [ 879 | "**zip( )** function is used to combine two lists" 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "execution_count": 48, 885 | "metadata": {}, 886 | "outputs": [ 887 | { 888 | "name": "stdout", 889 | "output_type": "stream", 890 | "text": [ 891 | "\n" 892 | ] 893 | } 894 | ], 895 | "source": [ 896 | "d2 = zip(names,numbers)\n", 897 | "print (d2)" 898 | ] 899 | }, 900 | { 901 | "cell_type": "markdown", 902 | "metadata": {}, 903 | "source": [ 904 | "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n", 905 | "\n", 906 | "Further, To convert the above into a dictionary. **dict( )** function is used." 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": 49, 912 | "metadata": {}, 913 | "outputs": [ 914 | { 915 | "name": "stdout", 916 | "output_type": "stream", 917 | "text": [ 918 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 919 | ] 920 | } 921 | ], 922 | "source": [ 923 | "a1 = dict(d2)\n", 924 | "print (a1)" 925 | ] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "### Built-in Functions" 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": {}, 937 | "source": [ 938 | "**clear( )** function is used to erase the entire database that was created." 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 50, 944 | "metadata": {}, 945 | "outputs": [ 946 | { 947 | "name": "stdout", 948 | "output_type": "stream", 949 | "text": [ 950 | "{}\n" 951 | ] 952 | } 953 | ], 954 | "source": [ 955 | "a1.clear()\n", 956 | "print (a1)" 957 | ] 958 | }, 959 | { 960 | "cell_type": "markdown", 961 | "metadata": {}, 962 | "source": [ 963 | "Dictionary can also be built using loops." 964 | ] 965 | }, 966 | { 967 | "cell_type": "code", 968 | "execution_count": 52, 969 | "metadata": {}, 970 | "outputs": [ 971 | { 972 | "name": "stdout", 973 | "output_type": "stream", 974 | "text": [ 975 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 976 | ] 977 | } 978 | ], 979 | "source": [ 980 | "for i in range(len(names)):\n", 981 | " a1[names[i]] = numbers[i]\n", 982 | "print (a1)" 983 | ] 984 | }, 985 | { 986 | "cell_type": "markdown", 987 | "metadata": {}, 988 | "source": [ 989 | "**values( )** function returns a list with all the assigned values in the dictionary." 990 | ] 991 | }, 992 | { 993 | "cell_type": "code", 994 | "execution_count": 53, 995 | "metadata": {}, 996 | "outputs": [ 997 | { 998 | "data": { 999 | "text/plain": [ 1000 | "dict_values([1, 2, 3, 4, 5])" 1001 | ] 1002 | }, 1003 | "execution_count": 53, 1004 | "metadata": {}, 1005 | "output_type": "execute_result" 1006 | } 1007 | ], 1008 | "source": [ 1009 | "a1.values()" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "markdown", 1014 | "metadata": {}, 1015 | "source": [ 1016 | "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": 54, 1022 | "metadata": {}, 1023 | "outputs": [ 1024 | { 1025 | "data": { 1026 | "text/plain": [ 1027 | "dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])" 1028 | ] 1029 | }, 1030 | "execution_count": 54, 1031 | "metadata": {}, 1032 | "output_type": "execute_result" 1033 | } 1034 | ], 1035 | "source": [ 1036 | "a1.keys()" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": {}, 1042 | "source": [ 1043 | "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "execution_count": 55, 1049 | "metadata": {}, 1050 | "outputs": [ 1051 | { 1052 | "data": { 1053 | "text/plain": [ 1054 | "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)])" 1055 | ] 1056 | }, 1057 | "execution_count": 55, 1058 | "metadata": {}, 1059 | "output_type": "execute_result" 1060 | } 1061 | ], 1062 | "source": [ 1063 | "a1.items()" 1064 | ] 1065 | }, 1066 | { 1067 | "cell_type": "markdown", 1068 | "metadata": {}, 1069 | "source": [ 1070 | "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": 56, 1076 | "metadata": {}, 1077 | "outputs": [ 1078 | { 1079 | "name": "stdout", 1080 | "output_type": "stream", 1081 | "text": [ 1082 | "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}\n", 1083 | "4\n" 1084 | ] 1085 | } 1086 | ], 1087 | "source": [ 1088 | "a2 = a1.pop('Four')\n", 1089 | "print (a1)\n", 1090 | "print (a2)" 1091 | ] 1092 | }, 1093 | { 1094 | "cell_type": "code", 1095 | "execution_count": null, 1096 | "metadata": {}, 1097 | "outputs": [], 1098 | "source": [] 1099 | } 1100 | ], 1101 | "metadata": { 1102 | "kernelspec": { 1103 | "display_name": "Python 3", 1104 | "language": "python", 1105 | "name": "python3" 1106 | }, 1107 | "language_info": { 1108 | "codemirror_mode": { 1109 | "name": "ipython", 1110 | "version": 3 1111 | }, 1112 | "file_extension": ".py", 1113 | "mimetype": "text/x-python", 1114 | "name": "python", 1115 | "nbconvert_exporter": "python", 1116 | "pygments_lexer": "ipython3", 1117 | "version": "3.7.3" 1118 | } 1119 | }, 1120 | "nbformat": 4, 1121 | "nbformat_minor": 1 1122 | } 1123 | -------------------------------------------------------------------------------- /Introduction-to-ML/5 # Control Flow Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Control Flow Statements" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## If" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "if some_condition:\n", 29 | " \n", 30 | " algorithm" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Hello\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "x = 12\n", 48 | "if x >10:\n", 49 | " print (\"Hello\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "## If-else" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "if some_condition:\n", 64 | " \n", 65 | " algorithm\n", 66 | " \n", 67 | "else:\n", 68 | " \n", 69 | " algorithm" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 3, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "hello\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "x = 12\n", 87 | "if x > 10:\n", 88 | " print (\"hello\")\n", 89 | "else:\n", 90 | " print (\"world\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## if-elif" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "if some_condition:\n", 105 | " \n", 106 | " algorithm\n", 107 | "\n", 108 | "elif some_condition:\n", 109 | " \n", 110 | " algorithm\n", 111 | "\n", 112 | "else:\n", 113 | " \n", 114 | " algorithm" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 4, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "x y:\n", 134 | " print (\"x>y\")\n", 135 | "elif x < y:\n", 136 | " print (\"x y:\n", 166 | " print (\"x>y\")\n", 167 | "elif x < y:\n", 168 | " print (\"x=7:\n", 362 | " break" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "##Continue" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement. " 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 12, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "0\n", 389 | "1\n", 390 | "2\n", 391 | "3\n", 392 | "4\n", 393 | "The end.\n", 394 | "The end.\n", 395 | "The end.\n", 396 | "The end.\n", 397 | "The end.\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "for i in range(10):\n", 403 | " if i>4:\n", 404 | " print (\"The end.\")\n", 405 | " continue\n", 406 | " elif i<7:\n", 407 | " print (i)" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": {}, 413 | "source": [ 414 | "## List Comprehensions" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "metadata": {}, 420 | "source": [ 421 | "Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as," 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 14, 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "res = []\n", 439 | "for i in range(1,11):\n", 440 | " x = 27*i\n", 441 | " res.append(x)\n", 442 | "print (res)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem." 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 15, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 461 | ] 462 | }, 463 | "execution_count": 15, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "[27*x for x in range(1,11)]" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "That's it!. Only remember to enclose it in square brackets" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 16, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "data": { 493 | "text/plain": [ 494 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 495 | ] 496 | }, 497 | "execution_count": 16, 498 | "metadata": {}, 499 | "output_type": "execute_result" 500 | } 501 | ], 502 | "source": [ 503 | "[27*x for x in range(1,20) if x<=10]" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "Let me add one more loop to make you understand better, " 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 17, 516 | "metadata": {}, 517 | "outputs": [ 518 | { 519 | "data": { 520 | "text/plain": [ 521 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 522 | ] 523 | }, 524 | "execution_count": 17, 525 | "metadata": {}, 526 | "output_type": "execute_result" 527 | } 528 | ], 529 | "source": [ 530 | "[27*z for i in range(50) if i==27 for z in range(1,11)]" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | } 540 | ], 541 | "metadata": { 542 | "kernelspec": { 543 | "display_name": "Python 3", 544 | "language": "python", 545 | "name": "python3" 546 | }, 547 | "language_info": { 548 | "codemirror_mode": { 549 | "name": "ipython", 550 | "version": 3 551 | }, 552 | "file_extension": ".py", 553 | "mimetype": "text/x-python", 554 | "name": "python", 555 | "nbconvert_exporter": "python", 556 | "pygments_lexer": "ipython3", 557 | "version": "3.7.3" 558 | } 559 | }, 560 | "nbformat": 4, 561 | "nbformat_minor": 1 562 | } 563 | -------------------------------------------------------------------------------- /Introduction-to-ML/6 # Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Functions" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Most of the times, In a algorithm the statements keep repeating and it will be a tedious job to execute the same statements again and again and will consume a lot of memory and is not efficient. Enter Functions." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "This is the basic syntax of a function" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "def funcname(arg1, arg2,... argN):\n", 36 | " \n", 37 | " ''' Document String'''\n", 38 | "\n", 39 | " statements\n", 40 | "\n", 41 | "\n", 42 | " return " 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Read the above syntax as, A function by name \"funcname\" is defined, which accepts arguements \"arg1,arg2,....argN\". The function is documented and it is '''Document String'''. The function after executing the statements returns a \"value\"." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Hey Vipul!\n", 62 | "Vipul, How do you do?\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "print (\"Hey Vipul!\")\n", 68 | "print (\"Vipul, How do you do?\")" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Instead of writing the above two statements every single time it can be replaced by defining a function which would do the job in just one line. \n", 76 | "\n", 77 | "Defining a function firstfunc()." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "def firstfunc():\n", 87 | " print (\"Hey Vipul!\")\n", 88 | " print (\"Vipul, How do you do?\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Hey Vipul!\n", 101 | "Vipul, How do you do?\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "firstfunc()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "def firstfunc(username):\n", 123 | " print (\"Hey\", username + '!')\n", 124 | " print (username + ',' ,\"How do you do?\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 9, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Please enter your name : Vipul\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "name1 = input('Please enter your name : ')" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "The name \"Vipul\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 11, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "Hey Vipul!\n", 161 | "Vipul, How do you do?\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "firstfunc(name1)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself." 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 12, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "def firstfunc(username):\n", 183 | " print (\"Hey\", username + '!')\n", 184 | " print (username + ',' ,\"How do you do?\")\n", 185 | "def secondfunc():\n", 186 | " name = input(\"Please enter your name : \")\n", 187 | " firstfunc(name)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 13, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "Please enter your name : V Gaur\n", 200 | "Hey V Gaur!\n", 201 | "V Gaur, How do you do?\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "secondfunc()" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "## Return Statement" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "When the function results in some value and that value has to be stored in a variable or needs to be sent back or returned for further operation to the main algorithm, return statement is used." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 14, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "def times(x,y):\n", 230 | " z = x*y\n", 231 | " return z" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 15, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "20\n" 251 | ] 252 | } 253 | ], 254 | "source": [ 255 | "c = times(4,5)\n", 256 | "print (c)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "The z value is stored in variable c and can be used for further operations." 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "Instead of declaring another variable the entire statement itself can be used in the return statement as shown." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 16, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "def times(x,y):\n", 280 | " '''This multiplies the two input arguments'''\n", 281 | " return x*y" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 17, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "20\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "c = times(4,5)\n", 299 | "print (c)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 18, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "Help on function times in module __main__:\n", 319 | "\n", 320 | "times(x, y)\n", 321 | " This multiplies the two input arguments\n", 322 | "\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "help(times)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "Multiple variable can also be returned, But keep in mind the order." 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 19, 340 | "metadata": {}, 341 | "outputs": [], 342 | "source": [ 343 | "eglist = [10,50,30,12,6,8,100]" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 20, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "def egfunc(eglist):\n", 353 | " highest = max(eglist)\n", 354 | " lowest = min(eglist)\n", 355 | " first = eglist[0]\n", 356 | " last = eglist[-1]\n", 357 | " return highest,lowest,first,last" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "If the function is just called without any variable for it to be assigned to, the result is returned inside a tuple. But if the variables are mentioned then the result is assigned to the variable in a particular order which is declared in the return statement." 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 21, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "(100, 6, 10, 100)" 376 | ] 377 | }, 378 | "execution_count": 21, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "egfunc(eglist)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 23, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | " a = 100 \n", 397 | " b = 6 \n", 398 | " c = 10 \n", 399 | " d = 100\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "a,b,c,d = egfunc(eglist)\n", 405 | "print (' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "## Implicit arguments" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 24, 425 | "metadata": {}, 426 | "outputs": [], 427 | "source": [ 428 | "def implicitadd(x,y=3):\n", 429 | " return x+y" 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 25, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "7" 455 | ] 456 | }, 457 | "execution_count": 25, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "implicitadd(4)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "metadata": {}, 469 | "source": [ 470 | "But if the second argument is specified then this value overrides the implicit value assigned to the argument " 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 26, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "8" 482 | ] 483 | }, 484 | "execution_count": 26, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "implicitadd(4,4)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "metadata": {}, 496 | "source": [ 497 | "## Any number of arguments" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "If the number of arguments that is to be accepted by a function is not known then a asterisk symbol is used before the argument." 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 29, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [ 513 | "def add_n(*args):\n", 514 | " res = 0\n", 515 | " reslist = []\n", 516 | " for i in args:\n", 517 | " reslist.append(i)\n", 518 | " print (reslist)\n", 519 | " return sum(reslist)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "The above function accepts any number of arguments, defines a list and appends all the arguments into that list and return the sum of all the arguments." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 30, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "[1, 2, 3, 4, 5]\n" 539 | ] 540 | }, 541 | { 542 | "data": { 543 | "text/plain": [ 544 | "15" 545 | ] 546 | }, 547 | "execution_count": 30, 548 | "metadata": {}, 549 | "output_type": "execute_result" 550 | } 551 | ], 552 | "source": [ 553 | "add_n(1,2,3,4,5)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 31, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "[1, 2, 3]\n" 566 | ] 567 | }, 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "6" 572 | ] 573 | }, 574 | "execution_count": 31, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "add_n(1,2,3)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "## Global and Local Variables" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "Whatever variable is declared inside a function is local variable and outside the function in global variable." 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 32, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "eg1 = [1,2,3,4,5]" 604 | ] 605 | }, 606 | { 607 | "cell_type": "markdown", 608 | "metadata": {}, 609 | "source": [ 610 | "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable." 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 35, 616 | "metadata": {}, 617 | "outputs": [], 618 | "source": [ 619 | "def egfunc1():\n", 620 | " def thirdfunc(arg1):\n", 621 | " eg2 = arg1[:]\n", 622 | " eg2.append(6)\n", 623 | " print (\"This is happening inside the function :\", eg2)\n", 624 | " print (\"This is happening before the function is called : \", eg1)\n", 625 | " thirdfunc(eg1)\n", 626 | " print (\"This is happening outside the function :\", eg1)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 36, 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 639 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 640 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "egfunc1()" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": {}, 651 | "source": [ 652 | "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 37, 658 | "metadata": {}, 659 | "outputs": [], 660 | "source": [ 661 | "eg3 = [1,2,3,4,5]" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 38, 667 | "metadata": {}, 668 | "outputs": [ 669 | { 670 | "ename": "SyntaxError", 671 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)? (, line 6)", 672 | "output_type": "error", 673 | "traceback": [ 674 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m print \"This is happening inside the function :\", eg2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)?\n" 675 | ] 676 | } 677 | ], 678 | "source": [ 679 | "def egfunc1():\n", 680 | " def thirdfunc(arg1):\n", 681 | " global eg2\n", 682 | " eg2 = arg1[:]\n", 683 | " eg2.append(6)\n", 684 | " print \"This is happening inside the function :\", eg2 \n", 685 | " print \"This is happening before the function is called : \", eg1\n", 686 | " thirdfunc(eg1)\n", 687 | " print \"This is happening outside the function :\", eg1 \n", 688 | " print \"Accessing a variable declared inside the function from outside :\" , eg2" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 39, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 701 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 702 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 703 | ] 704 | } 705 | ], 706 | "source": [ 707 | "egfunc1()" 708 | ] 709 | }, 710 | { 711 | "cell_type": "markdown", 712 | "metadata": {}, 713 | "source": [ 714 | "## Lambda Functions" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression." 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 1, 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [ 730 | "z = lambda x: x * x\n", 731 | "\n", 732 | "\n", 733 | "def squared(x):\n", 734 | " return x * x" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": 3, 740 | "metadata": {}, 741 | "outputs": [ 742 | { 743 | "name": "stdout", 744 | "output_type": "stream", 745 | "text": [ 746 | "64\n", 747 | "64\n" 748 | ] 749 | } 750 | ], 751 | "source": [ 752 | "print(z(8))\n", 753 | "\n", 754 | "print(squared(8))" 755 | ] 756 | }, 757 | { 758 | "cell_type": "markdown", 759 | "metadata": {}, 760 | "source": [ 761 | "###map" 762 | ] 763 | }, 764 | { 765 | "cell_type": "markdown", 766 | "metadata": {}, 767 | "source": [ 768 | "**map( )** function basically executes the function that is defined to each of the list's element separately." 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 42, 774 | "metadata": {}, 775 | "outputs": [], 776 | "source": [ 777 | "list1 = [1,2,3,4,5,6,7,8,9]" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": 44, 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "name": "stdout", 787 | "output_type": "stream", 788 | "text": [ 789 | "\n" 790 | ] 791 | } 792 | ], 793 | "source": [ 794 | "eg = map(lambda x:x+2, list1)\n", 795 | "print (eg)" 796 | ] 797 | }, 798 | { 799 | "cell_type": "markdown", 800 | "metadata": {}, 801 | "source": [ 802 | "You can also add two lists." 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 45, 808 | "metadata": {}, 809 | "outputs": [], 810 | "source": [ 811 | "list2 = [9,8,7,6,5,4,3,2,1]" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": 47, 817 | "metadata": {}, 818 | "outputs": [ 819 | { 820 | "name": "stdout", 821 | "output_type": "stream", 822 | "text": [ 823 | "\n" 824 | ] 825 | } 826 | ], 827 | "source": [ 828 | "eg2 = map(lambda x,y:x+y, list1,list2)\n", 829 | "print (eg2)" 830 | ] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": {}, 835 | "source": [ 836 | "Not only lambda function but also other built in functions can also be used." 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": 48, 842 | "metadata": {}, 843 | "outputs": [ 844 | { 845 | "name": "stdout", 846 | "output_type": "stream", 847 | "text": [ 848 | "\n" 849 | ] 850 | } 851 | ], 852 | "source": [ 853 | "eg3 = map(str,eg2)\n", 854 | "print (eg3)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "### filter" 862 | ] 863 | }, 864 | { 865 | "cell_type": "markdown", 866 | "metadata": {}, 867 | "source": [ 868 | "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 49, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "list1 = [1,2,3,4,5,6,7,8,9]" 878 | ] 879 | }, 880 | { 881 | "cell_type": "markdown", 882 | "metadata": {}, 883 | "source": [ 884 | "To get the elements which are less than 5," 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": 50, 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "data": { 894 | "text/plain": [ 895 | "" 896 | ] 897 | }, 898 | "execution_count": 50, 899 | "metadata": {}, 900 | "output_type": "execute_result" 901 | } 902 | ], 903 | "source": [ 904 | "filter(lambda x:x<5,list1)" 905 | ] 906 | }, 907 | { 908 | "cell_type": "markdown", 909 | "metadata": {}, 910 | "source": [ 911 | "Notice what happens when **map()** is used." 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 51, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "data": { 921 | "text/plain": [ 922 | "" 923 | ] 924 | }, 925 | "execution_count": 51, 926 | "metadata": {}, 927 | "output_type": "execute_result" 928 | } 929 | ], 930 | "source": [ 931 | "map(lambda x:x<5, list1)" 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": {}, 937 | "source": [ 938 | "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 52, 944 | "metadata": {}, 945 | "outputs": [ 946 | { 947 | "data": { 948 | "text/plain": [ 949 | "" 950 | ] 951 | }, 952 | "execution_count": 52, 953 | "metadata": {}, 954 | "output_type": "execute_result" 955 | } 956 | ], 957 | "source": [ 958 | "filter(lambda x:x%4==0,list1)" 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "execution_count": null, 964 | "metadata": {}, 965 | "outputs": [], 966 | "source": [] 967 | } 968 | ], 969 | "metadata": { 970 | "kernelspec": { 971 | "display_name": "Python 3", 972 | "language": "python", 973 | "name": "python3" 974 | }, 975 | "language_info": { 976 | "codemirror_mode": { 977 | "name": "ipython", 978 | "version": 3 979 | }, 980 | "file_extension": ".py", 981 | "mimetype": "text/x-python", 982 | "name": "python", 983 | "nbconvert_exporter": "python", 984 | "pygments_lexer": "ipython3", 985 | "version": "3.7.3" 986 | } 987 | }, 988 | "nbformat": 4, 989 | "nbformat_minor": 1 990 | } 991 | -------------------------------------------------------------------------------- /Introduction-to-ML/7 # Python Crash Course Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Crash Course Exercise" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Exercises\n", 15 | "\n", 16 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "** What is 7 to the power of 4?**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "2401\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "#7**4\n", 41 | "\n", 42 | "print(pow(7,4))" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "** Split this string:**\n", 50 | "\n", 51 | " s = \"Hi there Sam!\"\n", 52 | " \n", 53 | "**into a list. **" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 6, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "list" 65 | ] 66 | }, 67 | "execution_count": 6, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "s = 'Hi there Sam!'\n", 74 | "\n", 75 | "s2 = \"There are a lot's of something\"\n", 76 | "\n", 77 | "type(s.split())" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "['Hi', 'there', 'dad!']" 89 | ] 90 | }, 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "** Given the variables:**\n", 103 | "\n", 104 | " planet = \"Earth\"\n", 105 | " diameter = 12742\n", 106 | "\n", 107 | "** Use .format() to print the following string: **\n", 108 | "\n", 109 | " The diameter of Earth is 12742 kilometers." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 8, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "planet = \"Earth\"\n", 119 | "diameter = 12742" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 9, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "The diameter of Earth is 12742 kilometers\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "print('The diameter of {one} is {two} kilometers'.format(one=planet,two=diameter))" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "** Given this nested list, use indexing to grab the word \"hello\" **" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 11, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 18, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "'hello'" 164 | ] 165 | }, 166 | "execution_count": 18, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "lst[3][1][2][0]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 20, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 26, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "'hello'" 200 | ] 201 | }, 202 | "execution_count": 26, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "d['k1'][3]['tricky'][3]['target'][3]" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "** What is the main difference between a tuple and a list? **" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 23, 221 | "metadata": { 222 | "collapsed": true 223 | }, 224 | "outputs": [], 225 | "source": [ 226 | "# Tuple is immutable" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "** Create a function that grabs the email website domain from a string in the form: **\n", 234 | "\n", 235 | " user@domain.com\n", 236 | " \n", 237 | "**So for example, passing \"user@domain.com\" would return: domain.com**" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 29, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "def domainGet(email):\n", 247 | " return email.split('@')[1]" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 30, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "'domain.com'" 259 | ] 260 | }, 261 | "execution_count": 30, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "domainGet('user@domain.com')" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 33, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "False" 286 | ] 287 | }, 288 | "execution_count": 33, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "'dog' in 'Is there a DOG here?'" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 43, 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [ 303 | "def findDog(string):\n", 304 | " if 'dog' in string.lower():\n", 305 | " print(string.lower())\n", 306 | " return True\n", 307 | " else:\n", 308 | " return False" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 47, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "True" 320 | ] 321 | }, 322 | "execution_count": 47, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "findDog2('Is there a dog here?')" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 48, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "True" 340 | ] 341 | }, 342 | "execution_count": 48, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "findDog2('Is there a DOG here')" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 46, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "def findDog2(string):\n", 358 | " return 'dog' in string.lower()" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "** Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases. **" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 30, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 31, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "2" 384 | ] 385 | }, 386 | "execution_count": 31, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "countDog('This dog runs faster than the other dog dude!')" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", 400 | "\n", 401 | " seq = ['soup','dog','salad','cat','great']\n", 402 | "\n", 403 | "**should be filtered down to:**\n", 404 | "\n", 405 | " ['soup','salad']" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 55, 411 | "metadata": {}, 412 | "outputs": [], 413 | "source": [ 414 | "seq = ['soup','dog','salad','cat','great']" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 57, 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "['soup', 'salad']" 426 | ] 427 | }, 428 | "execution_count": 57, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "# the letter should start with s\n", 435 | "# in python the strings can be treated as list as well\n", 436 | "\n", 437 | "#word = 'cat'\n", 438 | "lambda word: word[0] == 's'\n", 439 | "\n", 440 | "list(filter(lambda word: word[0] == 's',seq))" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "### Final Problem\n", 448 | "**You are driving a little too fast, and a police officer stops you. Write a function\n", 449 | " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", 450 | " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", 451 | " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", 452 | " cases. **" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 59, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "def caught_speeding(speed, is_birthday):\n", 462 | " if is_birthday:\n", 463 | " speed -= 5\n", 464 | " \n", 465 | " if speed > 80:\n", 466 | " return 'Big Ticket'\n", 467 | " elif speed > 60:\n", 468 | " return 'Small Ticket'\n", 469 | " else:\n", 470 | " return 'No Ticket'" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 60, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "'Small Ticket'" 482 | ] 483 | }, 484 | "execution_count": 60, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "caught_speeding(81,True)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 61, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "'Big Ticket'" 502 | ] 503 | }, 504 | "execution_count": 61, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "caught_speeding(81,False)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "# Great job!" 518 | ] 519 | } 520 | ], 521 | "metadata": { 522 | "kernelspec": { 523 | "display_name": "Python 3", 524 | "language": "python", 525 | "name": "python3" 526 | }, 527 | "language_info": { 528 | "codemirror_mode": { 529 | "name": "ipython", 530 | "version": 3 531 | }, 532 | "file_extension": ".py", 533 | "mimetype": "text/x-python", 534 | "name": "python", 535 | "nbconvert_exporter": "python", 536 | "pygments_lexer": "ipython3", 537 | "version": "3.7.4" 538 | } 539 | }, 540 | "nbformat": 4, 541 | "nbformat_minor": 1 542 | } 543 | -------------------------------------------------------------------------------- /Introduction-to-ML/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Hello World !!" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Hello World\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "print('Hello World') # Shift/Ctrl + Enter" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "###### I am a markdown" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.4" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | --------------------------------------------------------------------------------