├── .ipynb_checkpoints └── Patterns-checkpoint.ipynb ├── Lecture_02_Intro ├── .ipynb_checkpoints │ └── Intro_To_Python-checkpoint.ipynb └── Intro_To_Python.ipynb ├── Lecture_03_Conditionals&Loops ├── .ipynb_checkpoints │ └── Operators_And_while-checkpoint.ipynb └── Operators_And_while.ipynb ├── Lecture_04_Patterns ├── .ipynb_checkpoints │ └── Patterns-checkpoint.ipynb └── Patterns.ipynb ├── Lecture_05_Loops&RangeMethod ├── .ipynb_checkpoints │ └── ForLoops&RangeMethod-checkpoint.ipynb └── ForLoops&RangeMethod.ipynb ├── Lecture_06_Strings_Lists_2DLists ├── .ipynb_checkpoints │ ├── Strings_Lists_2DLists-checkpoint.ipynb │ └── Untitled-checkpoint.ipynb └── Strings_Lists_2DLists.ipynb ├── Lecture_07_Functions ├── .ipynb_checkpoints │ └── Functions-checkpoint.ipynb └── Functions.ipynb ├── Lecture_08_Tuples_Dictionary&Sets ├── .ipynb_checkpoints │ ├── Dictionaries-checkpoint.ipynb │ ├── Sets-checkpoint.ipynb │ └── Tuples-checkpoint.ipynb ├── Dictionaries.ipynb ├── Sets.ipynb └── Tuples.ipynb ├── Lecture_09_OOPS ├── .ipynb_checkpoints │ └── OOPS-checkpoint.ipynb └── OOPS.ipynb ├── Lecture_10_workingWIthFiles ├── .ipynb_checkpoints │ └── WorkingWithFiles-checkpoint.ipynb └── WorkingWithFiles.ipynb ├── Lecture_11_Numpy ├── .ipynb_checkpoints │ └── Numpy-checkpoint.ipynb └── Numpy.ipynb ├── Lecture_12_Pandas ├── .ipynb_checkpoints │ └── Pandas-checkpoint.ipynb └── Pandas.ipynb ├── Lecture_13_plottingGraphs └── plottingGraphs.ipynb ├── Lecture_16_Indexing ├── Connect With DB.ipynb ├── DropIndex.ipynb ├── Indexing&SQLite.ipynb ├── SQLite_with_Pandas.ipynb ├── createEmployeeTable.ipynb ├── extractData.ipynb └── passingParameters.ipynb ├── Lecture_17_API_1 └── Lecture_17_API_1.ipynb ├── Lecture_18_API_2 └── API_2.ipynb ├── Lecture_19_webScrappingBeautifulSoup └── Intro_to_webScrapping&beautifulSoup.ipynb ├── ML ├── Feature_scaling in Sklearn │ └── Feature_Scaling.ipynb ├── Intro_to_ML │ ├── .ipynb_checkpoints │ │ └── Intro_to_ML-checkpoint.ipynb │ └── Intro_to_ML.ipynb └── linearRegression │ ├── .ipynb_checkpoints │ └── LR_on_dummy_data-checkpoint.ipynb │ ├── LR_on_dummy_data.ipynb │ └── data.csv └── README.md /.ipynb_checkpoints/Patterns-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_02_Intro/.ipynb_checkpoints/Intro_To_Python-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_03_Conditionals&Loops/.ipynb_checkpoints/Operators_And_while-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_03_Conditionals&Loops/Operators_And_while.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "'Ninjas'" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "a = True # Small t 'true' is not defined\n", 23 | "b = False\n", 24 | "\n", 25 | "c = \"Ninjas\"\n", 26 | "c" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "bool" 38 | ] 39 | }, 40 | "execution_count": 2, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "c = True\n", 47 | "type(c)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "### Relational Operators" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "False" 66 | ] 67 | }, 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "a = 10\n", 75 | "b = 20\n", 76 | "a > b" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "False\n", 89 | "True\n", 90 | "False\n", 91 | "True\n", 92 | "False\n", 93 | "True\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "print(a > b)\n", 99 | "print(a < b)\n", 100 | "print(a >= b)\n", 101 | "print(a <= b)\n", 102 | "print(a == b)\n", 103 | "print(a != b)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "### Logical Operations" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 5, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "False" 122 | ] 123 | }, 124 | "execution_count": 5, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "# Used to combine multiple Booleans\n", 131 | "\n", 132 | "c1 = a > 10\n", 133 | "c2 = b > 10\n", 134 | "c1 and c2" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 6, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "False\n", 147 | "True\n", 148 | "True\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "r1 = c1 and c2\n", 154 | "r2 = c1 or c2\n", 155 | "r3 = not(c1)\n", 156 | "print (r1)\n", 157 | "print (r2)\n", 158 | "print (r3)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "### IF ELSE\n", 166 | "\n" 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 | "I am inside if\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "a = True\n", 184 | "\n", 185 | "if a: \n", 186 | " print (\"I am inside if\")\n", 187 | " \n", 188 | "else:\n", 189 | " print(\"I am in else\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 8, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "I am in else\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "a = False\n", 207 | "\n", 208 | "if a: \n", 209 | " print (\"I am inside if\")\n", 210 | " \n", 211 | "else:\n", 212 | " print(\"I am in else\")" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 9, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "13\n", 225 | "n is odd\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "# check number to be even or odd\n", 231 | "n = int(input())\n", 232 | "r = n % 2\n", 233 | "is_even = (r == 0)\n", 234 | "\n", 235 | "if is_even:\n", 236 | " print(\"n is even\")\n", 237 | " \n", 238 | "else:\n", 239 | " print(\"n is odd\")" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 10, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "13\n", 252 | "n is odd\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "n = int(input())\n", 258 | "\n", 259 | "if n % 2 == 0:\n", 260 | " print(\"n is even\")\n", 261 | " \n", 262 | "else:\n", 263 | " print(\"n is odd\")" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 11, 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "14\n", 276 | "n is even\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "n = int(input())\n", 282 | "\n", 283 | "if not(n % 2):\n", 284 | " print(\"n is even\")\n", 285 | " \n", 286 | "else:\n", 287 | " print(\"n is odd\")" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 12, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "14\n", 300 | "8\n", 301 | "No they are not\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "a = int(input())\n", 307 | "b = int(input())\n", 308 | "\n", 309 | "c1 = a > 10\n", 310 | "c2 = b > 10\n", 311 | "\n", 312 | "r = c1 and c2\n", 313 | "\n", 314 | "if r:\n", 315 | " print(\"Both greater than 10\")\n", 316 | "\n", 317 | "else:\n", 318 | " print(\"No they are not\")" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 13, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "14\n", 331 | "145\n", 332 | "Both greater than 10\n" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "a = int(input())\n", 338 | "b = int(input())\n", 339 | "\n", 340 | "\n", 341 | "if a>10 and b>10:\n", 342 | " print(\"Both greater than 10\")\n", 343 | "\n", 344 | "else:\n", 345 | " print(\"No they are not\")" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 14, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "7\n", 358 | "It is 7\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "a = int(input())\n", 364 | "\n", 365 | "if a == 7:\n", 366 | " print(\"It is 7\")\n", 367 | " \n", 368 | "else:\n", 369 | " print(\"No its not 7\") " 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 15, 375 | "metadata": {}, 376 | "outputs": [ 377 | { 378 | "name": "stdout", 379 | "output_type": "stream", 380 | "text": [ 381 | "8\n", 382 | "No its not 7\n" 383 | ] 384 | } 385 | ], 386 | "source": [ 387 | "a = int(input())\n", 388 | "\n", 389 | "if a != 7:\n", 390 | " print(\"No its not 7\")\n", 391 | " \n", 392 | "else:\n", 393 | " print(\"It is 7\")" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": {}, 399 | "source": [ 400 | "### ELIF" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 16, 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "name": "stdout", 410 | "output_type": "stream", 411 | "text": [ 412 | "3\n", 413 | "3\n", 414 | "1\n", 415 | "1\n" 416 | ] 417 | } 418 | ], 419 | "source": [ 420 | "a = int(input())\n", 421 | "b = int(input())\n", 422 | "c = int(input())\n", 423 | "\n", 424 | "if a > b and a > c:\n", 425 | " print(a)\n", 426 | " \n", 427 | "elif b > a and b > c:\n", 428 | " print(b)\n", 429 | "\n", 430 | "else:\n", 431 | " print(c) # Would give wrong answer for 3, 3, 1 input" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 17, 437 | "metadata": {}, 438 | "outputs": [ 439 | { 440 | "name": "stdout", 441 | "output_type": "stream", 442 | "text": [ 443 | "14\n", 444 | "34\n", 445 | "24\n", 446 | "34\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "#Correct Code\n", 452 | "a = int(input())\n", 453 | "b = int(input())\n", 454 | "c = int(input())\n", 455 | "\n", 456 | "if a >= b and a >= c:\n", 457 | " print(a)\n", 458 | " \n", 459 | "elif b >= a and b >= c:\n", 460 | " print(b)\n", 461 | "\n", 462 | "else:\n", 463 | " print(c)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 18, 469 | "metadata": {}, 470 | "outputs": [ 471 | { 472 | "name": "stdout", 473 | "output_type": "stream", 474 | "text": [ 475 | "7\n", 476 | "green\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "n = int(input())\n", 482 | "if n > 10:\n", 483 | " print(\"red\")\n", 484 | "elif n >= 5 and n <= 10:\n", 485 | " print(\"green\")\n", 486 | "elif n > 0 and n < 5:\n", 487 | " print(\"yellow\")" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 19, 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "9\n", 500 | "green\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "n = int(input())\n", 506 | "if n > 10:\n", 507 | " print(\"red\")\n", 508 | "elif n >= 5: # Adding and n <= 10 is redundant\n", 509 | " print(\"green\")\n", 510 | "elif n > 0: # Adding and n < 5 is redundant\n", 511 | " print(\"yellow\")" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "### Nested If" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 20, 524 | "metadata": {}, 525 | "outputs": [ 526 | { 527 | "name": "stdout", 528 | "output_type": "stream", 529 | "text": [ 530 | "45\n", 531 | "n is odd\n" 532 | ] 533 | } 534 | ], 535 | "source": [ 536 | "n = int(input())\n", 537 | "\n", 538 | "if n % 2 == 0:\n", 539 | " print(\"n is even\")\n", 540 | " if n == 0:\n", 541 | " print(\"n is zero\")\n", 542 | " \n", 543 | "else:\n", 544 | " print(\"n is odd\")" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "### While loops" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 21, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "name": "stdout", 561 | "output_type": "stream", 562 | "text": [ 563 | "4\n", 564 | "1\n", 565 | "1\n", 566 | "1\n", 567 | "1\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "n = int(input())\n", 573 | "\n", 574 | "count = 1\n", 575 | "\n", 576 | "while count <= n:\n", 577 | " print(1)\n", 578 | " count += 1" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 22, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "name": "stdout", 588 | "output_type": "stream", 589 | "text": [ 590 | "5\n", 591 | "1\n", 592 | "2\n", 593 | "3\n", 594 | "4\n", 595 | "5\n" 596 | ] 597 | } 598 | ], 599 | "source": [ 600 | "# print first n natural numbers\n", 601 | "n = int(input())\n", 602 | "count = 1\n", 603 | "while count <= n:\n", 604 | " print (count)\n", 605 | " count += 1" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 23, 611 | "metadata": { 612 | "scrolled": true 613 | }, 614 | "outputs": [ 615 | { 616 | "name": "stdout", 617 | "output_type": "stream", 618 | "text": [ 619 | "56\n", 620 | "n is not prime\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "# Primality check - Given a number check if it is prime or not\n", 626 | "\n", 627 | "n = int(input())\n", 628 | "d = 2\n", 629 | "flag = False\n", 630 | "while d < n:\n", 631 | " if n % d == 0:\n", 632 | " flag = True\n", 633 | " d = d + 1\n", 634 | " \n", 635 | "if flag:\n", 636 | " print(\"n is not prime\")\n", 637 | "\n", 638 | "else:\n", 639 | " print(\"prime\")" 640 | ] 641 | }, 642 | { 643 | "cell_type": "markdown", 644 | "metadata": {}, 645 | "source": [ 646 | "### Nested Loops (While)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 24, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "name": "stdout", 656 | "output_type": "stream", 657 | "text": [ 658 | "45\n", 659 | "2\n", 660 | "3\n", 661 | "5\n", 662 | "7\n", 663 | "11\n", 664 | "13\n", 665 | "17\n", 666 | "19\n", 667 | "23\n", 668 | "29\n", 669 | "31\n", 670 | "37\n", 671 | "41\n", 672 | "43\n" 673 | ] 674 | } 675 | ], 676 | "source": [ 677 | "n = int(input())\n", 678 | "k = 2\n", 679 | "while k <= n: \n", 680 | " d = 2\n", 681 | " flag = False\n", 682 | " \n", 683 | " while d < k:\n", 684 | " if k % d == 0:\n", 685 | " flag = True\n", 686 | " d = d + 1\n", 687 | "\n", 688 | " if not(flag):\n", 689 | " print(k)\n", 690 | "\n", 691 | " \n", 692 | " k = k + 1" 693 | ] 694 | } 695 | ], 696 | "metadata": { 697 | "kernelspec": { 698 | "display_name": "Python 3", 699 | "language": "python", 700 | "name": "python3" 701 | }, 702 | "language_info": { 703 | "codemirror_mode": { 704 | "name": "ipython", 705 | "version": 3 706 | }, 707 | "file_extension": ".py", 708 | "mimetype": "text/x-python", 709 | "name": "python", 710 | "nbconvert_exporter": "python", 711 | "pygments_lexer": "ipython3", 712 | "version": "3.6.8" 713 | } 714 | }, 715 | "nbformat": 4, 716 | "nbformat_minor": 2 717 | } 718 | -------------------------------------------------------------------------------- /Lecture_04_Patterns/.ipynb_checkpoints/Patterns-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Pattern to print\n", 10 | "\n", 11 | "# ****\n", 12 | "# ****\n", 13 | "# ****\n", 14 | "# ****" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 5, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "4\n", 27 | "****\n", 28 | "****\n", 29 | "****\n", 30 | "****\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "n = int(input())\n", 36 | "i = 1\n", 37 | "while i <= n:\n", 38 | " j = 1\n", 39 | " while j <= n:\n", 40 | " print('*',end='') # By default end = newline character. End defines how you want the print to end\n", 41 | " j+=1\n", 42 | " print() \n", 43 | " i += 1" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 6, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# Pattern to print\n", 53 | "\n", 54 | "# 1111\n", 55 | "# 2222\n", 56 | "# 3333\n", 57 | "# 4444" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 8, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "4\n", 70 | "1111\n", 71 | "2222\n", 72 | "3333\n", 73 | "4444\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "n = int(input())\n", 79 | "i = 1\n", 80 | "while i <= n:\n", 81 | " j = 1\n", 82 | " while j <= n:\n", 83 | " print(i,end='')\n", 84 | " j+=1\n", 85 | " print() \n", 86 | " i += 1" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "# Pattern to print\n", 96 | "\n", 97 | "# 123\n", 98 | "# 123\n", 99 | "# 123\n", 100 | "# 123" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 9, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "3\n", 113 | "123\n", 114 | "123\n", 115 | "123\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "n = int(input())\n", 121 | "i = 1\n", 122 | "while i <= n:\n", 123 | " j = 1\n", 124 | " while j <= n:\n", 125 | " print(j,end='')\n", 126 | " j+=1\n", 127 | " print() \n", 128 | " i += 1" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 10, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "# Pattern to print\n", 138 | "\n", 139 | "# 4321\n", 140 | "# 4321\n", 141 | "# 4321\n", 142 | "# 4321" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 16, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "4\n", 155 | "4321\n", 156 | "4321\n", 157 | "4321\n", 158 | "4321\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "n = int(input())\n", 164 | "i = 1\n", 165 | "while i <= n:\n", 166 | " j = 1\n", 167 | " while j <= n:\n", 168 | " print(n+1-j,end='') \n", 169 | " j+=1\n", 170 | " print()\n", 171 | " i += 1" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 17, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "# Pattern to print\n", 181 | "\n", 182 | "# 1\n", 183 | "# 12\n", 184 | "# 123\n", 185 | "# 1234" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 20, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "4\n", 198 | "1\n", 199 | "12\n", 200 | "123\n", 201 | "1234\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "n = int(input())\n", 207 | "i = 1\n", 208 | "while i <= n:\n", 209 | " j = 1\n", 210 | " while j <= i:\n", 211 | " print(j,end='')\n", 212 | " j+=1\n", 213 | " print() \n", 214 | " i += 1" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 21, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "# Pattern to print\n", 224 | "\n", 225 | "# 1\n", 226 | "# 23\n", 227 | "# 345\n", 228 | "# 4567" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 25, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "4\n", 241 | "1\n", 242 | "23\n", 243 | "345\n", 244 | "4567\n" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "n = int(input())\n", 250 | "i = 1\n", 251 | "while i <= n:\n", 252 | " j = 1\n", 253 | " while j <= i:\n", 254 | " print(j+i-1,end='')\n", 255 | " j+=1\n", 256 | " print() \n", 257 | " i += 1" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "# Pattern to print\n", 267 | "\n", 268 | "# 1\n", 269 | "# 23\n", 270 | "# 456\n", 271 | "# 78910" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 26, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "name": "stdout", 281 | "output_type": "stream", 282 | "text": [ 283 | "4\n", 284 | "1\n", 285 | "23\n", 286 | "456\n", 287 | "78910\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "n = int(input())\n", 293 | "i = 1\n", 294 | "counter = 1\n", 295 | "while i <= n:\n", 296 | " j = 1\n", 297 | " while j <= i:\n", 298 | " print(counter,end='')\n", 299 | " counter += 1\n", 300 | " j+=1\n", 301 | " print() \n", 302 | " i += 1" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 40, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "# Character patterns\n", 312 | "# ABCD\n", 313 | "# ABCD\n", 314 | "# ABCD\n", 315 | "# ABCD" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 30, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "65" 327 | ] 328 | }, 329 | "execution_count": 30, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "# print kth alphabet\n", 336 | "\n", 337 | "#k = int(input())\n", 338 | "# 'A' + k - 1\n", 339 | "\n", 340 | "#First method (ord method)\n", 341 | "\n", 342 | "ord('A')" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 31, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "'B'" 354 | ] 355 | }, 356 | "execution_count": 31, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "chr(66) # ord -> ASCII of character, chr -> character of ASCII" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 37, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "4\n", 375 | "ABCD\n", 376 | "ABCD\n", 377 | "ABCD\n", 378 | "ABCD\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "n = int(input())\n", 384 | "i = 1\n", 385 | "while i <= n:\n", 386 | " j = 1\n", 387 | " while j <= n:\n", 388 | " print(chr(ord('A') + j -1), end='')\n", 389 | " j+=1\n", 390 | " print()\n", 391 | " i += 1" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 41, 397 | "metadata": {}, 398 | "outputs": [], 399 | "source": [ 400 | "# Character patterns\n", 401 | "# ABCD\n", 402 | "# BCDE\n", 403 | "# CDEF\n", 404 | "# DEFG" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 46, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "name": "stdout", 414 | "output_type": "stream", 415 | "text": [ 416 | "4\n", 417 | "ABCD\n", 418 | "BCDE\n", 419 | "CDEF\n", 420 | "DEFG\n" 421 | ] 422 | } 423 | ], 424 | "source": [ 425 | "n = int(input())\n", 426 | "i = 1\n", 427 | "while i <= n:\n", 428 | " j = 1\n", 429 | " while j <= n:\n", 430 | " print(chr(ord('A') + i + j - 2), end='')\n", 431 | " j+=1\n", 432 | " print()\n", 433 | " i += 1" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "metadata": {}, 447 | "outputs": [], 448 | "source": [ 449 | "n = int(input())\n", 450 | "i = 1\n", 451 | "while i <= n:\n", 452 | " j = 1\n", 453 | " while j <= n:\n", 454 | " print(chr(ord('A') + i + j - 2), end='')\n", 455 | " j+=1\n", 456 | " print()\n", 457 | " i += 1" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [ 466 | "# Pattern to print first\n", 467 | "\n", 468 | "# ****\n", 469 | "# ***\n", 470 | "# **\n", 471 | "# *" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 47, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "4\n", 484 | "****\n", 485 | "***\n", 486 | "**\n", 487 | "*\n" 488 | ] 489 | } 490 | ], 491 | "source": [ 492 | "n = int(input())\n", 493 | "i = 1\n", 494 | "while i <= n:\n", 495 | " j = 1\n", 496 | " while j <= n - i + 1:\n", 497 | " print(\"*\", end='')\n", 498 | " j+=1\n", 499 | " print()\n", 500 | " i += 1" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "metadata": {}, 507 | "outputs": [], 508 | "source": [ 509 | "# Pattern to print first\n", 510 | "\n", 511 | "# *\n", 512 | "# **\n", 513 | "# ***\n", 514 | "# ****" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 50, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "name": "stdout", 524 | "output_type": "stream", 525 | "text": [ 526 | "4\n", 527 | " *\n", 528 | " **\n", 529 | " ***\n", 530 | "****\n" 531 | ] 532 | } 533 | ], 534 | "source": [ 535 | "n = int(input())\n", 536 | "i = 1\n", 537 | "while i <= n:\n", 538 | " j = 1\n", 539 | " while j <= n - i:\n", 540 | " print(\" \", end='')\n", 541 | " j+=1\n", 542 | " while j <= n:\n", 543 | " print(\"*\", end='')\n", 544 | " j+=1\n", 545 | " print()\n", 546 | " i += 1" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 51, 552 | "metadata": {}, 553 | "outputs": [], 554 | "source": [ 555 | "# Pattern to print first\n", 556 | "\n", 557 | "# 1\n", 558 | "# 121\n", 559 | "# 12321\n", 560 | "# 1234321" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 71, 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "name": "stdout", 570 | "output_type": "stream", 571 | "text": [ 572 | "4\n", 573 | " 1\n", 574 | " 121\n", 575 | " 12321\n", 576 | "1234321\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "n = int(input())\n", 582 | "i = 1\n", 583 | "while i <= n:\n", 584 | " spaces = 1\n", 585 | " # spaces\n", 586 | " while spaces <= n - i:\n", 587 | " print(' ', end='')\n", 588 | " spaces += 1\n", 589 | " \n", 590 | " count = 1\n", 591 | " j = 1\n", 592 | " \n", 593 | " # Increasing sequence\n", 594 | " while j <= i-1:\n", 595 | " print(count, end='')\n", 596 | " j += 1\n", 597 | " count += 1\n", 598 | " \n", 599 | " #decreasing sequence\n", 600 | " \n", 601 | " while count>= 1:\n", 602 | " print(count, end='')\n", 603 | " count -= 1\n", 604 | " \n", 605 | " print()\n", 606 | " i += 1" 607 | ] 608 | } 609 | ], 610 | "metadata": { 611 | "kernelspec": { 612 | "display_name": "Python 3", 613 | "language": "python", 614 | "name": "python3" 615 | }, 616 | "language_info": { 617 | "codemirror_mode": { 618 | "name": "ipython", 619 | "version": 3 620 | }, 621 | "file_extension": ".py", 622 | "mimetype": "text/x-python", 623 | "name": "python", 624 | "nbconvert_exporter": "python", 625 | "pygments_lexer": "ipython3", 626 | "version": "3.6.8" 627 | } 628 | }, 629 | "nbformat": 4, 630 | "nbformat_minor": 2 631 | } 632 | -------------------------------------------------------------------------------- /Lecture_04_Patterns/Patterns.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Pattern to print\n", 10 | "\n", 11 | "# ****\n", 12 | "# ****\n", 13 | "# ****\n", 14 | "# ****" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "4\n", 27 | "****\n", 28 | "****\n", 29 | "****\n", 30 | "****\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "n = int(input())\n", 36 | "i = 1\n", 37 | "while i <= n:\n", 38 | " j = 1\n", 39 | " while j <= n:\n", 40 | " print('*',end='') # By default end = newline character. End defines how you want the print to end\n", 41 | " j+=1\n", 42 | " print() \n", 43 | " i += 1" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# Pattern to print\n", 53 | "\n", 54 | "# 1111\n", 55 | "# 2222\n", 56 | "# 3333\n", 57 | "# 4444" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "5\n", 70 | "11111\n", 71 | "22222\n", 72 | "33333\n", 73 | "44444\n", 74 | "55555\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "n = int(input())\n", 80 | "i = 1\n", 81 | "while i <= n:\n", 82 | " j = 1\n", 83 | " while j <= n:\n", 84 | " print(i,end='')\n", 85 | " j+=1\n", 86 | " print() \n", 87 | " i += 1" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 5, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "# Pattern to print\n", 97 | "\n", 98 | "# 123\n", 99 | "# 123\n", 100 | "# 123\n", 101 | "# 123" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 6, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "5\n", 114 | "12345\n", 115 | "12345\n", 116 | "12345\n", 117 | "12345\n", 118 | "12345\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "n = int(input())\n", 124 | "i = 1\n", 125 | "while i <= n:\n", 126 | " j = 1\n", 127 | " while j <= n:\n", 128 | " print(j,end='')\n", 129 | " j+=1\n", 130 | " print() \n", 131 | " i += 1" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 7, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# Pattern to print\n", 141 | "\n", 142 | "# 4321\n", 143 | "# 4321\n", 144 | "# 4321\n", 145 | "# 4321" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "3\n", 158 | "321\n", 159 | "321\n", 160 | "321\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "n = int(input())\n", 166 | "i = 1\n", 167 | "while i <= n:\n", 168 | " j = 1\n", 169 | " while j <= n:\n", 170 | " print(n+1-j,end='') \n", 171 | " j+=1\n", 172 | " print()\n", 173 | " i += 1" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 9, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "# Pattern to print\n", 183 | "\n", 184 | "# 1\n", 185 | "# 12\n", 186 | "# 123\n", 187 | "# 1234" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 10, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "4\n", 200 | "1\n", 201 | "12\n", 202 | "123\n", 203 | "1234\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "n = int(input())\n", 209 | "i = 1\n", 210 | "while i <= n:\n", 211 | " j = 1\n", 212 | " while j <= i:\n", 213 | " print(j,end='')\n", 214 | " j+=1\n", 215 | " print() \n", 216 | " i += 1" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 11, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [ 225 | "# Pattern to print\n", 226 | "\n", 227 | "# 1\n", 228 | "# 23\n", 229 | "# 345\n", 230 | "# 4567" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 12, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "4\n", 243 | "1\n", 244 | "23\n", 245 | "345\n", 246 | "4567\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "n = int(input())\n", 252 | "i = 1\n", 253 | "while i <= n:\n", 254 | " j = 1\n", 255 | " while j <= i:\n", 256 | " print(j+i-1,end='')\n", 257 | " j+=1\n", 258 | " print() \n", 259 | " i += 1" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 13, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "# Pattern to print\n", 269 | "\n", 270 | "# 1\n", 271 | "# 23\n", 272 | "# 456\n", 273 | "# 78910" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 14, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "name": "stdout", 283 | "output_type": "stream", 284 | "text": [ 285 | "4\n", 286 | "1\n", 287 | "23\n", 288 | "456\n", 289 | "78910\n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "n = int(input())\n", 295 | "i = 1\n", 296 | "counter = 1\n", 297 | "while i <= n:\n", 298 | " j = 1\n", 299 | " while j <= i:\n", 300 | " print(counter,end='')\n", 301 | " counter += 1\n", 302 | " j+=1\n", 303 | " print() \n", 304 | " i += 1" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 15, 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "# Character patterns\n", 314 | "# ABCD\n", 315 | "# ABCD\n", 316 | "# ABCD\n", 317 | "# ABCD" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 16, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "65" 329 | ] 330 | }, 331 | "execution_count": 16, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "# print kth alphabet\n", 338 | "\n", 339 | "#k = int(input())\n", 340 | "# 'A' + k - 1\n", 341 | "\n", 342 | "#First method (ord method)\n", 343 | "\n", 344 | "ord('A')" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 17, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "'B'" 356 | ] 357 | }, 358 | "execution_count": 17, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "chr(66) # ord -> ASCII of character, chr -> character of ASCII" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 18, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "name": "stdout", 374 | "output_type": "stream", 375 | "text": [ 376 | "5\n", 377 | "ABCDE\n", 378 | "ABCDE\n", 379 | "ABCDE\n", 380 | "ABCDE\n", 381 | "ABCDE\n" 382 | ] 383 | } 384 | ], 385 | "source": [ 386 | "n = int(input())\n", 387 | "i = 1\n", 388 | "while i <= n:\n", 389 | " j = 1\n", 390 | " while j <= n:\n", 391 | " print(chr(ord('A') + j -1), end='')\n", 392 | " j+=1\n", 393 | " print()\n", 394 | " i += 1" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 19, 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "# Character patterns\n", 404 | "# ABCD\n", 405 | "# BCDE\n", 406 | "# CDEF\n", 407 | "# DEFG" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 20, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "4\n", 420 | "ABCD\n", 421 | "BCDE\n", 422 | "CDEF\n", 423 | "DEFG\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "n = int(input())\n", 429 | "i = 1\n", 430 | "while i <= n:\n", 431 | " j = 1\n", 432 | " while j <= n:\n", 433 | " print(chr(ord('A') + i + j - 2), end='')\n", 434 | " j+=1\n", 435 | " print()\n", 436 | " i += 1" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 21, 442 | "metadata": {}, 443 | "outputs": [ 444 | { 445 | "name": "stdout", 446 | "output_type": "stream", 447 | "text": [ 448 | "5\n", 449 | "ABCDE\n", 450 | "BCDEF\n", 451 | "CDEFG\n", 452 | "DEFGH\n", 453 | "EFGHI\n" 454 | ] 455 | } 456 | ], 457 | "source": [ 458 | "n = int(input())\n", 459 | "i = 1\n", 460 | "while i <= n:\n", 461 | " j = 1\n", 462 | " while j <= n:\n", 463 | " print(chr(ord('A') + i + j - 2), end='')\n", 464 | " j+=1\n", 465 | " print()\n", 466 | " i += 1" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 22, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "# Pattern to print first\n", 476 | "\n", 477 | "# ****\n", 478 | "# ***\n", 479 | "# **\n", 480 | "# *" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 23, 486 | "metadata": {}, 487 | "outputs": [ 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "4\n", 493 | "****\n", 494 | "***\n", 495 | "**\n", 496 | "*\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "n = int(input())\n", 502 | "i = 1\n", 503 | "while i <= n:\n", 504 | " j = 1\n", 505 | " while j <= n - i + 1:\n", 506 | " print(\"*\", end='')\n", 507 | " j+=1\n", 508 | " print()\n", 509 | " i += 1" 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 24, 515 | "metadata": {}, 516 | "outputs": [], 517 | "source": [ 518 | "# Pattern to print first\n", 519 | "\n", 520 | "# *\n", 521 | "# **\n", 522 | "# ***\n", 523 | "# ****" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 25, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "5\n", 536 | " *\n", 537 | " **\n", 538 | " ***\n", 539 | " ****\n", 540 | "*****\n" 541 | ] 542 | } 543 | ], 544 | "source": [ 545 | "n = int(input())\n", 546 | "i = 1\n", 547 | "while i <= n:\n", 548 | " j = 1\n", 549 | " while j <= n - i:\n", 550 | " print(\" \", end='')\n", 551 | " j+=1\n", 552 | " while j <= n:\n", 553 | " print(\"*\", end='')\n", 554 | " j+=1\n", 555 | " print()\n", 556 | " i += 1" 557 | ] 558 | }, 559 | { 560 | "cell_type": "code", 561 | "execution_count": 26, 562 | "metadata": {}, 563 | "outputs": [], 564 | "source": [ 565 | "# Pattern to print first\n", 566 | "\n", 567 | "# 1\n", 568 | "# 121\n", 569 | "# 12321\n", 570 | "# 1234321" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 27, 576 | "metadata": {}, 577 | "outputs": [ 578 | { 579 | "name": "stdout", 580 | "output_type": "stream", 581 | "text": [ 582 | "4\n", 583 | " 1\n", 584 | " 121\n", 585 | " 12321\n", 586 | "1234321\n" 587 | ] 588 | } 589 | ], 590 | "source": [ 591 | "n = int(input())\n", 592 | "i = 1\n", 593 | "while i <= n:\n", 594 | " spaces = 1\n", 595 | " # spaces\n", 596 | " while spaces <= n - i:\n", 597 | " print(' ', end='')\n", 598 | " spaces += 1\n", 599 | " \n", 600 | " count = 1\n", 601 | " j = 1\n", 602 | " \n", 603 | " # Increasing sequence\n", 604 | " while j <= i-1:\n", 605 | " print(count, end='')\n", 606 | " j += 1\n", 607 | " count += 1\n", 608 | " \n", 609 | " #decreasing sequence\n", 610 | " \n", 611 | " while count>= 1:\n", 612 | " print(count, end='')\n", 613 | " count -= 1\n", 614 | " \n", 615 | " print()\n", 616 | " i += 1" 617 | ] 618 | } 619 | ], 620 | "metadata": { 621 | "kernelspec": { 622 | "display_name": "Python 3", 623 | "language": "python", 624 | "name": "python3" 625 | }, 626 | "language_info": { 627 | "codemirror_mode": { 628 | "name": "ipython", 629 | "version": 3 630 | }, 631 | "file_extension": ".py", 632 | "mimetype": "text/x-python", 633 | "name": "python", 634 | "nbconvert_exporter": "python", 635 | "pygments_lexer": "ipython3", 636 | "version": "3.6.8" 637 | } 638 | }, 639 | "nbformat": 4, 640 | "nbformat_minor": 2 641 | } 642 | -------------------------------------------------------------------------------- /Lecture_05_Loops&RangeMethod/.ipynb_checkpoints/ForLoops&RangeMethod-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# For i in x: here x will be a sequence (x is a collection of multiple things at one place) \n", 10 | "# Eg. strings, lists, arrays" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 3, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "a\n", 23 | "b\n", 24 | "c\n", 25 | "d\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "s = \"abcd\"\n", 31 | "\n", 32 | "for c in s:\n", 33 | " print(c)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 7, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "6\n", 46 | "1\n", 47 | "2\n", 48 | "3\n", 49 | "4\n", 50 | "5\n", 51 | "6\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "n = int(input())\n", 57 | "\n", 58 | "for i in range(1, n+1, 1): # range takes parameters: start, stop, stride (steps), range stops 1 step before stop\n", 59 | " print (i)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 10, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "4\n", 72 | "0\n", 73 | "1\n", 74 | "2\n", 75 | "3\n", 76 | "4\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "n = int(input())\n", 82 | "for i in range(n+1): # By default start = 0, step = 1\n", 83 | " print(i)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 11, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "5\n", 96 | "5\n", 97 | "4\n", 98 | "3\n", 99 | "2\n", 100 | "1\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "n = int(input())\n", 106 | "for i in range(n, 0, -1): # By default start = 0, step = 1\n", 107 | " print(i)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 16, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "2\n", 120 | "20\n", 121 | "3\n", 122 | "6\n", 123 | "9\n", 124 | "12\n", 125 | "15\n", 126 | "18\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "# Print multiples of 3 \n", 132 | "# a < b\n", 133 | "\n", 134 | "a = int(input())\n", 135 | "b = int(input())\n", 136 | "\n", 137 | "for i in range(a, b + 1):\n", 138 | " if i % 3 == 0:\n", 139 | " print(i)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 25, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "10\n", 152 | "40\n", 153 | "12\n", 154 | "15\n", 155 | "18\n", 156 | "21\n", 157 | "24\n", 158 | "27\n", 159 | "30\n", 160 | "33\n", 161 | "36\n", 162 | "39\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "# Print multiples of 3 \n", 168 | "# a < b\n", 169 | "\n", 170 | "a = int(input())\n", 171 | "b = int(input())\n", 172 | "\n", 173 | "a = a if a%3==0 else (a + 3 - a%3)\n", 174 | "\n", 175 | "for i in range(a, b + 1):\n", 176 | " if i % 3 == 0:\n", 177 | " print(i)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 27, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "31\n", 190 | "prime\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "# Check if number is prime\n", 196 | "\n", 197 | "n = int(input())\n", 198 | "flag = False\n", 199 | "\n", 200 | "for d in range(2, n):\n", 201 | " if n % d == 0:\n", 202 | " flag = True\n", 203 | " \n", 204 | "if flag:\n", 205 | " print(\"not prime\")\n", 206 | " \n", 207 | "else:\n", 208 | " print(\"prime\")" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 28, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "# # Pattern to print\n", 218 | "# 1\n", 219 | "# 232\n", 220 | "# 34542\n", 221 | "# 4567654" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 29, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "4\n", 234 | " 1\n", 235 | " 232\n", 236 | " 34543\n", 237 | "4567654\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "n = int(input())\n", 243 | "for i in range(1, n + 1):\n", 244 | " for s in range(n-i):\n", 245 | " print(' ', end = '')\n", 246 | " \n", 247 | " for j in range(i, 2*i):\n", 248 | " print(j, end = '')\n", 249 | " \n", 250 | " for j in range(2 * i - 2, i - 1, -1):\n", 251 | " print(j, end = '')\n", 252 | " \n", 253 | " print()" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 30, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "1\n", 266 | "2\n", 267 | "3\n", 268 | "4\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "# break keyword | Alternative way of breaking the loop\n", 274 | "\n", 275 | "i = 1\n", 276 | "\n", 277 | "while i < 10:\n", 278 | " if i == 5:\n", 279 | " break\n", 280 | " print(i)\n", 281 | " i = i + 1\n" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 31, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "34\n", 294 | "not prime\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "# Check if number is prime\n", 300 | "\n", 301 | "n = int(input())\n", 302 | "flag = False\n", 303 | "\n", 304 | "for d in range(2, n):\n", 305 | " if n % d == 0:\n", 306 | " flag = True\n", 307 | " break\n", 308 | " \n", 309 | "if flag:\n", 310 | " print(\"not prime\")\n", 311 | " \n", 312 | "else:\n", 313 | " print(\"prime\")" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 33, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "10\n", 326 | "2\n", 327 | "3\n", 328 | "5\n", 329 | "7\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "# break will break from the loop just outside it\n", 335 | "\n", 336 | "n = int(input())\n", 337 | "k = 2\n", 338 | "\n", 339 | "while k <= n:\n", 340 | " d = 2\n", 341 | " flag = False\n", 342 | " while d < k:\n", 343 | " if k % d == 0:\n", 344 | " flag = True\n", 345 | " break\n", 346 | " d = d + 1\n", 347 | " \n", 348 | " if (not (flag)):\n", 349 | " print (k)\n", 350 | " k += 1" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 35, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "print at end\n" 370 | ] 371 | } 372 | ], 373 | "source": [ 374 | "# in case of Python else can be used along with loops\n", 375 | "\n", 376 | "# else followed by 'while' will run after while loop is no more possiblei = 1\n", 377 | "while i < 10:\n", 378 | " print (i)\n", 379 | " i = i + 1\n", 380 | "else:\n", 381 | " print (\"print at end\")" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": null, 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [] 390 | } 391 | ], 392 | "metadata": { 393 | "kernelspec": { 394 | "display_name": "Python 3", 395 | "language": "python", 396 | "name": "python3" 397 | }, 398 | "language_info": { 399 | "codemirror_mode": { 400 | "name": "ipython", 401 | "version": 3 402 | }, 403 | "file_extension": ".py", 404 | "mimetype": "text/x-python", 405 | "name": "python", 406 | "nbconvert_exporter": "python", 407 | "pygments_lexer": "ipython3", 408 | "version": "3.6.8" 409 | } 410 | }, 411 | "nbformat": 4, 412 | "nbformat_minor": 2 413 | } 414 | -------------------------------------------------------------------------------- /Lecture_05_Loops&RangeMethod/ForLoops&RangeMethod.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# For i in x: here x will be a sequence (x is a collection of multiple things at one place) \n", 10 | "# Eg. strings, lists, arrays" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "a\n", 23 | "b\n", 24 | "c\n", 25 | "d\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "s = \"abcd\"\n", 31 | "\n", 32 | "for c in s:\n", 33 | " print(c)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "5\n", 46 | "1\n", 47 | "2\n", 48 | "3\n", 49 | "4\n", 50 | "5\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "n = int(input())\n", 56 | "\n", 57 | "for i in range(1, n+1, 1): # range takes parameters: start, stop, stride (steps), range stops 1 step before stop\n", 58 | " print (i)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "5\n", 71 | "0\n", 72 | "1\n", 73 | "2\n", 74 | "3\n", 75 | "4\n", 76 | "5\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "n = int(input())\n", 82 | "for i in range(n+1): # By default start = 0, step = 1\n", 83 | " print(i)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 5, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "5\n", 96 | "5\n", 97 | "4\n", 98 | "3\n", 99 | "2\n", 100 | "1\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "n = int(input())\n", 106 | "for i in range(n, 0, -1): # By default start = 0, step = 1\n", 107 | " print(i)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 6, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "13\n", 120 | "24\n", 121 | "15\n", 122 | "18\n", 123 | "21\n", 124 | "24\n" 125 | ] 126 | } 127 | ], 128 | "source": [ 129 | "# Print multiples of 3 \n", 130 | "# a < b\n", 131 | "\n", 132 | "a = int(input())\n", 133 | "b = int(input())\n", 134 | "\n", 135 | "for i in range(a, b + 1):\n", 136 | " if i % 3 == 0:\n", 137 | " print(i)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 7, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "13\n", 150 | "26\n", 151 | "15\n", 152 | "18\n", 153 | "21\n", 154 | "24\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "# Print multiples of 3 \n", 160 | "# a < b\n", 161 | "\n", 162 | "a = int(input())\n", 163 | "b = int(input())\n", 164 | "\n", 165 | "a = a if a%3==0 else (a + 3 - a%3)\n", 166 | "\n", 167 | "for i in range(a, b + 1):\n", 168 | " if i % 3 == 0:\n", 169 | " print(i)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 8, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "45\n", 182 | "not prime\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "# Check if number is prime\n", 188 | "\n", 189 | "n = int(input())\n", 190 | "flag = False\n", 191 | "\n", 192 | "for d in range(2, n):\n", 193 | " if n % d == 0:\n", 194 | " flag = True\n", 195 | " \n", 196 | "if flag:\n", 197 | " print(\"not prime\")\n", 198 | " \n", 199 | "else:\n", 200 | " print(\"prime\")" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 9, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "# # Pattern to print\n", 210 | "# 1\n", 211 | "# 232\n", 212 | "# 34542\n", 213 | "# 4567654" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 10, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "4\n", 226 | " 1\n", 227 | " 232\n", 228 | " 34543\n", 229 | "4567654\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "n = int(input())\n", 235 | "for i in range(1, n + 1):\n", 236 | " for s in range(n-i):\n", 237 | " print(' ', end = '')\n", 238 | " \n", 239 | " for j in range(i, 2*i):\n", 240 | " print(j, end = '')\n", 241 | " \n", 242 | " for j in range(2 * i - 2, i - 1, -1):\n", 243 | " print(j, end = '')\n", 244 | " \n", 245 | " print()" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 11, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "1\n", 258 | "2\n", 259 | "3\n", 260 | "4\n" 261 | ] 262 | } 263 | ], 264 | "source": [ 265 | "# break keyword | Alternative way of breaking the loop\n", 266 | "\n", 267 | "i = 1\n", 268 | "\n", 269 | "while i < 10:\n", 270 | " if i == 5:\n", 271 | " break\n", 272 | " print(i)\n", 273 | " i = i + 1\n" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 12, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "name": "stdout", 283 | "output_type": "stream", 284 | "text": [ 285 | "35\n", 286 | "not prime\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "# Check if number is prime\n", 292 | "\n", 293 | "n = int(input())\n", 294 | "flag = False\n", 295 | "\n", 296 | "for d in range(2, n):\n", 297 | " if n % d == 0:\n", 298 | " flag = True\n", 299 | " break\n", 300 | " \n", 301 | "if flag:\n", 302 | " print(\"not prime\")\n", 303 | " \n", 304 | "else:\n", 305 | " print(\"prime\")" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 13, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "name": "stdout", 315 | "output_type": "stream", 316 | "text": [ 317 | "23\n", 318 | "2\n", 319 | "3\n", 320 | "5\n", 321 | "7\n", 322 | "11\n", 323 | "13\n", 324 | "17\n", 325 | "19\n", 326 | "23\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "# break will break from the loop just outside it\n", 332 | "\n", 333 | "n = int(input())\n", 334 | "k = 2\n", 335 | "\n", 336 | "while k <= n:\n", 337 | " d = 2\n", 338 | " flag = False\n", 339 | " while d < k:\n", 340 | " if k % d == 0:\n", 341 | " flag = True\n", 342 | " break\n", 343 | " d = d + 1\n", 344 | " \n", 345 | " if (not (flag)):\n", 346 | " print (k)\n", 347 | " k += 1" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 14, 353 | "metadata": {}, 354 | "outputs": [], 355 | "source": [ 356 | "# In case of Python else can be used along with loops" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 15, 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "name": "stdout", 366 | "output_type": "stream", 367 | "text": [ 368 | "0\n", 369 | "1\n", 370 | "2\n", 371 | "3\n", 372 | "4\n", 373 | "5\n", 374 | "6\n", 375 | "7\n", 376 | "8\n", 377 | "9\n", 378 | "print at end\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "\n", 384 | "# else followed by 'while' will run after while loop is no more possiblei = 1\n", 385 | "i = 0\n", 386 | "while i < 10:\n", 387 | " print (i)\n", 388 | " i = i + 1\n", 389 | "else:\n", 390 | " print (\"print at end\")" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 16, 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "1\n", 403 | "2\n", 404 | "3\n", 405 | "4\n", 406 | "5\n", 407 | "6\n", 408 | "7\n", 409 | "8\n", 410 | "9\n", 411 | "print at end\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "# in case of else\n", 417 | "\n", 418 | "for i in range (1,10):\n", 419 | " print (i)\n", 420 | " i = i + 1\n", 421 | "else:\n", 422 | " print (\"print at end\")" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 17, 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [ 431 | "# why do we need else?\n", 432 | "# The situation becomes interesting when you use the break statement\n", 433 | "# code inside else will not be execute if the loop finished because of break statement" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 18, 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "name": "stdout", 443 | "output_type": "stream", 444 | "text": [ 445 | "1\n", 446 | "2\n", 447 | "3\n", 448 | "4\n" 449 | ] 450 | } 451 | ], 452 | "source": [ 453 | "for i in range(1, 10):\n", 454 | " if i == 5:\n", 455 | " break\n", 456 | " print(i)\n", 457 | "\n", 458 | "else:\n", 459 | " print(\"won't run\")" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 19, 465 | "metadata": {}, 466 | "outputs": [ 467 | { 468 | "name": "stdout", 469 | "output_type": "stream", 470 | "text": [ 471 | "26\n" 472 | ] 473 | } 474 | ], 475 | "source": [ 476 | "# Check if number is prime\n", 477 | "\n", 478 | "n = int(input())\n", 479 | "flag = False\n", 480 | "\n", 481 | "for d in range(2, n):\n", 482 | " if n % d == 0:\n", 483 | " break\n", 484 | " \n", 485 | "else:\n", 486 | " print(\"prime number\")" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 20, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "22\n", 499 | "2\n", 500 | "4\n", 501 | "6\n", 502 | "8\n", 503 | "10\n", 504 | "12\n", 505 | "16\n", 506 | "18\n", 507 | "20\n", 508 | "22\n" 509 | ] 510 | } 511 | ], 512 | "source": [ 513 | "# continue keyword -> used to skip an iteration\n", 514 | "\n", 515 | "# print all even numbers till n except multiples of 7\n", 516 | "n = int(input())\n", 517 | "for i in range(2, n+1, 2):\n", 518 | " if( i%7 == 0):\n", 519 | " continue\n", 520 | " print (i)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 21, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "24\n", 533 | "2\n", 534 | "2\n", 535 | "4\n", 536 | "4\n", 537 | "6\n", 538 | "6\n", 539 | "8\n", 540 | "8\n", 541 | "10\n", 542 | "10\n", 543 | "12\n", 544 | "12\n", 545 | "14\n", 546 | "16\n", 547 | "16\n", 548 | "18\n", 549 | "18\n", 550 | "20\n", 551 | "20\n", 552 | "22\n", 553 | "22\n", 554 | "24\n", 555 | "24\n" 556 | ] 557 | } 558 | ], 559 | "source": [ 560 | "# print all even numbers twice multiples of 7 once\n", 561 | "n = int(input())\n", 562 | "for i in range(2, n+1, 2):\n", 563 | " print (i)\n", 564 | " if( i%7 == 0):\n", 565 | " continue\n", 566 | " print(i)" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 27, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "name": "stdout", 576 | "output_type": "stream", 577 | "text": [ 578 | "3\n" 579 | ] 580 | } 581 | ], 582 | "source": [ 583 | "# pass keyword\n", 584 | "i = 3\n", 585 | "if i < 7:\n", 586 | " pass\n", 587 | "print(i)\n" 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 28, 593 | "metadata": {}, 594 | "outputs": [ 595 | { 596 | "name": "stdout", 597 | "output_type": "stream", 598 | "text": [ 599 | "7\n" 600 | ] 601 | } 602 | ], 603 | "source": [ 604 | "i = 3\n", 605 | "while i < 7:\n", 606 | " pass\n", 607 | " i+=1\n", 608 | "print(i)" 609 | ] 610 | } 611 | ], 612 | "metadata": { 613 | "kernelspec": { 614 | "display_name": "Python 3", 615 | "language": "python", 616 | "name": "python3" 617 | }, 618 | "language_info": { 619 | "codemirror_mode": { 620 | "name": "ipython", 621 | "version": 3 622 | }, 623 | "file_extension": ".py", 624 | "mimetype": "text/x-python", 625 | "name": "python", 626 | "nbconvert_exporter": "python", 627 | "pygments_lexer": "ipython3", 628 | "version": "3.6.8" 629 | } 630 | }, 631 | "nbformat": 4, 632 | "nbformat_minor": 2 633 | } 634 | -------------------------------------------------------------------------------- /Lecture_06_Strings_Lists_2DLists/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_07_Functions/Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions" 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 | "5\n", 20 | "2\n", 21 | "10\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "# Let us make a program and through it try to understand why we need functions\n", 27 | "\n", 28 | "#Program to find nCr\n", 29 | "\n", 30 | "n = int(input())\n", 31 | "r = int(input())\n", 32 | "\n", 33 | "n_fact = 1\n", 34 | "\n", 35 | "for i in range(1, n + 1):\n", 36 | " n_fact = n_fact*i\n", 37 | "\n", 38 | "r_fact = 1\n", 39 | "\n", 40 | "for i in range(1, r + 1):\n", 41 | " r_fact = r_fact*i\n", 42 | " \n", 43 | "n_r_fact = 1\n", 44 | "\n", 45 | "for i in range(1, n - r + 1):\n", 46 | " n_r_fact = n_r_fact*i\n", 47 | " \n", 48 | "ans = n_fact//(r_fact * n_r_fact)\n", 49 | "\n", 50 | "print (ans)\n", 51 | "\n", 52 | "# In the above code, we have repeated the almost the same code 3 times.\n", 53 | "# We would like if we had return some method fact which asks n and computes n!" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "720" 65 | ] 66 | }, 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Functions allow you to create block of code which you can call from anywhere\n", 74 | "\n", 75 | "# Syntax for function: def fact(n)\n", 76 | "\n", 77 | "def fact(n):\n", 78 | " n_fact = 1\n", 79 | " \n", 80 | " for i in range(1, n + 1):\n", 81 | " n_fact = n_fact*i\n", 82 | " \n", 83 | " return n_fact\n", 84 | "\n", 85 | "fact(6)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 3, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "40320" 97 | ] 98 | }, 99 | "execution_count": 3, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "# You can also store the value which was returned by function\n", 106 | "\n", 107 | "a = fact(8)\n", 108 | "a" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 4, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "10\n", 121 | "3\n", 122 | "120\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "n = int(input())\n", 128 | "r = int(input())\n", 129 | "n_fact = fact(n)\n", 130 | "r_fact = fact(r)\n", 131 | "n_r_fact = fact(n-r)\n", 132 | "ans = n_fact//(r_fact * n_r_fact)\n", 133 | "print(ans)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# Another place to use functions are Readability, Testability" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 6, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "# Function to check if it is prime\n", 152 | "\n", 153 | "def isPrime(n):\n", 154 | " for d in range(2, n):\n", 155 | " if (n % d == 0):\n", 156 | " break\n", 157 | " \n", 158 | " else:\n", 159 | " return True\n", 160 | " \n", 161 | " return False" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 7, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "data": { 171 | "text/plain": [ 172 | "False" 173 | ] 174 | }, 175 | "execution_count": 7, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "isPrime(10)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 8, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "True" 193 | ] 194 | }, 195 | "execution_count": 8, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "isPrime(31)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 9, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "# Print all prime numbers from 2 to n\n", 211 | "\n", 212 | "def printPrime(n):\n", 213 | " for k in range(2, n + 1):\n", 214 | " # check if k is prime and in case it is prime print k\n", 215 | " is_k_prime = isPrime(k)\n", 216 | " if(is_k_prime):\n", 217 | " print(k)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 10, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "2\n", 230 | "3\n", 231 | "5\n", 232 | "7\n", 233 | "11\n", 234 | "13\n", 235 | "17\n", 236 | "19\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "printPrime(20)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 11, 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [ 250 | "# Function to calculate nCr\n", 251 | "\n", 252 | "def ncr(n, r):\n", 253 | " n_fact = fact(n)\n", 254 | " r_fact = fact(r)\n", 255 | " n_r_fact = fact(n-r)\n", 256 | " ans = n_fact//(r_fact * n_r_fact)\n", 257 | " return ans\n", 258 | " " 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 12, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "3003" 270 | ] 271 | }, 272 | "execution_count": 12, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "ncr(15, 5)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 13, 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "data": { 288 | "text/plain": [ 289 | "10" 290 | ] 291 | }, 292 | "execution_count": 13, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "ncr(5, 3)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": {}, 304 | "source": [ 305 | "#### A call stack is a stack data structure that stores information about the active subroutines of a program." 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 14, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "name": "stdout", 315 | "output_type": "stream", 316 | "text": [ 317 | "10\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "# How does lists work along with functions\n", 323 | "def increment(i):\n", 324 | " i = i + 1\n", 325 | " \n", 326 | "k = 10\n", 327 | "increment(k)\n", 328 | "print(k)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 15, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "ids when k and i equal 10969088 10969088\n", 341 | "ids when k and i are different 10969088 10969120\n", 342 | "10\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "# In above code, k has not changed.\n", 348 | "# Because as soon as i = i + 1, i starts storing address of 11 (10+1)\n", 349 | "# Lets check ids of k and i at different stages\n", 350 | "\n", 351 | "# How does lists work along with functions\n", 352 | "def increment(i):\n", 353 | " print (\"ids when k and i equal\", id(k), id(i))\n", 354 | " i = i + 1\n", 355 | " print (\"ids when k and i are different\", id(k), id(i))\n", 356 | " \n", 357 | "k = 10\n", 358 | "increment(k)\n", 359 | "print(k)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 16, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "name": "stdout", 369 | "output_type": "stream", 370 | "text": [ 371 | "[2, 2, 3]\n" 372 | ] 373 | } 374 | ], 375 | "source": [ 376 | "#Example 1: How function takes value in case of a list\n", 377 | "\n", 378 | "def increment_list_first(l):\n", 379 | " l[0] = l[0] + 1\n", 380 | "a = [1, 2, 3]\n", 381 | "increment_list_first(a)\n", 382 | "print(a)" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 17, 388 | "metadata": {}, 389 | "outputs": [], 390 | "source": [ 391 | "# In the above example the list got modified\n", 392 | "# When you passed a in increment_list_first() function, both a, l pointed to same location\n", 393 | "# ie address to [1, 2, 3]" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 18, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "name": "stdout", 403 | "output_type": "stream", 404 | "text": [ 405 | "ids when a and l before modification in l 140236392259784 140236392259784\n", 406 | "ids when a and l after modification in l 140236392259784 140236392260104\n", 407 | "[1, 2, 3]\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "# Example 2: Let us try changes the list itself\n", 413 | "\n", 414 | "def increment_list_first(l):\n", 415 | " print (\"ids when a and l before modification in l\", id(a), id(l)) \n", 416 | " l = [2]\n", 417 | " print (\"ids when a and l after modification in l\", id(a), id(l))\n", 418 | "a = [1, 2, 3]\n", 419 | "increment_list_first(a)\n", 420 | "print(a)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 19, 426 | "metadata": {}, 427 | "outputs": [], 428 | "source": [ 429 | "# Above we can see that we said, \"forgot about a, point to a new list now\"\n", 430 | "# If you change variable to point somewhere else we cannot modify the data " 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 20, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "data": { 440 | "text/plain": [ 441 | "'def'" 442 | ] 443 | }, 444 | "execution_count": 20, 445 | "metadata": {}, 446 | "output_type": "execute_result" 447 | } 448 | ], 449 | "source": [ 450 | "# Strings are little different, cause' remember immutable?\n", 451 | "# You cannot write function like Example 1 for strings\n", 452 | "\n", 453 | "# Below function changing its own variable, not string\n", 454 | "\n", 455 | "def change_str(b):\n", 456 | " b = \"abc\"\n", 457 | "\n", 458 | "a = \"def\"\n", 459 | "change_str(a)\n", 460 | "a" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 21, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" 472 | ] 473 | }, 474 | "execution_count": 21, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "# Returning List from a function\n", 481 | "\n", 482 | "def create_rev_array(n):\n", 483 | " l = []\n", 484 | " for i in range(n, 0, -1):\n", 485 | " l.append(i)\n", 486 | " return l\n", 487 | "\n", 488 | "l = create_rev_array(10)\n", 489 | "l" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 22, 495 | "metadata": {}, 496 | "outputs": [ 497 | { 498 | "name": "stdout", 499 | "output_type": "stream", 500 | "text": [ 501 | "[4, 3, 6, 5]\n" 502 | ] 503 | } 504 | ], 505 | "source": [ 506 | "# Swap Alternate\n", 507 | "\n", 508 | "def swap_consecutive(l):\n", 509 | " i = 0\n", 510 | " while i+1 < len(l):\n", 511 | " l[i], l[i+1] = l[i+1], l[i]\n", 512 | " i = i + 2\n", 513 | " return l\n", 514 | "\n", 515 | "print(swap_consecutive([3,4,5,6])) #even numbered" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 23, 521 | "metadata": {}, 522 | "outputs": [ 523 | { 524 | "name": "stdout", 525 | "output_type": "stream", 526 | "text": [ 527 | "[4, 3, 5]\n" 528 | ] 529 | } 530 | ], 531 | "source": [ 532 | "print(swap_consecutive([3,4,5])) #odd numbered" 533 | ] 534 | }, 535 | { 536 | "cell_type": "markdown", 537 | "metadata": {}, 538 | "source": [ 539 | "## Scope of variable" 540 | ] 541 | }, 542 | { 543 | "cell_type": "markdown", 544 | "metadata": {}, 545 | "source": [ 546 | "### Broadly there are two types of variable in Python\n", 547 | "\n", 548 | "* Local variables: Defined inside a function\n", 549 | "* Global variables: Defined outside any function" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 24, 555 | "metadata": {}, 556 | "outputs": [ 557 | { 558 | "name": "stdout", 559 | "output_type": "stream", 560 | "text": [ 561 | "10\n" 562 | ] 563 | } 564 | ], 565 | "source": [ 566 | "a1 = 10 # global variable\n", 567 | "\n", 568 | "def f1():\n", 569 | " b1 = 12 # local variable\n", 570 | " \n", 571 | " \n", 572 | "print(a1)\n", 573 | "# print(b1) throws error - name 'b1' is not defined\n", 574 | "# As the function is local and avaiable only to f1()" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 25, 580 | "metadata": {}, 581 | "outputs": [ 582 | { 583 | "name": "stdout", 584 | "output_type": "stream", 585 | "text": [ 586 | "10\n", 587 | "12\n" 588 | ] 589 | } 590 | ], 591 | "source": [ 592 | "a1 = 10 # global variable\n", 593 | "\n", 594 | "def f2():\n", 595 | " b1 = 12 # local variable\n", 596 | " print (b1)\n", 597 | " \n", 598 | "print(a1)\n", 599 | "f2()\n", 600 | "# print(b1) throws error - name 'b1' is not defined" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 26, 606 | "metadata": {}, 607 | "outputs": [ 608 | { 609 | "name": "stdout", 610 | "output_type": "stream", 611 | "text": [ 612 | "10\n", 613 | "10\n", 614 | "12\n" 615 | ] 616 | } 617 | ], 618 | "source": [ 619 | "# You can access any global variable defined before the function call\n", 620 | "\n", 621 | "a1 = 10 # global variable\n", 622 | "\n", 623 | "def f3():\n", 624 | " b1 = 12 # local variable\n", 625 | " print(a1) # you can print global variable inside a function\n", 626 | " print (b1)\n", 627 | " \n", 628 | "print(a1) # a1 must be defined before calling the function\n", 629 | "# If you want to use a1 inside the function\n", 630 | "f3()" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 27, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "name": "stdout", 640 | "output_type": "stream", 641 | "text": [ 642 | "13 10969184\n", 643 | "12 10969152\n", 644 | "13 10969184\n" 645 | ] 646 | } 647 | ], 648 | "source": [ 649 | "a4 = 13\n", 650 | "\n", 651 | "def f4():\n", 652 | " a4 = 12\n", 653 | " print(a4, id(a4))\n", 654 | "\n", 655 | "print(a4, id(a4))\n", 656 | "f4()\n", 657 | "print(a4, id(a4))\n", 658 | "\n", 659 | "# If you want to print the value as is, there will be no problem\n", 660 | "# But when you try to change values,\n", 661 | "# Python assumes you want to create a local variabe." 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 28, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [ 670 | "# In case you want to use global variable just write global before its name" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 29, 676 | "metadata": {}, 677 | "outputs": [ 678 | { 679 | "name": "stdout", 680 | "output_type": "stream", 681 | "text": [ 682 | "13 10969184\n", 683 | "12 10969152\n", 684 | "12 10969152\n" 685 | ] 686 | } 687 | ], 688 | "source": [ 689 | "a4 = 13\n", 690 | "\n", 691 | "def f4():\n", 692 | " global a4\n", 693 | " a4 = 12\n", 694 | " print(a4, id(a4))\n", 695 | "\n", 696 | "print(a4, id(a4))\n", 697 | "f4()\n", 698 | "print(a4, id(a4))" 699 | ] 700 | }, 701 | { 702 | "cell_type": "code", 703 | "execution_count": 30, 704 | "metadata": {}, 705 | "outputs": [ 706 | { 707 | "name": "stdout", 708 | "output_type": "stream", 709 | "text": [ 710 | "10\n" 711 | ] 712 | } 713 | ], 714 | "source": [ 715 | "# Only way to get the local varables of a function is by returning those variables\n", 716 | "\n", 717 | "def f():\n", 718 | " b = 10\n", 719 | " return b\n", 720 | "\n", 721 | "x = f()\n", 722 | "print(x)" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": 31, 728 | "metadata": {}, 729 | "outputs": [ 730 | { 731 | "name": "stdout", 732 | "output_type": "stream", 733 | "text": [ 734 | "abcdabc\n" 735 | ] 736 | } 737 | ], 738 | "source": [ 739 | "# Default Arguments in Python\n", 740 | "\n", 741 | "# We were able to call print in multiple ways\n", 742 | "\n", 743 | "print(\"abcd\", end=\"\")\n", 744 | "print(\"abc\")" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": 32, 750 | "metadata": {}, 751 | "outputs": [ 752 | { 753 | "name": "stdout", 754 | "output_type": "stream", 755 | "text": [ 756 | "7\n" 757 | ] 758 | } 759 | ], 760 | "source": [ 761 | "def f(n):\n", 762 | " print(n)\n", 763 | " \n", 764 | "f(7) # f(n) requires one argument, compiler will throw error if you pass\n", 765 | "# less than 1 or more than one arguments\n", 766 | "\n", 767 | "#But print was able to accept both 1 argument and 2 arguments" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 33, 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "c is 4\n", 780 | "a + b + c is 9\n", 781 | "c is 0\n", 782 | "a + b is 5\n" 783 | ] 784 | } 785 | ], 786 | "source": [ 787 | "def sum(a, b):\n", 788 | " return a + b\n", 789 | "\n", 790 | "# we wish to have capability to pass 3 arguments also in the above function\n", 791 | "\n", 792 | "# Python allows you to have a default argument\n", 793 | "\n", 794 | "def sum1(a, b, c = 0):\n", 795 | " print(\"c is \", c)\n", 796 | " return a + b + c\n", 797 | "\n", 798 | "print(\"a + b + c is \", sum1(2, 3, 4)) # sum with 3 arguments passed for c\n", 799 | "print(\"a + b is\", sum1(2,3)) # sum with 2 arguments (c hence by default get understood as 3 by function)" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": 34, 805 | "metadata": {}, 806 | "outputs": [], 807 | "source": [ 808 | "# We cannot set a = 0 below (non-default argument cannot follow default argument)\n", 809 | "\n", 810 | "# meaning: Put all non-default argument first then all default\n", 811 | "\n", 812 | "def f(a, b, c):\n", 813 | " return a + b + c" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": 35, 819 | "metadata": {}, 820 | "outputs": [ 821 | { 822 | "name": "stdout", 823 | "output_type": "stream", 824 | "text": [ 825 | "a + b + c + d is 14\n", 826 | "a + b + c is 10\n", 827 | "a + b is 8\n" 828 | ] 829 | } 830 | ], 831 | "source": [ 832 | "def f1(a, b, c = 2, d = 1):\n", 833 | " return a + b + c + d\n", 834 | "\n", 835 | "print(\"a + b + c + d is\", f1(2, 3, 4, 5))\n", 836 | "print(\"a + b + c is\", f1(2, 3, 4))\n", 837 | "print(\"a + b is\", f1(2,3))\n" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": 36, 843 | "metadata": {}, 844 | "outputs": [ 845 | { 846 | "name": "stdout", 847 | "output_type": "stream", 848 | "text": [ 849 | "50\n" 850 | ] 851 | } 852 | ], 853 | "source": [ 854 | "# If you mention specifically the value of non default argument, you\n", 855 | "# dont have to maintain the order\n", 856 | "\n", 857 | "def f1(a, b, c = 2, d = 1):\n", 858 | " return a + b + c + d\n", 859 | "\n", 860 | "print(f1(d = 32, b = 12, a = 4))" 861 | ] 862 | } 863 | ], 864 | "metadata": { 865 | "kernelspec": { 866 | "display_name": "Python 3", 867 | "language": "python", 868 | "name": "python3" 869 | }, 870 | "language_info": { 871 | "codemirror_mode": { 872 | "name": "ipython", 873 | "version": 3 874 | }, 875 | "file_extension": ".py", 876 | "mimetype": "text/x-python", 877 | "name": "python", 878 | "nbconvert_exporter": "python", 879 | "pygments_lexer": "ipython3", 880 | "version": "3.6.8" 881 | } 882 | }, 883 | "nbformat": 4, 884 | "nbformat_minor": 2 885 | } 886 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/.ipynb_checkpoints/Dictionaries-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/.ipynb_checkpoints/Sets-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/.ipynb_checkpoints/Tuples-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Tuples" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# Tuples are similar to lists\n", 17 | "# Syntax b = (1,2)\n", 18 | "\n", 19 | "# In Python by default multiple things tend to behave like tuples\n", 20 | "\n", 21 | "# e = 1, 2 " 22 | ] 23 | } 24 | ], 25 | "metadata": { 26 | "kernelspec": { 27 | "display_name": "Python 3", 28 | "language": "python", 29 | "name": "python3" 30 | }, 31 | "language_info": { 32 | "codemirror_mode": { 33 | "name": "ipython", 34 | "version": 3 35 | }, 36 | "file_extension": ".py", 37 | "mimetype": "text/x-python", 38 | "name": "python", 39 | "nbconvert_exporter": "python", 40 | "pygments_lexer": "ipython3", 41 | "version": "3.6.8" 42 | } 43 | }, 44 | "nbformat": 4, 45 | "nbformat_minor": 2 46 | } 47 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Dictionaries" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "* In case of dictionaries you can use whatever key you wish unlike in lists where its the set of whole numbers\n", 15 | "\n", 16 | "* While other compound data types have only value as an element, a dictionary has a key: value pair. Dictionaries are optimized to retrieve values when the key is known.\n", 17 | "\n", 18 | "* Dictionarie are muttable" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "a = {} # An empty dictionary" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "dict" 39 | ] 40 | }, 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "type(a)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "{'the': 1, 'a': 5, 10000: 'abc'}" 59 | ] 60 | }, 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "# Dictionary is just a collection of key:value pairs\n", 68 | "\n", 69 | "a = {\"the\": 1, \"a\": 5, 10000:\"abc\"}\n", 70 | "a" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "3" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "len(a)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### Other ways of creating dictionary" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "{'the': 1, 'a': 5, 10000: 'abc'}" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "b = a.copy()\n", 118 | "b" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "{'the': 3, 'a': 10}" 130 | ] 131 | }, 132 | "execution_count": 6, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "c = dict(((\"the\", 3), (\"a\", 10))) # c = dict(tuple of 2 tuple elements)\n", 139 | "c" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 7, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "tuple" 151 | ] 152 | }, 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "type(((\"the\", 3), (\"a\", 10)))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 8, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "d = dict.fromkeys([\"abc\", 32, 4]) # created dictionary of only keys" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 9, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "{'abc': None, 32: None, 4: None}" 180 | ] 181 | }, 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "d" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 10, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "d['abc'] = 1" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 11, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "{'abc': 1, 32: None, 4: None}" 209 | ] 210 | }, 211 | "execution_count": 11, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "d" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 12, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "e = dict.fromkeys([\"abc\", 32, 4], 10) # created dictionary of all keys with value 10" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 13, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "{'abc': 10, 32: 10, 4: 10}" 238 | ] 239 | }, 240 | "execution_count": 13, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "e " 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "### How to access dictionary" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 14, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "{1: 2, 3: 4, 'list': [1, 23], 'dict': {1: 3}}" 265 | ] 266 | }, 267 | "execution_count": 14, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "a = {1:2, 3:4, \"list\":[1, 23], \"dict\":{1:3} }\n", 274 | "a" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 15, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "2" 286 | ] 287 | }, 288 | "execution_count": 15, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "a[1]" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 16, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "data": { 304 | "text/plain": [ 305 | "[1, 23]" 306 | ] 307 | }, 308 | "execution_count": 16, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "a['list']" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 17, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "[1, 23]" 326 | ] 327 | }, 328 | "execution_count": 17, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "a.get('list')" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 18, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "data": { 344 | "text/plain": [ 345 | "2" 346 | ] 347 | }, 348 | "execution_count": 18, 349 | "metadata": {}, 350 | "output_type": "execute_result" 351 | } 352 | ], 353 | "source": [ 354 | "a.get(1)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 19, 360 | "metadata": {}, 361 | "outputs": [], 362 | "source": [ 363 | "a.get(0) # in case key is not present, get will return nothing" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 20, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "'not present'" 375 | ] 376 | }, 377 | "execution_count": 20, 378 | "metadata": {}, 379 | "output_type": "execute_result" 380 | } 381 | ], 382 | "source": [ 383 | "a.get('li', \"not present\") # This will print \"not present\"(2nd arg) if key ('li') not found\"\n", 384 | "\n", 385 | "# if key is present it will return value associated with it" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 21, 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "data": { 395 | "text/plain": [ 396 | "dict_keys([1, 3, 'list', 'dict'])" 397 | ] 398 | }, 399 | "execution_count": 21, 400 | "metadata": {}, 401 | "output_type": "execute_result" 402 | } 403 | ], 404 | "source": [ 405 | "# To get all the keys\n", 406 | "a.keys()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 22, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "data": { 416 | "text/plain": [ 417 | "dict_values([2, 4, [1, 23], {1: 3}])" 418 | ] 419 | }, 420 | "execution_count": 22, 421 | "metadata": {}, 422 | "output_type": "execute_result" 423 | } 424 | ], 425 | "source": [ 426 | "# To get all the values\n", 427 | "a.values()" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 23, 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "data": { 437 | "text/plain": [ 438 | "dict_items([(1, 2), (3, 4), ('list', [1, 23]), ('dict', {1: 3})])" 439 | ] 440 | }, 441 | "execution_count": 23, 442 | "metadata": {}, 443 | "output_type": "execute_result" 444 | } 445 | ], 446 | "source": [ 447 | "# To get all the pairs in tuple of 2 tuples form\n", 448 | "a.items()" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 24, 454 | "metadata": {}, 455 | "outputs": [ 456 | { 457 | "name": "stdout", 458 | "output_type": "stream", 459 | "text": [ 460 | "1\n", 461 | "3\n", 462 | "list\n", 463 | "dict\n" 464 | ] 465 | } 466 | ], 467 | "source": [ 468 | "# i is the key here\n", 469 | "for i in a:\n", 470 | " print(i)" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 25, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "name": "stdout", 480 | "output_type": "stream", 481 | "text": [ 482 | "1 2\n", 483 | "3 4\n", 484 | "list [1, 23]\n", 485 | "dict {1: 3}\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "# to loop through the values \n", 491 | "\n", 492 | "for i in a:\n", 493 | " print(i, a[i])" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 26, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "2\n", 506 | "4\n", 507 | "[1, 23]\n", 508 | "{1: 3}\n" 509 | ] 510 | } 511 | ], 512 | "source": [ 513 | "for i in a.values():\n", 514 | " print(i)" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 27, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "data": { 524 | "text/plain": [ 525 | "True" 526 | ] 527 | }, 528 | "execution_count": 27, 529 | "metadata": {}, 530 | "output_type": "execute_result" 531 | } 532 | ], 533 | "source": [ 534 | "# Check if a key exist in the dictionary\n", 535 | "\n", 536 | "\"list\" in a" 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "metadata": {}, 542 | "source": [ 543 | "### How to add, delete data from Dictionary" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 28, 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "{1: 2, 3: 4, 'list': [1, 23], 'dict': {1: 3}}" 555 | ] 556 | }, 557 | "execution_count": 28, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "a" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 29, 569 | "metadata": {}, 570 | "outputs": [ 571 | { 572 | "data": { 573 | "text/plain": [ 574 | "{1: 2, 3: 4, 'list': [1, 23], 'dict': {1: 3}, 'tuple': (1, 2, 3)}" 575 | ] 576 | }, 577 | "execution_count": 29, 578 | "metadata": {}, 579 | "output_type": "execute_result" 580 | } 581 | ], 582 | "source": [ 583 | "a['tuple'] = (1, 2, 3) # add pair\n", 584 | "a" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 30, 590 | "metadata": {}, 591 | "outputs": [ 592 | { 593 | "data": { 594 | "text/plain": [ 595 | "{1: 10, 3: 4, 'list': [1, 23], 'dict': {1: 3}, 'tuple': (1, 2, 3)}" 596 | ] 597 | }, 598 | "execution_count": 30, 599 | "metadata": {}, 600 | "output_type": "execute_result" 601 | } 602 | ], 603 | "source": [ 604 | "a[1] = 10 # update pair (1:2 to 1:10)\n", 605 | "a" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 31, 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "data": { 615 | "text/plain": [ 616 | "{1: 10,\n", 617 | " 3: 5,\n", 618 | " 'list': [1, 23],\n", 619 | " 'dict': {1: 3},\n", 620 | " 'tuple': (1, 2, 3),\n", 621 | " 'the': 4,\n", 622 | " 2: 100}" 623 | ] 624 | }, 625 | "execution_count": 31, 626 | "metadata": {}, 627 | "output_type": "execute_result" 628 | } 629 | ], 630 | "source": [ 631 | "b = {3:5, 'the':4, 2:100} # b has one key (3) similar to a\n", 632 | "\n", 633 | "a.update(b) # key:value pairs in 'b' will be added to 'a', wherever same key update to that of b\n", 634 | "a" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": 32, 640 | "metadata": {}, 641 | "outputs": [ 642 | { 643 | "data": { 644 | "text/plain": [ 645 | "4" 646 | ] 647 | }, 648 | "execution_count": 32, 649 | "metadata": {}, 650 | "output_type": "execute_result" 651 | } 652 | ], 653 | "source": [ 654 | "# Removing data\n", 655 | "\n", 656 | "a.pop('the') #pop removes the pair and returns the value for the key, if key not found throws error" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 33, 662 | "metadata": {}, 663 | "outputs": [ 664 | { 665 | "data": { 666 | "text/plain": [ 667 | "{3: 5, 'list': [1, 23], 'dict': {1: 3}, 'tuple': (1, 2, 3), 2: 100}" 668 | ] 669 | }, 670 | "execution_count": 33, 671 | "metadata": {}, 672 | "output_type": "execute_result" 673 | } 674 | ], 675 | "source": [ 676 | "# you can call del on particular element\n", 677 | "del a[1]\n", 678 | "a" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 34, 684 | "metadata": {}, 685 | "outputs": [ 686 | { 687 | "data": { 688 | "text/plain": [ 689 | "{}" 690 | ] 691 | }, 692 | "execution_count": 34, 693 | "metadata": {}, 694 | "output_type": "execute_result" 695 | } 696 | ], 697 | "source": [ 698 | "# clear will remove everything\n", 699 | "\n", 700 | "a.clear()\n", 701 | "a" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 35, 707 | "metadata": {}, 708 | "outputs": [], 709 | "source": [ 710 | "# you can del the dictionary itself with del keyword\n", 711 | "\n", 712 | "del a\n", 713 | "# a, name 'a' is not defined" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "metadata": {}, 719 | "source": [ 720 | "### Print all words with k frequency" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": 36, 726 | "metadata": {}, 727 | "outputs": [], 728 | "source": [ 729 | "s = \"this is a words string having many many words\"\n", 730 | "k = 2\n", 731 | "\n", 732 | "words = s.split()" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": 37, 738 | "metadata": {}, 739 | "outputs": [ 740 | { 741 | "data": { 742 | "text/plain": [ 743 | "{'this': 1, 'is': 1, 'a': 1, 'words': 2, 'string': 1, 'having': 1, 'many': 2}" 744 | ] 745 | }, 746 | "execution_count": 37, 747 | "metadata": {}, 748 | "output_type": "execute_result" 749 | } 750 | ], 751 | "source": [ 752 | "# Method one\n", 753 | "\n", 754 | "d = {}\n", 755 | "\n", 756 | "for w in words:\n", 757 | " if w in d:\n", 758 | " d[w] = d[w] + 1\n", 759 | " else:\n", 760 | " d[w] = 1\n", 761 | "d" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": 38, 767 | "metadata": {}, 768 | "outputs": [ 769 | { 770 | "data": { 771 | "text/plain": [ 772 | "{'this': 1, 'is': 1, 'a': 1, 'words': 2, 'string': 1, 'having': 1, 'many': 2}" 773 | ] 774 | }, 775 | "execution_count": 38, 776 | "metadata": {}, 777 | "output_type": "execute_result" 778 | } 779 | ], 780 | "source": [ 781 | "# Method one\n", 782 | "\n", 783 | "d = {}\n", 784 | "\n", 785 | "for w in words:\n", 786 | " d[w] = d.get(w, 0) + 1\n", 787 | "d" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 39, 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "words\n", 800 | "many\n" 801 | ] 802 | } 803 | ], 804 | "source": [ 805 | "# Method two\n", 806 | "\n", 807 | "for w in d:\n", 808 | " if d[w] == k:\n", 809 | " print(w)" 810 | ] 811 | }, 812 | { 813 | "cell_type": "code", 814 | "execution_count": 40, 815 | "metadata": {}, 816 | "outputs": [], 817 | "source": [ 818 | "def printKFreqWords(s, k):\n", 819 | " words = s.split()\n", 820 | " d = {}\n", 821 | " \n", 822 | " for w in words:\n", 823 | " d[w] = d.get(w, 0) + 1\n", 824 | " \n", 825 | " for w in d:\n", 826 | " if d[w] == k:\n", 827 | " print(w)" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": 41, 833 | "metadata": {}, 834 | "outputs": [ 835 | { 836 | "name": "stdout", 837 | "output_type": "stream", 838 | "text": [ 839 | "this\n", 840 | "is\n", 841 | "a\n", 842 | "string\n", 843 | "having\n" 844 | ] 845 | } 846 | ], 847 | "source": [ 848 | "printKFreqWords(s, 1)" 849 | ] 850 | } 851 | ], 852 | "metadata": { 853 | "kernelspec": { 854 | "display_name": "Python 3", 855 | "language": "python", 856 | "name": "python3" 857 | }, 858 | "language_info": { 859 | "codemirror_mode": { 860 | "name": "ipython", 861 | "version": 3 862 | }, 863 | "file_extension": ".py", 864 | "mimetype": "text/x-python", 865 | "name": "python", 866 | "nbconvert_exporter": "python", 867 | "pygments_lexer": "ipython3", 868 | "version": "3.6.8" 869 | } 870 | }, 871 | "nbformat": 4, 872 | "nbformat_minor": 2 873 | } 874 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/Sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sets" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "* A set contains an unordered collection of unique objects.\n", 15 | "\n", 16 | "* Python implementation of the sets as they are known from mathematics. \n", 17 | "\n", 18 | "* Because of above point, sets unlike lists or tuples can't have multiple occurrences of the same element.\n", 19 | "\n", 20 | "* Sets are mutable" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "a = {\"apple\", \"abc\", 23} # Sytax resembles dictionary, but here you have only keys" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "{23, 'abc', 'apple'}" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "a" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "set" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "type(a)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# As sets are unordered a[0] doesn't make sense\n", 79 | "\n", 80 | "# a['abc'] doesn't make sense as there is no value associated with it\n", 81 | "# a['abc'] throws 'set' object is not subscriptable" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "True" 93 | ] 94 | }, 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "# To check if value exists in a set\n", 102 | "\n", 103 | "'abc' in a" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "False" 115 | ] 116 | }, 117 | "execution_count": 6, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "21 in a" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 7, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "apple\n", 136 | "abc\n", 137 | "23\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "# Iterating through a set\n", 143 | "\n", 144 | "for v in a:\n", 145 | " print(v)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "3" 157 | ] 158 | }, 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "# Length of a set\n", 166 | "\n", 167 | "len(a)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 9, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "# To add data in a set\n", 177 | "\n", 178 | "a.add(\"temp\")" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 10, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "{23, 'abc', 'apple', 'temp'}" 190 | ] 191 | }, 192 | "execution_count": 10, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "a" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 11, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "{'abc', 'ghi'}" 210 | ] 211 | }, 212 | "execution_count": 11, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "b = {'abc', 'ghi'}\n", 219 | "b" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 12, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "# Updating a with elements of another set\n", 229 | "\n", 230 | "a.update(b)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 13, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "{23, 'abc', 'apple', 'ghi', 'temp'}" 242 | ] 243 | }, 244 | "execution_count": 13, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "a" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 14, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "{23, 'abc', 'apple', 'ghi'}" 262 | ] 263 | }, 264 | "execution_count": 14, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "a.remove('temp')\n", 271 | "a" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 15, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [ 280 | "# removing element not present will throw error\n", 281 | "\n", 282 | "# for example a.remove('rrr') gives KeyError: 'rrr'" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 16, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "# discard function works like remove, but if key is not present no error is thrown\n", 292 | "\n", 293 | "a.discard(23)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 17, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "{'abc', 'apple', 'ghi'}" 305 | ] 306 | }, 307 | "execution_count": 17, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "a" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 18, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "a.discard('rrr')" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 19, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "{'abc', 'apple', 'ghi'}" 334 | ] 335 | }, 336 | "execution_count": 19, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "a" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 20, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "'ghi'" 354 | ] 355 | }, 356 | "execution_count": 20, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "# pop function removes and returns one element from the set, could remove any element\n", 363 | "\n", 364 | "a.pop()" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 21, 370 | "metadata": { 371 | "scrolled": true 372 | }, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "{'abc', 'apple'}" 378 | ] 379 | }, 380 | "execution_count": 21, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "a" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 22, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "set()" 398 | ] 399 | }, 400 | "execution_count": 22, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "# clear and delete functions\n", 407 | "\n", 408 | "a.clear()\n", 409 | "a" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 23, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [ 418 | "del a" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "### Functions in sets" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 24, 431 | "metadata": {}, 432 | "outputs": [], 433 | "source": [ 434 | "# Lets take two example sets a = {1,2 3,4} b = {3, 4, 5 ,6}\n", 435 | "\n", 436 | "a = {1, 2, 3, 4}\n", 437 | "b = {3, 4, 5, 6}" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 25, 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "data": { 447 | "text/plain": [ 448 | "{3, 4}" 449 | ] 450 | }, 451 | "execution_count": 25, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "# Intersection\n", 458 | "\n", 459 | "a.intersection(b)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 26, 465 | "metadata": {}, 466 | "outputs": [ 467 | { 468 | "data": { 469 | "text/plain": [ 470 | "{1, 2, 3, 4, 5, 6}" 471 | ] 472 | }, 473 | "execution_count": 26, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "# Union\n", 480 | "\n", 481 | "a.union(b)" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 27, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "{1, 2}" 493 | ] 494 | }, 495 | "execution_count": 27, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "# Difference\n", 502 | "\n", 503 | "a.difference(b) #( A - B = Everything in A - common elements of A and B)" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 28, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "{5, 6}" 515 | ] 516 | }, 517 | "execution_count": 28, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "b.difference(a)" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 29, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "data": { 533 | "text/plain": [ 534 | "{1, 2, 5, 6}" 535 | ] 536 | }, 537 | "execution_count": 29, 538 | "metadata": {}, 539 | "output_type": "execute_result" 540 | } 541 | ], 542 | "source": [ 543 | "# Symmetric difference # returns all uncommon elements\n", 544 | "\n", 545 | "a.symmetric_difference(b)" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 30, 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [ 554 | "# Updates a to intersection\n", 555 | "\n", 556 | "a.intersection_update(b)" 557 | ] 558 | }, 559 | { 560 | "cell_type": "code", 561 | "execution_count": 31, 562 | "metadata": {}, 563 | "outputs": [ 564 | { 565 | "data": { 566 | "text/plain": [ 567 | "{3, 4}" 568 | ] 569 | }, 570 | "execution_count": 31, 571 | "metadata": {}, 572 | "output_type": "execute_result" 573 | } 574 | ], 575 | "source": [ 576 | "a" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 32, 582 | "metadata": {}, 583 | "outputs": [], 584 | "source": [ 585 | "a = {1, 2, 3, 4}" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": 33, 591 | "metadata": {}, 592 | "outputs": [ 593 | { 594 | "data": { 595 | "text/plain": [ 596 | "{1, 2}" 597 | ] 598 | }, 599 | "execution_count": 33, 600 | "metadata": {}, 601 | "output_type": "execute_result" 602 | } 603 | ], 604 | "source": [ 605 | "# Update a to difference\n", 606 | "a.difference_update(b)\n", 607 | "a" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 34, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "a = {1, 2, 3, 4}" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": 35, 622 | "metadata": {}, 623 | "outputs": [], 624 | "source": [ 625 | "# Update a to symetric difference\n", 626 | "\n", 627 | "a.symmetric_difference_update(b)" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 36, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/plain": [ 638 | "{1, 2, 5, 6}" 639 | ] 640 | }, 641 | "execution_count": 36, 642 | "metadata": {}, 643 | "output_type": "execute_result" 644 | } 645 | ], 646 | "source": [ 647 | "a" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 37, 653 | "metadata": {}, 654 | "outputs": [], 655 | "source": [ 656 | "a = {1, 2, 3, 4}" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 38, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [ 665 | "# There is no union update available" 666 | ] 667 | }, 668 | { 669 | "cell_type": "code", 670 | "execution_count": 39, 671 | "metadata": {}, 672 | "outputs": [ 673 | { 674 | "data": { 675 | "text/plain": [ 676 | "False" 677 | ] 678 | }, 679 | "execution_count": 39, 680 | "metadata": {}, 681 | "output_type": "execute_result" 682 | } 683 | ], 684 | "source": [ 685 | "# If a set is a subset of another\n", 686 | "\n", 687 | "a.issubset(b)" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 40, 693 | "metadata": {}, 694 | "outputs": [ 695 | { 696 | "data": { 697 | "text/plain": [ 698 | "True" 699 | ] 700 | }, 701 | "execution_count": 40, 702 | "metadata": {}, 703 | "output_type": "execute_result" 704 | } 705 | ], 706 | "source": [ 707 | "c = {3, 4}\n", 708 | "\n", 709 | "c.issubset(a)" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 41, 715 | "metadata": {}, 716 | "outputs": [ 717 | { 718 | "data": { 719 | "text/plain": [ 720 | "True" 721 | ] 722 | }, 723 | "execution_count": 41, 724 | "metadata": {}, 725 | "output_type": "execute_result" 726 | } 727 | ], 728 | "source": [ 729 | "a.issuperset(c)" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 42, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "data": { 739 | "text/plain": [ 740 | "False" 741 | ] 742 | }, 743 | "execution_count": 42, 744 | "metadata": {}, 745 | "output_type": "execute_result" 746 | } 747 | ], 748 | "source": [ 749 | "a.isdisjoint(b)" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": 43, 755 | "metadata": {}, 756 | "outputs": [ 757 | { 758 | "data": { 759 | "text/plain": [ 760 | "True" 761 | ] 762 | }, 763 | "execution_count": 43, 764 | "metadata": {}, 765 | "output_type": "execute_result" 766 | } 767 | ], 768 | "source": [ 769 | "d = {7, 8}\n", 770 | "a.isdisjoint(d)" 771 | ] 772 | }, 773 | { 774 | "cell_type": "markdown", 775 | "metadata": {}, 776 | "source": [ 777 | "#### Find the sum of All Unique number in a List" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": 44, 783 | "metadata": {}, 784 | "outputs": [], 785 | "source": [ 786 | "# Eg : [1, 2, 1, 3, 2, 4, 3] should return 10 (1 + 2 + 3 + 4)\n", 787 | "\n", 788 | "# Repititions are not stored in set ie if you anything already in set, it doesnt gets added again\n", 789 | "\n", 790 | "a = {1, 2, 3, 1, 3}" 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": 45, 796 | "metadata": {}, 797 | "outputs": [ 798 | { 799 | "data": { 800 | "text/plain": [ 801 | "3" 802 | ] 803 | }, 804 | "execution_count": 45, 805 | "metadata": {}, 806 | "output_type": "execute_result" 807 | } 808 | ], 809 | "source": [ 810 | "len(a)" 811 | ] 812 | }, 813 | { 814 | "cell_type": "code", 815 | "execution_count": 46, 816 | "metadata": {}, 817 | "outputs": [ 818 | { 819 | "data": { 820 | "text/plain": [ 821 | "{1, 2, 3}" 822 | ] 823 | }, 824 | "execution_count": 46, 825 | "metadata": {}, 826 | "output_type": "execute_result" 827 | } 828 | ], 829 | "source": [ 830 | "a" 831 | ] 832 | }, 833 | { 834 | "cell_type": "code", 835 | "execution_count": 47, 836 | "metadata": {}, 837 | "outputs": [ 838 | { 839 | "data": { 840 | "text/plain": [ 841 | "dict" 842 | ] 843 | }, 844 | "execution_count": 47, 845 | "metadata": {}, 846 | "output_type": "execute_result" 847 | } 848 | ], 849 | "source": [ 850 | "# By default s = {} is a dictionary\n", 851 | "s = {}\n", 852 | "type(s)" 853 | ] 854 | }, 855 | { 856 | "cell_type": "code", 857 | "execution_count": 48, 858 | "metadata": {}, 859 | "outputs": [], 860 | "source": [ 861 | "# To create empty set\n", 862 | "s = set()" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 49, 868 | "metadata": {}, 869 | "outputs": [ 870 | { 871 | "data": { 872 | "text/plain": [ 873 | "set" 874 | ] 875 | }, 876 | "execution_count": 49, 877 | "metadata": {}, 878 | "output_type": "execute_result" 879 | } 880 | ], 881 | "source": [ 882 | "type(s)" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": 50, 888 | "metadata": {}, 889 | "outputs": [], 890 | "source": [ 891 | "def sumUnique(l):\n", 892 | " s = set()\n", 893 | " \n", 894 | " for i in l:\n", 895 | " s.add(i)\n", 896 | " \n", 897 | " ans = 0\n", 898 | " \n", 899 | " for i in s:\n", 900 | " ans += i\n", 901 | " \n", 902 | " return ans" 903 | ] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "execution_count": 51, 908 | "metadata": {}, 909 | "outputs": [ 910 | { 911 | "data": { 912 | "text/plain": [ 913 | "15" 914 | ] 915 | }, 916 | "execution_count": 51, 917 | "metadata": {}, 918 | "output_type": "execute_result" 919 | } 920 | ], 921 | "source": [ 922 | "sumUnique([1, 2, 3, 4, 4, 4, 4, 3, 2, 1, 5, 5])" 923 | ] 924 | } 925 | ], 926 | "metadata": { 927 | "kernelspec": { 928 | "display_name": "Python 3", 929 | "language": "python", 930 | "name": "python3" 931 | }, 932 | "language_info": { 933 | "codemirror_mode": { 934 | "name": "ipython", 935 | "version": 3 936 | }, 937 | "file_extension": ".py", 938 | "mimetype": "text/x-python", 939 | "name": "python", 940 | "nbconvert_exporter": "python", 941 | "pygments_lexer": "ipython3", 942 | "version": "3.6.8" 943 | } 944 | }, 945 | "nbformat": 4, 946 | "nbformat_minor": 2 947 | } 948 | -------------------------------------------------------------------------------- /Lecture_08_Tuples_Dictionary&Sets/Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Tuples" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# Tuples are similar to lists\n", 17 | "# Syntax b = (1,2)\n", 18 | "\n", 19 | "# In Python by default multiple things tend to behave like tuples\n", 20 | "# Tuples are also ordered type data structure\n", 21 | "\n", 22 | "# e = 1, 2 # e is a tuple\n", 23 | "\n", 24 | "a = (1,2)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "(1, 2)\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "print(a)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "print(type(a))" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "3\n", 71 | "4\n", 72 | "(5, 6)\n", 73 | "\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "b, c = 3, 4\n", 79 | "\n", 80 | "print(b)\n", 81 | "print(c)\n", 82 | "\n", 83 | "e = 5, 6\n", 84 | "print(e)\n", 85 | "print(type(e))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "5" 97 | ] 98 | }, 99 | "execution_count": 5, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "# accessing elements in tuple\n", 106 | "\n", 107 | "e[0]" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 6, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "6" 119 | ] 120 | }, 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "e[1]" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "6" 139 | ] 140 | }, 141 | "execution_count": 7, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "e[-1]" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 8, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "5" 159 | ] 160 | }, 161 | "execution_count": 8, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "e[-2]" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 9, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "data": { 177 | "text/plain": [ 178 | "(1, 2, 3)" 179 | ] 180 | }, 181 | "execution_count": 9, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ], 186 | "source": [ 187 | "# Slicing also takes place similar to lists\n", 188 | "\n", 189 | "f = (1, 2, 3, 4, 5, 6)\n", 190 | "f[0:3]" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 10, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "(3, 4, 5, 6)" 202 | ] 203 | }, 204 | "execution_count": 10, 205 | "metadata": {}, 206 | "output_type": "execute_result" 207 | } 208 | ], 209 | "source": [ 210 | "f[-4:]" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "### Difference between tuple and list" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 11, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "# Tuples are immutable, lists are mutable\n", 227 | "# Tuples can be updated, items cannot be added or deleted\n", 228 | "# deleting the Tuple itself allowed\n", 229 | "del f" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "### Functions available with tuple" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 12, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [ 245 | "a = (1, 2, 3)\n", 246 | "b = 4, 5" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 14, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "1\n", 259 | "2\n", 260 | "3\n" 261 | ] 262 | } 263 | ], 264 | "source": [ 265 | "# for loops\n", 266 | "for i in a:\n", 267 | " print(i)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 16, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "True" 279 | ] 280 | }, 281 | "execution_count": 16, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "1 in a" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 21, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "False" 299 | ] 300 | }, 301 | "execution_count": 21, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "1 in b" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 22, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "data": { 317 | "text/plain": [ 318 | "3" 319 | ] 320 | }, 321 | "execution_count": 22, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "# length of tuple\n", 328 | "\n", 329 | "len(a)" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 23, 335 | "metadata": {}, 336 | "outputs": [ 337 | { 338 | "data": { 339 | "text/plain": [ 340 | "(1, 2, 3, 4, 5)" 341 | ] 342 | }, 343 | "execution_count": 23, 344 | "metadata": {}, 345 | "output_type": "execute_result" 346 | } 347 | ], 348 | "source": [ 349 | "# concatenation of tuples\n", 350 | "c = a + b\n", 351 | "c" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 28, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "((1, 2, 3), (4, 5))" 363 | ] 364 | }, 365 | "execution_count": 28, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "d = (a, b) # tuple of tuples\n", 372 | "d" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 29, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "((1, 2, 3), (4, 5))" 384 | ] 385 | }, 386 | "execution_count": 29, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "d = a, b\n", 393 | "d" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 30, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)" 405 | ] 406 | }, 407 | "execution_count": 30, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "#repetition\n", 414 | "e = a*4\n", 415 | "e" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 31, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "1" 427 | ] 428 | }, 429 | "execution_count": 31, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "min(a) # minimum value" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 32, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/plain": [ 446 | "3" 447 | ] 448 | }, 449 | "execution_count": 32, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [ 455 | "max(a) # maximum value" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 34, 461 | "metadata": {}, 462 | "outputs": [], 463 | "source": [ 464 | "a2 = (1, 2, 's', (1))\n", 465 | "\n", 466 | "# max(a2) will throw error, as you cannot compare string or tuple with integers" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 35, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "data": { 476 | "text/plain": [ 477 | "2.2" 478 | ] 479 | }, 480 | "execution_count": 35, 481 | "metadata": {}, 482 | "output_type": "execute_result" 483 | } 484 | ], 485 | "source": [ 486 | "a3 = (1, 2.2)\n", 487 | "max(a3) # int and float are comparable" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 37, 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "data": { 497 | "text/plain": [ 498 | "float" 499 | ] 500 | }, 501 | "execution_count": 37, 502 | "metadata": {}, 503 | "output_type": "execute_result" 504 | } 505 | ], 506 | "source": [ 507 | "type(2.2)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 41, 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "data": { 517 | "text/plain": [ 518 | "(1, 2, 3, 4)" 519 | ] 520 | }, 521 | "execution_count": 41, 522 | "metadata": {}, 523 | "output_type": "execute_result" 524 | } 525 | ], 526 | "source": [ 527 | "# list to tuple\n", 528 | "l = [1, 2, 3, 4]\n", 529 | "tuple(l)" 530 | ] 531 | }, 532 | { 533 | "cell_type": "markdown", 534 | "metadata": {}, 535 | "source": [ 536 | "### Variable length Input and Output" 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "metadata": {}, 542 | "source": [ 543 | "#### Variable length Input" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 42, 549 | "metadata": {}, 550 | "outputs": [], 551 | "source": [ 552 | "# We wish to create a sum function which can take as many int as we want\n", 553 | "\n", 554 | "# ie function should be able to take variable length input\n", 555 | "\n", 556 | "def sum(a, b):\n", 557 | " return a + b" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 43, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "3" 569 | ] 570 | }, 571 | "execution_count": 43, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "sum(1,2)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 44, 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "name": "stdout", 587 | "output_type": "stream", 588 | "text": [ 589 | "2\n", 590 | "3\n", 591 | "()\n" 592 | ] 593 | } 594 | ], 595 | "source": [ 596 | "def sum2(a, b, *more):\n", 597 | " print(a)\n", 598 | " print(b)\n", 599 | " print(more)\n", 600 | "\n", 601 | "sum2(2, 3) # more turns out to be an empty tuple ()" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 45, 607 | "metadata": {}, 608 | "outputs": [ 609 | { 610 | "name": "stdout", 611 | "output_type": "stream", 612 | "text": [ 613 | "2\n", 614 | "3\n", 615 | "(4, 5)\n" 616 | ] 617 | } 618 | ], 619 | "source": [ 620 | "sum2(2, 3, 4, 5) # 4 and 5 are going inside the tuple called more" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 49, 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "name": "stdout", 630 | "output_type": "stream", 631 | "text": [ 632 | "2\n", 633 | "3\n", 634 | "(4, 5)\n", 635 | "14\n" 636 | ] 637 | } 638 | ], 639 | "source": [ 640 | "# Now we can modify the function in a way that it gives sum of all\n", 641 | "\n", 642 | "def sum3(a, b, *more):\n", 643 | " print(a)\n", 644 | " print(b)\n", 645 | " print(more)\n", 646 | " \n", 647 | " ans = a + b\n", 648 | " \n", 649 | " for i in more:\n", 650 | " ans = ans + i\n", 651 | " \n", 652 | " return ans\n", 653 | "\n", 654 | "print(sum3(2, 3, 4, 5)) # more turns out to be an empty tuple ()" 655 | ] 656 | }, 657 | { 658 | "cell_type": "markdown", 659 | "metadata": {}, 660 | "source": [ 661 | "#### Variable length output" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 50, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [ 670 | "def sum_diff(a, b):\n", 671 | " return a + b, a - b" 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": 51, 677 | "metadata": {}, 678 | "outputs": [], 679 | "source": [ 680 | "c = sum_diff(4, 1)" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 53, 686 | "metadata": {}, 687 | "outputs": [ 688 | { 689 | "name": "stdout", 690 | "output_type": "stream", 691 | "text": [ 692 | "(5, 3)\n" 693 | ] 694 | } 695 | ], 696 | "source": [ 697 | "print(c) # By default you were returning more values than 2, Python created a tuple" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 57, 703 | "metadata": {}, 704 | "outputs": [ 705 | { 706 | "name": "stdout", 707 | "output_type": "stream", 708 | "text": [ 709 | "7\n", 710 | "3\n" 711 | ] 712 | }, 713 | { 714 | "data": { 715 | "text/plain": [ 716 | "(7, 3)" 717 | ] 718 | }, 719 | "execution_count": 57, 720 | "metadata": {}, 721 | "output_type": "execute_result" 722 | } 723 | ], 724 | "source": [ 725 | "d, e = sum_diff(5, 2) # If you have only two value to take the 3 values output ..\n", 726 | "# Python would say too many values to unpack\n", 727 | "print(d)\n", 728 | "print(e)\n", 729 | "d, e" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 58, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "name": "stdout", 739 | "output_type": "stream", 740 | "text": [ 741 | "(5, 3)\n" 742 | ] 743 | } 744 | ], 745 | "source": [ 746 | "# if you save output in only one value, thats fine it'll be assigned as tuple\n", 747 | "\n", 748 | "d = sum_diff(4,1)\n", 749 | "print(d)" 750 | ] 751 | } 752 | ], 753 | "metadata": { 754 | "kernelspec": { 755 | "display_name": "Python 3", 756 | "language": "python", 757 | "name": "python3" 758 | }, 759 | "language_info": { 760 | "codemirror_mode": { 761 | "name": "ipython", 762 | "version": 3 763 | }, 764 | "file_extension": ".py", 765 | "mimetype": "text/x-python", 766 | "name": "python", 767 | "nbconvert_exporter": "python", 768 | "pygments_lexer": "ipython3", 769 | "version": "3.6.8" 770 | } 771 | }, 772 | "nbformat": 4, 773 | "nbformat_minor": 2 774 | } 775 | -------------------------------------------------------------------------------- /Lecture_09_OOPS/.ipynb_checkpoints/OOPS-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Every object has two characterists\n", 10 | "# First it can have property associated with it and next it can have some behavior associated with it\n", 11 | "\n", 12 | "# Properties -> Atrributes\n", 13 | "# Behavior -> Methods" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "a = 10\n", 31 | "print(type(a))" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "l = [1, 2, 3]\n", 49 | "print(type(l))" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# User defined class\n", 59 | "\n", 60 | "class Student:\n", 61 | " pass" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 5, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "# create 3 objects\n", 71 | "\n", 72 | "s1 = Student()\n", 73 | "s2 = Student()\n", 74 | "s3 = Student()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "## Types of Attribute\n", 82 | "\n", 83 | "### Instance attributes\n", 84 | "\n", 85 | "* Object attribute, that is these attributes are bound to a specific object. Eg: Name, Student, Age for the Student example.\n", 86 | "\n", 87 | "### Class attributes\n", 88 | "\n", 89 | "* Attribute bound to class. Eg: Class teacher name, total number of students.\n" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "# Instance attributes\n", 99 | "# Objects can have different instance attributes\n", 100 | "# Adding Attributes: s1 -> Name, Age; s2 -> Roll no; s3 -> Age will not throw error\n", 101 | "\n", 102 | "s1.name = \"Mohit\" # This creates new attribute \"name\" to s1\n", 103 | "s1.age = 20" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 7, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "s2.rollNumber = 101" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 8, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "s3.age = 21" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 9, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "s3.age = 34" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 10, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "# In Python each and every instance of class (ie object) possesses a dictionary which holds list of attributes along with value\n", 140 | "\n", 141 | "# Syntax to access the dictionary: objName.__dict__" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 11, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "{'name': 'Mohit', 'age': 20}\n", 154 | "{'rollNumber': 101}\n", 155 | "{'age': 34}\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "print(s1.__dict__)\n", 161 | "print(s2.__dict__)\n", 162 | "print(s3.__dict__)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 12, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "{}\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "s4 = Student()\n", 180 | "print(s4.__dict__)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 13, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "Mohit\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "print(s1.name)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 14, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "True\n" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "# Inbuilt functions\n", 215 | "\n", 216 | "# hasattr(objectName, attributeToCheck)\n", 217 | "\n", 218 | "print(hasattr(s1, 'name'))" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 15, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "False\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "print(hasattr(s2, 'name'))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 16, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "# setattr(objectName, attribute, valuetoSet)\n", 245 | "# delattr(objectName, attributeToDelete)\n", 246 | "# getattr(objectName, attributeTovalueToGet)\n", 247 | "# getattr(objectName, attributeTovalueToGet, defaultValue) # In case attribute is not present" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 17, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "Mohit\n", 260 | "No such attribute\n" 261 | ] 262 | } 263 | ], 264 | "source": [ 265 | "print(getattr(s1, 'name'))\n", 266 | "print(getattr(s2, 'name','No such attribute')) # Had we passed no defaultValue, we'd have gotten error" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 18, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "{'age': 20}\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "delattr(s1, 'name')\n", 284 | "print(s1.__dict__)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 19, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "{'__module__': '__main__', '__dict__': , '__weakref__': , '__doc__': None, 'totalStudents': 20}\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "# Class attributes\n", 302 | "# Syntax like we used to set instance attribute\n", 303 | "\n", 304 | "Student.totalStudents = 20\n", 305 | "print(Student.__dict__)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 20, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "name": "stdout", 315 | "output_type": "stream", 316 | "text": [ 317 | "{'__module__': '__main__', 'totalStudents': 20, 'classTeacherName': 'Komal', '__dict__': , '__weakref__': , '__doc__': None}\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "# Another way of adding class attribute\n", 323 | "\n", 324 | "class Student:\n", 325 | " ## Class attributes\n", 326 | " totalStudents = 20\n", 327 | " classTeacherName = 'Komal'\n", 328 | " \n", 329 | "s1 = Student()\n", 330 | "s2 = Student()\n", 331 | "print(Student.__dict__)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 21, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "{}\n", 344 | "{}\n" 345 | ] 346 | } 347 | ], 348 | "source": [ 349 | "print(s1.__dict__)\n", 350 | "print(s2.__dict__)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 22, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [ 359 | "# The objects s1 and s2 are still empty, its just that they have access to totalStudents" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 23, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "name": "stdout", 369 | "output_type": "stream", 370 | "text": [ 371 | "20\n", 372 | "20\n", 373 | "20\n" 374 | ] 375 | } 376 | ], 377 | "source": [ 378 | "# We can access the class attribute from the objects but cannot modify them\n", 379 | "\n", 380 | "print(Student.totalStudents)\n", 381 | "print(s1.totalStudents)\n", 382 | "print(s2.totalStudents)" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 24, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "30\n", 395 | "30\n", 396 | "30\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "Student.totalStudents = 30 # Updating value\n", 402 | "\n", 403 | "print(Student.totalStudents)\n", 404 | "print(s1.totalStudents)\n", 405 | "print(s2.totalStudents)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 25, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "name": "stdout", 415 | "output_type": "stream", 416 | "text": [ 417 | "30\n", 418 | "40\n", 419 | "30\n" 420 | ] 421 | } 422 | ], 423 | "source": [ 424 | "s1.totalStudents = 40\n", 425 | "\n", 426 | "print(Student.totalStudents)\n", 427 | "print(s1.totalStudents)\n", 428 | "print(s2.totalStudents)" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 26, 434 | "metadata": {}, 435 | "outputs": [ 436 | { 437 | "name": "stdout", 438 | "output_type": "stream", 439 | "text": [ 440 | "{'totalStudents': 40}\n", 441 | "{}\n" 442 | ] 443 | } 444 | ], 445 | "source": [ 446 | "# A new instance attribute was created for s1 leaving Student class' attribute value unchanged\n", 447 | "\n", 448 | "print(s1.__dict__)\n", 449 | "print(s2.__dict__)" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 27, 455 | "metadata": {}, 456 | "outputs": [], 457 | "source": [ 458 | "# If you try to access obj.prop, first it is checked if the object has that instance, if no -> it checks if its class has that, if no -> then error " 459 | ] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": {}, 464 | "source": [ 465 | "### Methods\n", 466 | "\n", 467 | "There are three types of Methods\n", 468 | "* Instance Methods: Just like Instance Attribute these are bound to Objects, eg updateName\n", 469 | "\n", 470 | "* Class Methods: Bound to class, eg changeClassTeacherName\n", 471 | "* Static Methods:" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 28, 477 | "metadata": {}, 478 | "outputs": [], 479 | "source": [ 480 | "class Student:\n", 481 | " ## Class attributes\n", 482 | " totalStudents = 20\n", 483 | " classTeacherName = 'Komal'\n", 484 | " \n", 485 | " ## Instance methods\n", 486 | " def printHello():\n", 487 | " print(\"Hello\")\n", 488 | " \n", 489 | "s1 = Student()\n", 490 | "s2 = Student()" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 29, 496 | "metadata": {}, 497 | "outputs": [], 498 | "source": [ 499 | "# s1.printHello() throws -> TypeError: printHello() takes 0 positional arguments but 1 was given\n", 500 | "\n", 501 | "# This is because s1.printHello() is interpreted as Student.printHello(s1)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 30, 507 | "metadata": {}, 508 | "outputs": [], 509 | "source": [ 510 | "# To correct we can add a parameter in printHello\n", 511 | "# conventionally we name it as self (analogues to 'this' in Cpp)\n", 512 | "\n", 513 | "class Student:\n", 514 | " ## Class attributes\n", 515 | " totalStudents = 20\n", 516 | " classTeacherName = 'Komal'\n", 517 | " \n", 518 | " ## Instance methods\n", 519 | " def printHello(self):\n", 520 | " print(\"Hello\")\n", 521 | " \n", 522 | "s1 = Student()\n", 523 | "s2 = Student()" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 31, 529 | "metadata": { 530 | "scrolled": true 531 | }, 532 | "outputs": [ 533 | { 534 | "name": "stdout", 535 | "output_type": "stream", 536 | "text": [ 537 | "Hello\n" 538 | ] 539 | } 540 | ], 541 | "source": [ 542 | "s1.printHello()" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 32, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "name": "stdout", 552 | "output_type": "stream", 553 | "text": [ 554 | "Hello\n" 555 | ] 556 | } 557 | ], 558 | "source": [ 559 | "# To call method from a Class, add any object as parameter\n", 560 | "\n", 561 | "class Student:\n", 562 | " ## Class attributes\n", 563 | " totalStudents = 20\n", 564 | " classTeacherName = 'Komal'\n", 565 | " \n", 566 | " ## Instance methods\n", 567 | " def printHello(self):\n", 568 | " print(\"Hello\")\n", 569 | " \n", 570 | "s1 = Student()\n", 571 | "s2 = Student()\n", 572 | "\n", 573 | "Student.printHello(s2)" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": 37, 579 | "metadata": { 580 | "scrolled": true 581 | }, 582 | "outputs": [], 583 | "source": [ 584 | "class Student:\n", 585 | " ## Class attributes\n", 586 | " totalStudents = 20\n", 587 | " classTeacherName = 'Komal'\n", 588 | " \n", 589 | " ## Instance methods\n", 590 | " def printHello(self):\n", 591 | " print(str)\n", 592 | " \n", 593 | " def print(self, str):\n", 594 | " print(str)\n", 595 | " \n", 596 | " def printName(self, str):\n", 597 | " print(str)\n", 598 | " \n", 599 | "s1 = Student()\n", 600 | "s2 = Student()\n" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 38, 606 | "metadata": {}, 607 | "outputs": [ 608 | { 609 | "name": "stdout", 610 | "output_type": "stream", 611 | "text": [ 612 | "Mohit\n" 613 | ] 614 | } 615 | ], 616 | "source": [ 617 | "s1.name = \"Mohit\"\n", 618 | "s1.print(s1.name)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": null, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "class Student:\n", 628 | " ## Class attributes\n", 629 | " totalStudents = 20\n", 630 | " classTeacherName = 'Komal'\n", 631 | " \n", 632 | " ## Instance methods\n", 633 | " def printHello(self):\n", 634 | " print(str)\n", 635 | " \n", 636 | " def print(self, str):\n", 637 | " print(str)\n", 638 | " \n", 639 | " def printName(self, str):\n", 640 | " print(self.name)\n", 641 | " \n", 642 | "s1 = Student()\n", 643 | "s2 = Student()" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": null, 649 | "metadata": {}, 650 | "outputs": [], 651 | "source": [ 652 | "s1.print(s1.name)" 653 | ] 654 | } 655 | ], 656 | "metadata": { 657 | "kernelspec": { 658 | "display_name": "Python 3", 659 | "language": "python", 660 | "name": "python3" 661 | }, 662 | "language_info": { 663 | "codemirror_mode": { 664 | "name": "ipython", 665 | "version": 3 666 | }, 667 | "file_extension": ".py", 668 | "mimetype": "text/x-python", 669 | "name": "python", 670 | "nbconvert_exporter": "python", 671 | "pygments_lexer": "ipython3", 672 | "version": "3.6.8" 673 | } 674 | }, 675 | "nbformat": 4, 676 | "nbformat_minor": 2 677 | } 678 | -------------------------------------------------------------------------------- /Lecture_10_workingWIthFiles/.ipynb_checkpoints/WorkingWithFiles-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture_10_workingWIthFiles/WorkingWithFiles.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Open and read text files" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# We can use Python's inbuilt function 'open'\n", 17 | "\n", 18 | "# Syntax -> open(file_name with complete/relative path, access mode)\n", 19 | "# Access mode is an optional argument, represents in what kind of mode you want to open the file\n", 20 | "# One mode can be 'r' -> represents read mode (it is the default value)\n", 21 | "# 'w' -> write mode, if we open file in this mode, we'll use the old content\n", 22 | "# 'a' -> append mode, retain content and add new information" 23 | ] 24 | } 25 | ], 26 | "metadata": { 27 | "kernelspec": { 28 | "display_name": "Python 3", 29 | "language": "python", 30 | "name": "python3" 31 | }, 32 | "language_info": { 33 | "codemirror_mode": { 34 | "name": "ipython", 35 | "version": 3 36 | }, 37 | "file_extension": ".py", 38 | "mimetype": "text/x-python", 39 | "name": "python", 40 | "nbconvert_exporter": "python", 41 | "pygments_lexer": "ipython3", 42 | "version": "3.6.8" 43 | } 44 | }, 45 | "nbformat": 4, 46 | "nbformat_minor": 2 47 | } 48 | -------------------------------------------------------------------------------- /Lecture_11_Numpy/.ipynb_checkpoints/Numpy-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Numpy\n", 8 | "* Numpy means Numerical Python\n", 9 | "* Python's Linear Algebra library\n", 10 | "* Gives us multi dimensional (multi-dim) array" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "### Why do we need Numpy?\n", 18 | " * Memory requirement: Numpy lists require less memory than Python ones, see below example.\n", 19 | " * Operations on Numpy list are faster than that on normal lists.\n", 20 | " * Numpy is more convenient and has wider functionality." 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 5, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n", 33 | "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n", 34 | " 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47\n", 35 | " 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n", 36 | " 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95\n", 37 | " 96 97 98 99]\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "# Memory requirement: Numpy lists require less memory than Python ones, see below example.\n", 43 | "\n", 44 | "import numpy as np\n", 45 | "\n", 46 | "li_arr = [i for i in range(0, 100)]\n", 47 | "np_arr = np.arange(100)\n", 48 | "\n", 49 | "print(li_arr)\n", 50 | "print(np_arr)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 6, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "8\n", 63 | "800\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "## Size of one element in numpy array\n", 69 | "print(np_arr.itemsize)\n", 70 | "\n", 71 | "## Size of 100 elements in numpy array\n", 72 | "print(np_arr.itemsize * np_arr.size)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 7, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "28\n", 85 | "2800\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "import sys\n", 91 | "\n", 92 | "a = 10\n", 93 | "\n", 94 | "## Size req of 1 element in list\n", 95 | "print(sys.getsizeof(a))\n", 96 | "\n", 97 | "## Size of 100 elements in list\n", 98 | "print(sys.getsizeof(1) * len(li_arr))" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 13, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "List = 205.48200607299805\n", 111 | "Numpy = 0.9255409240722656\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "import numpy as np\n", 117 | "import time\n", 118 | "\n", 119 | "size = 100000\n", 120 | "\n", 121 | "## Addd elements of array a and b\n", 122 | "\n", 123 | "def addition_using_list():\n", 124 | " t1 = time.time() # Epoch time\n", 125 | " a = range(size)\n", 126 | " b = np.arange(size)\n", 127 | " c = [a[i] + b[i] for i in range(size)]\n", 128 | " t2 = time.time()\n", 129 | " return (t2 - t1)\n", 130 | "\n", 131 | "def addtion_using_numpy():\n", 132 | " t1 = time.time()\n", 133 | " a = np.arange(size)\n", 134 | " b = np.arange(size)\n", 135 | " c = a + b\n", 136 | " t2 = time.time()\n", 137 | " return (t2 - t1)\n", 138 | "\n", 139 | "t_list = addition_using_list()\n", 140 | "t_numpy = addtion_using_numpy()\n", 141 | "\n", 142 | "print('List = ', t_list * 1000) ## Time in Milliseconds\n", 143 | "print('Numpy = ', t_numpy * 1000) ## Time in Milliseconds" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "### Why are Numpy arrays fast?\n", 151 | "In Numpy Array operations take place in chunks rather than element wise.\n", 152 | "\n", 153 | "For example in case of adding respective elements of two lists, the addition takes place in chunks and not one element at a time.\n", 154 | "\n", 155 | "**What is vectorization?**\n", 156 | "\n", 157 | "\"Vectorization\" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.\n", 158 | "\n", 159 | "Source: https://stackoverflow.com/questions/1422149/what-is-vectorization" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [] 175 | } 176 | ], 177 | "metadata": { 178 | "kernelspec": { 179 | "display_name": "Python 3", 180 | "language": "python", 181 | "name": "python3" 182 | }, 183 | "language_info": { 184 | "codemirror_mode": { 185 | "name": "ipython", 186 | "version": 3 187 | }, 188 | "file_extension": ".py", 189 | "mimetype": "text/x-python", 190 | "name": "python", 191 | "nbconvert_exporter": "python", 192 | "pygments_lexer": "ipython3", 193 | "version": "3.6.8" 194 | } 195 | }, 196 | "nbformat": 4, 197 | "nbformat_minor": 2 198 | } 199 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/Connect With DB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "import sqlite3\n", 21 | "\n", 22 | "db = sqlite3.connect('IMDB.sqlite')\n", 23 | "db" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "db = sqlite3.connect('IMDB_1.sqlite')\n", 44 | "db" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "" 56 | ] 57 | }, 58 | "execution_count": 3, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "db = sqlite3.connect('School.sqlite')\n", 65 | "db" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "" 77 | ] 78 | }, 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "cur = db.cursor()\n", 86 | "cur" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "" 98 | ] 99 | }, 100 | "execution_count": 5, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "sql_query = 'Create table Student (RollNumber int Primary key, Name Text, Age int)'\n", 107 | "cur.execute(sql_query)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 6, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "" 119 | ] 120 | }, 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "cur.execute('Insert into Student values (101, \"Aman\", 20)')" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "db.commit()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 8, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "" 148 | ] 149 | }, 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "cur.execute('Insert into Student values (102, \"Amit\", 21)')" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "db.commit()" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "cur.execute('Insert into Student values()')" 175 | ] 176 | } 177 | ], 178 | "metadata": { 179 | "kernelspec": { 180 | "display_name": "Python 3", 181 | "language": "python", 182 | "name": "python3" 183 | }, 184 | "language_info": { 185 | "codemirror_mode": { 186 | "name": "ipython", 187 | "version": 3 188 | }, 189 | "file_extension": ".py", 190 | "mimetype": "text/x-python", 191 | "name": "python", 192 | "nbconvert_exporter": "python", 193 | "pygments_lexer": "ipython3", 194 | "version": "3.6.8" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 2 199 | } 200 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/DropIndex.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 17, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sqlite3\n", 10 | "\n", 11 | "db = sqlite3.connect('EmployeeDB.sqlite')\n", 12 | "\n", 13 | "cur = db.cursor()" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": { 20 | "scrolled": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "sql_query = \"delete from Employee where employee_id = 101\"\n", 25 | "\n", 26 | "cur.execute(sql_query)\n", 27 | "db.commit()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 4, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "id = 102\n", 37 | "\n", 38 | "cur.execute(\"delete from Employee where employee_id = ?\", (id,))\n", 39 | "db.commit()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 15, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "employee_id=[101,102,103,104,105,106,107,108,109,110,111,112,113]\n", 49 | "name=['Aadarsh','Aarti','Siddharth','Aman','Amit','Shivansh','Vaibhav','Himanshu','Raman','Kunal','Adhira','Tanya']\n", 50 | "age=[25,27,25,24,30,26,23,26,25,26,29,24]\n", 51 | "department=['Marketing','Operations','Finance','Human Resource','Marketing','IT','Finance','IT','Operations','Marketing','Human Resource','Marketing']\n", 52 | "salary=[50000,60000,85000,75000,50000,90000,85000,90000,60000,50000,75000,50000]\n", 53 | "\n", 54 | "values = [(employee_id[i], name[i], age[i], department[i], salary[i]) for i in range(len(age))]\n", 55 | "\n", 56 | "#print(values)\n", 57 | "cur.executemany('insert into Employee values (?, ?, ?, ?, ?)', values)\n", 58 | "\n", 59 | "db.commit()" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 21, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "103\n", 72 | "107\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "cur.execute(\"select employee_id from Employee where department = 'Finance'\")\n", 78 | "for i in cur:\n", 79 | " print(i[0])" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 42, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "[('Finance', 2),\n", 91 | " ('Human Resource', 2),\n", 92 | " ('IT', 2),\n", 93 | " ('Marketing', 5),\n", 94 | " ('Operations', 2)]" 95 | ] 96 | }, 97 | "execution_count": 42, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "cur.execute(\"select department, count(department) from Employee group by department\")\n", 104 | "cur.fetchall()" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 41, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "[('Marketing',),\n", 116 | " ('Marketing',),\n", 117 | " ('Operations',),\n", 118 | " ('Finance',),\n", 119 | " ('Human Resource',),\n", 120 | " ('Marketing',),\n", 121 | " ('IT',),\n", 122 | " ('Finance',),\n", 123 | " ('IT',),\n", 124 | " ('Operations',),\n", 125 | " ('Marketing',),\n", 126 | " ('Human Resource',),\n", 127 | " ('Marketing',)]" 128 | ] 129 | }, 130 | "execution_count": 41, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "cur.execute(\"select department from employee \")\n", 137 | "cur.fetchall()" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 3", 179 | "language": "python", 180 | "name": "python3" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 3 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython3", 192 | "version": "3.6.8" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 2 197 | } 198 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/Indexing&SQLite.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 30, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "" 12 | ] 13 | }, 14 | "execution_count": 30, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "import sqlite3\n", 21 | "\n", 22 | "db = sqlite3.connect('IMDB.sqlite') # No error thrown even if wrong filename is used\n", 23 | "\n", 24 | "# Instead a file of that name gets created\n", 25 | "\n", 26 | "db" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 31, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "" 38 | ] 39 | }, 40 | "execution_count": 31, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "# Cursor object - Allows us to run sql queries on our database\n", 47 | "\n", 48 | "cur = db.cursor()\n", 49 | "\n", 50 | "cur" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 32, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "db = sqlite3.connect('School.sqlite') " 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 33, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "" 71 | ] 72 | }, 73 | "execution_count": 33, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "cur = db.cursor()\n", 80 | "cur" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 34, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "" 92 | ] 93 | }, 94 | "execution_count": 34, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "# Create new table with name 'Student' columns = RNo, Name, Age\n", 101 | "\n", 102 | "sql_query = \"Create table Student (RollNumber int Primary key, Name Text, Age int)\"\n", 103 | "\n", 104 | "cur.execute(sql_query) # execute(query) used to execute query on table cur is pointing to" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 37, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "" 116 | ] 117 | }, 118 | "execution_count": 37, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "cur.execute(\"Insert into Student values (101, 'Aman', 20)\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 38, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# Any insertion will reflect only after commit!\n", 134 | "\n", 135 | "db.commit()" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 39, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "cur.execute(\"Insert into Student values (102, 'Varun', 20)\")\n", 145 | "\n", 146 | "db.commit()" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 40, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "db.close()" 156 | ] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.6.8" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 2 180 | } 181 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/SQLite_with_Pandas.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sqlite3\n", 10 | "import pandas as pd\n", 11 | "\n", 12 | "db = sqlite3.connect('School.sqlite')" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "data": { 22 | "text/html": [ 23 | "
\n", 24 | "\n", 37 | "\n", 38 | " \n", 39 | " \n", 40 | " \n", 41 | " \n", 42 | " \n", 43 | " \n", 44 | " \n", 45 | " \n", 46 | " \n", 47 | " \n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | "
RollNumberNameAge
0101Aman20
1102Amit21
2103Mohit24
3104Nikhil34
4105Nidhi20
5106Manisha21
6107Ankush22
\n", 91 | "
" 92 | ], 93 | "text/plain": [ 94 | " RollNumber Name Age\n", 95 | "0 101 Aman 20\n", 96 | "1 102 Amit 21\n", 97 | "2 103 Mohit 24\n", 98 | "3 104 Nikhil 34\n", 99 | "4 105 Nidhi 20\n", 100 | "5 106 Manisha 21\n", 101 | "6 107 Ankush 22" 102 | ] 103 | }, 104 | "execution_count": 2, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# pd.read_sql_query(query, connectionObject)\n", 111 | "\n", 112 | "data = pd.read_sql_query('Select * from Student', db)\n", 113 | "\n", 114 | "data" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 4, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/html": [ 125 | "
\n", 126 | "\n", 139 | "\n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | "
RollNumberNameAge
1102Amit21
5106Manisha21
\n", 163 | "
" 164 | ], 165 | "text/plain": [ 166 | " RollNumber Name Age\n", 167 | "1 102 Amit 21\n", 168 | "5 106 Manisha 21" 169 | ] 170 | }, 171 | "execution_count": 4, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "# tuples with age = 30\n", 178 | "\n", 179 | "data[data['Age'] == 21]" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 5, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "data": { 189 | "text/html": [ 190 | "
\n", 191 | "\n", 204 | "\n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | "
IDNameCourseSalary
0201AmanEnglish20000
1202KomalMath22000
2203MaheshScience23000
\n", 238 | "
" 239 | ], 240 | "text/plain": [ 241 | " ID Name Course Salary\n", 242 | "0 201 Aman English 20000\n", 243 | "1 202 Komal Math 22000\n", 244 | "2 203 Mahesh Science 23000" 245 | ] 246 | }, 247 | "execution_count": 5, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "# Creating new tables with Pandas\n", 254 | "\n", 255 | "data = pd.DataFrame([\n", 256 | " [201, \"Aman\", \"English\", 20000], \n", 257 | " [202, \"Komal\", \"Math\", 22000],\n", 258 | " [203, \"Mahesh\", \"Science\", 23000]\n", 259 | " ], columns = [\"ID\", \"Name\", \"Course\", \"Salary\"])\n", 260 | "\n", 261 | "data" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 6, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [ 270 | "# Converting DataFrame into a Table\n", 271 | "\n", 272 | "data.to_sql(\"Faculty\", db)" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 7, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/html": [ 283 | "
\n", 284 | "\n", 297 | "\n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | "
RollNumberNameAgeAddress
0101Aman20Test
1102Amit21Test
2103Mohit24Test
3104Nikhil34Test
4105Nidhi20Test
5106Manisha21Test
6107Ankush22Test
\n", 359 | "
" 360 | ], 361 | "text/plain": [ 362 | " RollNumber Name Age Address\n", 363 | "0 101 Aman 20 Test\n", 364 | "1 102 Amit 21 Test\n", 365 | "2 103 Mohit 24 Test\n", 366 | "3 104 Nikhil 34 Test\n", 367 | "4 105 Nidhi 20 Test\n", 368 | "5 106 Manisha 21 Test\n", 369 | "6 107 Ankush 22 Test" 370 | ] 371 | }, 372 | "execution_count": 7, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "# Add Column in table\n", 379 | "\n", 380 | "data = pd.read_sql_query('Select * from Student', db)\n", 381 | "data[\"Address\"] = \"Test\"\n", 382 | "\n", 383 | "data" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 8, 389 | "metadata": {}, 390 | "outputs": [], 391 | "source": [ 392 | "data.to_sql(\"Student\", db, if_exists = \"replace\")" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [] 401 | } 402 | ], 403 | "metadata": { 404 | "kernelspec": { 405 | "display_name": "Python 3", 406 | "language": "python", 407 | "name": "python3" 408 | }, 409 | "language_info": { 410 | "codemirror_mode": { 411 | "name": "ipython", 412 | "version": 3 413 | }, 414 | "file_extension": ".py", 415 | "mimetype": "text/x-python", 416 | "name": "python", 417 | "nbconvert_exporter": "python", 418 | "pygments_lexer": "ipython3", 419 | "version": "3.6.8" 420 | } 421 | }, 422 | "nbformat": 4, 423 | "nbformat_minor": 2 424 | } 425 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/createEmployeeTable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 13, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sqlite3\n", 10 | "\n", 11 | "db = sqlite3.connect('EmployeeDB.sqlite')\n", 12 | "\n", 13 | "cur = db.cursor()" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 14, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "" 25 | ] 26 | }, 27 | "execution_count": 14, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "sql_query = \"Create table Employee (employee_id int Primary key, name Text, age int, department Text, salary int)\"\n", 34 | "\n", 35 | "cur.execute(sql_query)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 15, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "db.commit()" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 16, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "cur.execute(\"Insert into Employee values (100, 'Rishav', 25, 'Marketing', 50000)\" )\n", 54 | "db.commit()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 17, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "cur.execute(\"Insert into Employee values (101, 'Abhishek', 26, 'Operations', 60000)\" )\n", 64 | "cur.execute(\"Insert into Employee values (102, 'Aman', 24, 'Human Resources', 75000)\" )\n", 65 | "\n", 66 | "db.commit()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.6.8" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 2 98 | } 99 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/extractData.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sqlite3\n", 10 | "\n", 11 | "db = sqlite3.connect('School.sqlite')\n", 12 | "\n", 13 | "cur = db.cursor()" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 2, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "(101, 'Aman', 20)\n", 26 | "(102, 'Amit', 21)\n", 27 | "(103, 'Mohit', 24)\n", 28 | "(104, 'Nikhil', 34)\n", 29 | "(105, 'Nidhi', 20)\n", 30 | "(106, 'Manisha', 21)\n", 31 | "(107, 'Ankush', 22)\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "# Extract data from table, method 1\n", 37 | "\n", 38 | "cur.execute(\"Select * from Student\")\n", 39 | "\n", 40 | "for row in cur:\n", 41 | " print(row)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "(101, 'Aman', 20)" 53 | ] 54 | }, 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "# Extract data from table, method 2 fetchone\n", 62 | "\n", 63 | "cur.execute(\"Select * from Student\")\n", 64 | "cur.fetchone() # cur will point to next row after execution" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 4, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "[(101, 'Aman', 20),\n", 76 | " (102, 'Amit', 21),\n", 77 | " (103, 'Mohit', 24),\n", 78 | " (104, 'Nikhil', 34),\n", 79 | " (105, 'Nidhi', 20),\n", 80 | " (106, 'Manisha', 21),\n", 81 | " (107, 'Ankush', 22)]" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "# Extract data from table, method 3 fetchall\n", 91 | "\n", 92 | "cur.execute(\"Select * from Student\")\n", 93 | "cur.fetchall() # prints list of tuple" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "[(101, 'Aman', 20),\n", 105 | " (102, 'Amit', 21),\n", 106 | " (103, 'Mohit', 24),\n", 107 | " (104, 'Nikhil', 34),\n", 108 | " (105, 'Nidhi', 20)]" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# Extract data from table, method 4 fetchmany\n", 118 | "\n", 119 | "cur.execute(\"Select * from Student\")\n", 120 | "cur.fetchmany(5) # prints list of 'n' tuple" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "[(103, 'Mohit', 24),\n", 132 | " (104, 'Nikhil', 34),\n", 133 | " (105, 'Nidhi', 20),\n", 134 | " (106, 'Manisha', 21),\n", 135 | " (107, 'Ankush', 22)]" 136 | ] 137 | }, 138 | "execution_count": 6, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "cur.execute('Select * from Student where RollNumber >= 103')\n", 145 | "cur.fetchall()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 9, 151 | "metadata": { 152 | "scrolled": true 153 | }, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "[(101, 'Aman', 20), (102, 'Amit', 21), (107, 'Ankush', 22)]" 159 | ] 160 | }, 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "cur.execute(\"Select * from Student where Name like 'A%' \")\n", 168 | "cur.fetchall()" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.6.8" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 2 200 | } 201 | -------------------------------------------------------------------------------- /Lecture_16_Indexing/passingParameters.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sqlite3\n", 10 | "\n", 11 | "db = sqlite3.connect('School.sqlite')\n", 12 | "\n", 13 | "cur = db.cursor()" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 5, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "cur.execute('Insert into Student values(103, \"Mohit\", 24)')\n", 23 | "db.commit()" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 6, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "rollNumber = 104\n", 33 | "Name = \"Nikhil\"\n", 34 | "age = 34\n", 35 | "\n", 36 | "cur.execute('Insert into Student values (?, ?, ?)', (rollNumber, Name, age))\n", 37 | "\n", 38 | "db.commit()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 8, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# execute multiple statements\n", 48 | "\n", 49 | "values = [(105, \"Nidhi\", 20), (106, \"Manisha\", 21), (107, \"Ankush\", 22)]\n", 50 | "\n", 51 | "cur.executemany('Insert into Student values (?, ?, ?)', values)\n", 52 | "\n", 53 | "db.commit()" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### In case only one value require change\n", 61 | "\n", 62 | "```\n", 63 | "Make age 50 for student with roll number 103\n", 64 | "\n", 65 | "new_age = 50\n", 66 | "\n", 67 | "cur.execute(\"Update Student set Age = ? where RollNumber = 103\", (new_age, ))\n", 68 | "```" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [] 77 | } 78 | ], 79 | "metadata": { 80 | "kernelspec": { 81 | "display_name": "Python 3", 82 | "language": "python", 83 | "name": "python3" 84 | }, 85 | "language_info": { 86 | "codemirror_mode": { 87 | "name": "ipython", 88 | "version": 3 89 | }, 90 | "file_extension": ".py", 91 | "mimetype": "text/x-python", 92 | "name": "python", 93 | "nbconvert_exporter": "python", 94 | "pygments_lexer": "ipython3", 95 | "version": "3.6.8" 96 | } 97 | }, 98 | "nbformat": 4, 99 | "nbformat_minor": 2 100 | } 101 | -------------------------------------------------------------------------------- /ML/Feature_scaling in Sklearn/Feature_Scaling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "X = np.array([[1., -1., 2.],\n", 19 | " [2., 0., 0.],\n", 20 | " [0., 1., -1.]])" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 4, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "from sklearn import preprocessing" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 9, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "X_scaled = preprocessing.scale(X) # makes mean = zero, variance = one, column-wise" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 10, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "array([[ 0. , -1.22474487, 1.33630621],\n", 50 | " [ 1.22474487, 0. , -0.26726124],\n", 51 | " [-1.22474487, 1.22474487, -1.06904497]])" 52 | ] 53 | }, 54 | "execution_count": 10, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "X_scaled" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 11, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "[0. 0. 0.]\n", 73 | "[1. 1. 1.]\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "print(X_scaled.mean(axis = 0))\n", 79 | "print(X_scaled.std(axis = 0))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 16, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "[[ 1. -1. 2.]\n", 92 | " [ 2. 0. 0.]\n", 93 | " [ 0. 1. -1.]]\n", 94 | "[[ 0. -1.22474487 1.33630621]\n", 95 | " [ 1.22474487 0. -0.26726124]\n", 96 | " [-1.22474487 1.22474487 -1.06904497]]\n", 97 | "[[ 1. 2. 0.]\n", 98 | " [-1. 0. 1.]\n", 99 | " [ 2. 0. -1.]]\n" 100 | ] 101 | }, 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "array([[ 0. , 2.44948974, -0.26726124],\n", 106 | " [-2.44948974, 0. , 0.53452248],\n", 107 | " [ 1.22474487, 0. , -1.06904497]])" 108 | ] 109 | }, 110 | "execution_count": 16, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "scalar = preprocessing.StandardScaler()\n", 117 | "scalar.fit(X)\n", 118 | "print(X)\n", 119 | "print(scalar.transform(X))\n", 120 | "print(X.T)\n", 121 | "scalar.transform(X.T)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 3", 135 | "language": "python", 136 | "name": "python3" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 3 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython3", 148 | "version": "3.6.8" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 2 153 | } 154 | -------------------------------------------------------------------------------- /ML/Intro_to_ML/.ipynb_checkpoints/Intro_to_ML-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /ML/linearRegression/.ipynb_checkpoints/LR_on_dummy_data-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /ML/linearRegression/data.csv: -------------------------------------------------------------------------------- 1 | 32.502345269453031,31.70700584656992 2 | 53.426804033275019,68.77759598163891 3 | 61.530358025636438,62.562382297945803 4 | 47.475639634786098,71.546632233567777 5 | 59.813207869512318,87.230925133687393 6 | 55.142188413943821,78.211518270799232 7 | 52.211796692214001,79.64197304980874 8 | 39.299566694317065,59.171489321869508 9 | 48.10504169176825,75.331242297063056 10 | 52.550014442733818,71.300879886850353 11 | 45.419730144973755,55.165677145959123 12 | 54.351634881228918,82.478846757497919 13 | 44.164049496773352,62.008923245725825 14 | 58.16847071685779,75.392870425994957 15 | 56.727208057096611,81.43619215887864 16 | 48.955888566093719,60.723602440673965 17 | 44.687196231480904,82.892503731453715 18 | 60.297326851333466,97.379896862166078 19 | 45.618643772955828,48.847153317355072 20 | 38.816817537445637,56.877213186268506 21 | 66.189816606752601,83.878564664602763 22 | 65.41605174513407,118.59121730252249 23 | 47.48120860786787,57.251819462268969 24 | 41.57564261748702,51.391744079832307 25 | 51.84518690563943,75.380651665312357 26 | 59.370822011089523,74.765564032151374 27 | 57.31000343834809,95.455052922574737 28 | 63.615561251453308,95.229366017555307 29 | 46.737619407976972,79.052406169565586 30 | 50.556760148547767,83.432071421323712 31 | 52.223996085553047,63.358790317497878 32 | 35.567830047746632,41.412885303700563 33 | 42.436476944055642,76.617341280074044 34 | 58.16454011019286,96.769566426108199 35 | 57.504447615341789,74.084130116602523 36 | 45.440530725319981,66.588144414228594 37 | 61.89622268029126,77.768482417793024 38 | 33.093831736163963,50.719588912312084 39 | 36.436009511386871,62.124570818071781 40 | 37.675654860850742,60.810246649902211 41 | 44.555608383275356,52.682983366387781 42 | 43.318282631865721,58.569824717692867 43 | 50.073145632289034,82.905981485070512 44 | 43.870612645218372,61.424709804339123 45 | 62.997480747553091,115.24415280079529 46 | 32.669043763467187,45.570588823376085 47 | 40.166899008703702,54.084054796223612 48 | 53.575077531673656,87.994452758110413 49 | 33.864214971778239,52.725494375900425 50 | 64.707138666121296,93.576118692658241 51 | 38.119824026822805,80.166275447370964 52 | 44.502538064645101,65.101711570560326 53 | 40.599538384552318,65.562301260400375 54 | 41.720676356341293,65.280886920822823 55 | 51.088634678336796,73.434641546324301 56 | 55.078095904923202,71.13972785861894 57 | 41.377726534895203,79.102829683549857 58 | 62.494697427269791,86.520538440347153 59 | 49.203887540826003,84.742697807826218 60 | 41.102685187349664,59.358850248624933 61 | 41.182016105169822,61.684037524833627 62 | 50.186389494880601,69.847604158249183 63 | 52.378446219236217,86.098291205774103 64 | 50.135485486286122,59.108839267699643 65 | 33.644706006191782,69.89968164362763 66 | 39.557901222906828,44.862490711164398 67 | 56.130388816875467,85.498067778840223 68 | 57.362052133238237,95.536686846467219 69 | 60.269214393997906,70.251934419771587 70 | 35.678093889410732,52.721734964774988 71 | 31.588116998132829,50.392670135079896 72 | 53.66093226167304,63.642398775657753 73 | 46.682228649471917,72.247251068662365 74 | 43.107820219102464,57.812512976181402 75 | 70.34607561504933,104.25710158543822 76 | 44.492855880854073,86.642020318822006 77 | 57.50453330326841,91.486778000110135 78 | 36.930076609191808,55.231660886212836 79 | 55.805733357942742,79.550436678507609 80 | 38.954769073377065,44.847124242467601 81 | 56.901214702247074,80.207523139682763 82 | 56.868900661384046,83.14274979204346 83 | 34.33312470421609,55.723489260543914 84 | 59.04974121466681,77.634182511677864 85 | 57.788223993230673,99.051414841748269 86 | 54.282328705967409,79.120646274680027 87 | 51.088719898979143,69.588897851118475 88 | 50.282836348230731,69.510503311494389 89 | 44.211741752090113,73.687564318317285 90 | 38.005488008060688,61.366904537240131 91 | 32.940479942618296,67.170655768995118 92 | 53.691639571070056,85.668203145001542 93 | 68.76573426962166,114.85387123391394 94 | 46.230966498310252,90.123572069967423 95 | 68.319360818255362,97.919821035242848 96 | 50.030174340312143,81.536990783015028 97 | 49.239765342753763,72.111832469615663 98 | 50.039575939875988,85.232007342325673 99 | 48.149858891028863,66.224957888054632 100 | 25.128484647772304,53.454394214850524 101 | 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Science and Machine Learning Course by Coding Ninjas 2 | This repository contains my Jupyter notebook notes for the said course by [CodingNinjas](https://github.com/CodingNinjasCodes). 3 | 4 | I have tried to write notebooks in a way that they are sufficient on their own to learn the basics of Python, Machine Learning and Data Science. --------------------------------------------------------------------------------