├── .gitattributes ├── .gitignore ├── 01.ipynb ├── 02.ipynb ├── 03.ipynb ├── 04.ipynb ├── 05.ipynb ├── 06.ipynb ├── 07.ipynb ├── README.md ├── data ├── boundary.png ├── boundary2.PNG ├── c++.PNG ├── canny.PNG ├── chardata.jpg ├── done.gif ├── drawthis.png ├── firebase1.PNG ├── haters.gif ├── im1.png ├── im2.PNG ├── im3.png ├── masking.PNG ├── resC.PNG ├── resCp.PNG ├── research.gif ├── tenor.gif ├── theend.gif └── wtf.gif ├── image_proc01.ipynb ├── image_proc01.pdf ├── image_proc02.ipynb ├── image_proc02.pdf ├── image_proc03.ipynb ├── image_proc03.pdf ├── samples ├── 1 (1).jpg ├── 1 (1).png ├── 1 (2).jpg ├── 1 (2).png ├── 1 (3).jpg ├── 1 (3).png ├── 1 (4).jpg ├── B (105).jpg ├── C (60).jpg ├── D (198).jpg ├── bouncingBall.avi ├── obt.png ├── proc3.png └── samples.jpg └── untitled.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Zen Of Python" 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 | "The Zen of Python, by Tim Peters\n", 20 | "\n", 21 | "Beautiful is better than ugly.\n", 22 | "Explicit is better than implicit.\n", 23 | "Simple is better than complex.\n", 24 | "Complex is better than complicated.\n", 25 | "Flat is better than nested.\n", 26 | "Sparse is better than dense.\n", 27 | "Readability counts.\n", 28 | "Special cases aren't special enough to break the rules.\n", 29 | "Although practicality beats purity.\n", 30 | "Errors should never pass silently.\n", 31 | "Unless explicitly silenced.\n", 32 | "In the face of ambiguity, refuse the temptation to guess.\n", 33 | "There should be one-- and preferably only one --obvious way to do it.\n", 34 | "Although that way may not be obvious at first unless you're Dutch.\n", 35 | "Now is better than never.\n", 36 | "Although never is often better than *right* now.\n", 37 | "If the implementation is hard to explain, it's a bad idea.\n", 38 | "If the implementation is easy to explain, it may be a good idea.\n", 39 | "Namespaces are one honking great idea -- let's do more of those!\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "import this" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "# Variables" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "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," 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 2, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "x = 2\n", 68 | "y = 5\n", 69 | "xy = 'Hey'" 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": 7, 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": 8, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "3" 160 | ] 161 | }, 162 | "execution_count": 8, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "1+2" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 9, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "1" 180 | ] 181 | }, 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "2-1" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 10, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "2" 200 | ] 201 | }, 202 | "execution_count": 10, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "1*2" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 11, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "0.5" 220 | ] 221 | }, 222 | "execution_count": 11, 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": 12, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "0.5" 247 | ] 248 | }, 249 | "execution_count": 12, 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": 13, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "5" 267 | ] 268 | }, 269 | "execution_count": 13, 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": 14, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "1.0" 294 | ] 295 | }, 296 | "execution_count": 14, 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": 15, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "z = 1" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 16, 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "True" 344 | ] 345 | }, 346 | "execution_count": 16, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "z == 1" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 17, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "False" 364 | ] 365 | }, 366 | "execution_count": 17, 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": 18, 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": "code", 577 | "execution_count": 27, 578 | "metadata": {}, 579 | "outputs": [ 580 | { 581 | "data": { 582 | "text/plain": [ 583 | "16" 584 | ] 585 | }, 586 | "execution_count": 27, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "0x10" 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "**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." 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 28, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "name": "stdout", 609 | "output_type": "stream", 610 | "text": [ 611 | "8\n", 612 | "170\n", 613 | "10\n" 614 | ] 615 | } 616 | ], 617 | "source": [ 618 | "print(int('010',8))\n", 619 | "print(int('0xaa',16))\n", 620 | "print(int('1010',2))" 621 | ] 622 | }, 623 | { 624 | "cell_type": "markdown", 625 | "metadata": {}, 626 | "source": [ 627 | "**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" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 29, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "name": "stdout", 637 | "output_type": "stream", 638 | "text": [ 639 | "7\n", 640 | "7\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "print(int(7.7))\n", 646 | "print(int('7'))" 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": {}, 652 | "source": [ 653 | "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." 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 30, 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": [ 664 | "'b'" 665 | ] 666 | }, 667 | "execution_count": 30, 668 | "metadata": {}, 669 | "output_type": "execute_result" 670 | } 671 | ], 672 | "source": [ 673 | "chr(98)" 674 | ] 675 | }, 676 | { 677 | "cell_type": "code", 678 | "execution_count": 31, 679 | "metadata": {}, 680 | "outputs": [ 681 | { 682 | "data": { 683 | "text/plain": [ 684 | "98" 685 | ] 686 | }, 687 | "execution_count": 31, 688 | "metadata": {}, 689 | "output_type": "execute_result" 690 | } 691 | ], 692 | "source": [ 693 | "ord('b')" 694 | ] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "metadata": {}, 699 | "source": [ 700 | "## Simplifying Arithmetic Operations" 701 | ] 702 | }, 703 | { 704 | "cell_type": "markdown", 705 | "metadata": {}, 706 | "source": [ 707 | "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 32, 713 | "metadata": { 714 | "scrolled": false 715 | }, 716 | "outputs": [ 717 | { 718 | "name": "stdout", 719 | "output_type": "stream", 720 | "text": [ 721 | "6\n", 722 | "4.56\n" 723 | ] 724 | } 725 | ], 726 | "source": [ 727 | "print(round(5.6231))\n", 728 | "print(round(4.55892, 2))" 729 | ] 730 | }, 731 | { 732 | "cell_type": "markdown", 733 | "metadata": {}, 734 | "source": [ 735 | "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." 736 | ] 737 | }, 738 | { 739 | "cell_type": "code", 740 | "execution_count": 33, 741 | "metadata": {}, 742 | "outputs": [ 743 | { 744 | "name": "stdout", 745 | "output_type": "stream", 746 | "text": [ 747 | "5.385164807134504\n" 748 | ] 749 | } 750 | ], 751 | "source": [ 752 | "c = complex('5+2j')\n", 753 | "print(abs(c))" 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "**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). " 761 | ] 762 | }, 763 | { 764 | "cell_type": "code", 765 | "execution_count": 34, 766 | "metadata": {}, 767 | "outputs": [ 768 | { 769 | "data": { 770 | "text/plain": [ 771 | "(4, 1)" 772 | ] 773 | }, 774 | "execution_count": 34, 775 | "metadata": {}, 776 | "output_type": "execute_result" 777 | } 778 | ], 779 | "source": [ 780 | "divmod(9,2)" 781 | ] 782 | }, 783 | { 784 | "cell_type": "markdown", 785 | "metadata": {}, 786 | "source": [ 787 | "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 35, 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "True\n", 800 | "False\n", 801 | "True\n" 802 | ] 803 | } 804 | ], 805 | "source": [ 806 | "print(isinstance(1, int))\n", 807 | "print(isinstance(1.0,int))\n", 808 | "print(isinstance(1.0,(int,float)))" 809 | ] 810 | }, 811 | { 812 | "cell_type": "markdown", 813 | "metadata": {}, 814 | "source": [ 815 | "**cmp(x,y)**\n", 816 | "\n", 817 | "|x ? y|Output|\n", 818 | "|---|---|\n", 819 | "| x < y | -1 |\n", 820 | "| x == y | 0 |\n", 821 | "| x > y | 1 |" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": 4, 827 | "metadata": {}, 828 | "outputs": [ 829 | { 830 | "name": "stdout", 831 | "output_type": "stream", 832 | "text": [ 833 | "-1\n", 834 | "1\n", 835 | "0\n" 836 | ] 837 | } 838 | ], 839 | "source": [ 840 | "# cmp(x,y) by default exists in Python 2, I don't know why this function doesn't exist in Python 3.\n", 841 | "# On the bright side, let's build this function.\n", 842 | "def cmp(x,y):\n", 843 | " if xy:\n", 848 | " return 1\n", 849 | "\n", 850 | "print(cmp(1,2))\n", 851 | "print(cmp(2,1))\n", 852 | "print(cmp(2,2))" 853 | ] 854 | }, 855 | { 856 | "cell_type": "markdown", 857 | "metadata": {}, 858 | "source": [ 859 | "**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)." 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": 37, 865 | "metadata": {}, 866 | "outputs": [ 867 | { 868 | "name": "stdout", 869 | "output_type": "stream", 870 | "text": [ 871 | "27\n", 872 | "2\n" 873 | ] 874 | } 875 | ], 876 | "source": [ 877 | "print(pow(3,3))\n", 878 | "print(pow(3,3,5))" 879 | ] 880 | }, 881 | { 882 | "cell_type": "markdown", 883 | "metadata": {}, 884 | "source": [ 885 | "**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.)" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": 38, 891 | "metadata": {}, 892 | "outputs": [ 893 | { 894 | "name": "stdout", 895 | "output_type": "stream", 896 | "text": [ 897 | "range(0, 3)\n", 898 | "range(2, 9)\n", 899 | "range(2, 27, 8)\n" 900 | ] 901 | } 902 | ], 903 | "source": [ 904 | "print(range(3))\n", 905 | "print(range(2,9))\n", 906 | "print(range(2,27,8))" 907 | ] 908 | }, 909 | { 910 | "cell_type": "markdown", 911 | "metadata": {}, 912 | "source": [ 913 | "## Accepting User Inputs" 914 | ] 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "**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." 921 | ] 922 | }, 923 | { 924 | "cell_type": "code", 925 | "execution_count": 41, 926 | "metadata": {}, 927 | "outputs": [ 928 | { 929 | "name": "stdout", 930 | "output_type": "stream", 931 | "text": [ 932 | "Type something here and it will be stored in variable abc \tnnnn\n" 933 | ] 934 | } 935 | ], 936 | "source": [ 937 | "abc = input(\"Type something here and it will be stored in variable abc \\t\")" 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "execution_count": 42, 943 | "metadata": {}, 944 | "outputs": [ 945 | { 946 | "data": { 947 | "text/plain": [ 948 | "str" 949 | ] 950 | }, 951 | "execution_count": 42, 952 | "metadata": {}, 953 | "output_type": "execute_result" 954 | } 955 | ], 956 | "source": [ 957 | "type(abc)" 958 | ] 959 | }, 960 | { 961 | "cell_type": "markdown", 962 | "metadata": {}, 963 | "source": [ 964 | "**input( )**, this is used only for accepting only integer inputs." 965 | ] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": 43, 970 | "metadata": {}, 971 | "outputs": [ 972 | { 973 | "name": "stdout", 974 | "output_type": "stream", 975 | "text": [ 976 | "Only integer can be stored in variable abc \t66\n" 977 | ] 978 | } 979 | ], 980 | "source": [ 981 | "abc1 = input(\"Only integer can be stored in variable abc \\t\")" 982 | ] 983 | }, 984 | { 985 | "cell_type": "code", 986 | "execution_count": 44, 987 | "metadata": {}, 988 | "outputs": [ 989 | { 990 | "data": { 991 | "text/plain": [ 992 | "str" 993 | ] 994 | }, 995 | "execution_count": 44, 996 | "metadata": {}, 997 | "output_type": "execute_result" 998 | } 999 | ], 1000 | "source": [ 1001 | "type(abc1)" 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "markdown", 1006 | "metadata": {}, 1007 | "source": [ 1008 | "Note that **type( )** returns the format or the type of a variable or a number" 1009 | ] 1010 | } 1011 | ], 1012 | "metadata": { 1013 | "kernelspec": { 1014 | "display_name": "Python 3", 1015 | "language": "python", 1016 | "name": "python3" 1017 | }, 1018 | "language_info": { 1019 | "codemirror_mode": { 1020 | "name": "ipython", 1021 | "version": 3 1022 | }, 1023 | "file_extension": ".py", 1024 | "mimetype": "text/x-python", 1025 | "name": "python", 1026 | "nbconvert_exporter": "python", 1027 | "pygments_lexer": "ipython3", 1028 | "version": "3.6.2" 1029 | } 1030 | }, 1031 | "nbformat": 4, 1032 | "nbformat_minor": 1 1033 | } 1034 | -------------------------------------------------------------------------------- /02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Print Statement" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "The **print** statement can be used in the following different ways :\n", 15 | "\n", 16 | " - print \"Hello World\"\n", 17 | " - print \"Hello\", \n", 18 | " - print \"Hello\" + \n", 19 | " - print \"Hello %s\" % " 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Hello World\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "print(\"Hello World\")" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "In Python, single, double and triple quotes are used to denote a string.\n", 44 | "Most use single quotes when declaring a single character. \n", 45 | "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Hey\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "print('Hey')" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 6, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "My name is Rajath Kumar M.P.\n", 75 | "\n", 76 | "I love Python.\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "print(\"\"\"My name is Rajath Kumar M.P.\n", 82 | "\n", 83 | "I love Python.\"\"\")" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 7, 96 | "metadata": { 97 | "scrolled": true 98 | }, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "Hello World\n", 105 | "Hello World !\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "string1 = 'World'\n", 111 | "print('Hello', string1)\n", 112 | "\n", 113 | "string2 = '!'\n", 114 | "print('Hello', string1, string2)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 8, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "HelloWorld!\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "print('Hello' + string1 + string2)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "**%s** is used to refer to a variable which contains a string." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 9, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "Hello World\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "print(\"Hello %s\" % string1)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "Similarly, when using other data types\n", 170 | "\n", 171 | " - %s -> string\n", 172 | " - %d -> Integer\n", 173 | " - %f -> Float\n", 174 | " - %o -> Octal\n", 175 | " - %x -> Hexadecimal\n", 176 | " - %e -> exponential\n", 177 | " \n", 178 | "This can be used for conversions inside the print statement itself." 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 10, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Actual Number = 18\n", 191 | "Float of the number = 18.000000\n", 192 | "Octal equivalent of the number = 22\n", 193 | "Hexadecimal equivalent of the number = 12\n", 194 | "Exponential equivalent of the number = 1.800000e+01\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "print(\"Actual Number = %d\" %18)\n", 200 | "print(\"Float of the number = %f\" %18)\n", 201 | "print(\"Octal equivalent of the number = %o\" %18)\n", 202 | "print(\"Hexadecimal equivalent of the number = %x\" %18)\n", 203 | "print(\"Exponential equivalent of the number = %e\" %18)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "When referring to multiple variables parenthesis is used." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 11, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "Hello World !\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(\"Hello %s %s\" %(string1,string2))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "##Other Examples" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "The following are other different ways the print statement can be put to use." 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 12, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "I want %d to be printed here\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "print(\"I want %%d to be printed %s\" %'here')" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 13, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "_A_A_A_A_A_A_A_A_A_A\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "print('_A'*10)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 14, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "Jan\n", 288 | "Feb\n", 289 | "Mar\n", 290 | "Apr\n", 291 | "May\n", 292 | "Jun\n", 293 | "Jul\n", 294 | "Aug\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 15, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "name": "stdout", 309 | "output_type": "stream", 310 | "text": [ 311 | "I want \\n to be printed.\n" 312 | ] 313 | } 314 | ], 315 | "source": [ 316 | "print(\"I want \\\\n to be printed.\")" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 16, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "name": "stdout", 326 | "output_type": "stream", 327 | "text": [ 328 | "\n", 329 | "Routine:\n", 330 | "\t- Eat\n", 331 | "\t- Sleep\n", 332 | "\t- Repeat\n", 333 | "\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "print(\"\"\"\n", 339 | "Routine:\n", 340 | "\\t- Eat\n", 341 | "\\t- Sleep\\n\\t- Repeat\n", 342 | "\"\"\")" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "#PrecisionWidth and FieldWidth" 350 | ] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "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", 357 | "\n", 358 | "The default Precision Width is set to 6." 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 17, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "'3.121312'" 370 | ] 371 | }, 372 | "execution_count": 17, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "\"%f\" % 3.121312312312" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 18, 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "data": { 395 | "text/plain": [ 396 | "'3.12131'" 397 | ] 398 | }, 399 | "execution_count": 18, 400 | "metadata": {}, 401 | "output_type": "execute_result" 402 | } 403 | ], 404 | "source": [ 405 | "\"%.5f\" % 3.121312312312" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 19, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "data": { 422 | "text/plain": [ 423 | "' 3.12131'" 424 | ] 425 | }, 426 | "execution_count": 19, 427 | "metadata": {}, 428 | "output_type": "execute_result" 429 | } 430 | ], 431 | "source": [ 432 | "\"%9.5f\" % 3.121312312312" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "Zero padding is done by adding a 0 at the start of fieldwidth." 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 20, 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "data": { 449 | "text/plain": [ 450 | "'00000000000003.12131'" 451 | ] 452 | }, 453 | "execution_count": 20, 454 | "metadata": {}, 455 | "output_type": "execute_result" 456 | } 457 | ], 458 | "source": [ 459 | "\"%020.5f\" % 3.121312312312" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "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." 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 21, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "name": "stdout", 476 | "output_type": "stream", 477 | "text": [ 478 | " 3.121312\n", 479 | "-3.121312\n" 480 | ] 481 | } 482 | ], 483 | "source": [ 484 | "print(\"% 9f\" % 3.121312312312)\n", 485 | "print(\"% 9f\" % -3.121312312312)" 486 | ] 487 | }, 488 | { 489 | "cell_type": "markdown", 490 | "metadata": {}, 491 | "source": [ 492 | "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": 22, 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "name": "stdout", 502 | "output_type": "stream", 503 | "text": [ 504 | "+3.121312\n", 505 | "-3.121312\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "print(\"%+9f\" % 3.121312312312)\n", 511 | "print(\"% 9f\" % -3.121312312312)" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width." 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 23, 524 | "metadata": {}, 525 | "outputs": [ 526 | { 527 | "data": { 528 | "text/plain": [ 529 | "'3.121 '" 530 | ] 531 | }, 532 | "execution_count": 23, 533 | "metadata": {}, 534 | "output_type": "execute_result" 535 | } 536 | ], 537 | "source": [ 538 | "\"%-9.3f\" % 3.121312312312" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": null, 544 | "metadata": {}, 545 | "outputs": [], 546 | "source": [] 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.6.2" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 1 570 | } 571 | -------------------------------------------------------------------------------- /04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Strings" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "String0 = 'Taj Mahal is beautiful'\n", 24 | "String1 = \"Taj Mahal is beautiful\"\n", 25 | "String2 = '''Taj Mahal\n", 26 | "is\n", 27 | "beautiful'''" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Taj Mahal is beautiful \n", 40 | "Taj Mahal is beautiful \n", 41 | "Taj Mahal\n", 42 | "is\n", 43 | "beautiful \n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "print(String0 , type(String0))\n", 49 | "print(String1, type(String1))\n", 50 | "print(String2, type(String2))" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "String Indexing and Slicing are similar to Lists which was explained in detail earlier." 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "M\n", 70 | "Mahal is beautiful\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "print(String0[4])\n", 76 | "print(String0[4:])" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### Built-in Functions" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "**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." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "7\n", 103 | "-1\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "print(String0.find('al'))\n", 109 | "print(String0.find('am'))" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "The index value returned is the index of the first element in the input data." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "a\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "print(String0[7])" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "One can also input **find( )** function between which index values it has to search." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 6, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "2\n", 153 | "2\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "print(String0.find('j',1))\n", 159 | "print(String0.find('j',1,3))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "**capitalize( )** is used to capitalize the first element in the string." 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 7, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "Observe the first letter in this sentence.\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "String3 = 'observe the first letter in this sentence.'\n", 184 | "print(String3.capitalize())" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "**center( )** is used to center align the string by specifying the field width." 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 8, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "' Taj Mahal is beautiful '" 203 | ] 204 | }, 205 | "execution_count": 8, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "String0.center(70)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "One can also fill the left out spaces with any other character." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 9, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "'------------------------Taj Mahal is beautiful------------------------'" 230 | ] 231 | }, 232 | "execution_count": 9, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "String0.center(70,'-')" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "**zfill( )** is used for zero padding by specifying the field width." 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 10, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "data": { 255 | "text/plain": [ 256 | "'00000000Taj Mahal is beautiful'" 257 | ] 258 | }, 259 | "execution_count": 10, 260 | "metadata": {}, 261 | "output_type": "execute_result" 262 | } 263 | ], 264 | "source": [ 265 | "String0.zfill(30)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 11, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "h\te\tl\tl\to\n", 285 | "h e l l o\n", 286 | "h e l l o\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "s = 'h\\te\\tl\\tl\\to'\n", 292 | "print(s)\n", 293 | "print(s.expandtabs(1))\n", 294 | "print(s.expandtabs())" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": { 300 | "collapsed": true 301 | }, 302 | "source": [ 303 | "**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" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 12, 309 | "metadata": {}, 310 | "outputs": [ 311 | { 312 | "name": "stdout", 313 | "output_type": "stream", 314 | "text": [ 315 | "Taj Mahal is beautiful\n", 316 | "0\n", 317 | "4\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "print(String0)\n", 323 | "print(String0.index('Taj'))\n", 324 | "print(String0.index('Mahal'))" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 13, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "False\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "print(String0.endswith('y'))" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "The start and stop index values can also be specified." 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 14, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "True\n", 368 | "True\n" 369 | ] 370 | } 371 | ], 372 | "source": [ 373 | "print(String0.endswith('l',0))\n", 374 | "print(String0.endswith('M',0,5))" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "**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)" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 15, 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "name": "stdout", 391 | "output_type": "stream", 392 | "text": [ 393 | "4\n", 394 | "2\n" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "print(String0.count('a',0))\n", 400 | "print(String0.count('a',5,10))" 401 | ] 402 | }, 403 | { 404 | "cell_type": "markdown", 405 | "metadata": {}, 406 | "source": [ 407 | "**join( )** function is used add a char in between the elements of the input string." 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 16, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/plain": [ 418 | "'*a_a-'" 419 | ] 420 | }, 421 | "execution_count": 16, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "'a'.join('*_-')" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "'*_-' is the input string and char 'a' is added in between each element" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "**join( )** function can also be used to convert a list into a string." 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 17, 447 | "metadata": {}, 448 | "outputs": [ 449 | { 450 | "name": "stdout", 451 | "output_type": "stream", 452 | "text": [ 453 | "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", 454 | "Taj Mahal is beautiful\n" 455 | ] 456 | } 457 | ], 458 | "source": [ 459 | "a = list(String0)\n", 460 | "print(a)\n", 461 | "b = ''.join(a)\n", 462 | "print(b)" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 18, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "name": "stdout", 479 | "output_type": "stream", 480 | "text": [ 481 | " /i/s/ /b/e/a/u/t/i/f/u/l\n" 482 | ] 483 | } 484 | ], 485 | "source": [ 486 | "c = '/'.join(a)[18:]\n", 487 | "print(c)" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 19, 500 | "metadata": {}, 501 | "outputs": [ 502 | { 503 | "name": "stdout", 504 | "output_type": "stream", 505 | "text": [ 506 | "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" 507 | ] 508 | } 509 | ], 510 | "source": [ 511 | "d = c.split('/')\n", 512 | "print(d)" 513 | ] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "metadata": {}, 518 | "source": [ 519 | "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." 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 20, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "name": "stdout", 529 | "output_type": "stream", 530 | "text": [ 531 | "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", 532 | "4\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "e = c.split('/',3)\n", 538 | "print(e)\n", 539 | "print(len(e))" 540 | ] 541 | }, 542 | { 543 | "cell_type": "markdown", 544 | "metadata": {}, 545 | "source": [ 546 | "**lower( )** converts any capital letter to small letter." 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 21, 552 | "metadata": {}, 553 | "outputs": [ 554 | { 555 | "name": "stdout", 556 | "output_type": "stream", 557 | "text": [ 558 | "Taj Mahal is beautiful\n", 559 | "taj mahal is beautiful\n" 560 | ] 561 | } 562 | ], 563 | "source": [ 564 | "print(String0)\n", 565 | "print(String0.lower())" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "**upper( )** converts any small letter to capital letter." 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": 22, 578 | "metadata": {}, 579 | "outputs": [ 580 | { 581 | "data": { 582 | "text/plain": [ 583 | "'TAJ MAHAL IS BEAUTIFUL'" 584 | ] 585 | }, 586 | "execution_count": 22, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "String0.upper()" 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "**replace( )** function replaces the element with another element." 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 23, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "'Bengaluru is beautiful'" 611 | ] 612 | }, 613 | "execution_count": 23, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [ 619 | "String0.replace('Taj Mahal','Bengaluru')" 620 | ] 621 | }, 622 | { 623 | "cell_type": "markdown", 624 | "metadata": {}, 625 | "source": [ 626 | "**strip( )** function is used to delete elements from the right end and the left end which is not required." 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 24, 632 | "metadata": {}, 633 | "outputs": [], 634 | "source": [ 635 | "f = ' hello '" 636 | ] 637 | }, 638 | { 639 | "cell_type": "markdown", 640 | "metadata": {}, 641 | "source": [ 642 | "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." 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 25, 648 | "metadata": {}, 649 | "outputs": [ 650 | { 651 | "data": { 652 | "text/plain": [ 653 | "'hello'" 654 | ] 655 | }, 656 | "execution_count": 25, 657 | "metadata": {}, 658 | "output_type": "execute_result" 659 | } 660 | ], 661 | "source": [ 662 | "f.strip()" 663 | ] 664 | }, 665 | { 666 | "cell_type": "markdown", 667 | "metadata": {}, 668 | "source": [ 669 | "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": 26, 675 | "metadata": {}, 676 | "outputs": [], 677 | "source": [ 678 | "f = ' ***----hello---******* '" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 27, 684 | "metadata": {}, 685 | "outputs": [ 686 | { 687 | "data": { 688 | "text/plain": [ 689 | "' ***----hello---******* '" 690 | ] 691 | }, 692 | "execution_count": 27, 693 | "metadata": {}, 694 | "output_type": "execute_result" 695 | } 696 | ], 697 | "source": [ 698 | "f.strip('*')" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "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." 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 28, 711 | "metadata": {}, 712 | "outputs": [ 713 | { 714 | "name": "stdout", 715 | "output_type": "stream", 716 | "text": [ 717 | "----hello---\n", 718 | "hello\n" 719 | ] 720 | } 721 | ], 722 | "source": [ 723 | "print(f.strip(' *'))\n", 724 | "print(f.strip(' *-'))" 725 | ] 726 | }, 727 | { 728 | "cell_type": "markdown", 729 | "metadata": {}, 730 | "source": [ 731 | "**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." 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 29, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "----hello---******* \n", 744 | " ***----hello---\n" 745 | ] 746 | } 747 | ], 748 | "source": [ 749 | "print(f.lstrip(' *'))\n", 750 | "print(f.rstrip(' *'))" 751 | ] 752 | }, 753 | { 754 | "cell_type": "markdown", 755 | "metadata": {}, 756 | "source": [ 757 | "## Dictionaries" 758 | ] 759 | }, 760 | { 761 | "cell_type": "markdown", 762 | "metadata": {}, 763 | "source": [ 764 | "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": {}, 770 | "source": [ 771 | "To define a dictionary, equate a variable to { } or dict()" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 30, 777 | "metadata": {}, 778 | "outputs": [ 779 | { 780 | "name": "stdout", 781 | "output_type": "stream", 782 | "text": [ 783 | " \n" 784 | ] 785 | } 786 | ], 787 | "source": [ 788 | "d0 = {}\n", 789 | "d1 = dict()\n", 790 | "print(type(d0), type(d1))" 791 | ] 792 | }, 793 | { 794 | "cell_type": "markdown", 795 | "metadata": {}, 796 | "source": [ 797 | "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 31, 803 | "metadata": {}, 804 | "outputs": [ 805 | { 806 | "name": "stdout", 807 | "output_type": "stream", 808 | "text": [ 809 | "{'One': 1, 'OneTwo': 12}\n" 810 | ] 811 | } 812 | ], 813 | "source": [ 814 | "d0['One'] = 1\n", 815 | "d0['OneTwo'] = 12 \n", 816 | "print(d0)" 817 | ] 818 | }, 819 | { 820 | "cell_type": "markdown", 821 | "metadata": {}, 822 | "source": [ 823 | "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" 824 | ] 825 | }, 826 | { 827 | "cell_type": "code", 828 | "execution_count": 32, 829 | "metadata": {}, 830 | "outputs": [ 831 | { 832 | "name": "stdout", 833 | "output_type": "stream", 834 | "text": [ 835 | "1\n" 836 | ] 837 | } 838 | ], 839 | "source": [ 840 | "print(d0['One'])" 841 | ] 842 | }, 843 | { 844 | "cell_type": "markdown", 845 | "metadata": {}, 846 | "source": [ 847 | "Two lists which are related can be merged to form a dictionary." 848 | ] 849 | }, 850 | { 851 | "cell_type": "code", 852 | "execution_count": 33, 853 | "metadata": {}, 854 | "outputs": [], 855 | "source": [ 856 | "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", 857 | "numbers = [1, 2, 3, 4, 5]" 858 | ] 859 | }, 860 | { 861 | "cell_type": "markdown", 862 | "metadata": {}, 863 | "source": [ 864 | "**zip( )** function is used to combine two lists" 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": 34, 870 | "metadata": {}, 871 | "outputs": [ 872 | { 873 | "name": "stdout", 874 | "output_type": "stream", 875 | "text": [ 876 | "\n" 877 | ] 878 | } 879 | ], 880 | "source": [ 881 | "d2 = zip(names,numbers)\n", 882 | "print(d2)" 883 | ] 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "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", 890 | "\n", 891 | "Further, To convert the above into a dictionary. **dict( )** function is used." 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": 35, 897 | "metadata": {}, 898 | "outputs": [ 899 | { 900 | "name": "stdout", 901 | "output_type": "stream", 902 | "text": [ 903 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 904 | ] 905 | } 906 | ], 907 | "source": [ 908 | "a1 = dict(d2)\n", 909 | "print(a1)" 910 | ] 911 | }, 912 | { 913 | "cell_type": "markdown", 914 | "metadata": {}, 915 | "source": [ 916 | "### Built-in Functions" 917 | ] 918 | }, 919 | { 920 | "cell_type": "markdown", 921 | "metadata": {}, 922 | "source": [ 923 | "**clear( )** function is used to erase the entire database that was created." 924 | ] 925 | }, 926 | { 927 | "cell_type": "code", 928 | "execution_count": 36, 929 | "metadata": {}, 930 | "outputs": [ 931 | { 932 | "name": "stdout", 933 | "output_type": "stream", 934 | "text": [ 935 | "{}\n" 936 | ] 937 | } 938 | ], 939 | "source": [ 940 | "a1.clear()\n", 941 | "print(a1)" 942 | ] 943 | }, 944 | { 945 | "cell_type": "markdown", 946 | "metadata": {}, 947 | "source": [ 948 | "Dictionary can also be built using loops." 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": 37, 954 | "metadata": {}, 955 | "outputs": [ 956 | { 957 | "name": "stdout", 958 | "output_type": "stream", 959 | "text": [ 960 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 961 | ] 962 | } 963 | ], 964 | "source": [ 965 | "for i in range(len(names)):\n", 966 | " a1[names[i]] = numbers[i]\n", 967 | "print(a1)" 968 | ] 969 | }, 970 | { 971 | "cell_type": "markdown", 972 | "metadata": {}, 973 | "source": [ 974 | "**values( )** function returns a list with all the assigned values in the dictionary." 975 | ] 976 | }, 977 | { 978 | "cell_type": "code", 979 | "execution_count": 38, 980 | "metadata": {}, 981 | "outputs": [ 982 | { 983 | "data": { 984 | "text/plain": [ 985 | "dict_values([1, 2, 3, 4, 5])" 986 | ] 987 | }, 988 | "execution_count": 38, 989 | "metadata": {}, 990 | "output_type": "execute_result" 991 | } 992 | ], 993 | "source": [ 994 | "a1.values()" 995 | ] 996 | }, 997 | { 998 | "cell_type": "markdown", 999 | "metadata": {}, 1000 | "source": [ 1001 | "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "code", 1006 | "execution_count": 39, 1007 | "metadata": {}, 1008 | "outputs": [ 1009 | { 1010 | "data": { 1011 | "text/plain": [ 1012 | "dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])" 1013 | ] 1014 | }, 1015 | "execution_count": 39, 1016 | "metadata": {}, 1017 | "output_type": "execute_result" 1018 | } 1019 | ], 1020 | "source": [ 1021 | "a1.keys()" 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "markdown", 1026 | "metadata": {}, 1027 | "source": [ 1028 | "**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." 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": 40, 1034 | "metadata": {}, 1035 | "outputs": [ 1036 | { 1037 | "data": { 1038 | "text/plain": [ 1039 | "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)])" 1040 | ] 1041 | }, 1042 | "execution_count": 40, 1043 | "metadata": {}, 1044 | "output_type": "execute_result" 1045 | } 1046 | ], 1047 | "source": [ 1048 | "a1.items()" 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "markdown", 1053 | "metadata": {}, 1054 | "source": [ 1055 | "**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." 1056 | ] 1057 | }, 1058 | { 1059 | "cell_type": "code", 1060 | "execution_count": 41, 1061 | "metadata": {}, 1062 | "outputs": [ 1063 | { 1064 | "name": "stdout", 1065 | "output_type": "stream", 1066 | "text": [ 1067 | "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}\n", 1068 | "4\n" 1069 | ] 1070 | } 1071 | ], 1072 | "source": [ 1073 | "a2 = a1.pop('Four')\n", 1074 | "print(a1)\n", 1075 | "print(a2)" 1076 | ] 1077 | } 1078 | ], 1079 | "metadata": { 1080 | "kernelspec": { 1081 | "display_name": "Python 3", 1082 | "language": "python", 1083 | "name": "python3" 1084 | }, 1085 | "language_info": { 1086 | "codemirror_mode": { 1087 | "name": "ipython", 1088 | "version": 3 1089 | }, 1090 | "file_extension": ".py", 1091 | "mimetype": "text/x-python", 1092 | "name": "python", 1093 | "nbconvert_exporter": "python", 1094 | "pygments_lexer": "ipython3", 1095 | "version": "3.6.2" 1096 | } 1097 | }, 1098 | "nbformat": 4, 1099 | "nbformat_minor": 1 1100 | } 1101 | -------------------------------------------------------------------------------- /05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Control Flow Statements" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## If" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "if some_condition:\n", 22 | " \n", 23 | " algorithm" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "x = 12\n", 41 | "if x >10:\n", 42 | " print(\"Hello\")" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## If-else" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "if some_condition:\n", 57 | " \n", 58 | " algorithm\n", 59 | " \n", 60 | "else:\n", 61 | " \n", 62 | " algorithm" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "hello\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "x = 12\n", 80 | "if x > 10:\n", 81 | " print(\"hello\")\n", 82 | "else:\n", 83 | " print(\"world\")" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "## if-elif" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "if some_condition:\n", 98 | " \n", 99 | " algorithm\n", 100 | "\n", 101 | "elif some_condition:\n", 102 | " \n", 103 | " algorithm\n", 104 | "\n", 105 | "else:\n", 106 | " \n", 107 | " algorithm" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 3, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "x y:\n", 127 | " print(\"x>y\")\n", 128 | "elif x < y:\n", 129 | " print(\"x y:\n", 159 | " print(\"x>y\")\n", 160 | "elif x < y:\n", 161 | " print(\"x=7:\n", 372 | " break" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "## Continue" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "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. " 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 10, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "0\n", 399 | "1\n", 400 | "2\n", 401 | "3\n", 402 | "4\n", 403 | "The end.\n", 404 | "The end.\n", 405 | "The end.\n", 406 | "The end.\n", 407 | "The end.\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "for i in range(10):\n", 413 | " if i>4:\n", 414 | " print(\"The end.\")\n", 415 | " continue\n", 416 | " elif i<7:\n", 417 | " print(i)" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "## List Comprehensions" 425 | ] 426 | }, 427 | { 428 | "cell_type": "markdown", 429 | "metadata": {}, 430 | "source": [ 431 | "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," 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 11, 437 | "metadata": {}, 438 | "outputs": [ 439 | { 440 | "name": "stdout", 441 | "output_type": "stream", 442 | "text": [ 443 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" 444 | ] 445 | } 446 | ], 447 | "source": [ 448 | "res = []\n", 449 | "for i in range(1,11):\n", 450 | " x = 27*i\n", 451 | " res.append(x)\n", 452 | "print(res)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem." 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 12, 465 | "metadata": {}, 466 | "outputs": [ 467 | { 468 | "data": { 469 | "text/plain": [ 470 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 471 | ] 472 | }, 473 | "execution_count": 12, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "[27*x for x in range(1,11)]" 480 | ] 481 | }, 482 | { 483 | "cell_type": "markdown", 484 | "metadata": {}, 485 | "source": [ 486 | "That's it!. Only remember to enclose it in square brackets" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "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." 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 13, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "data": { 503 | "text/plain": [ 504 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 505 | ] 506 | }, 507 | "execution_count": 13, 508 | "metadata": {}, 509 | "output_type": "execute_result" 510 | } 511 | ], 512 | "source": [ 513 | "[27*x for x in range(1,20) if x<=10]" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "Let me add one more loop to make you understand better, " 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 14, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "data": { 530 | "text/plain": [ 531 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 532 | ] 533 | }, 534 | "execution_count": 14, 535 | "metadata": {}, 536 | "output_type": "execute_result" 537 | } 538 | ], 539 | "source": [ 540 | "[27*z for i in range(50) if i==27 for z in range(1,11)]" 541 | ] 542 | } 543 | ], 544 | "metadata": { 545 | "kernelspec": { 546 | "display_name": "Python 3", 547 | "language": "python", 548 | "name": "python3" 549 | }, 550 | "language_info": { 551 | "codemirror_mode": { 552 | "name": "ipython", 553 | "version": 3 554 | }, 555 | "file_extension": ".py", 556 | "mimetype": "text/x-python", 557 | "name": "python", 558 | "nbconvert_exporter": "python", 559 | "pygments_lexer": "ipython3", 560 | "version": "3.6.2" 561 | } 562 | }, 563 | "nbformat": 4, 564 | "nbformat_minor": 1 565 | } 566 | -------------------------------------------------------------------------------- /06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "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." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "This is the basic syntax of a function" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "def funcname(arg1, arg2,... argN):\n", 29 | " \n", 30 | " ''' Document String'''\n", 31 | "\n", 32 | " statements\n", 33 | "\n", 34 | "\n", 35 | " return " 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "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\"." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 1, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "Hey Rajath!\n", 55 | "Rajath, How do you do?\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "print(\"Hey Rajath!\")\n", 61 | "print(\"Rajath, How do you do?\")" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "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", 69 | "\n", 70 | "Defining a function firstfunc()." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "def firstfunc():\n", 80 | " print(\"Hey Rajath!\")\n", 81 | " print(\"Rajath, How do you do?\") " 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 3, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "Hey Rajath!\n", 94 | "Rajath, How do you do?\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "firstfunc()" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "**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." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 4, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "Please enter your name : dua lipa\n", 119 | "Hey dua lipa!\n", 120 | "dua lipa, How do you do?\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "name1 = input('Please enter your name : ')\n", 126 | "\n", 127 | "def firstfunc(username):\n", 128 | " print(\"Hey\", username + '!')\n", 129 | " print(username + ',' ,\"How do you do?\")\n", 130 | "\n", 131 | "firstfunc(name1)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "The name \"Guido\" 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." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 5, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "Hey dua lipa!\n", 151 | "dua lipa, How do you do?\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "firstfunc(name1)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "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." 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 8, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "def firstfunc(username):\n", 173 | " print(\"Hey\", username + '!')\n", 174 | " print(username + ',' ,\"How do you do?\")\n", 175 | "def secondfunc():\n", 176 | " name = input(\"Please enter your name : \")\n", 177 | " firstfunc(name)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Please enter your name : starboy\n", 190 | "Hey starboy!\n", 191 | "starboy, How do you do?\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "secondfunc()" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "## Return Statement" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "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." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 10, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "def times(x,y):\n", 220 | " z = x*y\n", 221 | " return z" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 11, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "20\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "c = times(4,5)\n", 246 | "print(c)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "The z value is stored in variable c and can be used for further operations." 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "Instead of declaring another variable the entire statement itself can be used in the return statement as shown." 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 12, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "def times(x,y):\n", 270 | " '''This multiplies the two input arguments'''\n", 271 | " return x*y" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 13, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "name": "stdout", 281 | "output_type": "stream", 282 | "text": [ 283 | "20\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "c = times(4,5)\n", 289 | "print(c)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "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." 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 14, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "Help on function times in module __main__:\n", 309 | "\n", 310 | "times(x, y)\n", 311 | " This multiplies the two input arguments\n", 312 | "\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "help(times)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "Multiple variable can also be returned, But keep in mind the order." 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 15, 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [ 333 | "eglist = [10,50,30,12,6,8,100]" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 16, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "def egfunc(eglist):\n", 343 | " highest = max(eglist)\n", 344 | " lowest = min(eglist)\n", 345 | " first = eglist[0]\n", 346 | " last = eglist[-1]\n", 347 | " return highest,lowest,first,last" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": {}, 353 | "source": [ 354 | "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." 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 17, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "data": { 364 | "text/plain": [ 365 | "(100, 6, 10, 100)" 366 | ] 367 | }, 368 | "execution_count": 17, 369 | "metadata": {}, 370 | "output_type": "execute_result" 371 | } 372 | ], 373 | "source": [ 374 | "egfunc(eglist)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 18, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "name": "stdout", 384 | "output_type": "stream", 385 | "text": [ 386 | " a = 100 \n", 387 | " b = 6 \n", 388 | " c = 10 \n", 389 | " d = 100\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "a,b,c,d = egfunc(eglist)\n", 395 | "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": {}, 401 | "source": [ 402 | "## Implicit arguments" 403 | ] 404 | }, 405 | { 406 | "cell_type": "markdown", 407 | "metadata": {}, 408 | "source": [ 409 | "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 19, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [ 418 | "def implicitadd(x,y=3):\n", 419 | " return x+y" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "**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." 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "metadata": {}, 432 | "source": [ 433 | "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 20, 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "data": { 443 | "text/plain": [ 444 | "7" 445 | ] 446 | }, 447 | "execution_count": 20, 448 | "metadata": {}, 449 | "output_type": "execute_result" 450 | } 451 | ], 452 | "source": [ 453 | "implicitadd(4)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "But if the second argument is specified then this value overrides the implicit value assigned to the argument " 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 21, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "8" 472 | ] 473 | }, 474 | "execution_count": 21, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "implicitadd(4,4)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": {}, 486 | "source": [ 487 | "## Any number of arguments" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "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." 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 22, 500 | "metadata": {}, 501 | "outputs": [], 502 | "source": [ 503 | "def add_n(*args):\n", 504 | " res = 0\n", 505 | " reslist = []\n", 506 | " for i in args:\n", 507 | " reslist.append(i)\n", 508 | " print(reslist)\n", 509 | " return sum(reslist)" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": {}, 515 | "source": [ 516 | "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." 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 23, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "name": "stdout", 526 | "output_type": "stream", 527 | "text": [ 528 | "[1, 2, 3, 4, 5]\n" 529 | ] 530 | }, 531 | { 532 | "data": { 533 | "text/plain": [ 534 | "15" 535 | ] 536 | }, 537 | "execution_count": 23, 538 | "metadata": {}, 539 | "output_type": "execute_result" 540 | } 541 | ], 542 | "source": [ 543 | "add_n(1,2,3,4,5)" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 24, 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "name": "stdout", 553 | "output_type": "stream", 554 | "text": [ 555 | "[1, 2, 3]\n" 556 | ] 557 | }, 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "6" 562 | ] 563 | }, 564 | "execution_count": 24, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "add_n(1,2,3)" 571 | ] 572 | }, 573 | { 574 | "cell_type": "markdown", 575 | "metadata": {}, 576 | "source": [ 577 | "## Global and Local Variables" 578 | ] 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "metadata": {}, 583 | "source": [ 584 | "Whatever variable is declared inside a function is local variable and outside the function in global variable." 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 25, 590 | "metadata": {}, 591 | "outputs": [], 592 | "source": [ 593 | "eg1 = [1,2,3,4,5]" 594 | ] 595 | }, 596 | { 597 | "cell_type": "markdown", 598 | "metadata": {}, 599 | "source": [ 600 | "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." 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 26, 606 | "metadata": {}, 607 | "outputs": [], 608 | "source": [ 609 | "def egfunc1():\n", 610 | " def thirdfunc(arg1):\n", 611 | " eg2 = arg1[:]\n", 612 | " eg2.append(6)\n", 613 | " print(\"This is happening inside the function :\", eg2) \n", 614 | " print(\"This is happening before the function is called : \", eg1)\n", 615 | " thirdfunc(eg1)\n", 616 | " print(\"This is happening outside the function :\", eg1) \n", 617 | " print(\"Accessing a variable declared inside the function from outside :\" , eg2)" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 27, 623 | "metadata": {}, 624 | "outputs": [ 625 | { 626 | "name": "stdout", 627 | "output_type": "stream", 628 | "text": [ 629 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 630 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 631 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 632 | ] 633 | }, 634 | { 635 | "ename": "NameError", 636 | "evalue": "name 'eg2' is not defined", 637 | "output_type": "error", 638 | "traceback": [ 639 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 640 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 641 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0megfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 642 | "\u001b[1;32m\u001b[0m in \u001b[0;36megfunc1\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mthirdfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"This is happening outside the function :\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Accessing a variable declared inside the function from outside :\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 643 | "\u001b[1;31mNameError\u001b[0m: name 'eg2' is not defined" 644 | ] 645 | } 646 | ], 647 | "source": [ 648 | "egfunc1()" 649 | ] 650 | }, 651 | { 652 | "cell_type": "markdown", 653 | "metadata": {}, 654 | "source": [ 655 | "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 28, 661 | "metadata": {}, 662 | "outputs": [], 663 | "source": [ 664 | "eg3 = [1,2,3,4,5]" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 29, 670 | "metadata": {}, 671 | "outputs": [], 672 | "source": [ 673 | "def egfunc1():\n", 674 | " def thirdfunc(arg1):\n", 675 | " global eg2\n", 676 | " eg2 = arg1[:]\n", 677 | " eg2.append(6)\n", 678 | " print(\"This is happening inside the function :\", eg2) \n", 679 | " print(\"This is happening before the function is called : \", eg1)\n", 680 | " thirdfunc(eg1)\n", 681 | " print(\"This is happening outside the function :\", eg1) \n", 682 | " print(\"Accessing a variable declared inside the function from outside :\" , eg2)" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 30, 688 | "metadata": {}, 689 | "outputs": [ 690 | { 691 | "name": "stdout", 692 | "output_type": "stream", 693 | "text": [ 694 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 695 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 696 | "This is happening outside the function : [1, 2, 3, 4, 5]\n", 697 | "Accessing a variable declared inside the function from outside : [1, 2, 3, 4, 5, 6]\n" 698 | ] 699 | } 700 | ], 701 | "source": [ 702 | "egfunc1()" 703 | ] 704 | }, 705 | { 706 | "cell_type": "markdown", 707 | "metadata": {}, 708 | "source": [ 709 | "## Lambda Functions" 710 | ] 711 | }, 712 | { 713 | "cell_type": "markdown", 714 | "metadata": {}, 715 | "source": [ 716 | "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." 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": 31, 722 | "metadata": {}, 723 | "outputs": [], 724 | "source": [ 725 | "z = lambda x: x * x" 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": 32, 731 | "metadata": {}, 732 | "outputs": [ 733 | { 734 | "data": { 735 | "text/plain": [ 736 | "64" 737 | ] 738 | }, 739 | "execution_count": 32, 740 | "metadata": {}, 741 | "output_type": "execute_result" 742 | } 743 | ], 744 | "source": [ 745 | "z(8)" 746 | ] 747 | }, 748 | { 749 | "cell_type": "markdown", 750 | "metadata": {}, 751 | "source": [ 752 | "### map" 753 | ] 754 | }, 755 | { 756 | "cell_type": "markdown", 757 | "metadata": {}, 758 | "source": [ 759 | "**map( )** function basically executes the function that is defined to each of the list's element separately." 760 | ] 761 | }, 762 | { 763 | "cell_type": "code", 764 | "execution_count": 33, 765 | "metadata": {}, 766 | "outputs": [], 767 | "source": [ 768 | "list1 = [1,2,3,4,5,6,7,8,9]" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 34, 774 | "metadata": {}, 775 | "outputs": [ 776 | { 777 | "name": "stdout", 778 | "output_type": "stream", 779 | "text": [ 780 | "\n" 781 | ] 782 | } 783 | ], 784 | "source": [ 785 | "eg = map(lambda x:x+2, list1)\n", 786 | "print(eg)" 787 | ] 788 | }, 789 | { 790 | "cell_type": "markdown", 791 | "metadata": {}, 792 | "source": [ 793 | "You can also add two lists." 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "execution_count": 35, 799 | "metadata": {}, 800 | "outputs": [], 801 | "source": [ 802 | "list2 = [9,8,7,6,5,4,3,2,1]" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 36, 808 | "metadata": {}, 809 | "outputs": [ 810 | { 811 | "name": "stdout", 812 | "output_type": "stream", 813 | "text": [ 814 | "\n" 815 | ] 816 | } 817 | ], 818 | "source": [ 819 | "eg2 = map(lambda x,y:x+y, list1,list2)\n", 820 | "print(eg2)" 821 | ] 822 | }, 823 | { 824 | "cell_type": "markdown", 825 | "metadata": {}, 826 | "source": [ 827 | "Not only lambda function but also other built in functions can also be used." 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": 37, 833 | "metadata": {}, 834 | "outputs": [ 835 | { 836 | "name": "stdout", 837 | "output_type": "stream", 838 | "text": [ 839 | "\n" 840 | ] 841 | } 842 | ], 843 | "source": [ 844 | "eg3 = map(str,eg2)\n", 845 | "print(eg3)" 846 | ] 847 | }, 848 | { 849 | "cell_type": "markdown", 850 | "metadata": {}, 851 | "source": [ 852 | "### filter" 853 | ] 854 | }, 855 | { 856 | "cell_type": "markdown", 857 | "metadata": {}, 858 | "source": [ 859 | "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": 38, 865 | "metadata": {}, 866 | "outputs": [], 867 | "source": [ 868 | "list1 = [1,2,3,4,5,6,7,8,9]" 869 | ] 870 | }, 871 | { 872 | "cell_type": "markdown", 873 | "metadata": {}, 874 | "source": [ 875 | "To get the elements which are less than 5," 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": 39, 881 | "metadata": {}, 882 | "outputs": [ 883 | { 884 | "data": { 885 | "text/plain": [ 886 | "" 887 | ] 888 | }, 889 | "execution_count": 39, 890 | "metadata": {}, 891 | "output_type": "execute_result" 892 | } 893 | ], 894 | "source": [ 895 | "filter(lambda x:x<5,list1)" 896 | ] 897 | }, 898 | { 899 | "cell_type": "markdown", 900 | "metadata": {}, 901 | "source": [ 902 | "Notice what happens when **map()** is used." 903 | ] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "execution_count": 40, 908 | "metadata": {}, 909 | "outputs": [ 910 | { 911 | "data": { 912 | "text/plain": [ 913 | "" 914 | ] 915 | }, 916 | "execution_count": 40, 917 | "metadata": {}, 918 | "output_type": "execute_result" 919 | } 920 | ], 921 | "source": [ 922 | "map(lambda x:x<5, list1)" 923 | ] 924 | }, 925 | { 926 | "cell_type": "markdown", 927 | "metadata": {}, 928 | "source": [ 929 | "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 41, 935 | "metadata": {}, 936 | "outputs": [ 937 | { 938 | "data": { 939 | "text/plain": [ 940 | "" 941 | ] 942 | }, 943 | "execution_count": 41, 944 | "metadata": {}, 945 | "output_type": "execute_result" 946 | } 947 | ], 948 | "source": [ 949 | "filter(lambda x:x%4==0,list1)" 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "execution_count": null, 955 | "metadata": {}, 956 | "outputs": [], 957 | "source": [] 958 | } 959 | ], 960 | "metadata": { 961 | "kernelspec": { 962 | "display_name": "Python 3", 963 | "language": "python", 964 | "name": "python3" 965 | }, 966 | "language_info": { 967 | "codemirror_mode": { 968 | "name": "ipython", 969 | "version": 3 970 | }, 971 | "file_extension": ".py", 972 | "mimetype": "text/x-python", 973 | "name": "python", 974 | "nbconvert_exporter": "python", 975 | "pygments_lexer": "ipython3", 976 | "version": "3.6.2" 977 | } 978 | }, 979 | "nbformat": 4, 980 | "nbformat_minor": 1 981 | } 982 | -------------------------------------------------------------------------------- /07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Classes" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "A class is declared as follows" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "class class_name:\n", 29 | "\n", 30 | " Functions" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "class FirstClass:\n", 40 | " pass" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "**pass** in python means do nothing. " 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Above, a class object named \"FirstClass\" is declared now consider a \"egclass\" which has all the characteristics of \"FirstClass\". So all you have to do is, equate the \"egclass\" to \"FirstClass\". In python jargon this is called as creating an instance. \"egclass\" is the instance of \"FirstClass\"" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 2, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "egclass = FirstClass()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "__main__.FirstClass" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "type(egclass)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "type" 95 | ] 96 | }, 97 | "execution_count": 4, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "type(FirstClass)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "Now let us add some \"functionality\" to the class. So that our \"FirstClass\" is defined in a better way. A function inside a class is called as a \"Method\" of that class" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Most of the classes will have a function named \"\\_\\_init\\_\\_\". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute." 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "These helps simplify the process of initializing a instance. For example,\n", 125 | "\n", 126 | "Without the use of magic method or \\_\\_init\\_\\_ which is otherwise called as constructors. One had to define a **init( )** method and call the **init( )** function." 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 6, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "eg0 = FirstClass()\n", 136 | "eg0.__init__()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "But when the constructor is defined the \\_\\_init\\_\\_ is called thus intializing the instance created. " 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "We will make our \"FirstClass\" to accept two variables name and symbol.\n", 151 | "\n", 152 | "I will be explaining about the \"self\" in a while." 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 7, 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [ 161 | "class FirstClass:\n", 162 | " def __init__(self,name,symbol):\n", 163 | " self.name = name\n", 164 | " self.symbol = symbol" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Now that we have defined a function and added the \\_\\_init\\_\\_ method. We can create a instance of FirstClass which now accepts two arguments. " 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 8, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "eg1 = FirstClass('one',1)\n", 181 | "eg2 = FirstClass('two',2)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 9, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "one 1\n", 194 | "two 2\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "print (eg1.name, eg1.symbol)\n", 200 | "print (eg2.name, eg2.symbol)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "**dir( )** function comes very handy in looking into what the class contains and what all method it offers" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 10, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "['__class__',\n", 219 | " '__delattr__',\n", 220 | " '__dict__',\n", 221 | " '__dir__',\n", 222 | " '__doc__',\n", 223 | " '__eq__',\n", 224 | " '__format__',\n", 225 | " '__ge__',\n", 226 | " '__getattribute__',\n", 227 | " '__gt__',\n", 228 | " '__hash__',\n", 229 | " '__init__',\n", 230 | " '__init_subclass__',\n", 231 | " '__le__',\n", 232 | " '__lt__',\n", 233 | " '__module__',\n", 234 | " '__ne__',\n", 235 | " '__new__',\n", 236 | " '__reduce__',\n", 237 | " '__reduce_ex__',\n", 238 | " '__repr__',\n", 239 | " '__setattr__',\n", 240 | " '__sizeof__',\n", 241 | " '__str__',\n", 242 | " '__subclasshook__',\n", 243 | " '__weakref__']" 244 | ] 245 | }, 246 | "execution_count": 10, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "dir(FirstClass)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "**dir( )** of an instance also shows it's defined attributes." 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 11, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "['__class__',\n", 271 | " '__delattr__',\n", 272 | " '__dict__',\n", 273 | " '__dir__',\n", 274 | " '__doc__',\n", 275 | " '__eq__',\n", 276 | " '__format__',\n", 277 | " '__ge__',\n", 278 | " '__getattribute__',\n", 279 | " '__gt__',\n", 280 | " '__hash__',\n", 281 | " '__init__',\n", 282 | " '__init_subclass__',\n", 283 | " '__le__',\n", 284 | " '__lt__',\n", 285 | " '__module__',\n", 286 | " '__ne__',\n", 287 | " '__new__',\n", 288 | " '__reduce__',\n", 289 | " '__reduce_ex__',\n", 290 | " '__repr__',\n", 291 | " '__setattr__',\n", 292 | " '__sizeof__',\n", 293 | " '__str__',\n", 294 | " '__subclasshook__',\n", 295 | " '__weakref__',\n", 296 | " 'name',\n", 297 | " 'symbol']" 298 | ] 299 | }, 300 | "execution_count": 11, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "dir(eg1)" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Changing the FirstClass function a bit," 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 12, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "class FirstClass:\n", 323 | " def __init__(self,name,symbol):\n", 324 | " self.n = name\n", 325 | " self.s = symbol" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "Changing self.name and self.symbol to self.n and self.s respectively will yield," 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 13, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "eg1 = FirstClass('one',1)\n", 342 | "eg2 = FirstClass('two',2)" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 17, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "one 1\n", 355 | "two 2\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "print (eg1.n, eg1.s)\n", 361 | "print (eg2.n, eg2.s)" 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": {}, 367 | "source": [ 368 | "AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance." 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 18, 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "data": { 378 | "text/plain": [ 379 | "['__class__',\n", 380 | " '__delattr__',\n", 381 | " '__dict__',\n", 382 | " '__dir__',\n", 383 | " '__doc__',\n", 384 | " '__eq__',\n", 385 | " '__format__',\n", 386 | " '__ge__',\n", 387 | " '__getattribute__',\n", 388 | " '__gt__',\n", 389 | " '__hash__',\n", 390 | " '__init__',\n", 391 | " '__init_subclass__',\n", 392 | " '__le__',\n", 393 | " '__lt__',\n", 394 | " '__module__',\n", 395 | " '__ne__',\n", 396 | " '__new__',\n", 397 | " '__reduce__',\n", 398 | " '__reduce_ex__',\n", 399 | " '__repr__',\n", 400 | " '__setattr__',\n", 401 | " '__sizeof__',\n", 402 | " '__str__',\n", 403 | " '__subclasshook__',\n", 404 | " '__weakref__',\n", 405 | " 'n',\n", 406 | " 's']" 407 | ] 408 | }, 409 | "execution_count": 18, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "dir(eg1)" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 19, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "name": "stdout", 425 | "output_type": "stream", 426 | "text": [ 427 | "one 1\n", 428 | "two 2\n" 429 | ] 430 | } 431 | ], 432 | "source": [ 433 | "print (eg1.n, eg1.s)\n", 434 | "print (eg2.n, eg2.s)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "So now we have solved the error. Now let us compare the two examples that we saw.\n", 442 | "\n", 443 | "When I declared self.name and self.symbol, there was no attribute error for eg1.name and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s\n", 444 | "\n", 445 | "From the above we can conclude that self is nothing but the instance itself.\n", 446 | "\n", 447 | "Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self." 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 20, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "class FirstClass:\n", 457 | " def __init__(asdf1234,name,symbol):\n", 458 | " asdf1234.n = name\n", 459 | " asdf1234.s = symbol" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 21, 465 | "metadata": {}, 466 | "outputs": [], 467 | "source": [ 468 | "eg1 = FirstClass('one',1)\n", 469 | "eg2 = FirstClass('two',2)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 22, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "name": "stdout", 479 | "output_type": "stream", 480 | "text": [ 481 | "one 1\n", 482 | "two 2\n" 483 | ] 484 | } 485 | ], 486 | "source": [ 487 | "print (eg1.n, eg1.s)\n", 488 | "print (eg2.n, eg2.s)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "markdown", 493 | "metadata": {}, 494 | "source": [ 495 | "Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass." 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 23, 501 | "metadata": {}, 502 | "outputs": [], 503 | "source": [ 504 | "eg1.cube = 1\n", 505 | "eg2.cube = 8" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 24, 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "data": { 515 | "text/plain": [ 516 | "['__class__',\n", 517 | " '__delattr__',\n", 518 | " '__dict__',\n", 519 | " '__dir__',\n", 520 | " '__doc__',\n", 521 | " '__eq__',\n", 522 | " '__format__',\n", 523 | " '__ge__',\n", 524 | " '__getattribute__',\n", 525 | " '__gt__',\n", 526 | " '__hash__',\n", 527 | " '__init__',\n", 528 | " '__init_subclass__',\n", 529 | " '__le__',\n", 530 | " '__lt__',\n", 531 | " '__module__',\n", 532 | " '__ne__',\n", 533 | " '__new__',\n", 534 | " '__reduce__',\n", 535 | " '__reduce_ex__',\n", 536 | " '__repr__',\n", 537 | " '__setattr__',\n", 538 | " '__sizeof__',\n", 539 | " '__str__',\n", 540 | " '__subclasshook__',\n", 541 | " '__weakref__',\n", 542 | " 'cube',\n", 543 | " 'n',\n", 544 | " 's']" 545 | ] 546 | }, 547 | "execution_count": 24, 548 | "metadata": {}, 549 | "output_type": "execute_result" 550 | } 551 | ], 552 | "source": [ 553 | "dir(eg1)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "markdown", 558 | "metadata": { 559 | "collapsed": true 560 | }, 561 | "source": [ 562 | "Just like global and local variables as we saw earlier, even classes have it's own types of variables.\n", 563 | "\n", 564 | "Class Attribute : attributes defined outside the method and is applicable to all the instances.\n", 565 | "\n", 566 | "Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance." 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 25, 572 | "metadata": {}, 573 | "outputs": [], 574 | "source": [ 575 | "class FirstClass:\n", 576 | " test = 'test'\n", 577 | " def __init__(self,name,symbol):\n", 578 | " self.name = name\n", 579 | " self.symbol = symbol" 580 | ] 581 | }, 582 | { 583 | "cell_type": "markdown", 584 | "metadata": {}, 585 | "source": [ 586 | "Here test is a class attribute and name is a instance attribute." 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 28, 592 | "metadata": {}, 593 | "outputs": [], 594 | "source": [ 595 | "eg3 = FirstClass('Three',3)" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 30, 601 | "metadata": {}, 602 | "outputs": [ 603 | { 604 | "name": "stdout", 605 | "output_type": "stream", 606 | "text": [ 607 | "test Three\n" 608 | ] 609 | } 610 | ], 611 | "source": [ 612 | "print (eg3.test, eg3.name)" 613 | ] 614 | }, 615 | { 616 | "cell_type": "markdown", 617 | "metadata": {}, 618 | "source": [ 619 | "Let us add some more methods to FirstClass." 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 31, 625 | "metadata": {}, 626 | "outputs": [], 627 | "source": [ 628 | "class FirstClass:\n", 629 | " def __init__(self,name,symbol):\n", 630 | " self.name = name\n", 631 | " self.symbol = symbol\n", 632 | " def square(self):\n", 633 | " return self.symbol * self.symbol\n", 634 | " def cube(self):\n", 635 | " return self.symbol * self.symbol * self.symbol\n", 636 | " def multiply(self, x):\n", 637 | " return self.symbol * x" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": 32, 643 | "metadata": {}, 644 | "outputs": [], 645 | "source": [ 646 | "eg4 = FirstClass('Five',5)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 33, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "name": "stdout", 656 | "output_type": "stream", 657 | "text": [ 658 | "25\n", 659 | "125\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "print (eg4.square())\n", 665 | "print (eg4.cube())" 666 | ] 667 | }, 668 | { 669 | "cell_type": "code", 670 | "execution_count": 34, 671 | "metadata": {}, 672 | "outputs": [ 673 | { 674 | "data": { 675 | "text/plain": [ 676 | "10" 677 | ] 678 | }, 679 | "execution_count": 34, 680 | "metadata": {}, 681 | "output_type": "execute_result" 682 | } 683 | ], 684 | "source": [ 685 | "eg4.multiply(2)" 686 | ] 687 | }, 688 | { 689 | "cell_type": "markdown", 690 | "metadata": {}, 691 | "source": [ 692 | "The above can also be written as," 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 35, 698 | "metadata": {}, 699 | "outputs": [ 700 | { 701 | "data": { 702 | "text/plain": [ 703 | "10" 704 | ] 705 | }, 706 | "execution_count": 35, 707 | "metadata": {}, 708 | "output_type": "execute_result" 709 | } 710 | ], 711 | "source": [ 712 | "FirstClass.multiply(eg4,2)" 713 | ] 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": {}, 718 | "source": [ 719 | "## Inheritance" 720 | ] 721 | }, 722 | { 723 | "cell_type": "markdown", 724 | "metadata": {}, 725 | "source": [ 726 | "There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can \"inherit\" the previous class and add it's own methods to it. This is called as inheritance." 727 | ] 728 | }, 729 | { 730 | "cell_type": "markdown", 731 | "metadata": {}, 732 | "source": [ 733 | "Consider class SoftwareEngineer which has a method salary." 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": 37, 739 | "metadata": {}, 740 | "outputs": [], 741 | "source": [ 742 | "class SoftwareEngineer:\n", 743 | " def __init__(self,name,age):\n", 744 | " self.name = name\n", 745 | " self.age = age\n", 746 | " def salary(self, value):\n", 747 | " self.money = value\n", 748 | " print (self.name,\"earns\",self.money)" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 38, 754 | "metadata": {}, 755 | "outputs": [], 756 | "source": [ 757 | "a = SoftwareEngineer('Kartik',26)" 758 | ] 759 | }, 760 | { 761 | "cell_type": "code", 762 | "execution_count": 39, 763 | "metadata": {}, 764 | "outputs": [ 765 | { 766 | "name": "stdout", 767 | "output_type": "stream", 768 | "text": [ 769 | "Kartik earns 40000\n" 770 | ] 771 | } 772 | ], 773 | "source": [ 774 | "a.salary(40000)" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 40, 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "data": { 784 | "text/plain": [ 785 | "['__class__',\n", 786 | " '__delattr__',\n", 787 | " '__dict__',\n", 788 | " '__dir__',\n", 789 | " '__doc__',\n", 790 | " '__eq__',\n", 791 | " '__format__',\n", 792 | " '__ge__',\n", 793 | " '__getattribute__',\n", 794 | " '__gt__',\n", 795 | " '__hash__',\n", 796 | " '__init__',\n", 797 | " '__init_subclass__',\n", 798 | " '__le__',\n", 799 | " '__lt__',\n", 800 | " '__module__',\n", 801 | " '__ne__',\n", 802 | " '__new__',\n", 803 | " '__reduce__',\n", 804 | " '__reduce_ex__',\n", 805 | " '__repr__',\n", 806 | " '__setattr__',\n", 807 | " '__sizeof__',\n", 808 | " '__str__',\n", 809 | " '__subclasshook__',\n", 810 | " '__weakref__',\n", 811 | " 'salary']" 812 | ] 813 | }, 814 | "execution_count": 40, 815 | "metadata": {}, 816 | "output_type": "execute_result" 817 | } 818 | ], 819 | "source": [ 820 | "dir(SoftwareEngineer)" 821 | ] 822 | }, 823 | { 824 | "cell_type": "markdown", 825 | "metadata": {}, 826 | "source": [ 827 | "Now consider another class Artist which tells us about the amount of money an artist earns and his artform." 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": 41, 833 | "metadata": {}, 834 | "outputs": [], 835 | "source": [ 836 | "class Artist:\n", 837 | " def __init__(self,name,age):\n", 838 | " self.name = name\n", 839 | " self.age = age\n", 840 | " def money(self,value):\n", 841 | " self.money = value\n", 842 | " print (self.name,\"earns\",self.money)\n", 843 | " def artform(self, job):\n", 844 | " self.job = job\n", 845 | " print (self.name,\"is a\", self.job)" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 42, 851 | "metadata": {}, 852 | "outputs": [], 853 | "source": [ 854 | "b = Artist('Nadir Ali',20)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 43, 860 | "metadata": {}, 861 | "outputs": [ 862 | { 863 | "name": "stdout", 864 | "output_type": "stream", 865 | "text": [ 866 | "Nadir Ali earns 50000\n", 867 | "Nadir Ali is a Prankster\n" 868 | ] 869 | } 870 | ], 871 | "source": [ 872 | "b.money(50000)\n", 873 | "b.artform('Prankster')" 874 | ] 875 | }, 876 | { 877 | "cell_type": "code", 878 | "execution_count": 44, 879 | "metadata": {}, 880 | "outputs": [ 881 | { 882 | "data": { 883 | "text/plain": [ 884 | "['__class__',\n", 885 | " '__delattr__',\n", 886 | " '__dict__',\n", 887 | " '__dir__',\n", 888 | " '__doc__',\n", 889 | " '__eq__',\n", 890 | " '__format__',\n", 891 | " '__ge__',\n", 892 | " '__getattribute__',\n", 893 | " '__gt__',\n", 894 | " '__hash__',\n", 895 | " '__init__',\n", 896 | " '__init_subclass__',\n", 897 | " '__le__',\n", 898 | " '__lt__',\n", 899 | " '__module__',\n", 900 | " '__ne__',\n", 901 | " '__new__',\n", 902 | " '__reduce__',\n", 903 | " '__reduce_ex__',\n", 904 | " '__repr__',\n", 905 | " '__setattr__',\n", 906 | " '__sizeof__',\n", 907 | " '__str__',\n", 908 | " '__subclasshook__',\n", 909 | " '__weakref__',\n", 910 | " 'artform',\n", 911 | " 'money']" 912 | ] 913 | }, 914 | "execution_count": 44, 915 | "metadata": {}, 916 | "output_type": "execute_result" 917 | } 918 | ], 919 | "source": [ 920 | "dir(Artist)" 921 | ] 922 | }, 923 | { 924 | "cell_type": "markdown", 925 | "metadata": {}, 926 | "source": [ 927 | "money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes," 928 | ] 929 | }, 930 | { 931 | "cell_type": "code", 932 | "execution_count": 45, 933 | "metadata": {}, 934 | "outputs": [], 935 | "source": [ 936 | "class Artist(SoftwareEngineer):\n", 937 | " def artform(self, job):\n", 938 | " self.job = job\n", 939 | " print (self.name,\"is a\", self.job)" 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "execution_count": 47, 945 | "metadata": {}, 946 | "outputs": [], 947 | "source": [ 948 | "c = Artist('Sofia',21)" 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": 48, 954 | "metadata": {}, 955 | "outputs": [ 956 | { 957 | "data": { 958 | "text/plain": [ 959 | "['__class__',\n", 960 | " '__delattr__',\n", 961 | " '__dict__',\n", 962 | " '__dir__',\n", 963 | " '__doc__',\n", 964 | " '__eq__',\n", 965 | " '__format__',\n", 966 | " '__ge__',\n", 967 | " '__getattribute__',\n", 968 | " '__gt__',\n", 969 | " '__hash__',\n", 970 | " '__init__',\n", 971 | " '__init_subclass__',\n", 972 | " '__le__',\n", 973 | " '__lt__',\n", 974 | " '__module__',\n", 975 | " '__ne__',\n", 976 | " '__new__',\n", 977 | " '__reduce__',\n", 978 | " '__reduce_ex__',\n", 979 | " '__repr__',\n", 980 | " '__setattr__',\n", 981 | " '__sizeof__',\n", 982 | " '__str__',\n", 983 | " '__subclasshook__',\n", 984 | " '__weakref__',\n", 985 | " 'artform',\n", 986 | " 'salary']" 987 | ] 988 | }, 989 | "execution_count": 48, 990 | "metadata": {}, 991 | "output_type": "execute_result" 992 | } 993 | ], 994 | "source": [ 995 | "dir(Artist)" 996 | ] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "execution_count": 49, 1001 | "metadata": {}, 1002 | "outputs": [ 1003 | { 1004 | "name": "stdout", 1005 | "output_type": "stream", 1006 | "text": [ 1007 | "Sofia earns 60000\n", 1008 | "Sofia is a Dancer\n" 1009 | ] 1010 | } 1011 | ], 1012 | "source": [ 1013 | "c.salary(60000)\n", 1014 | "c.artform('Dancer')" 1015 | ] 1016 | }, 1017 | { 1018 | "cell_type": "markdown", 1019 | "metadata": {}, 1020 | "source": [ 1021 | "Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class." 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "execution_count": 53, 1027 | "metadata": {}, 1028 | "outputs": [], 1029 | "source": [ 1030 | "class Artist(SoftwareEngineer):\n", 1031 | " def artform(self, job):\n", 1032 | " self.job = job\n", 1033 | " print (self.name,\"is a\", self.job)\n", 1034 | " def salary(self, value):\n", 1035 | " self.money = value\n", 1036 | " print (self.name,\"earns\",self.money)\n", 1037 | " print (\"I am overriding the SoftwareEngineer class's salary method\")" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "code", 1042 | "execution_count": 55, 1043 | "metadata": {}, 1044 | "outputs": [], 1045 | "source": [ 1046 | "c = Artist('Fahad',21)" 1047 | ] 1048 | }, 1049 | { 1050 | "cell_type": "code", 1051 | "execution_count": 56, 1052 | "metadata": {}, 1053 | "outputs": [ 1054 | { 1055 | "name": "stdout", 1056 | "output_type": "stream", 1057 | "text": [ 1058 | "Fahad earns 60000\n", 1059 | "I am overriding the SoftwareEngineer class's salary method\n", 1060 | "Fahad is a Marketer\n" 1061 | ] 1062 | } 1063 | ], 1064 | "source": [ 1065 | "c.salary(60000)\n", 1066 | "c.artform('Marketer')" 1067 | ] 1068 | }, 1069 | { 1070 | "cell_type": "markdown", 1071 | "metadata": {}, 1072 | "source": [ 1073 | "If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result." 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": 57, 1079 | "metadata": {}, 1080 | "outputs": [], 1081 | "source": [ 1082 | "class emptylist:\n", 1083 | " def __init__(self):\n", 1084 | " self.data = []\n", 1085 | " def one(self,x):\n", 1086 | " self.data.append(x)\n", 1087 | " def two(self, x ):\n", 1088 | " self.data.append(x**2)\n", 1089 | " def three(self, x):\n", 1090 | " self.data.append(x**3)" 1091 | ] 1092 | }, 1093 | { 1094 | "cell_type": "code", 1095 | "execution_count": 58, 1096 | "metadata": {}, 1097 | "outputs": [], 1098 | "source": [ 1099 | "xc = emptylist()" 1100 | ] 1101 | }, 1102 | { 1103 | "cell_type": "code", 1104 | "execution_count": 59, 1105 | "metadata": {}, 1106 | "outputs": [ 1107 | { 1108 | "name": "stdout", 1109 | "output_type": "stream", 1110 | "text": [ 1111 | "[1]\n" 1112 | ] 1113 | } 1114 | ], 1115 | "source": [ 1116 | "xc.one(1)\n", 1117 | "print (xc.data)" 1118 | ] 1119 | }, 1120 | { 1121 | "cell_type": "markdown", 1122 | "metadata": {}, 1123 | "source": [ 1124 | "Since xc.data is a list direct list operations can also be performed." 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "execution_count": 61, 1130 | "metadata": {}, 1131 | "outputs": [ 1132 | { 1133 | "name": "stdout", 1134 | "output_type": "stream", 1135 | "text": [ 1136 | "[1, 8]\n" 1137 | ] 1138 | } 1139 | ], 1140 | "source": [ 1141 | "xc.data.append(8)\n", 1142 | "print (xc.data)" 1143 | ] 1144 | }, 1145 | { 1146 | "cell_type": "code", 1147 | "execution_count": 63, 1148 | "metadata": {}, 1149 | "outputs": [ 1150 | { 1151 | "name": "stdout", 1152 | "output_type": "stream", 1153 | "text": [ 1154 | "[1, 8, 9]\n" 1155 | ] 1156 | } 1157 | ], 1158 | "source": [ 1159 | "xc.two(3)\n", 1160 | "print (xc.data)" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "markdown", 1165 | "metadata": {}, 1166 | "source": [ 1167 | "If the number of input arguments varies from instance to instance asterisk can be used as shown." 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "code", 1172 | "execution_count": 64, 1173 | "metadata": {}, 1174 | "outputs": [], 1175 | "source": [ 1176 | "class NotSure:\n", 1177 | " def __init__(self, *args):\n", 1178 | " self.data = ''.join(list(args)) " 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "code", 1183 | "execution_count": 65, 1184 | "metadata": {}, 1185 | "outputs": [], 1186 | "source": [ 1187 | "yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')" 1188 | ] 1189 | }, 1190 | { 1191 | "cell_type": "code", 1192 | "execution_count": 66, 1193 | "metadata": {}, 1194 | "outputs": [ 1195 | { 1196 | "data": { 1197 | "text/plain": [ 1198 | "'IDoNotKnowWhatToType'" 1199 | ] 1200 | }, 1201 | "execution_count": 66, 1202 | "metadata": {}, 1203 | "output_type": "execute_result" 1204 | } 1205 | ], 1206 | "source": [ 1207 | "yz.data" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": null, 1213 | "metadata": {}, 1214 | "outputs": [], 1215 | "source": [] 1216 | } 1217 | ], 1218 | "metadata": { 1219 | "kernelspec": { 1220 | "display_name": "Python 3", 1221 | "language": "python", 1222 | "name": "python3" 1223 | }, 1224 | "language_info": { 1225 | "codemirror_mode": { 1226 | "name": "ipython", 1227 | "version": 3 1228 | }, 1229 | "file_extension": ".py", 1230 | "mimetype": "text/x-python", 1231 | "name": "python", 1232 | "nbconvert_exporter": "python", 1233 | "pygments_lexer": "ipython3", 1234 | "version": "3.6.2" 1235 | } 1236 | }, 1237 | "nbformat": 4, 1238 | "nbformat_minor": 1 1239 | } 1240 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python 3 Tutorial Using Jupyter Notebook 2 | 3 | ### Disclaimer 4 | 5 | Image Processing tutorials are designed and written by me. 6 | 7 | General python (Python 2.7) tutorials are originally written by rajathkmp: https://github.com/rajathkmp/Python-Lectures 8 | 9 | I rewrote rajathkmp's codes for Python 3.x on this repo and revised it further. I will keep updating this repo for more tutorials on further topics in the future. 10 | 11 | Download the complete repo and run it on Jupyter Notebook 12 | -------------------------------------------------------------------------------- /data/boundary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/boundary.png -------------------------------------------------------------------------------- /data/boundary2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/boundary2.PNG -------------------------------------------------------------------------------- /data/c++.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/c++.PNG -------------------------------------------------------------------------------- /data/canny.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/canny.PNG -------------------------------------------------------------------------------- /data/chardata.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/chardata.jpg -------------------------------------------------------------------------------- /data/done.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/done.gif -------------------------------------------------------------------------------- /data/drawthis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/drawthis.png -------------------------------------------------------------------------------- /data/firebase1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/firebase1.PNG -------------------------------------------------------------------------------- /data/haters.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/haters.gif -------------------------------------------------------------------------------- /data/im1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/im1.png -------------------------------------------------------------------------------- /data/im2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/im2.PNG -------------------------------------------------------------------------------- /data/im3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/im3.png -------------------------------------------------------------------------------- /data/masking.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/masking.PNG -------------------------------------------------------------------------------- /data/resC.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/resC.PNG -------------------------------------------------------------------------------- /data/resCp.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/resCp.PNG -------------------------------------------------------------------------------- /data/research.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/research.gif -------------------------------------------------------------------------------- /data/tenor.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/tenor.gif -------------------------------------------------------------------------------- /data/theend.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/theend.gif -------------------------------------------------------------------------------- /data/wtf.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/data/wtf.gif -------------------------------------------------------------------------------- /image_proc01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Image Processing\n", 8 | "It is a technique used for the analysis of an image in order to manipulate it and extract information from it." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "\n", 16 | "\n", 17 | "" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "### What is the purpose of image processing?\n", 25 | "- Visualization - Observe the objects that are not visible.\n", 26 | "- Image Sharpening and Restoration - To create a better image.\n", 27 | "- Image Retrieval - Seek for the image of interest.\n", 28 | "- Measurement of Pattern - Measures various objects in an image.\n", 29 | "- Image Recognition - Distinguish the objects in an image." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### Python can do this all!\n", 37 | "Is there anything that Python can't do. You have to be kidding me!\n", 38 | "\n", 39 | "**OpenCV is a Open-source computer vision** library available for multiple platforms including C++.\n", 40 | "But first let's take a sneak peek at code of C++ I used for simplistic image processing task.\n", 41 | "\n", 42 | "\n", 43 | "**Too many words, the hell with this shit!**\n", 44 | "It literally took me two months to actually learn to code this, with clear concepts of course. Plus it's tedious to set up the libraries and do the required configurations before you can write the actual code. If you do really want to check that out though, I have maintained a journal containing core code concepts for C++ to use with OpenCV." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### Moving forward with Python\n", 52 | "Yes, people may claim why Python when much faster language exists that can process images way faster. I ask them, even if it is faster is it faster to debug them or write the code and not get stuck with the grammar of it?\n", 53 | "\n", 54 | "**Disclaimer:** If you are hating C++ right at this instant, Python is built on C++.\n", 55 | "***So C++ is going nowhere!***\n", 56 | "" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "### Installing OpenCV on Python\n", 64 | "OpenCV have a pre-compiled solution **(pip install opencv-python==3.4.7.28)** or you can compile it from source (check opencv github repo). \n", 65 | "\n", 66 | "\n", 67 | "\n", 68 | "Still worried, this site: https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv would get you covered. Open-source community is heavenly and it would go straight to ***Jana'ah***. This community has compiled the package of nearly every popular library and API for **Windows** and provided the complete solution. The download will be a **.whl** file. Choose appropriate version according to your Python version and your processor (32-bit or 64-bit).\n", 69 | "\n", 70 | "Open CMD and go to directory where the downloaded file is present and install in this way:\n", 71 | "- pip install \"opencv_python‑3.4.3‑cp36‑cp36m‑win_amd64.whl\"\n", 72 | "\n", 73 | "### And now let's code\n", 74 | "***With few functions always in mind:***\n", 75 | "- imread( ***directory*** )\n", 76 | "- imshow( image )\n", 77 | "- cvtColor( image, ***conversion_from_and_to*** )\n", 78 | "- imwrite( ***directory***, image )\n", 79 | "- VideoCapture( ***address*** )" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 1, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "UsageError: Line magic function `%` not found.\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "import cv2\n", 97 | "import numpy as np\n", 98 | "import matplotlib.pyplot as plt\n", 99 | "% matplotlib inline" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 22, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "" 111 | ] 112 | }, 113 | "execution_count": 22, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | }, 117 | { 118 | "data": { 119 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAJhklEQVR4nO3dz4uchR3H8c+nu4omFhUyF5PQzUEsQSiRQdSAB+NBq+ilhwgK9ZJL1SiCaC/+AyJ6ECFEvRj0EHMQEbWgHnoJromgySqEmOaHESeHqniJwU8PO4U0MTvPzs7TZ/br+wVCZnYyfpB9+8zMzjzrJAJQx++6HgBgsogaKIaogWKIGiiGqIFiZtu403Xr1mVubq6NuwYg6dixYzpz5ox/7WutRD03N6f5+fk27hqApH6/f8mv8fAbKIaogWKIGiiGqIFiiBoohqiBYhpFbfsu21/ZPmL76bZHARjfyKhtz0h6SdLdkjZLesD25raHARhPkyP1zZKOJDma5KykNyXd3+4sAONqEvV6SSfOu3xyeN3/sL3D9rzt+cFgMKl9AJZpYi+UJdmVpJ+k3+v1JnW3AJapSdSnJG087/KG4XUAplCTqD+RdL3tTbYvl7Rd0tvtzgIwrpGf0kpyzvYjkt6XNCPp1SSHWl8GYCyNPnqZ5F1J77a8BcAE8I4yoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqCYRr9LC9PDdtcTliVJ1xN+czhSA8UQNVAMUQPFEDVQDFEDxRA1UAxRA8WMjNr2Rtsf2T5s+5Dtnf+PYQDG0+TNJ+ckPZnkgO3fS/rU9j+SHG55G4AxjDxSJzmd5MDwzz9KWpC0vu1hAMazrOfUtuckbZG0/1e+tsP2vO35wWAwoXkAlqtx1LavkvSWpMeT/HDh15PsStJP0u/1epPcCGAZGkVt+zItBr0nyb52JwFYiSavflvSK5IWkjzf/iQAK9HkSL1V0kOS7rD92fCfP7e8C8CYRv5IK8k/Ja2uD/ECv2G8owwohqiBYogaKIaogWI48WBL2jpBICfywygcqYFiiBoohqiBYogaKIaogWKIGiiGqIFiiBoohqiBYogaKIaogWKIGiiGqIFiiBoohqiBYogaKIaogWKIGiiGqIFiiBoohqiBYjibaEs46ye6wpEaKIaogWKIGiiGqIFiiBoohqiBYogaKKZx1LZnbB+0/U6bgwCszHKO1DslLbQ1BMBkNIra9gZJ90ja3e4cACvV9Ej9gqSnJP1yqRvY3mF73vb8YDCYxDYAYxgZte17JX2X5NOlbpdkV5J+kn6v15vYQADL0+RIvVXSfbaPSXpT0h22X291FYCxjYw6yTNJNiSZk7Rd0odJHmx9GYCx8HNqoJhlfZ46yceSPm5lCYCJ4EgNFEPUQDFEDRRD1EAxRA0Uw9lEJdme+H1yNlF0hSM1UAxRA8UQNVAMUQPFEDVQDFEDxRA1UAxRA8UQNVAMUQPFEDVQDFEDxRA1UAxRA8UQNVAMUQPFEDVQDFEDxRA1UAxRA8UQNVAMZxMVZ/5ELRypgWKIGiiGqIFiiBoohqiBYogaKIaogWIaRW37Gtt7bX9pe8H2rW0PAzCepm8+eVHSe0n+YvtySWta3ARgBUZGbftqSbdL+qskJTkr6Wy7swCMq8nD702SBpJes33Q9m7bay+8ke0dtudtzw8Gg4kPBdBMk6hnJd0k6eUkWyT9JOnpC2+UZFeSfpJ+r9eb8EwATTWJ+qSkk0n2Dy/v1WLkAKbQyKiTfCvphO0bhldtk3S41VUAxtb01e9HJe0ZvvJ9VNLD7U0CsBKNok7ymaR+u1MATALvKAOKIWqgGKIGiiFqoBiiBorhbKItsd31hNI4A+ylcaQGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBiiBoohaqAYogaKIWqgGKIGiiFqoBhOPNgSToyHrnCkBoohaqAYogaKIWqgGKIGiiFqoBiiBoppFLXtJ2wfsv2F7TdsX9H2MADjGRm17fWSHpPUT3KjpBlJ29seBmA8TR9+z0q60vaspDWSvmlvEoCVGBl1klOSnpN0XNJpSd8n+eDC29neYXve9vxgMJj8UgCNNHn4fa2k+yVtknSdpLW2H7zwdkl2Jekn6fd6vckvBdBIk4ffd0r6Oskgyc+S9km6rd1ZAMbVJOrjkm6xvca2JW2TtNDuLADjavKcer+kvZIOSPp8+Hd2tbwLwJgafZ46ybOSnm15C4AJ4B1lQDFEDRRD1EAxRA0UQ9RAMUQNFEPUQDFEDRRD1EAxRA0UQ9RAMUQNFEPUQDFEDRRD1EAxRA0UQ9RAMUQNFEPUQDFEDRRD1EAxRA0UQ9RAMUQNFEPUQDFEDRRD1EAxTjL5O7UHkv7V4KbrJJ2Z+ID2rKa9q2mrtLr2TsPWPyT51V8E30rUTdmeT9LvbMAyraa9q2mrtLr2TvtWHn4DxRA1UEzXUa+2X16/mvaupq3S6to71Vs7fU4NYPK6PlIDmDCiBorpLGrbd9n+yvYR2093tWMU2xttf2T7sO1Dtnd2vakJ2zO2D9p+p+stS7F9je29tr+0vWD71q43LcX2E8Pvgy9sv2H7iq43XaiTqG3PSHpJ0t2SNkt6wPbmLrY0cE7Sk0k2S7pF0t+meOv5dkpa6HpEAy9Kei/JHyX9SVO82fZ6SY9J6ie5UdKMpO3drrpYV0fqmyUdSXI0yVlJb0q6v6MtS0pyOsmB4Z9/1OI33fpuVy3N9gZJ90ja3fWWpdi+WtLtkl6RpCRnk/y701GjzUq60vaspDWSvul4z0W6inq9pBPnXT6pKQ9FkmzPSdoiaX/HU0Z5QdJTkn7peMcomyQNJL02fKqw2/barkddSpJTkp6TdFzSaUnfJ/mg21UX44WyhmxfJektSY8n+aHrPZdi+15J3yX5tOstDcxKuknSy0m2SPpJ0jS/vnKtFh9RbpJ0naS1th/sdtXFuor6lKSN513eMLxuKtm+TItB70myr+s9I2yVdJ/tY1p8WnOH7de7nXRJJyWdTPLfRz57tRj5tLpT0tdJBkl+lrRP0m0db7pIV1F/Iul625tsX67FFxve7mjLkmxbi8/5FpI83/WeUZI8k2RDkjkt/nf9MMnUHU0kKcm3kk7YvmF41TZJhzucNMpxSbfYXjP8vtimKXxhb7aLf2mSc7YfkfS+Fl9BfDXJoS62NLBV0kOSPrf92fC6vyd5t7tJpTwqac/wf+5HJT3c8Z5LSrLf9l5JB7T4U5GDmsK3jPI2UaAYXigDiiFqoBiiBoohaqAYogaKIWqgGKIGivkPTQ4Y4IeQxHEAAAAASUVORK5CYII=\n", 120 | "text/plain": [ 121 | "
" 122 | ] 123 | }, 124 | "metadata": { 125 | "needs_background": "light" 126 | }, 127 | "output_type": "display_data" 128 | } 129 | ], 130 | "source": [ 131 | "# Read the image\n", 132 | "im = cv2.imread('samples/1 (1).png')\n", 133 | "# Display the original image\n", 134 | "im = cv2.resize(im, (10,10))\n", 135 | "plt.imshow(im)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "#### Processing the original image\n", 143 | "original -> BGR to gray -> resizing -> display" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 23, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "" 155 | ] 156 | }, 157 | "execution_count": 23, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | }, 161 | { 162 | "data": { 163 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAJmklEQVR4nO3dz49dBR2G8ed1CsGCUazd9EdsF0bTmAhmQlASF2CCCIGNC0gw0U03ImBICLjxHzBGF8akQd1IZFFZGENEE2XhpjIUEmirCUGkBYxDjT/iplC/LmZMakt7z9ze45n5+nwSks6d28ubZp6ee8/cOU1VIamP90w9QNJiGbXUjFFLzRi11IxRS81sG+NBP/TBpdq394oxHloS8OrJt3nrL2fzbp8bJep9e6/gt0/vHeOhJQE33Hryop/z6bfUjFFLzRi11IxRS80YtdSMUUvNDIo6yeeS/D7Jy0keGXuUpPnNjDrJEvBd4DbgAHBPkgNjD5M0nyFH6huAl6vqlao6AzwB3DXuLEnzGhL1buDct6+cWr/tvyQ5mGQlycrq6bOL2idpgxZ2oqyqDlXVclUt79yxtKiHlbRBQ6J+HTj3jdx71m+TtAkNifpZ4CNJ9ie5Ergb+Om4syTNa+ZPaVXVO0nuA54GloAfVNWx0ZdJmsugH72sqqeAp0beImkBfEeZ1IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdTMoH9LS5vHrbuum3rChjz9xgtTT/i/45FaasaopWaMWmrGqKVmjFpqxqilZoxaamZm1En2Jvl1kuNJjiV54H8xTNJ8hrz55B3goao6muR9wHNJfllVx0feJmkOM4/UVfVmVR1d//U/gBPA7rGHSZrPhl5TJ9kHXA8ceZfPHUyykmRl9fTZBc2TtFGDo05yDfAT4MGq+vv5n6+qQ1W1XFXLO3csLXKjpA0YFHWSK1gL+vGqenLcSZIux5Cz3wG+D5yoqm+NP0nS5RhypL4J+CJwc5IX1v/7/Mi7JM1p5re0quo3QP4HWyQtgO8ok5oxaqkZo5aaMWqpGS88OJKxLhDohfw0i0dqqRmjlpoxaqkZo5aaMWqpGaOWmjFqqRmjlpoxaqkZo5aaMWqpGaOWmjFqqRmjlpoxaqkZo5aaMWqpGaOWmjFqqRmjlpoxaqkZryY6Eq/6qal4pJaaMWqpGaOWmjFqqRmjlpoxaqkZo5aaGRx1kqUkzyf52ZiDJF2ejRypHwBOjDVE0mIMijrJHuB24LFx50i6XEOP1N8GHgb+dbE7JDmYZCXJyurps4vYJmkOM6NOcgfw56p67lL3q6pDVbVcVcs7dywtbKCkjRlypL4JuDPJq8ATwM1JfjTqKklzmxl1VT1aVXuqah9wN/Crqrp39GWS5uL3qaVmNvTz1FX1DPDMKEskLYRHaqkZo5aaMWqpGaOWmjFqqRmvJgrcuuu6hT+mVxPVVDxSS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNGLXUjFFLzRi11IxRS80YtdSMUUvNeDVRvPKnevFILTVj1FIzRi01Y9RSM0YtNWPUUjNGLTUzKOokH0hyOMnvkpxI8qmxh0maz9A3n3wH+HlVfSHJlcD2ETdJugwzo07yfuAzwJcAquoMcGbcWZLmNeTp935gFfhhkueTPJbk6vPvlORgkpUkK6unzy58qKRhhkS9Dfgk8L2quh74J/DI+XeqqkNVtVxVyzt3LC14pqShhkR9CjhVVUfWPz7MWuSSNqGZUVfVn4CTST66ftMtwPFRV0ma29Cz318FHl8/8/0K8OXxJkm6HIOirqoXgOVxp0haBN9RJjVj1FIzRi01Y9RSM0YtNePVREdy667rpp7QmleAvTiP1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi0144UHR+KF8TQVj9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM4OiTvK1JMeSvJTkx0muGnuYpPnMjDrJbuB+YLmqPg4sAXePPUzSfIY+/d4GvDfJNmA78MZ4kyRdjplRV9XrwDeB14A3gb9V1S/Ov1+Sg0lWkqysnj67+KWSBhny9Pta4C5gP7ALuDrJveffr6oOVdVyVS3v3LG0+KWSBhny9PuzwB+qarWq3gaeBD497ixJ8xoS9WvAjUm2JwlwC3Bi3FmS5jXkNfUR4DBwFHhx/fccGnmXpDkN+nnqqvoG8I2Rt0haAN9RJjVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01Y9RSM0YtNWPUUjNGLTVj1FIzRi01k6pa/IMmq8AfB9z1Q8BbCx8wnq20dyttha21dzNs/XBV7Xy3T4wS9VBJVqpqebIBG7SV9m6lrbC19m72rT79lpoxaqmZqaPeav94/Vbau5W2wtbau6m3TvqaWtLiTX2klrRgRi01M1nUST6X5PdJXk7yyFQ7ZkmyN8mvkxxPcizJA1NvGiLJUpLnk/xs6i2XkuQDSQ4n+V2SE0k+NfWmS0nytfWvg5eS/DjJVVNvOt8kUSdZAr4L3AYcAO5JcmCKLQO8AzxUVQeAG4GvbOKt53oAODH1iAG+A/y8qj4GfIJNvDnJbuB+YLmqPg4sAXdPu+pCUx2pbwBerqpXquoM8ARw10RbLqmq3qyqo+u//gdrX3S7p111aUn2ALcDj0295VKSvB/4DPB9gKo6U1V/nXTUbNuA9ybZBmwH3ph4zwWmino3cPKcj0+xyUMBSLIPuB44MvGUWb4NPAz8a+Ids+wHVoEfrr9UeCzJ1VOPupiqeh34JvAa8Cbwt6r6xbSrLuSJsoGSXAP8BHiwqv4+9Z6LSXIH8Oeqem7qLQNsAz4JfK+qrgf+CWzm8yvXsvaMcj+wC7g6yb3TrrrQVFG/Duw95+M967dtSkmuYC3ox6vqyan3zHATcGeSV1l7WXNzkh9NO+miTgGnquo/z3wOsxb5ZvVZ4A9VtVpVbwNPAp+eeNMFpor6WeAjSfYnuZK1kw0/nWjLJSUJa6/5TlTVt6beM0tVPVpVe6pqH2t/rr+qqk13NAGoqj8BJ5N8dP2mW4DjE06a5TXgxiTb178ubmETntjbNsX/tKreSXIf8DRrZxB/UFXHptgywE3AF4EXk7ywftvXq+qp6Sa18lXg8fW/3F8BvjzxnouqqiNJDgNHWfuuyPNswreM+jZRqRlPlEnNGLXUjFFLzRi11IxRS80YtdSMUUvN/BvVHxcYFyjbcgAAAABJRU5ErkJggg==\n", 164 | "text/plain": [ 165 | "
" 166 | ] 167 | }, 168 | "metadata": { 169 | "needs_background": "light" 170 | }, 171 | "output_type": "display_data" 172 | } 173 | ], 174 | "source": [ 175 | "# Let's change the RGB image (or BGR as said in OpenCV) to Grayscale\n", 176 | "#im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)\n", 177 | "im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)\n", 178 | "# Now if you want to resize the image to 10x10 \n", 179 | "im = cv2.resize(im, (10,10))\n", 180 | "# Display the processed image\n", 181 | "#cv2.imshow('gray',im)\n", 182 | "#cv2.waitKey(0)\n", 183 | "plt.imshow(im)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "### Print the normalized pixel values\n", 191 | "Normalization can be done by dividing the pixel values by the higest number possible which in the case of images in 255.\n", 192 | "**Notice how matrix values printed below is exact representation of the image**\n", 193 | "- 0 -> Black\n", 194 | "- 1 -> White" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 24, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 207 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 208 | " [1. 1. 1. 1. 0. 0. 1. 1. 1. 1.]\n", 209 | " [1. 1. 1. 0. 1. 1. 1. 1. 1. 1.]\n", 210 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 211 | " [1. 1. 0. 1. 1. 1. 1. 1. 1. 1.]\n", 212 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 213 | " [1. 1. 1. 0. 0. 0. 0. 1. 1. 1.]\n", 214 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 215 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "print(im/255)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "### Compare the image with the pixel values\n", 228 | "" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "### A video is just a collection of images\n", 236 | "When a set of 60 images are displayed in a second, a video is born. A 10 second video basically contains 60x10 = 600 images.\n", 237 | "In OpenCV you can process the video in a very similar fashion like that of still images." 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 33, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "# Read the video or open the webcam.\n", 247 | "cap = cv2.VideoCapture(0)\n", 248 | "# Read the first frame, then the second, then the third, and so on.\n", 249 | "# You will require a loop that can read new frame in every iteration and display it\n", 250 | "while(cap.isOpened()):\n", 251 | " # Read the frame\n", 252 | " _, frame = cap.read()\n", 253 | " cv2.imshow('original',frame)\n", 254 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 255 | " cv2.imshow('gray', gray)\n", 256 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 257 | " cv2.imshow('hsv', hsv)\n", 258 | " \n", 259 | " # In order to end the process or video forcefully, press 'q'\n", 260 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 261 | " break\n", 262 | "\n", 263 | "# Release the memory. If you don't do this, it will eat up your memory and program will crash ultimately \n", 264 | "cap.release()\n", 265 | "# Close all the opened windows because either the video has been finished or you forcefully ended the video\n", 266 | "cv2.destroyAllWindows()" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "***Notice the while block:*** The process taking place in the body is exactly the same as that of the still images." 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "### Concept Alert!\n", 281 | "There is a **don't care variable** in the code above represented by an underscore. Sometimes you don't care about the variable name because they are not used anywhere in the code so you just let Python take care of it and name it anything it want." 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 26, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "24\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "_ = 24\n", 299 | "print(_)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Another thing you might have seen is this:\n", 307 | "- a, b = somefunc()\n", 308 | "\n", 309 | "This simply means that if a function returns two values, you can say that first value goes into 'a' and the second goes into 'b'." 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 27, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "3\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "def twonum():\n", 327 | " return 2,3\n", 328 | "\n", 329 | "_, b = twonum()\n", 330 | "print(b)" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "### Flip the frames\n", 338 | "Sometimes during the training of the AI system, the data available is not enough. One way to increase the data is to make small changes to the existing data. This is known as **Data Augmentation**. In case of the images, you can flip them and add these generated images to the dataset." 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 34, 344 | "metadata": {}, 345 | "outputs": [], 346 | "source": [ 347 | "cap = cv2.VideoCapture(0)\n", 348 | "while(cap.isOpened()):\n", 349 | " ret, frame = cap.read()\n", 350 | " if ret==True:\n", 351 | " frame = cv2.flip(frame, 0)\n", 352 | " # Flip the frame\n", 353 | " cv2.imshow('frame',frame)\n", 354 | " \n", 355 | " # In order to end the process or video forcefully, press 'q'\n", 356 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 357 | " break\n", 358 | " else:\n", 359 | " break\n", 360 | "\n", 361 | "cap.release()\n", 362 | "cv2.destroyAllWindows()" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "### Drawing on the images\n", 370 | "Very often in **Object Localization** the computer draws a boundary around the image showing you where the object is. \n", 371 | "\n", 372 | "**For Example:** In the image below, AI system has not only accurately predicted the character but also shows where the character is in the image.\n", 373 | "\n", 374 | "\n", 375 | "### Further examples include\n", 376 | "Complex image segmentation and localization, many of which is done by a simple function call in Python.\n", 377 | "\n", 378 | "\n", 379 | "\n", 380 | "### With that motivation\n", 381 | "Let's learn how the computer draws on the image, shall we? " 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": null, 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [ 390 | "# A 2D array of zeros as we saw means the image is black\n", 391 | "# The following code is to create a black image of size 100x100\n", 392 | "img = np.zeros((500,500,3), np.uint8)\n", 393 | "\n", 394 | "# cv2.line(image, (x_init, y_init), (h, w), (b, g, r), line_width)\n", 395 | "img = cv2.line(img, (0,80), (500,80), (255,0,0), 2)\n", 396 | "img = cv2.line(img, (0,60), (260,500), (0,255,0), 2)\n", 397 | "img = cv2.line(img, (240,500), (500,60), (0,0,255), 2)\n", 398 | "\n", 399 | "# cv2.rectangle(img, (x_2, y_1), (x_1, y_2), (b, g, r), line_width)\n", 400 | "img = cv2.rectangle(img, (350,200), (150,300), (255,255,255), 2)\n", 401 | "\n", 402 | "# cv2.circle(img, (x_cen, y_cen), radius, (b, g, r), fill)\n", 403 | "img = cv2.circle(img,(250,250), 20, (255,0,255), -1)\n", 404 | "\n", 405 | "# cv2.putText(img, text, (x, y), font, size, (b, g, r), boldness, 0)\n", 406 | "cv2.putText(img,'Hello World~', (150,150), 2, 1, (255,255,255), 1, 0)\n", 407 | "\n", 408 | "cv2.imshow('img', img)\n", 409 | "cv2.waitKey(0)\n", 410 | "img = cv2.resize(img, (1000,1000))\n", 411 | "plt.imshow(img)" 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "### Try drawing these\n", 419 | "" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "### Masking\n", 427 | "In order to extract a specific color from the image, a concept known as masking is used. This concept can be understood intuitively. Mask is simply the array of pixels which you want to consider only and all the other pixels are turned to zero. \n", 428 | "\n", 429 | "\n", 430 | "\n", 431 | "**Mask is a Binary Image!**\n", 432 | "\n", 433 | "But now you want to retain the color of the object as well. Therefore, we can apply AND operation on the mask and the original image. The pixels in the mask which were zero will turn the result of AND operation to become zero. Similarly the pixels in the mask which were ones will turn the result of AND operation to be unaffected.\n", 434 | "\n", 435 | "It is better explained by the example below." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 2, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "img = cv2.imread('samples/C (60).jpg')\n", 445 | "im = cv2.resize(img, (500,500))\n", 446 | "cv2.imshow('image', im)\n", 447 | "\n", 448 | "# Always convert the image to HSV for masking\n", 449 | "hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)\n", 450 | "\n", 451 | "# Define the color you want for mask\n", 452 | "lower_red = np.array([100,130,150])\n", 453 | "upper_red = np.array([255,255,255])\n", 454 | "\n", 455 | "# Apply the mask\n", 456 | "mask = cv2.inRange(hsv, lower_red, upper_red)\n", 457 | "cv2.imshow('mask', mask)\n", 458 | "\n", 459 | "# Do the AND operation on mask and original image\n", 460 | "res = cv2.bitwise_and(im, im, mask=mask)\n", 461 | "cv2.imshow('res', res)\n", 462 | "\n", 463 | "cv2.waitKey(0)\n", 464 | "cv2.destroyAllWindows()" 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": {}, 470 | "source": [ 471 | "### But how to find out the masking values?\n", 472 | "Trust me, you have to play with it and test it repeatedly on images containing similar color until you find that sweet spot.\n", 473 | "\n", 474 | "### Wasn't anything not impossible with Python?\n", 475 | "So can we not resolve this trial and error thing to find the sweet spot and make an algorithm that makes it easy to do so. **Let's find out!**\n", 476 | "### Trackbars for images" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 31, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "name": "stdout", 486 | "output_type": "stream", 487 | "text": [ 488 | "hsv -> 0 0 0\n" 489 | ] 490 | } 491 | ], 492 | "source": [ 493 | "img = cv2.imread('samples/1 (3).jpg')\n", 494 | "im = cv2.resize(img, (500,500))\n", 495 | "hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)\n", 496 | "\n", 497 | "# initialize by making a blank result image\n", 498 | "res = np.zeros((500,500,3), np.uint8)\n", 499 | "\n", 500 | "# creating a window on which image will be displayed\n", 501 | "cv2.imshow('image', im)\n", 502 | "\n", 503 | "def nothing(x):\n", 504 | " pass\n", 505 | "\n", 506 | "#initializing the values to be set using trackbars to be zero\n", 507 | "l_h,l_s,l_v = 0,0,0\n", 508 | "higher = 255\n", 509 | "\n", 510 | "# creating trackbars\n", 511 | "cv2.createTrackbar('h', 'image', l_h, higher, nothing)\n", 512 | "cv2.createTrackbar('s', 'image', l_s, higher, nothing)\n", 513 | "cv2.createTrackbar('v', 'image', l_v, higher, nothing)\n", 514 | "\n", 515 | "# with every new value from trackbar, image is needed to be refreshed and redisplayed\n", 516 | "while(True):\n", 517 | " cv2.imshow('res', res)\n", 518 | " \n", 519 | " # get the trackbar positions and set them to l_h, l_s, l_v respectively\n", 520 | " l_h = cv2.getTrackbarPos('h', 'image')\n", 521 | " l_s = cv2.getTrackbarPos('s', 'image')\n", 522 | " l_v = cv2.getTrackbarPos('v', 'image')\n", 523 | " \n", 524 | " lower_red = np.array([l_h,l_s,l_v])\n", 525 | " upper_red = np.array([255,255,255])\n", 526 | " \n", 527 | " mask = cv2.inRange(hsv, lower_red, upper_red)\n", 528 | " res = cv2.bitwise_and(im, im, mask=mask)\n", 529 | " \n", 530 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 531 | " print(\"hsv -> \",l_h,l_s,l_v)\n", 532 | " break\n", 533 | "\n", 534 | "# save the image\n", 535 | "cv2.imwrite('samples/samples.jpg', res)\n", 536 | "cv2.destroyAllWindows()" 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "metadata": {}, 542 | "source": [ 543 | "### Can we make trackbars for video\n", 544 | "Sure we can, just a little more code and more copy paste from above!" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 3, 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "name": "stdout", 554 | "output_type": "stream", 555 | "text": [ 556 | "hsv -> 162 58 62\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "cap = cv2.VideoCapture(0)\n", 562 | "\n", 563 | "def nothing(x):\n", 564 | " pass\n", 565 | "\n", 566 | "#initializing the values to be set using trackbars to be zero\n", 567 | "l_h,l_s,l_v = 0,0,0\n", 568 | "higher = 255\n", 569 | "\n", 570 | "# creating trackbars\n", 571 | "cv2.namedWindow('image')\n", 572 | "cv2.createTrackbar('h', 'image', l_h, higher, nothing)\n", 573 | "cv2.createTrackbar('s', 'image', l_s, higher, nothing)\n", 574 | "cv2.createTrackbar('v', 'image', l_v, higher, nothing)\n", 575 | "\n", 576 | "while(cap.isOpened()):\n", 577 | " _,frame = cap.read()\n", 578 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 579 | " \n", 580 | " l_h = cv2.getTrackbarPos('h', 'image')\n", 581 | " l_s = cv2.getTrackbarPos('s', 'image')\n", 582 | " l_v = cv2.getTrackbarPos('v', 'image')\n", 583 | " \n", 584 | " lower_red = np.array([l_h,l_s,l_v])\n", 585 | " upper_red = np.array([255,255,255])\n", 586 | " \n", 587 | " mask = cv2.inRange(hsv, lower_red, upper_red)\n", 588 | " res = cv2.bitwise_and(frame, frame, mask=mask)\n", 589 | " \n", 590 | " cv2.imshow('image', frame)\n", 591 | " cv2.imshow('res', res)\n", 592 | " \n", 593 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 594 | " print(\"hsv -> \",l_h,l_s,l_v)\n", 595 | " break\n", 596 | " \n", 597 | "cap.release()\n", 598 | "cv2.destroyAllWindows()" 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": {}, 604 | "source": [ 605 | "### Image Shape\n", 606 | "OpenCV gives you a handy facility to know the dimensions of your image." 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 19, 612 | "metadata": {}, 613 | "outputs": [ 614 | { 615 | "name": "stdout", 616 | "output_type": "stream", 617 | "text": [ 618 | "img: (2988, 5312, 3)\n", 619 | "im: (500, 500, 3)\n", 620 | "hsv: (480, 640, 3)\n", 621 | "mask: (480, 640)\n", 622 | "res: (480, 640, 3)\n" 623 | ] 624 | } 625 | ], 626 | "source": [ 627 | "print(\"img: \", img.shape)\n", 628 | "print(\"im: \", im.shape)\n", 629 | "print(\"hsv: \", hsv.shape)\n", 630 | "print(\"mask: \", mask.shape)\n", 631 | "print(\"res: \", res.shape)" 632 | ] 633 | }, 634 | { 635 | "cell_type": "markdown", 636 | "metadata": {}, 637 | "source": [ 638 | "### Done with basics!\n", 639 | "" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": null, 645 | "metadata": {}, 646 | "outputs": [], 647 | "source": [] 648 | } 649 | ], 650 | "metadata": { 651 | "kernelspec": { 652 | "display_name": "Python 3 (ipykernel)", 653 | "language": "python", 654 | "name": "python3" 655 | }, 656 | "language_info": { 657 | "codemirror_mode": { 658 | "name": "ipython", 659 | "version": 3 660 | }, 661 | "file_extension": ".py", 662 | "mimetype": "text/x-python", 663 | "name": "python", 664 | "nbconvert_exporter": "python", 665 | "pygments_lexer": "ipython3", 666 | "version": "3.8.10" 667 | } 668 | }, 669 | "nbformat": 4, 670 | "nbformat_minor": 4 671 | } 672 | -------------------------------------------------------------------------------- /image_proc01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/image_proc01.pdf -------------------------------------------------------------------------------- /image_proc02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Knowing how to detect edges\n", 8 | "This is a follow up and advancement of the first tutorial on image processing. \n", 9 | "\n", 10 | "#### Disclaimer:\n", 11 | "You can always refer to **the first tutorial** anytime.\n", 12 | "\n", 13 | "But if you have understood all that was described in the first, **congrats on your first research!**\n", 14 | "\n", 15 | "" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 7, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stderr", 25 | "output_type": "stream", 26 | "text": [ 27 | "UsageError: Line magic function `%` not found.\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "import cv2\n", 33 | "import numpy as np\n", 34 | "import matplotlib.pyplot as plt\n", 35 | "% matplotlib inline" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Edge Detection\n", 43 | "Boundaries of any object is called an edge. In image processing you can extract the edges of object by very ease. It is basically a high pass filter that completely removes anything that is not an edge. \n", 44 | "\n", 45 | "" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "113" 57 | ] 58 | }, 59 | "execution_count": 2, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "img = cv2.imread('samples/D (198).jpg')\n", 66 | "im = cv2.resize(img, (500,500))\n", 67 | "gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)\n", 68 | "\n", 69 | "edges = cv2.Canny(gray, 550, 150)\n", 70 | "\n", 71 | "cv2.imshow('im', im)\n", 72 | "cv2.imshow('gray', gray)\n", 73 | "cv2.imshow('edges', edges)\n", 74 | "\n", 75 | "cv2.waitKey(0)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### Try it out with a video" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 8, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "cap = cv2.VideoCapture(0)\n", 92 | "\n", 93 | "while(cap.isOpened()):\n", 94 | " _, frame = cap.read()\n", 95 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 96 | " edges = cv2.Canny(gray, 50, 100)\n", 97 | " \n", 98 | " cv2.imshow('frame', frame)\n", 99 | " cv2.imshow('edges', edges)\n", 100 | " \n", 101 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 102 | " break\n", 103 | "\n", 104 | "cap.release()\n", 105 | "cv2.destroyAllWindows()" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "### Object Tracking\n", 113 | "As we have covered all the basics of image processing which includes masking, finding edges, knowing the shape of images and frames of video; we can now proceed towards **making an object tracker from scratch!**\n", 114 | "\n", 115 | "This will include the concept of:\n", 116 | "- threshold\n", 117 | "- contours \n", 118 | "- image moments \n", 119 | "- finding the area of images\n", 120 | "- finding the coordinates of contours\n", 121 | "- drawing the boundary around the object using those coordinates\n", 122 | "\n", 123 | "#### Contours:\n", 124 | "Any closed area is a contour. As seen from the edge detector concept, every closed area you see there is a contour.\n", 125 | "#### Image Moments:\n", 126 | "The weighted average of all the pixel intensities in a image is called moment. We can find out the center of the weighted average hence, can calculate the centroid of an image." 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 11, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "cap = cv2.VideoCapture(0)\n", 136 | "min_area = 100*100\n", 137 | "\n", 138 | "while(cap.isOpened):\n", 139 | " _,frame = cap.read()\n", 140 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 141 | " \n", 142 | " upper = np.array([162,58,62])\n", 143 | " lower = np.array([255,255,255])\n", 144 | " \n", 145 | " mask = cv2.inRange(hsv, upper, lower)\n", 146 | " res = cv2.bitwise_and(frame, frame, mask=mask)\n", 147 | " \n", 148 | " # detection of contours\n", 149 | " res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)\n", 150 | " _, threshold = cv2.threshold(res_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n", 151 | " contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 152 | " \n", 153 | " for contour in contours:\n", 154 | " area = cv2.contourArea(contour)\n", 155 | " if area>min_area:\n", 156 | " # calculating the centroid\n", 157 | " moment = cv2.moments(contour)\n", 158 | " cx = int(moment['m10']/moment['m00'])\n", 159 | " cy = int(moment['m01']/moment['m00'])\n", 160 | " \n", 161 | " # make a rectangle bounding the contour\n", 162 | " [x, y, w, h] = cv2.boundingRect(contour)\n", 163 | " \n", 164 | " # draw a rectangle surrounding the contour image\n", 165 | " cv2.rectangle(frame, (x, y), (w+x, h+y), (0,255,0), 2)\n", 166 | " \n", 167 | " # put the centroid text\n", 168 | " cv2.putText(frame, str(cx)+','+str(cy), (cx,cy), 2, 1, (255,0,0), 1, 0)\n", 169 | " #endif\n", 170 | " #endfor\n", 171 | " cv2.imshow('frame', frame)\n", 172 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 173 | " break\n", 174 | " #endif\n", 175 | "#endwhile\n", 176 | "\n", 177 | "cap.release()\n", 178 | "cv2.destroyAllWindows()" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "### Let's do the fancy stuff in the next\n", 186 | "\n", 187 | "Open the third tutorial where we will implement simple trignometry rules and plot them" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | } 197 | ], 198 | "metadata": { 199 | "kernelspec": { 200 | "display_name": "Python 3 (ipykernel)", 201 | "language": "python", 202 | "name": "python3" 203 | }, 204 | "language_info": { 205 | "codemirror_mode": { 206 | "name": "ipython", 207 | "version": 3 208 | }, 209 | "file_extension": ".py", 210 | "mimetype": "text/x-python", 211 | "name": "python", 212 | "nbconvert_exporter": "python", 213 | "pygments_lexer": "ipython3", 214 | "version": "3.8.10" 215 | } 216 | }, 217 | "nbformat": 4, 218 | "nbformat_minor": 4 219 | } 220 | -------------------------------------------------------------------------------- /image_proc02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/image_proc02.pdf -------------------------------------------------------------------------------- /image_proc03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "74f6dd6d-ad6b-45c3-834c-b8febacf1601", 6 | "metadata": {}, 7 | "source": [ 8 | "# Tracking an object\n", 9 | "This is a follow up of previous two tutorials. If you haven't studied them before, it is recommended to study them first" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "id": "0022accd-8532-40c7-ad7a-791711c29a62", 15 | "metadata": {}, 16 | "source": [ 17 | "## In this lesson, we will learn:\n", 18 | "* to calculate euclidean distance\n", 19 | "* drawing on image and video\n", 20 | "* simple trignometry (sine and cosine law)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "2717c6bd-ea6f-465a-972a-fe7efb5c3879", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import numpy as np\n", 31 | "import cv2\n", 32 | "import matplotlib.pyplot as plt\n", 33 | "import math\n", 34 | "from numpy import *" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "5f873ca4-18eb-44ea-80e8-162299506c18", 40 | "metadata": {}, 41 | "source": [ 42 | "## Draw a line between center of a picture and center of the target\n", 43 | "As we know how to calculate the centroid points of a contour using its moment, and we also have a clear understanding of how\n", 44 | "to draw on an image - we can draw a line between the COT and COI, calculate the euclidean distance between them. This\n", 45 | "comes in handy when you are trying to estimate the GPS location of a target in an image. By knowing the distance and angle\n", 46 | "between the line and the y-axis, we can easily interpolate the coordinates.\n", 47 | "\n", 48 | "\n", 49 | "But first, let's make a function to calculate the euclidean distance between two points." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 2, 55 | "id": "95fa7daf-64e9-463f-8591-2eaef76e29b2", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "def euclidean_distance(x,y):\n", 60 | " eud = math.sqrt((x[0]-y[0])**2 + (x[1]-y[1])**2)\n", 61 | " return eud" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "id": "7ee58919-2766-4170-94d7-c724a926538f", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "2.8284271247461903" 74 | ] 75 | }, 76 | "execution_count": 3, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "euclidean_distance([1,2],[3,4])" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "44ce6308-7a37-4369-91a5-4ccf2486849f", 88 | "metadata": {}, 89 | "source": [ 90 | "**Or just simply use the numpy version of calculating euclidean distance**" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "id": "98b50f7a-f251-4ead-95a4-699809bbc752", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "2.8284271247461903" 103 | ] 104 | }, 105 | "execution_count": 4, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "np.linalg.norm(np.array([1,2]) - np.array([3,4]))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "id": "a4abded7-37e4-4557-93d0-bfcf9691d2e7", 117 | "metadata": {}, 118 | "source": [ 119 | "### Cosine Law\n", 120 | "This is the law we will use to calculate the angles between two lines." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 5, 126 | "id": "1441a0e2-c044-4550-9f1f-1fe900bb3b0c", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# first_side, second_side, side_opposite_to_angle, radian_boolean\n", 131 | "def cosineLaw(a,b,c, radian):\n", 132 | " angle = math.acos(((a**2)+(b**2)-(c**2))/(2*a*b))\n", 133 | " if radian==True:\n", 134 | " return angle\n", 135 | " elif radian==False:\n", 136 | " return math.degrees(angle)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "id": "b228ba22-f80d-423c-b95b-5d12e1fb316c", 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "cosineLaw(4,5,6,radian=False)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "id": "902f3994-10a5-4447-9704-a6dad78831fc", 152 | "metadata": {}, 153 | "source": [ 154 | "### Calculate the COI and COT on images\n", 155 | "After calculations, we will draw some lines, mark some points, and calculate euclidean distance between points using function we made earlier." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "id": "8883e4db-acc0-4d5c-85db-e864757f4b5d", 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "im = cv2.imread('samples/obt.png')\n", 166 | "#im = cv2.resize(im, (600,480))\n", 167 | "height,width,_ = im.shape\n", 168 | "im_cx,im_cy = int(width/2),int(height/2)\n", 169 | "print(im_cx,im_cy)\n", 170 | "# Contour Finding\n", 171 | "hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)\n", 172 | "upper = np.array([104,114,112])\n", 173 | "lower = np.array([255,255,255])\n", 174 | "\n", 175 | "mask = cv2.inRange(hsv, upper, lower)\n", 176 | "res = cv2.bitwise_and(im, im, mask=mask)\n", 177 | "res = cv2.GaussianBlur(res, (1,1), 0)\n", 178 | "res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)\n", 179 | "_, threshold = cv2.threshold(res_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n", 180 | "contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 181 | "min_area = 30**2\n", 182 | "for contour in contours:\n", 183 | " area = cv2.contourArea(contour)\n", 184 | " if area>min_area:\n", 185 | " # calculating the centroid\n", 186 | " moment = cv2.moments(contour)\n", 187 | " cx = int(moment['m10']/moment['m00'])\n", 188 | " cy = int(moment['m01']/moment['m00'])\n", 189 | "\n", 190 | " # make a rectangle bounding the contour\n", 191 | " [x, y, w, h] = cv2.boundingRect(contour)\n", 192 | "\n", 193 | " # draw a rectangle surrounding the contour image\n", 194 | " cv2.rectangle(im, (x, y), (w+x, h+y), (0,255,0), 2)\n", 195 | "\n", 196 | " # put the centroid text\n", 197 | " #cv2.circle(im,(cx,cy), 5, (255,0,255), -1)\n", 198 | " #cv2.putText(im, str(cx)+','+str(cy), (cx,cy), 2, 1, (0,0,0), 1, 0)\n", 199 | " #endif\n", 200 | "#endfor\n", 201 | "# Calculating Euclidean Distances\n", 202 | "pi2pt = round(euclidean_distance([im_cx,im_cy],[cx,cy]),2)\n", 203 | "pi2py = round(euclidean_distance([im_cx,im_cy],[0,im_cy]),2)\n", 204 | "pt2py = round(euclidean_distance([cx,cy],[0,im_cy]),2)\n", 205 | "# Calculating the angles\n", 206 | "a_pi = round(cosineLaw(pi2pt,pi2py,pt2py,radian=False),1)\n", 207 | "a_pt = round(cosineLaw(pi2pt,pt2py,pi2py,radian=False),1)\n", 208 | "a_py = round(cosineLaw(pt2py,pi2py,pi2pt,radian=False),1)\n", 209 | "# Marking the center of COI\n", 210 | "cv2.circle(im,(im_cx,im_cy), 5, (255,0,255), -1)\n", 211 | "cv2.putText(im, str(im_cx)+','+str(im_cy)+', ang: '+str(a_pi), (im_cx,im_cy), 2, 1, (0,255,255), 1, 0)\n", 212 | "# Marking the center of COT\n", 213 | "cv2.circle(im,(cx,cy), 5, (255,0,255), -1)\n", 214 | "cv2.putText(im, str(cx)+','+str(cy)+', ang: '+str(a_pt), (cx,cy), 2, 1, (0,255,255), 1, 0)\n", 215 | "# Marking the center of y-axis\n", 216 | "cv2.circle(im,(0,im_cy), 5, (255,0,255), -1)\n", 217 | "cv2.putText(im, str(0)+','+str(im_cy)+', ang: '+str(a_py), (0,im_cy), 2, 1, (0,255,255), 1, 0)\n", 218 | "# line from COI to COT\n", 219 | "cv2.line(im, (im_cx,im_cy), (cx,cy), (0,0,255), 2)\n", 220 | "# Line from COI to y-axis\n", 221 | "cv2.line(im, (im_cx,im_cy), (0,im_cy), (0,0,255), 2)\n", 222 | "# Line from COT to y-axis\n", 223 | "cv2.line(im, (cx,cy), (0,im_cy), (0,0,255), 2)\n", 224 | "# Putting the pi2pt in the midpoint of the COI and COT\n", 225 | "cv2.putText(im, str(pi2pt), (int((im_cx+cx)/2),int((im_cy+cy)/2)), 2, 1, (0,0,255), 1, 0)\n", 226 | "# Putting the pi2py in the midpoint of the COI and y-axis\n", 227 | "cv2.putText(im, str(pi2py), (int((im_cx+0)/2),int((im_cy+im_cy)/2)), 2, 1, (0,0,255), 1, 0)\n", 228 | "# Putting the pt2py in the midpoint of the COT and y-axis\n", 229 | "cv2.putText(im, str(pt2py), (int((cx+0)/2),int((cy+im_cy)/2)), 2, 1, (0,0,255), 1, 0)\n", 230 | "cv2.imshow('im', im)\n", 231 | "#cv2.imwrite('data/dist.png', im)\n", 232 | "cv2.waitKey(0)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 6, 238 | "id": "ccb6f227-8abb-4975-99dc-1da2ba06e729", 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "cap = cv2.VideoCapture(0)\n", 243 | "cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2000)\n", 244 | "cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2000)\n", 245 | "min_area = 50**2\n", 246 | "while(cap.isOpened):\n", 247 | " _,frame = cap.read()\n", 248 | " height,width,_ = frame.shape\n", 249 | " im_cx,im_cy = int(width/2),int(height/2)\n", 250 | "\n", 251 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 252 | "\n", 253 | " upper = np.array([162,58,62])\n", 254 | " lower = np.array([255,255,255])\n", 255 | "\n", 256 | " mask = cv2.inRange(hsv, upper, lower)\n", 257 | " res = cv2.bitwise_and(frame, frame, mask=mask)\n", 258 | " res = cv2.GaussianBlur(res, (1,1), 0)\n", 259 | "\n", 260 | " # detection of contours\n", 261 | " res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)\n", 262 | " _, threshold = cv2.threshold(res_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n", 263 | " contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n", 264 | "\n", 265 | " for contour in contours:\n", 266 | " area = cv2.contourArea(contour)\n", 267 | " if area>min_area:\n", 268 | " # calculating the centroid\n", 269 | " moment = cv2.moments(contour)\n", 270 | " cx = int(moment['m10']/moment['m00'])\n", 271 | " cy = int(moment['m01']/moment['m00'])\n", 272 | "\n", 273 | " # make a rectangle bounding the contour\n", 274 | " [x, y, w, h] = cv2.boundingRect(contour)\n", 275 | "\n", 276 | " # draw a rectangle surrounding the contour image\n", 277 | " cv2.rectangle(frame, (x, y), (w+x, h+y), (0,255,0), 2)\n", 278 | "\n", 279 | " # put the centroid text\n", 280 | " cv2.putText(frame, str(cx)+','+str(cy), (cx,cy), 2, 1, (255,255,255), 2, 0)\n", 281 | "\n", 282 | " # Calculating Euclidean Distances\n", 283 | " pi2pt = round(euclidean_distance([im_cx,im_cy],[cx,cy]),2)\n", 284 | " pi2py = round(euclidean_distance([im_cx,im_cy],[0,im_cy]),2)\n", 285 | " pt2py = round(euclidean_distance([cx,cy],[0,im_cy]),2)\n", 286 | "\n", 287 | " # Calculating the angles\n", 288 | " a_pi = round(cosineLaw(pi2pt,pi2py,pt2py,radian=False),1)\n", 289 | " a_pt = round(cosineLaw(pi2pt,pt2py,pi2py,radian=False),1)\n", 290 | " a_py = round(cosineLaw(pt2py,pi2py,pi2pt,radian=False),1)\n", 291 | "\n", 292 | " # Marking the center of COI\n", 293 | " cv2.circle(frame,(im_cx,im_cy), 5, (255,0,255), -1)\n", 294 | " cv2.putText(frame, str(im_cx)+','+str(im_cy)+', ang: '+str(a_pi), (im_cx,im_cy), 2, 1, (0,255,255), 1, 0)\n", 295 | " # Marking the center of COT\n", 296 | " cv2.circle(frame,(cx,cy), 5, (255,0,255), -1)\n", 297 | " cv2.putText(frame, str(cx)+','+str(cy)+', ang: '+str(a_pt), (cx,cy), 2, 1, (0,255,255), 1, 0)\n", 298 | " # Marking the center of y-axis\n", 299 | " cv2.circle(frame,(0,im_cy), 5, (255,0,255), -1)\n", 300 | " cv2.putText(frame, str(0)+','+str(im_cy)+', ang: '+str(a_py), (0,im_cy), 2, 1,(0,255,255), 1, 0)\n", 301 | " # line from COI to COT\n", 302 | " cv2.line(frame, (im_cx,im_cy), (cx,cy), (0,0,255), 2)\n", 303 | " # Line from COI to y-axis\n", 304 | " cv2.line(frame, (im_cx,im_cy), (0,im_cy), (0,0,255), 2)\n", 305 | " # Line from COT to y-axis\n", 306 | " cv2.line(frame, (cx,cy), (0,im_cy), (0,0,255), 2)\n", 307 | "\n", 308 | " # Putting the pi2pt in the midpoint of the COI and COT\n", 309 | " cv2.putText(frame, str(pi2pt), (int((im_cx+cx)/2),int((im_cy+cy)/2)), 2, 1, (0,0,255), 1, 0)\n", 310 | " # Putting the pi2py in the midpoint of the COI and y-axis\n", 311 | " cv2.putText(frame, str(pi2py), (int((im_cx+0)/2),int((im_cy+im_cy)/2)), 2, 1,(0,0,255), 1, 0)\n", 312 | " # Putting the pt2py in the midpoint of the COT and y-axis\n", 313 | " cv2.putText(frame, str(pt2py), (int((cx+0)/2),int((cy+im_cy)/2)), 2, 1, (0,0,255), 1, 0)\n", 314 | " #endif\n", 315 | " #endfor\n", 316 | " cv2.imshow('frame', frame)\n", 317 | " if cv2.waitKey(1) & 0xff == ord('q'):\n", 318 | " cv2.imwrite('samples/proc3.png',frame)\n", 319 | " break\n", 320 | " #endif\n", 321 | "#endwhile\n", 322 | "cap.release()\n", 323 | "cv2.destroyAllWindows()" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "id": "a042e6fa-fd9d-4d91-8a35-f268ae408abb", 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [] 333 | } 334 | ], 335 | "metadata": { 336 | "kernelspec": { 337 | "display_name": "Python 3 (ipykernel)", 338 | "language": "python", 339 | "name": "python3" 340 | }, 341 | "language_info": { 342 | "codemirror_mode": { 343 | "name": "ipython", 344 | "version": 3 345 | }, 346 | "file_extension": ".py", 347 | "mimetype": "text/x-python", 348 | "name": "python", 349 | "nbconvert_exporter": "python", 350 | "pygments_lexer": "ipython3", 351 | "version": "3.8.10" 352 | } 353 | }, 354 | "nbformat": 4, 355 | "nbformat_minor": 5 356 | } 357 | -------------------------------------------------------------------------------- /image_proc03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/image_proc03.pdf -------------------------------------------------------------------------------- /samples/1 (1).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (1).jpg -------------------------------------------------------------------------------- /samples/1 (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (1).png -------------------------------------------------------------------------------- /samples/1 (2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (2).jpg -------------------------------------------------------------------------------- /samples/1 (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (2).png -------------------------------------------------------------------------------- /samples/1 (3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (3).jpg -------------------------------------------------------------------------------- /samples/1 (3).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (3).png -------------------------------------------------------------------------------- /samples/1 (4).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/1 (4).jpg -------------------------------------------------------------------------------- /samples/B (105).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/B (105).jpg -------------------------------------------------------------------------------- /samples/C (60).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/C (60).jpg -------------------------------------------------------------------------------- /samples/D (198).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/D (198).jpg -------------------------------------------------------------------------------- /samples/bouncingBall.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/bouncingBall.avi -------------------------------------------------------------------------------- /samples/obt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/obt.png -------------------------------------------------------------------------------- /samples/proc3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/proc3.png -------------------------------------------------------------------------------- /samples/samples.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/samples/samples.jpg -------------------------------------------------------------------------------- /untitled.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZainUlMustafa/Python-3-Tutorial-Using-Jupyter-Notebook/bbb08d5e3eb112a41bb0ebae482566f0181a0117/untitled.txt --------------------------------------------------------------------------------