├── Conditional Statements and Loops.ipynb ├── Exception_Handling.ipynb ├── Functions in Python.ipynb ├── Introduction to Python.ipynb ├── Lists, Tuples and Strings.ipynb ├── Operators.ipynb ├── README.md ├── Sets and Dictionaries.ipynb └── Types of Functions in Python.ipynb /Conditional Statements and Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "72721443", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Enter your marks: 67\n", 14 | "Hurray! You Passed.\n", 15 | "Sorry! You Failed\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "marks = int(input('Enter your marks: '))\n", 21 | "\n", 22 | "print('Hurray! You Passed.')\n", 23 | "print('Sorry! You Failed')" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "id": "0d3cc985", 29 | "metadata": {}, 30 | "source": [ 31 | "### `if-else` statements" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 8, 37 | "id": "f68be562", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "Enter the marks: 67\n", 45 | "Hurray! You Passed.\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "marks = int(input('Enter the marks: '))\n", 51 | "\n", 52 | "if marks >= 35:\n", 53 | " print('Hurray! You Passed.')\n", 54 | "else:\n", 55 | " print('Sorry! You Failed')" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 7, 61 | "id": "57d6f6d2", 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "Enter the marks: 210\n", 69 | "Sorry! You Failed\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "marks = int(input('Enter the marks: '))\n", 75 | "\n", 76 | "if marks >= 35 and marks <= 100:\n", 77 | " print('Hurray! You Passed.')\n", 78 | "else:\n", 79 | " print('Sorry! You Failed')" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "0a135266", 85 | "metadata": {}, 86 | "source": [ 87 | "### Nested If-else" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 15, 93 | "id": "1392ac7b", 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Enter the marks: 0\n", 101 | "Sorry! You Failed\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "marks = int(input('Enter the marks: '))\n", 107 | "\n", 108 | "if marks >=0 and marks <= 100:\n", 109 | " if marks >= 35 and marks <= 100:\n", 110 | " print('Hurray! You Passed.')\n", 111 | " else:\n", 112 | " print('Sorry! You Failed')\n", 113 | "else:\n", 114 | " print('Invalid Input. Please enter marks between 0 and 100.')" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "id": "f00708c7", 120 | "metadata": {}, 121 | "source": [ 122 | "### if-elif-else statement" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 18, 128 | "id": "1df9867b", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "Zero\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "number = 0\n", 141 | "\n", 142 | "if number >= 0:\n", 143 | " if number == 0:\n", 144 | " print('Zero')\n", 145 | " else:\n", 146 | " print('Positive')\n", 147 | "else:\n", 148 | " print('Negative')" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "id": "0bbf6c10-0941-4c25-9ede-887bcd88d9a6", 154 | "metadata": {}, 155 | "source": [ 156 | "##### The above nested if else statements can be written with if-elif-else statements to make it look crisp" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 20, 162 | "id": "2226c25c", 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Negative\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "number = -10\n", 175 | "\n", 176 | "if number == 0:\n", 177 | " print('Zero')\n", 178 | "elif number > 0:\n", 179 | " print('Positive')\n", 180 | "else:\n", 181 | " print('Negative')" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "id": "af2c12c2", 187 | "metadata": {}, 188 | "source": [ 189 | "marks < 35 - Failed\n", 190 | "\n", 191 | "marks >= 35 and marks <= 50: Grade C\n", 192 | " \n", 193 | "marks >= 51 and marks <= 65 : Grade B\n", 194 | "\n", 195 | "marks >= 66 and marks <= 80 : Grade A\n", 196 | "\n", 197 | "marks >= 81 and marks <= 100 : Outstanding" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 28, 203 | "id": "1d8c8a6f", 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "name": "stdout", 208 | "output_type": "stream", 209 | "text": [ 210 | "Enter the marks : -10\n", 211 | "Invalid Input.\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "marks = int(input('Enter the marks : '))\n", 217 | "\n", 218 | "if marks >=0 and marks <= 100:\n", 219 | " if marks < 35:\n", 220 | " print('Failed')\n", 221 | " elif marks >= 35 and marks <= 50:\n", 222 | " print('Grade C')\n", 223 | " elif marks >= 51 and marks <= 65:\n", 224 | " print('Grade B')\n", 225 | " elif marks >= 66 and marks <= 80:\n", 226 | " print('Grade A')\n", 227 | " else:\n", 228 | " print('Outstanding')\n", 229 | "else:\n", 230 | " print('Invalid Input.')" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "id": "172fe929", 236 | "metadata": {}, 237 | "source": [ 238 | "## Loops\n", 239 | "\n", 240 | "- while loops\n", 241 | "- for loops" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 34, 247 | "id": "62e11920", 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "Hello\n", 255 | "Hello\n", 256 | "Hello\n", 257 | "Hello\n", 258 | "Hello\n", 259 | "Hello\n", 260 | "Hello\n", 261 | "Hello\n", 262 | "Hello\n", 263 | "Hello\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "num1 = 10\n", 269 | "num2 = 20\n", 270 | "\n", 271 | "while num1 < num2:\n", 272 | " print('Hello')\n", 273 | " num2 -= 1" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 37, 279 | "id": "7470064c", 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "Enter a number: 13\n", 287 | "Enter a number: 14\n", 288 | "Enter a number: 16\n", 289 | "Enter a number: 99\n", 290 | "Enter a number: 63\n" 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "num1 = 10\n", 296 | "list1 = []\n", 297 | "\n", 298 | "while num1 < 15:\n", 299 | " value = int(input('Enter a number: '))\n", 300 | " list1.append(value)\n", 301 | " num1 += 1" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 38, 307 | "id": "dbaf2223", 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "[13, 14, 16, 99, 63]" 314 | ] 315 | }, 316 | "execution_count": 38, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "list1" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "id": "14e97812", 328 | "metadata": {}, 329 | "source": [ 330 | "### for loops\n", 331 | "\n", 332 | "- for loop uses iterables(lists, tuples, sets, dictionaries, string, range keyword) to run the loop." 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 1, 338 | "id": "9d61bc2a", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "list1 = [1,2,3,4,5,6,7,8]" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 2, 348 | "id": "26a06c9c", 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "name": "stdout", 353 | "output_type": "stream", 354 | "text": [ 355 | "Hello\n", 356 | "Hello\n", 357 | "Hello\n", 358 | "Hello\n", 359 | "Hello\n", 360 | "Hello\n", 361 | "Hello\n", 362 | "Hello\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "for value in list1:\n", 368 | " print('Hello')" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 3, 374 | "id": "606a6324", 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "word = 'Python'" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 4, 384 | "id": "e6f39942", 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "name": "stdout", 389 | "output_type": "stream", 390 | "text": [ 391 | "Hello\n", 392 | "Hello\n", 393 | "Hello\n", 394 | "Hello\n", 395 | "Hello\n", 396 | "Hello\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "for char in word:\n", 402 | " print('Hello')" 403 | ] 404 | }, 405 | { 406 | "cell_type": "markdown", 407 | "id": "0e04a2b3", 408 | "metadata": {}, 409 | "source": [ 410 | "#### range keyword" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 11, 416 | "id": "876b7143", 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "data": { 421 | "text/plain": [ 422 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]" 423 | ] 424 | }, 425 | "execution_count": 11, 426 | "metadata": {}, 427 | "output_type": "execute_result" 428 | } 429 | ], 430 | "source": [ 431 | "list(range(15))" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 12, 437 | "id": "0ed76502", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]" 444 | ] 445 | }, 446 | "execution_count": 12, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "list(range(1, 16)) #(start, stop+1)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 2, 458 | "id": "93bff88a", 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]" 465 | ] 466 | }, 467 | "execution_count": 2, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "list(range(1, 100, 10))" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 14, 479 | "id": "c03459f8", 480 | "metadata": {}, 481 | "outputs": [], 482 | "source": [ 483 | "numbers = list(range(1,21))" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 15, 489 | "id": "6a1a53e3", 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "data": { 494 | "text/plain": [ 495 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" 496 | ] 497 | }, 498 | "execution_count": 15, 499 | "metadata": {}, 500 | "output_type": "execute_result" 501 | } 502 | ], 503 | "source": [ 504 | "numbers" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 16, 510 | "id": "e3a0d4f1", 511 | "metadata": {}, 512 | "outputs": [], 513 | "source": [ 514 | "odd = []\n", 515 | "\n", 516 | "for value in numbers:\n", 517 | " if value % 2 != 0:\n", 518 | " odd.append(value)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 17, 524 | "id": "8a179c9c", 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]" 531 | ] 532 | }, 533 | "execution_count": 17, 534 | "metadata": {}, 535 | "output_type": "execute_result" 536 | } 537 | ], 538 | "source": [ 539 | "odd" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 18, 545 | "id": "135ad1b9", 546 | "metadata": {}, 547 | "outputs": [], 548 | "source": [ 549 | "even = []\n", 550 | "\n", 551 | "for value in numbers:\n", 552 | " if value % 2 == 0:\n", 553 | " even.append(value)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 19, 559 | "id": "2247ca14", 560 | "metadata": {}, 561 | "outputs": [ 562 | { 563 | "data": { 564 | "text/plain": [ 565 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" 566 | ] 567 | }, 568 | "execution_count": 19, 569 | "metadata": {}, 570 | "output_type": "execute_result" 571 | } 572 | ], 573 | "source": [ 574 | "even" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 20, 580 | "id": "1ea9b1d8", 581 | "metadata": {}, 582 | "outputs": [], 583 | "source": [ 584 | "odd_new = []\n", 585 | "even_new = []\n", 586 | "\n", 587 | "for value in numbers:\n", 588 | " if value % 2 != 0:\n", 589 | " odd_new.append(value)\n", 590 | " else:\n", 591 | " even_new.append(value)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": null, 597 | "id": "d41fabbc", 598 | "metadata": {}, 599 | "outputs": [], 600 | "source": [] 601 | }, 602 | { 603 | "cell_type": "markdown", 604 | "id": "1379fbeb", 605 | "metadata": {}, 606 | "source": [ 607 | "### Control Flow Statements\n", 608 | "\n", 609 | "- break \n", 610 | "- continue" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "id": "639d52a2", 616 | "metadata": {}, 617 | "source": [ 618 | "#### continue(skip an iteration)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 21, 624 | "id": "e9121fb8", 625 | "metadata": {}, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "[1, 2, 3, 4, 5, 6, 7, 8]" 631 | ] 632 | }, 633 | "execution_count": 21, 634 | "metadata": {}, 635 | "output_type": "execute_result" 636 | } 637 | ], 638 | "source": [ 639 | "list1" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": 22, 645 | "id": "2cb97908", 646 | "metadata": {}, 647 | "outputs": [ 648 | { 649 | "name": "stdout", 650 | "output_type": "stream", 651 | "text": [ 652 | "1\n", 653 | "2\n", 654 | "3\n", 655 | "4\n", 656 | "6\n", 657 | "7\n", 658 | "8\n" 659 | ] 660 | } 661 | ], 662 | "source": [ 663 | "for value in list1:\n", 664 | " if value == 5:\n", 665 | " continue\n", 666 | " print(value)" 667 | ] 668 | }, 669 | { 670 | "cell_type": "code", 671 | "execution_count": 23, 672 | "id": "61f81f35", 673 | "metadata": {}, 674 | "outputs": [], 675 | "source": [ 676 | "odd_list = []\n", 677 | "\n", 678 | "for value in list1:\n", 679 | " if value % 2 == 0:\n", 680 | " continue\n", 681 | " odd_list.append(value)" 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": 24, 687 | "id": "9d2d057e", 688 | "metadata": {}, 689 | "outputs": [ 690 | { 691 | "data": { 692 | "text/plain": [ 693 | "[1, 3, 5, 7]" 694 | ] 695 | }, 696 | "execution_count": 24, 697 | "metadata": {}, 698 | "output_type": "execute_result" 699 | } 700 | ], 701 | "source": [ 702 | "odd_list" 703 | ] 704 | }, 705 | { 706 | "cell_type": "markdown", 707 | "id": "9c248b1f", 708 | "metadata": {}, 709 | "source": [ 710 | "#### break (stop the iteration)" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 25, 716 | "id": "e468f3ff", 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "name": "stdout", 721 | "output_type": "stream", 722 | "text": [ 723 | "1\n", 724 | "2\n", 725 | "3\n", 726 | "4\n" 727 | ] 728 | } 729 | ], 730 | "source": [ 731 | "for value in list1:\n", 732 | " if value == 5:\n", 733 | " break\n", 734 | " print(value)" 735 | ] 736 | }, 737 | { 738 | "cell_type": "markdown", 739 | "id": "c3582c6e", 740 | "metadata": {}, 741 | "source": [ 742 | "### List Comprehension" 743 | ] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "execution_count": 26, 748 | "id": "a26c6dce", 749 | "metadata": {}, 750 | "outputs": [ 751 | { 752 | "data": { 753 | "text/plain": [ 754 | "[1, 2, 3, 4, 5, 6, 7, 8]" 755 | ] 756 | }, 757 | "execution_count": 26, 758 | "metadata": {}, 759 | "output_type": "execute_result" 760 | } 761 | ], 762 | "source": [ 763 | "list1" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 27, 769 | "id": "9556a5a3", 770 | "metadata": {}, 771 | "outputs": [], 772 | "source": [ 773 | "squared = []\n", 774 | "\n", 775 | "for value in list1:\n", 776 | " squared.append(value ** 2)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 28, 782 | "id": "5e26ea80", 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "data": { 787 | "text/plain": [ 788 | "[1, 4, 9, 16, 25, 36, 49, 64]" 789 | ] 790 | }, 791 | "execution_count": 28, 792 | "metadata": {}, 793 | "output_type": "execute_result" 794 | } 795 | ], 796 | "source": [ 797 | "squared" 798 | ] 799 | }, 800 | { 801 | "cell_type": "markdown", 802 | "id": "16fab93d-e89c-4db1-b48f-68366597cc71", 803 | "metadata": {}, 804 | "source": [ 805 | "##### The above code can be written in a short and simple way using list comprehension as shown below" 806 | ] 807 | }, 808 | { 809 | "cell_type": "code", 810 | "execution_count": 29, 811 | "id": "b1362c8e", 812 | "metadata": {}, 813 | "outputs": [], 814 | "source": [ 815 | "squared_new = [value**2 for value in list1]" 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": 30, 821 | "id": "a05ffbfb", 822 | "metadata": {}, 823 | "outputs": [ 824 | { 825 | "data": { 826 | "text/plain": [ 827 | "[1, 4, 9, 16, 25, 36, 49, 64]" 828 | ] 829 | }, 830 | "execution_count": 30, 831 | "metadata": {}, 832 | "output_type": "execute_result" 833 | } 834 | ], 835 | "source": [ 836 | "squared_new" 837 | ] 838 | }, 839 | { 840 | "cell_type": "markdown", 841 | "id": "131b21e8-fc46-45f2-bd50-d60157cc6cf8", 842 | "metadata": {}, 843 | "source": [ 844 | "##### Another example of list comprehension" 845 | ] 846 | }, 847 | { 848 | "cell_type": "code", 849 | "execution_count": 37, 850 | "id": "c2df3bab", 851 | "metadata": {}, 852 | "outputs": [], 853 | "source": [ 854 | "new_list = [{value : value**2} for value in list1]" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 38, 860 | "id": "006eadb4", 861 | "metadata": {}, 862 | "outputs": [ 863 | { 864 | "data": { 865 | "text/plain": [ 866 | "[{1: 1}, {2: 4}, {3: 9}, {4: 16}, {5: 25}, {6: 36}, {7: 49}, {8: 64}]" 867 | ] 868 | }, 869 | "execution_count": 38, 870 | "metadata": {}, 871 | "output_type": "execute_result" 872 | } 873 | ], 874 | "source": [ 875 | "new_list" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": null, 881 | "id": "c5500f5e", 882 | "metadata": {}, 883 | "outputs": [], 884 | "source": [ 885 | "##### Odd numbers list code with and without list comprehension" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": 39, 891 | "id": "19a246d5", 892 | "metadata": {}, 893 | "outputs": [ 894 | { 895 | "data": { 896 | "text/plain": [ 897 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" 898 | ] 899 | }, 900 | "execution_count": 39, 901 | "metadata": {}, 902 | "output_type": "execute_result" 903 | } 904 | ], 905 | "source": [ 906 | "numbers" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": null, 912 | "id": "51a32cdd", 913 | "metadata": {}, 914 | "outputs": [], 915 | "source": [ 916 | "odd = []\n", 917 | "\n", 918 | "for value in numbers:\n", 919 | " if value % 2 != 0:\n", 920 | " odd.append(value)" 921 | ] 922 | }, 923 | { 924 | "cell_type": "code", 925 | "execution_count": 40, 926 | "id": "1472db5b", 927 | "metadata": {}, 928 | "outputs": [], 929 | "source": [ 930 | "new_odd = [value for value in numbers if value % 2 != 0]" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": 41, 936 | "id": "7ea2f607", 937 | "metadata": {}, 938 | "outputs": [ 939 | { 940 | "data": { 941 | "text/plain": [ 942 | "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]" 943 | ] 944 | }, 945 | "execution_count": 41, 946 | "metadata": {}, 947 | "output_type": "execute_result" 948 | } 949 | ], 950 | "source": [ 951 | "new_odd" 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": null, 957 | "id": "bdd64382", 958 | "metadata": {}, 959 | "outputs": [], 960 | "source": [] 961 | }, 962 | { 963 | "cell_type": "markdown", 964 | "id": "c6fdf8df", 965 | "metadata": {}, 966 | "source": [ 967 | "**Tasks to be performed**\n", 968 | "\n", 969 | "1. Create a small database which has some username and passwords.\n", 970 | "2. Ask the user for username and password. If those match with our database, then print Hello {username}.\n", 971 | "3. If the username and password doesnot match with our database, then we ask them whether they want tot be added to the database.\n", 972 | "4. If their response is Yes, then add them to the database, otherwise print a Good Bye message to them.\n", 973 | "5. Show the updated Database." 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "execution_count": 42, 979 | "id": "3487bd1c", 980 | "metadata": {}, 981 | "outputs": [], 982 | "source": [ 983 | "database = {'virat': 3345, 'sharan': 6600, 'vipul':9054, 'krishna': 7732, 'chandan': 1100}" 984 | ] 985 | }, 986 | { 987 | "cell_type": "code", 988 | "execution_count": 56, 989 | "id": "9de974ed", 990 | "metadata": {}, 991 | "outputs": [ 992 | { 993 | "name": "stdout", 994 | "output_type": "stream", 995 | "text": [ 996 | "Enter your username: virat\n", 997 | "Enter your password: 9000\n", 998 | "Incorrect Password. Please try again.\n", 999 | "{'virat': 3345, 'sharan': 6600, 'vipul': 9054, 'krishna': 7732, 'chandan': 1100, 'sachin': 7765, 'neha': 6655}\n" 1000 | ] 1001 | } 1002 | ], 1003 | "source": [ 1004 | "username = input('Enter your username: ').lower()\n", 1005 | "\n", 1006 | "if username in database.keys():\n", 1007 | " password = int(input('Enter your password: '))\n", 1008 | " if database[username] == password:\n", 1009 | " print(f'Hello {username}')\n", 1010 | " else:\n", 1011 | " print('Incorrect Password. Please try again.')\n", 1012 | "else:\n", 1013 | " print('It seems that either your username is incorrect or you are not in our database.')\n", 1014 | " response = input('Do you want to be added to the database? (Yes/No): ').lower()\n", 1015 | " \n", 1016 | " if response in ['yes', 'y']:\n", 1017 | " password = int(input('Enter your password: '))\n", 1018 | " database[username] = password\n", 1019 | " else:\n", 1020 | " print('Thank You for Visiting.')\n", 1021 | "\n", 1022 | "print(database)" 1023 | ] 1024 | } 1025 | ], 1026 | "metadata": { 1027 | "kernelspec": { 1028 | "display_name": "Python 3 (ipykernel)", 1029 | "language": "python", 1030 | "name": "python3" 1031 | }, 1032 | "language_info": { 1033 | "codemirror_mode": { 1034 | "name": "ipython", 1035 | "version": 3 1036 | }, 1037 | "file_extension": ".py", 1038 | "mimetype": "text/x-python", 1039 | "name": "python", 1040 | "nbconvert_exporter": "python", 1041 | "pygments_lexer": "ipython3", 1042 | "version": "3.11.7" 1043 | } 1044 | }, 1045 | "nbformat": 4, 1046 | "nbformat_minor": 5 1047 | } 1048 | -------------------------------------------------------------------------------- /Exception_Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "RKOTCuKlJVZ8" 8 | }, 9 | "source": [ 10 | "# Python Errors and Built-in-Exceptions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "colab_type": "text", 17 | "id": "6s108dVnJVaB" 18 | }, 19 | "source": [ 20 | "When writing a program, we, more often than not, will encounter errors.\n", 21 | "\n", 22 | "Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "ename": "IndentationError", 32 | "evalue": "expected an indented block after 'if' statement on line 1 (3598588081.py, line 2)", 33 | "output_type": "error", 34 | "traceback": [ 35 | "\u001b[1;36m Cell \u001b[1;32mIn[1], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"Hello\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block after 'if' statement on line 1\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "if True:\n", 41 | "print(\"Hello\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "### Broadly speaking there are 3 main kinds of errors in programming:\n", 49 | "\n", 50 | "**1. Compile-Time Errors**\n", 51 | "\n", 52 | "- We don’t get these in Python\n", 53 | "- Python is a dynamically typed language\n", 54 | "- No compilation of code as such\n", 55 | "- We have `Syntax Errors` in Python\n", 56 | "\n", 57 | "**2. Logical Errors**\n", 58 | "\n", 59 | "- Whenever there is a problem with our logic\n", 60 | "- Example - Doing Subtraction where Addition was required logically\n", 61 | "\n", 62 | "**3. Run-Time Errors**\n", 63 | "\n", 64 | "- Occur when we run the code\n", 65 | "\n", 66 | "**There can be different types of `Run-Time` errors**\n", 67 | "- Syntax Error\n", 68 | "- Name Error\n", 69 | "- Value Error\n", 70 | "- DivisionByZero Error\n", 71 | "- ModuleNotFound Error\n", 72 | "- FileNotFound Error\n", 73 | "- … and so on" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "#### What’s the difference between Errors and Expections?\n", 81 | "- Errors, in general, refer to **Syntactical or Interpreter-related Errors**\n", 82 | "- Errors detected during **execution are called Exceptions.**" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": { 88 | "colab_type": "text", 89 | "id": "xsuCeIpEJVaT" 90 | }, 91 | "source": [ 92 | "They occur, for example, when a file we try to open does not exist **(FileNotFoundError)**, dividing a number by zero **(ZeroDivisionError)**, module we try to import is not found **(ModuleNotFoundError)** etc." 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "colab_type": "text", 99 | "id": "W5dOI-SbJVaV" 100 | }, 101 | "source": [ 102 | "Whenever these type of runtime error occur, Python creates an exception object. If not handled properly, it prints a traceback to that error along with some details about why that error occurred." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 17, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "ename": "NameError", 112 | "evalue": "name 'num' is not defined", 113 | "output_type": "error", 114 | "traceback": [ 115 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 116 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 117 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 118 | "\u001b[1;31mNameError\u001b[0m: name 'num' is not defined" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "print(num)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 18, 129 | "metadata": { 130 | "colab": {}, 131 | "colab_type": "code", 132 | "id": "2OofqS54JVaY", 133 | "outputId": "99857d27-a308-4152-ed24-f145748fb2cc" 134 | }, 135 | "outputs": [ 136 | { 137 | "ename": "ZeroDivisionError", 138 | "evalue": "division by zero", 139 | "output_type": "error", 140 | "traceback": [ 141 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 142 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 143 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m1\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 144 | "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "1 / 0" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 19, 155 | "metadata": { 156 | "colab": {}, 157 | "colab_type": "code", 158 | "id": "oopH8CqAJVah", 159 | "outputId": "65a06ba0-579d-4f16-c9cd-fb18a205a6b4" 160 | }, 161 | "outputs": [ 162 | { 163 | "ename": "FileNotFoundError", 164 | "evalue": "[Errno 2] No such file or directory: 'new_file.txt'", 165 | "output_type": "error", 166 | "traceback": [ 167 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 168 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 169 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'new_file.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 170 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'new_file.txt'" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "open('new_file.txt')" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 20, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "ename": "SyntaxError", 185 | "evalue": "unexpected EOF while parsing (, line 1)", 186 | "output_type": "error", 187 | "traceback": [ 188 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m if (5>3):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "if (5>3):" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 23, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "ename": "ModuleNotFoundError", 203 | "evalue": "No module named 'something'", 204 | "output_type": "error", 205 | "traceback": [ 206 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 207 | "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 208 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0msomething\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 209 | "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'something'" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "import something" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": { 220 | "colab_type": "text", 221 | "id": "zPvLQlNvJVan" 222 | }, 223 | "source": [ 224 | "# Python Built-in Exceptions" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 3, 230 | "metadata": { 231 | "colab": {}, 232 | "colab_type": "code", 233 | "id": "g-KuGghuJVao", 234 | "outputId": "9c5203c5-5648-4fe4-ec03-6042a3d2789b" 235 | }, 236 | "outputs": [], 237 | "source": [ 238 | "temp = dir(__builtins__)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 4, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "ArithmeticError\n", 251 | "AssertionError\n", 252 | "AttributeError\n", 253 | "BlockingIOError\n", 254 | "BrokenPipeError\n", 255 | "BufferError\n", 256 | "ChildProcessError\n", 257 | "ConnectionAbortedError\n", 258 | "ConnectionError\n", 259 | "ConnectionRefusedError\n", 260 | "ConnectionResetError\n", 261 | "EOFError\n", 262 | "EnvironmentError\n", 263 | "FileExistsError\n", 264 | "FileNotFoundError\n", 265 | "FloatingPointError\n", 266 | "IOError\n", 267 | "ImportError\n", 268 | "IndentationError\n", 269 | "IndexError\n", 270 | "InterruptedError\n", 271 | "IsADirectoryError\n", 272 | "KeyError\n", 273 | "LookupError\n", 274 | "MemoryError\n", 275 | "ModuleNotFoundError\n", 276 | "NameError\n", 277 | "NotADirectoryError\n", 278 | "NotImplementedError\n", 279 | "OSError\n", 280 | "OverflowError\n", 281 | "PermissionError\n", 282 | "ProcessLookupError\n", 283 | "RecursionError\n", 284 | "ReferenceError\n", 285 | "RuntimeError\n", 286 | "SyntaxError\n", 287 | "SystemError\n", 288 | "TabError\n", 289 | "TimeoutError\n", 290 | "TypeError\n", 291 | "UnboundLocalError\n", 292 | "UnicodeDecodeError\n", 293 | "UnicodeEncodeError\n", 294 | "UnicodeError\n", 295 | "UnicodeTranslateError\n", 296 | "ValueError\n", 297 | "WindowsError\n", 298 | "ZeroDivisionError\n", 299 | "Total Exceptions: 49\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "count = 0\n", 305 | "for element in temp:\n", 306 | " if \"Error\" in element:\n", 307 | " print(element)\n", 308 | " count += 1\n", 309 | "print('Total Exceptions:', count)" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": { 315 | "colab_type": "text", 316 | "id": "Oz1CmO7QJVat" 317 | }, 318 | "source": [ 319 | "# Python Exception Handling - Try, Except and Finally" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": { 325 | "colab_type": "text", 326 | "id": "zuin4AchJVau" 327 | }, 328 | "source": [ 329 | "- Python has many built-in exceptions which forces your program to output an error when something in it goes wrong.\n", 330 | "- When these exceptions occur, it causes the current process to stop and passes it to the calling process until it is handled. If not handled, our program will crash.\n", 331 | "- For example, if function A calls function B which in turn calls function C and an exception occurs in function C. If it is not handled in C, the exception passes to B and then to A.\n", 332 | "- If never handled, an error message is spit out and our program come to a sudden, unexpected halt." 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": { 338 | "colab_type": "text", 339 | "id": "DP3IEeUzJVax" 340 | }, 341 | "source": [ 342 | "# Catching Exceptions in Python" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": { 348 | "colab_type": "text", 349 | "id": "0OoY_4ByJVay" 350 | }, 351 | "source": [ 352 | "In Python, exceptions can be handled using a try statement.\n", 353 | "\n", 354 | "A critical operation which can raise exception is placed inside the `try clause` and the code that handles exception is written in `except clause`." 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 5, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdin", 364 | "output_type": "stream", 365 | "text": [ 366 | "Enter first number : 43\n", 367 | "Enter second number: 0\n" 368 | ] 369 | }, 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "Some error occured.\n" 375 | ] 376 | } 377 | ], 378 | "source": [ 379 | "#num1 = int(input('Enter first number :'))\n", 380 | "#num2 = int(input('Enter second number: '))\n", 381 | "try:\n", 382 | " num1 = int(input('Enter first number :'))\n", 383 | " num2 = int(input('Enter second number: '))\n", 384 | "\n", 385 | " result = num1 / num2\n", 386 | " print(result)\n", 387 | "except:\n", 388 | " print('Some error occured.')" 389 | ] 390 | }, 391 | { 392 | "cell_type": "markdown", 393 | "metadata": {}, 394 | "source": [ 395 | "**except** block receives the program flow if any line inside try block throws an error." 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 6, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "1.0\n", 408 | "0.5\n", 409 | "0.3333333333333333\n", 410 | " occured\n", 411 | "0.2\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "import sys\n", 417 | "\n", 418 | "list1 = [1,2,3,'a', '5']\n", 419 | "\n", 420 | "for ele in list1:\n", 421 | " try:\n", 422 | " result = 1/int(ele)\n", 423 | " print(result)\n", 424 | " except:\n", 425 | " print(sys.exc_info()[0],\"occured\")" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": { 431 | "colab_type": "text", 432 | "id": "AQf5aGKGJVa5" 433 | }, 434 | "source": [ 435 | "# Catching Specific Exceptions in Python" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": { 441 | "colab_type": "text", 442 | "id": "1MyRM0IsJVa6" 443 | }, 444 | "source": [ 445 | "In the above example, we did not mention any exception in the except clause.\n", 446 | "\n", 447 | "This is not a good programming practice as it will catch all exceptions and handle every case in the same way. We can specify which exceptions an except clause will catch.\n", 448 | "\n", 449 | "A try clause can have any number of except clause to handle them differently but only one will be executed in case an exception occurs." 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 7, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "1.0\n", 462 | "0.5\n", 463 | "0.3333333333333333\n" 464 | ] 465 | }, 466 | { 467 | "ename": "ValueError", 468 | "evalue": "invalid literal for int() with base 10: 'a'", 469 | "output_type": "error", 470 | "traceback": [ 471 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 472 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 473 | "Cell \u001b[1;32mIn[7], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m list1 \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m5\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m ele \u001b[38;5;129;01min\u001b[39;00m list1:\n\u001b[1;32m----> 4\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;241m/\u001b[39m \u001b[38;5;28mint\u001b[39m(ele)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n", 474 | "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a'" 475 | ] 476 | } 477 | ], 478 | "source": [ 479 | "list1 = [1,2,3,'a', '5', 0]\n", 480 | "\n", 481 | "for ele in list1:\n", 482 | " result = 1 / int(ele)\n", 483 | " print(result)" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 8, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "1.0\n", 496 | "0.5\n", 497 | "0.3333333333333333\n", 498 | "Value Error occured.\n", 499 | "0.2\n", 500 | "You were trying to divide a number by zero.\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "list1 = [1,2,3,'a', '5', 0]\n", 506 | "\n", 507 | "for ele in list1:\n", 508 | " try:\n", 509 | " result = 1/int(ele)\n", 510 | " print(result)\n", 511 | " except (ValueError):\n", 512 | " print(\"Value Error occured.\")\n", 513 | " except (ZeroDivisionError):\n", 514 | " print(\"You were trying to divide a number by zero.\")\n", 515 | " except (NameError):\n", 516 | " print(\"Please check the variable names.\")\n", 517 | " except:\n", 518 | " print(\"Some other error occured\")" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": { 524 | "colab_type": "text", 525 | "id": "H2wD1Y1jJVa_" 526 | }, 527 | "source": [ 528 | "# Raising Exceptions" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": { 534 | "colab_type": "text", 535 | "id": "ZKgobp0RJVbA" 536 | }, 537 | "source": [ 538 | "In Python programming, exceptions are raised when corresponding errors occur at run time, but we can forcefully raise it using the keyword raise.\n", 539 | "\n", 540 | "We can also optionally pass in value to the exception to clarify why that exception was raised." 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 11, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "name": "stdin", 550 | "output_type": "stream", 551 | "text": [ 552 | "Enter the marks: -10\n" 553 | ] 554 | }, 555 | { 556 | "ename": "ValueError", 557 | "evalue": "Marks entered should be between 0 to 100.", 558 | "output_type": "error", 559 | "traceback": [ 560 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 561 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 562 | "Cell \u001b[1;32mIn[11], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m marks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEnter the marks: \u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m marks \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m marks \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m100\u001b[39m:\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMarks entered should be between 0 to 100.\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", 563 | "\u001b[1;31mValueError\u001b[0m: Marks entered should be between 0 to 100." 564 | ] 565 | } 566 | ], 567 | "source": [ 568 | "marks = int(input('Enter the marks: '))\n", 569 | "\n", 570 | "if marks < 0 or marks > 100:\n", 571 | " raise ValueError('Marks entered should be between 0 to 100.')" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 12, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "name": "stdin", 581 | "output_type": "stream", 582 | "text": [ 583 | "Enter a number : -6\n" 584 | ] 585 | }, 586 | { 587 | "ename": "ValueError", 588 | "evalue": "x should not be less than 0.", 589 | "output_type": "error", 590 | "traceback": [ 591 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 592 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 593 | "Cell \u001b[1;32mIn[12], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a number :\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m x \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx should not be less than 0.\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", 594 | "\u001b[1;31mValueError\u001b[0m: x should not be less than 0." 595 | ] 596 | } 597 | ], 598 | "source": [ 599 | "x = int(input(\"Enter a number :\"))\n", 600 | "if x < 0:\n", 601 | " raise ValueError('x should not be less than 0.')" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 13, 607 | "metadata": { 608 | "colab": {}, 609 | "colab_type": "code", 610 | "id": "XHquZUEpJVbB", 611 | "outputId": "4e950ee7-61fe-4703-99e5-96df11a94fc6" 612 | }, 613 | "outputs": [ 614 | { 615 | "ename": "KeyboardInterrupt", 616 | "evalue": "", 617 | "output_type": "error", 618 | "traceback": [ 619 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 620 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 621 | "Cell \u001b[1;32mIn[13], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Will Interrupt the flow of the program if we press keys on keyboard\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m\n", 622 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 623 | ] 624 | } 625 | ], 626 | "source": [ 627 | "# Will Interrupt the flow of the program if we press keys on keyboard\n", 628 | "raise KeyboardInterrupt" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 14, 634 | "metadata": { 635 | "colab": {}, 636 | "colab_type": "code", 637 | "id": "3jTxg_RPJVbF", 638 | "outputId": "2555bc78-679a-4b8c-a95d-91b19a2524d9" 639 | }, 640 | "outputs": [ 641 | { 642 | "ename": "MemoryError", 643 | "evalue": "This is memory Error", 644 | "output_type": "error", 645 | "traceback": [ 646 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 647 | "\u001b[1;31mMemoryError\u001b[0m Traceback (most recent call last)", 648 | "Cell \u001b[1;32mIn[14], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# I don't want my system to use more than 2GB RAM, in that case we can use this\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mMemoryError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis is memory Error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", 649 | "\u001b[1;31mMemoryError\u001b[0m: This is memory Error" 650 | ] 651 | } 652 | ], 653 | "source": [ 654 | "# I don't want my system to use more than 2GB RAM, in that case we can use this\n", 655 | "raise MemoryError(\"This is memory Error\")" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 15, 661 | "metadata": { 662 | "colab": {}, 663 | "colab_type": "code", 664 | "id": "XD_gufcbJVbJ", 665 | "outputId": "ea0418bb-f31c-4a59-af2e-9033ce304b3d" 666 | }, 667 | "outputs": [ 668 | { 669 | "name": "stdin", 670 | "output_type": "stream", 671 | "text": [ 672 | "Enter a positive integer: -6\n" 673 | ] 674 | }, 675 | { 676 | "name": "stdout", 677 | "output_type": "stream", 678 | "text": [ 679 | "Error:Entered negative number\n" 680 | ] 681 | } 682 | ], 683 | "source": [ 684 | "try:\n", 685 | " num = int(input(\"Enter a positive integer:\"))\n", 686 | " if num <= 0:\n", 687 | " raise ValueError(\"Error:Entered negative number\")\n", 688 | "except ValueError as e:\n", 689 | " print(e)" 690 | ] 691 | }, 692 | { 693 | "cell_type": "markdown", 694 | "metadata": { 695 | "colab_type": "text", 696 | "id": "okNSiFRYJVbN" 697 | }, 698 | "source": [ 699 | "# try ... finally" 700 | ] 701 | }, 702 | { 703 | "cell_type": "markdown", 704 | "metadata": { 705 | "colab_type": "text", 706 | "id": "9xOuS1PgJVbN" 707 | }, 708 | "source": [ 709 | "The try statement in Python can have an optional finally clause. This clause is executed no matter what, and is generally used to release external resources." 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 16, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "def divide(x, y):\n", 719 | " try:\n", 720 | " # Floor Division : Gives only Fractional\n", 721 | " # Part as Answer\n", 722 | " result = x // y\n", 723 | " except ZeroDivisionError:\n", 724 | " print(\"Sorry ! You are dividing by zero \")\n", 725 | " else:\n", 726 | " print(\"Yeah ! Your answer is :\", result)\n", 727 | " finally: \n", 728 | " # this block is always executed \n", 729 | " # regardless of exception generation. \n", 730 | " print('This is always executed')" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 17, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "name": "stdout", 740 | "output_type": "stream", 741 | "text": [ 742 | "Yeah ! Your answer is : 1\n", 743 | "This is always executed\n" 744 | ] 745 | } 746 | ], 747 | "source": [ 748 | "# Look at parameters and note the working of Program\n", 749 | "divide(3, 2)" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": 18, 755 | "metadata": {}, 756 | "outputs": [ 757 | { 758 | "name": "stdout", 759 | "output_type": "stream", 760 | "text": [ 761 | "Sorry ! You are dividing by zero \n", 762 | "This is always executed\n" 763 | ] 764 | } 765 | ], 766 | "source": [ 767 | "divide(3, 0)" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": null, 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [] 776 | } 777 | ], 778 | "metadata": { 779 | "colab": { 780 | "name": "errors_built_in_exceptions.ipynb", 781 | "provenance": [], 782 | "version": "0.3.2" 783 | }, 784 | "kernelspec": { 785 | "display_name": "Python 3 (ipykernel)", 786 | "language": "python", 787 | "name": "python3" 788 | }, 789 | "language_info": { 790 | "codemirror_mode": { 791 | "name": "ipython", 792 | "version": 3 793 | }, 794 | "file_extension": ".py", 795 | "mimetype": "text/x-python", 796 | "name": "python", 797 | "nbconvert_exporter": "python", 798 | "pygments_lexer": "ipython3", 799 | "version": "3.11.7" 800 | } 801 | }, 802 | "nbformat": 4, 803 | "nbformat_minor": 4 804 | } 805 | -------------------------------------------------------------------------------- /Functions in Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d3ae2dd9", 6 | "metadata": {}, 7 | "source": [ 8 | "- Functions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems.\n", 9 | "\n", 10 | "- Formally, a function is a group of set of statements so they can be run more than once. We can also specify parameters that can serve as inputs to the functions.\n", 11 | "\n", 12 | "- Functions allow us to not have to repeatedly write the same code again and again. If we see the Python Data Types like strings and lists we used a function len() to get the length of a string. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command.\n", 13 | "\n", 14 | "- Functions will be one of the most basic levels of reusing code in Python." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "id": "2f4b0a3c", 20 | "metadata": {}, 21 | "source": [ 22 | "**Two main advantages of using Functions are:** \n", 23 | "- Makes your code more organized and manageable.\n", 24 | "- Brings resuability there by avoiding code redundency. " 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "08f9e1a4", 30 | "metadata": {}, 31 | "source": [ 32 | "#### Some terminologies of a function :\n", 33 | "\n", 34 | "- **`def`** - Marks the start of the function header.\n", 35 | "- **`function name`** - used to uniquely identify the function. Function naming follows the same check list which we followed for variables.\n", 36 | "- **`Params/Args`** - used to pass values to a function. Params are optional.\n", 37 | "- **`Colon (:)`**- marks the end of the function header.\n", 38 | "- **`Doc String`** - A short description about the function.This is optional.\n", 39 | "- **`Business logic/Statements`** - One ore more valid Python statements to perform the required task.\n", 40 | "- **`return`** - This is optional. But this statement will help you to return a value from the function.\n", 41 | "- **`print`** - To dispaly the value from the function. This is optional.\n", 42 | "Either print or return need to be included at the end of the function.\n", 43 | "Make sure proper indentation is given inside the function body." 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "70bdf08f", 49 | "metadata": {}, 50 | "source": [ 51 | "#### There are essentially two parts of a function: \n", 52 | "1. Function Definition\n", 53 | "2. Function Call" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "id": "c325b3fe", 59 | "metadata": {}, 60 | "source": [ 61 | "### Function Definition" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "id": "b418a3e5", 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "def maximum(num1, num2):\n", 72 | " if num1 > num2:\n", 73 | " print(num1, 'is greater than', num2)\n", 74 | " else:\n", 75 | " print(num2, 'is greater than', num1)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "5ca1e8d1", 81 | "metadata": {}, 82 | "source": [ 83 | "Above we have few things to know:\n", 84 | "1. `maximum` is the function name\n", 85 | "2. `num1` and `num2` are parameter which will be inputs for the function\n", 86 | "3. Then we have main logic in the function body" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "7a9cf1fa", 92 | "metadata": {}, 93 | "source": [ 94 | "### Function Call" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "42e47379", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "num1 = int(input('Enter the first number: '))\n", 105 | "num2 = int(input('Enter the second number: '))\n", 106 | "\n", 107 | "maximum(num1, num2)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "id": "33b6adf3", 113 | "metadata": {}, 114 | "source": [ 115 | "### Execution Flow when the functions are involved" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "id": "92482f1b", 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "print('A function is defined.')\n", 126 | "\n", 127 | "def maximum(num1, num2):\n", 128 | " if num1 > num2:\n", 129 | " print(num1, 'is greater than', num2)\n", 130 | " else:\n", 131 | " print(num2, 'is greater than', num1)\n", 132 | " \n", 133 | "print('The function has been defined but not called yet.')\n", 134 | "\n", 135 | "maximum(76, 22)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "id": "22aba513", 141 | "metadata": {}, 142 | "source": [ 143 | "### Parameter and Arguments\n", 144 | "\n", 145 | "The `parameter` is the name defined `in the parenthesis of the function` and can be used in the function body.\n", 146 | "\n", 147 | "The `argument` is the data `that is passed in when we call the function`, which is then assigned to the parameter name." 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "id": "9eab7824", 153 | "metadata": {}, 154 | "source": [ 155 | "### Types of Arguments\n", 156 | "\n", 157 | "In Python, there are 3 different types of arguments we can give a function.\n", 158 | "\n", 159 | "- **Positional arguments:** arguments that can be called by their position in the function definition.\n", 160 | "\n", 161 | "- **Keyword arguments:** arguments that can be called by their name.\n", 162 | "\n", 163 | "- **Default arguments:** arguments that are given default values." 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "c344c30c", 169 | "metadata": {}, 170 | "source": [ 171 | "#### Keyword Arguments\n", 172 | "Keyword Arguments are where we explicitly refer to what each argument is assigned to in the function call." 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "id": "0c2cddb8", 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "maximum(num2 = 43, num1 = 99) #notice the positions of arguments are not same as the parameters" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "id": "e06149be", 188 | "metadata": {}, 189 | "source": [ 190 | "#### Default Arguments\n", 191 | "\n", 192 | "Sometimes we want to give our function parameters default values. We can provide a default value to a parameter by using the assignment operator (=). This will happen in the function declaration rather than the function call." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "id": "0bf46df2", 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "def maximum(num1, num2 = 0): #num2 here is default argument since it has default value assigned\n", 203 | " if num1 > num2:\n", 204 | " print(num1, 'is greater than', num2)\n", 205 | " else:\n", 206 | " print(num2, 'is greater than', num1)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "id": "58408ad1", 212 | "metadata": {}, 213 | "source": [ 214 | "When using the default arguments, we can skip the value of default argument(num2 here) or we can overwrite it by giving the new values while function call." 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "id": "396d7798", 220 | "metadata": {}, 221 | "source": [ 222 | "#### Arbitrary arguments" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "id": "5a15c23e", 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "def add(*num):\n", 233 | " total = 0\n", 234 | " for value in num:\n", 235 | " total += value\n", 236 | " \n", 237 | " return total" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "3c9dd644", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "add(34, 32, 98, 90, 55, 67)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "id": "a4e157f9", 253 | "metadata": {}, 254 | "source": [ 255 | "### Built-in Function vs User-Defined Function\n", 256 | "\n", 257 | "Till now whatever we were working with are User-Defined Functions. Another set of functions which are called built-in functions comes built into Python for us to use. We have been using a lot of them till now like `print()`, `len()`, `input()`, `id()`, `type()` etc. We can see the list of in-built functions [here](https://docs.python.org/3/library/functions.html)." 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "id": "bd75baec", 263 | "metadata": {}, 264 | "source": [ 265 | "### Function with or without `return`" 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "id": "d670e855", 271 | "metadata": {}, 272 | "source": [ 273 | "Till now, our functions have been using print() to help us visualize the output in our interpreter. Functions can also return a value to the program so that this value can be modified or used later." 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "id": "6a13a7d4", 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "def greater(num1, num2):\n", 284 | " if num1 > num2:\n", 285 | " result = num1\n", 286 | " else:\n", 287 | " result = num2\n", 288 | " \n", 289 | " return result" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": null, 295 | "id": "07a6e9da", 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "returned_val = greater(87, 32)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "id": "b7f3f629", 305 | "metadata": {}, 306 | "source": [ 307 | "Saving the values returned from a function can be reused throughout the program. The same can't be done if we print the result instead of returning it." 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "id": "9362b9d6", 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "num1 = int(input('Enter the first number: '))\n", 318 | "num2 = int(input('Enter the second number: '))\n", 319 | "\n", 320 | "print('The maximum of two given numbers {num1} and {num2} is', greater(num1, num2))" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "id": "03251658", 326 | "metadata": {}, 327 | "source": [ 328 | "### Scope and Lifetime of a Variable in Functions\n", 329 | "\n", 330 | "- Scope of a variable is the portion of a program where the variable is recognized.\n", 331 | "\n", 332 | "- Variables defined inside a function is not visible from outside. Hence, they have a local scope.\n", 333 | "\n", 334 | "- Lifetime of a variable is the period throughout which the variable exits in the memory.\n", 335 | "\n", 336 | "- The lifetime of variables inside a function is as long as the function executes.\n", 337 | "\n", 338 | "- Variables created in the functions are destroyed once we return from the function." 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": null, 344 | "id": "b65d76cb", 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "def greater(num1, num2):\n", 349 | " if num1 > num2:\n", 350 | " result = num1\n", 351 | " else:\n", 352 | " result = num2\n", 353 | " \n", 354 | " return result" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "id": "18e85ba2", 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [ 364 | "greater(999.45, 334.32)" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "id": "34401e86", 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [ 374 | "print(result) #throws error because the result variable has been destroyed once it was returned" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": null, 380 | "id": "292097ae", 381 | "metadata": {}, 382 | "outputs": [], 383 | "source": [] 384 | } 385 | ], 386 | "metadata": { 387 | "kernelspec": { 388 | "display_name": "Python 3", 389 | "language": "python", 390 | "name": "python3" 391 | }, 392 | "language_info": { 393 | "codemirror_mode": { 394 | "name": "ipython", 395 | "version": 3 396 | }, 397 | "file_extension": ".py", 398 | "mimetype": "text/x-python", 399 | "name": "python", 400 | "nbconvert_exporter": "python", 401 | "pygments_lexer": "ipython3", 402 | "version": "3.8.8" 403 | } 404 | }, 405 | "nbformat": 4, 406 | "nbformat_minor": 5 407 | } 408 | -------------------------------------------------------------------------------- /Introduction to Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# What is Python?\n", 8 | "- Python is an interpreted, high-level, multipurpose programming language. Python is one of the most popular languages. It is being regarded as the language of Data Science these days." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "### So what makes Python popular? :\n", 16 | "\n", 17 | "- **General-purpose language:** It is a general-purpose language and is lightweight and fast. Therefore, it can be used for many different purposes.\n", 18 | "\n", 19 | "- **Support for Data Science:** Python was not built for Data Science but its general-purpose nature and the support of the community in providing third-party tools like Numpy, Pandas, matplotlib, and Scipy has made it the most popular in data science.\n", 20 | "\n", 21 | "- **Flat learning curve:** Python has a very simple and easy to learn syntax and programming requirements which make it a popular choice for people who are not programmers. It is known for its usability.\n", 22 | "\n", 23 | "- **Large active community:** Python has a very big active community of users. If someone faces an issue while working, there is a high probability that there will be solutions for it easily available on the Internet." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "# Literals and Variables\n", 31 | "\n", 32 | "### Literals\n", 33 | "A literal is nothing but a data types which conveys information. there are many kinds of literals in python where the four main datatypes are:\n", 34 | "- integral values: `int`\n", 35 | "- decimal values: `float`\n", 36 | "- true or false statements: `bool`\n", 37 | "- text data: `str` \n", 38 | "Apart from these, there are many other datas type which we'll we looking at later" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "10\n", 48 | "\n", 49 | "20.5\n", 50 | "\n", 51 | "'Python'\n", 52 | "\n", 53 | "True/False" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Variable\n", 61 | "Variables are nothing but place holders to store the literal values. Variables can be accessed by their names and some rules have to be followed while naming variables\n", 62 | "\n", 63 | "Naming conventions for variables:\n", 64 | "- A variable name can only contain alphabets, numbers and under score\n", 65 | "- A variable name can only start with an underscore or an alphabet\n", 66 | "- A variable name can't be the same as any keyword(keywords are sequences of characters which have some special meaning to them)\n", 67 | "- A variable name can't have any spaces between the various parts of the variable. If we want to distinguish them, we can use and underscore or make the variable name CamelCase\n", 68 | "> variable_name\n", 69 | "> VariableName\n", 70 | "- It is suggested that we make our variable names as descriptive as possible\n", 71 | "> a = 21\n", 72 | "> age = 21\n", 73 | "\n", 74 | "Storing a literal into a variable is called as assigning the variable with a literal and is accomplished using the equality(`=`) symbol" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "num = 10" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "name = 'Shikhar'" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "num1 = 10" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 2, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "num_2 = 20" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "_num3 = 30" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 4, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "ename": "SyntaxError", 129 | "evalue": "invalid syntax (, line 1)", 130 | "output_type": "error", 131 | "traceback": [ 132 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 4num = 40\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "4num = 40" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 6, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "ename": "SyntaxError", 147 | "evalue": "invalid syntax (, line 1)", 148 | "output_type": "error", 149 | "traceback": [ 150 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m for = 50\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "for = 50" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 9, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "first_name = 'Shikhar'" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "firstName = 'Virat'" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "FirstName = 'Rohit'" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "### Some of the rules in python\n", 190 | "**Indentation matters in Python.** Note that the statements are entered from the first column in the new line. The Python interpreter will report an error if the program is typed as follows:\n", 191 | "\n", 192 | "\tprint(\"Welcome to Python\")\n", 193 | "print(\"Python is fun\")\n", 194 | "\n", 195 | "**Don’t put any punctuation at the end of a statement**. For example, the Python interpreter will report errors for the following code:\n", 196 | "\n", 197 | "print(\"Welcome to Python\").\n", 198 | "print(\"Python is fun\"),\n", 199 | "\n", 200 | "**Python programs are case sensitive.** It would be wrong, for example, to replace print in the program with Print." 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "Print('Hello')" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "# Writing comments in python\n", 217 | "Comment is not a python statement and thus is ignored by python interpreter. But, it is plain text that documents what the program is and how it is constructed.\n", 218 | "\n", 219 | "It is always good to write a comment that describes purpose of each statement included in a program.\n", 220 | "\n", 221 | "It increases readability of the program.\n", 222 | "\n", 223 | "In Python, one can write comments as:\n", 224 | "1. Single line comment starting with a pound (#) sign\n", 225 | "2. Paragraph comment that starts and ends with three consecutive single quotes (''') or three consecutive double quotes (\"\"\")." 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 12, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [ 234 | "#Declared a variable first_name\n", 235 | "#\n", 236 | "#\n", 237 | "first_name = 'Shikhar'" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "# Python Keywords\n", 250 | "**Keywords are the pre-defined or built-in words whose meaning is already known to python interpreter.**\n", 251 | "\n", 252 | "Keywords are the reserved words in python\n", 253 | "\n", 254 | "We can't use a keyword as variable name, function name or any other identifier\n", 255 | "\n", 256 | "All the keywords except True, False and None are in lowercase and they must be written as is. " 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 22, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n", 269 | "Total number of keywords 35\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "import keyword\n", 275 | "\n", 276 | "print(keyword.kwlist)\n", 277 | "\n", 278 | "#print(keyword.kwlist)\n", 279 | "print(\"Total number of keywords \", len(keyword.kwlist))" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "# Identifiers\n", 287 | "**Identifiers are the user-defined names given to various program elements such as variable, function, class...etc.**\n", 288 | "\n", 289 | "**Rules for Writing Identifiers:**\n", 290 | "\n", 291 | "1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).\n", 292 | "\n", 293 | "2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.\n", 294 | "\n", 295 | "3. Keywords cannot be used as identifiers.\n", 296 | "\n", 297 | "4. No special symbol can be used\n", 298 | "\n", 299 | "5. An identifier can be of any length.\n", 300 | "\n", 301 | "**Note-1**: \n", 302 | "* Python is a case-sensitive language. This means, the identifiers amount, AMOUNT, and aMouNT are not the same. \n", 303 | "* Always name identifiers that make sense. For example, num is meaningful compared to x\n", 304 | "* Multiple words can be separated using an underscore. Ex.,compound_interest\n", 305 | "* We can also use camel-case style of writing, i.e., capitalize every first letter of the word except the initial word without any spaces. For example: camelCaseExample" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 13, 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "num1 = 10" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 14, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [ 323 | "Num1 = 20" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 17, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "ename": "NameError", 333 | "evalue": "name 'NUM1' is not defined", 334 | "output_type": "error", 335 | "traceback": [ 336 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 337 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 338 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mNUM1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 339 | "\u001b[1;31mNameError\u001b[0m: name 'NUM1' is not defined" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "NUM1" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "### Note: 2.\n", 352 | "Python is dynamically typed language. i.e., a variable can hold different types of literals at different times. This is not possible with static-typed languages like C, C++ and Java." 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 22, 358 | "metadata": {}, 359 | "outputs": [], 360 | "source": [ 361 | "num = 10\n", 362 | "num = 20\n", 363 | "num = 'Python'" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 23, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "'Python'" 375 | ] 376 | }, 377 | "execution_count": 23, 378 | "metadata": {}, 379 | "output_type": "execute_result" 380 | } 381 | ], 382 | "source": [ 383 | "num" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "### Note: 3.\n", 391 | "In Python, multiple variables are assigned with corresponding values within single assignment statement." 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": null, 397 | "metadata": {}, 398 | "outputs": [], 399 | "source": [ 400 | "num1 = 10\n", 401 | "num2 = 20\n", 402 | "num3 = 30" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": null, 408 | "metadata": {}, 409 | "outputs": [], 410 | "source": [ 411 | "#The code in the above can also be written like this\n", 412 | "num1, num2, num3 = 10, 20, 30" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "val1 = 40\n", 422 | "val2 = 40\n", 423 | "val3 = 40" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 37, 429 | "metadata": {}, 430 | "outputs": [], 431 | "source": [ 432 | "#The code in the above cell can also be written like this\n", 433 | "val1 = val2 = val3 = 40" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 38, 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "data": { 443 | "text/plain": [ 444 | "140706048584720" 445 | ] 446 | }, 447 | "execution_count": 38, 448 | "metadata": {}, 449 | "output_type": "execute_result" 450 | } 451 | ], 452 | "source": [ 453 | "id(val1)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 39, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "140706048584720" 465 | ] 466 | }, 467 | "execution_count": 39, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "id(val2)" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 40, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "data": { 483 | "text/plain": [ 484 | "140706048584720" 485 | ] 486 | }, 487 | "execution_count": 40, 488 | "metadata": {}, 489 | "output_type": "execute_result" 490 | } 491 | ], 492 | "source": [ 493 | "id(val3)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "### Note: 4. Type Conversion\n", 501 | "You can convert one variable type to another type using type(value)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 31, 507 | "metadata": {}, 508 | "outputs": [ 509 | { 510 | "data": { 511 | "text/plain": [ 512 | "10" 513 | ] 514 | }, 515 | "execution_count": 31, 516 | "metadata": {}, 517 | "output_type": "execute_result" 518 | } 519 | ], 520 | "source": [ 521 | "num1" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 32, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "data": { 531 | "text/plain": [ 532 | "int" 533 | ] 534 | }, 535 | "execution_count": 32, 536 | "metadata": {}, 537 | "output_type": "execute_result" 538 | } 539 | ], 540 | "source": [ 541 | "type(num1)" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 35, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [ 550 | "num1 = float(num1)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 36, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "10.0" 562 | ] 563 | }, 564 | "execution_count": 36, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "num1" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 27, 583 | "metadata": {}, 584 | "outputs": [], 585 | "source": [ 586 | "val1 = 30.3" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 28, 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "data": { 596 | "text/plain": [ 597 | "30" 598 | ] 599 | }, 600 | "execution_count": 28, 601 | "metadata": {}, 602 | "output_type": "execute_result" 603 | } 604 | ], 605 | "source": [ 606 | "int(val1)" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 29, 612 | "metadata": {}, 613 | "outputs": [ 614 | { 615 | "data": { 616 | "text/plain": [ 617 | "'10'" 618 | ] 619 | }, 620 | "execution_count": 29, 621 | "metadata": {}, 622 | "output_type": "execute_result" 623 | } 624 | ], 625 | "source": [ 626 | "str(num1)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 30, 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "ename": "ValueError", 636 | "evalue": "invalid literal for int() with base 10: 'Python'", 637 | "output_type": "error", 638 | "traceback": [ 639 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 640 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 641 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Python'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 642 | "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Python'" 643 | ] 644 | } 645 | ], 646 | "source": [ 647 | "int('Python')" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": null, 653 | "metadata": {}, 654 | "outputs": [], 655 | "source": [] 656 | } 657 | ], 658 | "metadata": { 659 | "kernelspec": { 660 | "display_name": "Python 3 (ipykernel)", 661 | "language": "python", 662 | "name": "python3" 663 | }, 664 | "language_info": { 665 | "codemirror_mode": { 666 | "name": "ipython", 667 | "version": 3 668 | }, 669 | "file_extension": ".py", 670 | "mimetype": "text/x-python", 671 | "name": "python", 672 | "nbconvert_exporter": "python", 673 | "pygments_lexer": "ipython3", 674 | "version": "3.11.7" 675 | } 676 | }, 677 | "nbformat": 4, 678 | "nbformat_minor": 4 679 | } 680 | -------------------------------------------------------------------------------- /Lists, Tuples and Strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4c13fd58", 6 | "metadata": {}, 7 | "source": [ 8 | "## Lists\n", 9 | "\n", 10 | "- It is a collection of heterogenous data.\n", 11 | "- Mutable data." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "id": "4fb6e095", 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "list1 = [10,20,30,40,50,60,70,60.7, 90.1, 'Python', [11,12,13,14]]" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "id": "b756cdd0", 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "[10, 20, 30, 40, 50, 60, 70, 60.7, 90.1, 'Python', [11, 12, 13, 14]]" 34 | ] 35 | }, 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "list1" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "id": "e094cb52", 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "list2 = [10,20,30,40,50,60,70]" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "id": "ce8a55d7", 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "list3 = ['Python', 'Maths', 'Stats', 'Machine', 'Learning']" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "0b2fd3d8", 68 | "metadata": {}, 69 | "source": [ 70 | "### Selection of the data using `indexing and slicing`" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "id": "7e2e8784", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "[10, 20, 30, 40, 50, 60, 70, 60.7, 90.1, 'Python', [11, 12, 13, 14]]" 83 | ] 84 | }, 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "list1" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "id": "f542d309", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "[10, 20, 30, 40, 50, 60, 70]" 104 | ] 105 | }, 106 | "execution_count": 6, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "list2" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 7, 118 | "id": "45b7f5cc", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "40" 125 | ] 126 | }, 127 | "execution_count": 7, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "list2[3]" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 8, 139 | "id": "8e21460b", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "10" 146 | ] 147 | }, 148 | "execution_count": 8, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "list2[0]" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 9, 160 | "id": "3804f685", 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "[30, 40, 50]" 167 | ] 168 | }, 169 | "execution_count": 9, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "list2[2:5]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 10, 181 | "id": "60545a71", 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "[50, 60, 70]" 188 | ] 189 | }, 190 | "execution_count": 10, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "list2[4:7]" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 11, 202 | "id": "742b813d", 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "[10, 30, 50, 70]" 209 | ] 210 | }, 211 | "execution_count": 11, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "list2[0:7:2]" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 12, 223 | "id": "20401cbf", 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "[10, 20, 60, 70]" 230 | ] 231 | }, 232 | "execution_count": 12, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "list2[0:2] + list2[5:7]" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 13, 244 | "id": "c53f87da", 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "[10, 20, 30, 40, 50, 60, 70]" 251 | ] 252 | }, 253 | "execution_count": 13, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "list2" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 15, 265 | "id": "804a7994", 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "[20, 50, 60]" 272 | ] 273 | }, 274 | "execution_count": 15, 275 | "metadata": {}, 276 | "output_type": "execute_result" 277 | } 278 | ], 279 | "source": [ 280 | "list2[1:2] + list2[4:6]" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "id": "2c391ea3", 286 | "metadata": {}, 287 | "source": [ 288 | "### Mutable Properties of a list" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "id": "04856607", 294 | "metadata": {}, 295 | "source": [ 296 | "#### Replacing the values in a list" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 17, 302 | "id": "06a87efa", 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "data": { 307 | "text/plain": [ 308 | "[10, 20, 30, 40, 50, 60, 70]" 309 | ] 310 | }, 311 | "execution_count": 17, 312 | "metadata": {}, 313 | "output_type": "execute_result" 314 | } 315 | ], 316 | "source": [ 317 | "list2" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 19, 323 | "id": "2d718066", 324 | "metadata": {}, 325 | "outputs": [], 326 | "source": [ 327 | "list2[3] = 67" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 20, 333 | "id": "2f165170", 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "[10, 20, 30, 67, 50, 60, 70]" 340 | ] 341 | }, 342 | "execution_count": 20, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "list2" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 23, 354 | "id": "e4235fa6", 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "list2[0:3] = [11, 35, 44]" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 24, 364 | "id": "f6d64e03", 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "data": { 369 | "text/plain": [ 370 | "[11, 35, 44, 67, 50, 60, 70]" 371 | ] 372 | }, 373 | "execution_count": 24, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | } 377 | ], 378 | "source": [ 379 | "list2" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 25, 385 | "id": "d8075a41", 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "list2[0:3] = [100]" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 26, 395 | "id": "8940d87a", 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "data": { 400 | "text/plain": [ 401 | "[100, 67, 50, 60, 70]" 402 | ] 403 | }, 404 | "execution_count": 26, 405 | "metadata": {}, 406 | "output_type": "execute_result" 407 | } 408 | ], 409 | "source": [ 410 | "list2" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 27, 416 | "id": "730d5455", 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "data": { 421 | "text/plain": [ 422 | "[10, 20, 30, 40, 50, 60, 70, 60.7, 90.1, 'Python', [11, 12, 13, 14]]" 423 | ] 424 | }, 425 | "execution_count": 27, 426 | "metadata": {}, 427 | "output_type": "execute_result" 428 | } 429 | ], 430 | "source": [ 431 | "list1" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 28, 437 | "id": "4a6b9553", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "[11, 12, 13, 14]" 444 | ] 445 | }, 446 | "execution_count": 28, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "list1[10]" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 29, 458 | "id": "a33dcf2d", 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "8" 465 | ] 466 | }, 467 | "execution_count": 29, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "list1.index(90.1)" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 30, 479 | "id": "45ee24fd", 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "data": { 484 | "text/plain": [ 485 | "10" 486 | ] 487 | }, 488 | "execution_count": 30, 489 | "metadata": {}, 490 | "output_type": "execute_result" 491 | } 492 | ], 493 | "source": [ 494 | "list1.index([11, 12, 13, 14])" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 33, 500 | "id": "73353f0e", 501 | "metadata": {}, 502 | "outputs": [], 503 | "source": [ 504 | "list1[10][2] = 42" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 34, 510 | "id": "542ea868", 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "data": { 515 | "text/plain": [ 516 | "[10, 20, 30, 40, 50, 60, 70, 60.7, 90.1, 'Python', [11, 12, 42, 14]]" 517 | ] 518 | }, 519 | "execution_count": 34, 520 | "metadata": {}, 521 | "output_type": "execute_result" 522 | } 523 | ], 524 | "source": [ 525 | "list1" 526 | ] 527 | }, 528 | { 529 | "cell_type": "markdown", 530 | "id": "68e2b057", 531 | "metadata": {}, 532 | "source": [ 533 | "#### Inserting the values in the list" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": 35, 539 | "id": "6c9f5d25", 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "data": { 544 | "text/plain": [ 545 | "[100, 67, 50, 60, 70]" 546 | ] 547 | }, 548 | "execution_count": 35, 549 | "metadata": {}, 550 | "output_type": "execute_result" 551 | } 552 | ], 553 | "source": [ 554 | "list2" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 36, 560 | "id": "71c0d5f2", 561 | "metadata": {}, 562 | "outputs": [], 563 | "source": [ 564 | "#insert a single value in a list at the end\n", 565 | "list2.append(100)" 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": 37, 571 | "id": "4df8d74a", 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "data": { 576 | "text/plain": [ 577 | "[100, 67, 50, 60, 70, 100]" 578 | ] 579 | }, 580 | "execution_count": 37, 581 | "metadata": {}, 582 | "output_type": "execute_result" 583 | } 584 | ], 585 | "source": [ 586 | "list2" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 38, 592 | "id": "643207ca", 593 | "metadata": {}, 594 | "outputs": [], 595 | "source": [ 596 | "list2.append(5)" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": 39, 602 | "id": "b99e3652", 603 | "metadata": {}, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "[100, 67, 50, 60, 70, 100, 5]" 609 | ] 610 | }, 611 | "execution_count": 39, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ], 616 | "source": [ 617 | "list2" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 40, 623 | "id": "0cf9934b", 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "list2.append('Maths')" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 41, 633 | "id": "05233b94", 634 | "metadata": {}, 635 | "outputs": [ 636 | { 637 | "data": { 638 | "text/plain": [ 639 | "[100, 67, 50, 60, 70, 100, 5, 'Maths']" 640 | ] 641 | }, 642 | "execution_count": 41, 643 | "metadata": {}, 644 | "output_type": "execute_result" 645 | } 646 | ], 647 | "source": [ 648 | "list2" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": 42, 654 | "id": "a33335db", 655 | "metadata": {}, 656 | "outputs": [], 657 | "source": [ 658 | "#insert multiple values into the list\n", 659 | "list2.extend([66, 87, 92])" 660 | ] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "execution_count": 43, 665 | "id": "a08443ad", 666 | "metadata": {}, 667 | "outputs": [ 668 | { 669 | "data": { 670 | "text/plain": [ 671 | "[100, 67, 50, 60, 70, 100, 5, 'Maths', 66, 87, 92]" 672 | ] 673 | }, 674 | "execution_count": 43, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "list2" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 45, 686 | "id": "3db4ee08", 687 | "metadata": {}, 688 | "outputs": [], 689 | "source": [ 690 | "#append insert the given list as it is into the original list\n", 691 | "list2.append([90, 33])" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": 46, 697 | "id": "8faa46a6", 698 | "metadata": {}, 699 | "outputs": [ 700 | { 701 | "data": { 702 | "text/plain": [ 703 | "[100, 67, 50, 60, 70, 100, 5, 'Maths', 66, 87, 92, [90, 33]]" 704 | ] 705 | }, 706 | "execution_count": 46, 707 | "metadata": {}, 708 | "output_type": "execute_result" 709 | } 710 | ], 711 | "source": [ 712 | "list2" 713 | ] 714 | }, 715 | { 716 | "cell_type": "code", 717 | "execution_count": 47, 718 | "id": "ecd88a66", 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [ 722 | "#insert an item at a particular item\n", 723 | "list2.insert(2, 200)" 724 | ] 725 | }, 726 | { 727 | "cell_type": "markdown", 728 | "id": "f0038213", 729 | "metadata": {}, 730 | "source": [ 731 | "#### Removing values from the list" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 49, 737 | "id": "b1961413", 738 | "metadata": {}, 739 | "outputs": [ 740 | { 741 | "data": { 742 | "text/plain": [ 743 | "[100, 67, 200, 50, 60, 70, 100, 5, 'Maths', 66, 87, 92, [90, 33]]" 744 | ] 745 | }, 746 | "execution_count": 49, 747 | "metadata": {}, 748 | "output_type": "execute_result" 749 | } 750 | ], 751 | "source": [ 752 | "list2" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": 50, 758 | "id": "92873125", 759 | "metadata": {}, 760 | "outputs": [ 761 | { 762 | "data": { 763 | "text/plain": [ 764 | "100" 765 | ] 766 | }, 767 | "execution_count": 50, 768 | "metadata": {}, 769 | "output_type": "execute_result" 770 | } 771 | ], 772 | "source": [ 773 | "list2.pop(0)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": 51, 779 | "id": "defce492", 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "data": { 784 | "text/plain": [ 785 | "[67, 200, 50, 60, 70, 100, 5, 'Maths', 66, 87, 92, [90, 33]]" 786 | ] 787 | }, 788 | "execution_count": 51, 789 | "metadata": {}, 790 | "output_type": "execute_result" 791 | } 792 | ], 793 | "source": [ 794 | "list2" 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": 53, 800 | "id": "9246ad17", 801 | "metadata": {}, 802 | "outputs": [ 803 | { 804 | "data": { 805 | "text/plain": [ 806 | "100" 807 | ] 808 | }, 809 | "execution_count": 53, 810 | "metadata": {}, 811 | "output_type": "execute_result" 812 | } 813 | ], 814 | "source": [ 815 | "list2.pop(5)" 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": 57, 821 | "id": "9e76b287", 822 | "metadata": {}, 823 | "outputs": [ 824 | { 825 | "data": { 826 | "text/plain": [ 827 | "[90, 33]" 828 | ] 829 | }, 830 | "execution_count": 57, 831 | "metadata": {}, 832 | "output_type": "execute_result" 833 | } 834 | ], 835 | "source": [ 836 | "list2.pop(list2.index([90,33]))" 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": 58, 842 | "id": "5b047731", 843 | "metadata": {}, 844 | "outputs": [ 845 | { 846 | "data": { 847 | "text/plain": [ 848 | "[67, 200, 50, 60, 70, 5, 66, 87, 92]" 849 | ] 850 | }, 851 | "execution_count": 58, 852 | "metadata": {}, 853 | "output_type": "execute_result" 854 | } 855 | ], 856 | "source": [ 857 | "list2" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": 55, 863 | "id": "95f3f0c4", 864 | "metadata": {}, 865 | "outputs": [], 866 | "source": [ 867 | "list2.remove('Maths')" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 59, 873 | "id": "4f561310", 874 | "metadata": {}, 875 | "outputs": [ 876 | { 877 | "data": { 878 | "text/plain": [ 879 | "[67, 200, 50, 60, 70, 5, 66, 87, 92]" 880 | ] 881 | }, 882 | "execution_count": 59, 883 | "metadata": {}, 884 | "output_type": "execute_result" 885 | } 886 | ], 887 | "source": [ 888 | "list2" 889 | ] 890 | }, 891 | { 892 | "cell_type": "code", 893 | "execution_count": 61, 894 | "id": "e3392bfa", 895 | "metadata": {}, 896 | "outputs": [], 897 | "source": [ 898 | "del list2[0:2]" 899 | ] 900 | }, 901 | { 902 | "cell_type": "code", 903 | "execution_count": 62, 904 | "id": "6fc761e8", 905 | "metadata": {}, 906 | "outputs": [ 907 | { 908 | "data": { 909 | "text/plain": [ 910 | "[50, 60, 70, 5, 66, 87, 92]" 911 | ] 912 | }, 913 | "execution_count": 62, 914 | "metadata": {}, 915 | "output_type": "execute_result" 916 | } 917 | ], 918 | "source": [ 919 | "list2" 920 | ] 921 | }, 922 | { 923 | "cell_type": "markdown", 924 | "id": "087c62ae", 925 | "metadata": {}, 926 | "source": [ 927 | "### Some Methods on the list" 928 | ] 929 | }, 930 | { 931 | "cell_type": "code", 932 | "execution_count": 63, 933 | "id": "619bec6f", 934 | "metadata": {}, 935 | "outputs": [ 936 | { 937 | "data": { 938 | "text/plain": [ 939 | "[50, 60, 70, 5, 66, 87, 92]" 940 | ] 941 | }, 942 | "execution_count": 63, 943 | "metadata": {}, 944 | "output_type": "execute_result" 945 | } 946 | ], 947 | "source": [ 948 | "list2" 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": 64, 954 | "id": "8828c424", 955 | "metadata": {}, 956 | "outputs": [], 957 | "source": [ 958 | "list3 = list2.copy()" 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "execution_count": 65, 964 | "id": "8923e20f", 965 | "metadata": {}, 966 | "outputs": [ 967 | { 968 | "data": { 969 | "text/plain": [ 970 | "[50, 60, 70, 5, 66, 87, 92]" 971 | ] 972 | }, 973 | "execution_count": 65, 974 | "metadata": {}, 975 | "output_type": "execute_result" 976 | } 977 | ], 978 | "source": [ 979 | "list3" 980 | ] 981 | }, 982 | { 983 | "cell_type": "code", 984 | "execution_count": 66, 985 | "id": "617a1e04", 986 | "metadata": {}, 987 | "outputs": [ 988 | { 989 | "data": { 990 | "text/plain": [ 991 | "3127613397376" 992 | ] 993 | }, 994 | "execution_count": 66, 995 | "metadata": {}, 996 | "output_type": "execute_result" 997 | } 998 | ], 999 | "source": [ 1000 | "id(list3)" 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "code", 1005 | "execution_count": 67, 1006 | "id": "c59bca24", 1007 | "metadata": {}, 1008 | "outputs": [ 1009 | { 1010 | "data": { 1011 | "text/plain": [ 1012 | "3127612254208" 1013 | ] 1014 | }, 1015 | "execution_count": 67, 1016 | "metadata": {}, 1017 | "output_type": "execute_result" 1018 | } 1019 | ], 1020 | "source": [ 1021 | "id(list2)" 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "execution_count": 68, 1027 | "id": "c0104bf7", 1028 | "metadata": {}, 1029 | "outputs": [], 1030 | "source": [ 1031 | "list4 = list2" 1032 | ] 1033 | }, 1034 | { 1035 | "cell_type": "code", 1036 | "execution_count": 70, 1037 | "id": "1c921c33", 1038 | "metadata": {}, 1039 | "outputs": [ 1040 | { 1041 | "data": { 1042 | "text/plain": [ 1043 | "3127612254208" 1044 | ] 1045 | }, 1046 | "execution_count": 70, 1047 | "metadata": {}, 1048 | "output_type": "execute_result" 1049 | } 1050 | ], 1051 | "source": [ 1052 | "id(list4)" 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "code", 1057 | "execution_count": 71, 1058 | "id": "27cdea7f", 1059 | "metadata": {}, 1060 | "outputs": [ 1061 | { 1062 | "data": { 1063 | "text/plain": [ 1064 | "3127612254208" 1065 | ] 1066 | }, 1067 | "execution_count": 71, 1068 | "metadata": {}, 1069 | "output_type": "execute_result" 1070 | } 1071 | ], 1072 | "source": [ 1073 | "id(list2)" 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": 72, 1079 | "id": "a3775680", 1080 | "metadata": {}, 1081 | "outputs": [], 1082 | "source": [ 1083 | "list2.sort() #sort the list by default in ascending order" 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 73, 1089 | "id": "32dbba75", 1090 | "metadata": {}, 1091 | "outputs": [ 1092 | { 1093 | "data": { 1094 | "text/plain": [ 1095 | "[5, 50, 60, 66, 70, 87, 92]" 1096 | ] 1097 | }, 1098 | "execution_count": 73, 1099 | "metadata": {}, 1100 | "output_type": "execute_result" 1101 | } 1102 | ], 1103 | "source": [ 1104 | "list2" 1105 | ] 1106 | }, 1107 | { 1108 | "cell_type": "code", 1109 | "execution_count": 74, 1110 | "id": "f958d69a", 1111 | "metadata": {}, 1112 | "outputs": [], 1113 | "source": [ 1114 | "list2.sort(reverse = True)" 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "code", 1119 | "execution_count": 75, 1120 | "id": "47563709", 1121 | "metadata": {}, 1122 | "outputs": [ 1123 | { 1124 | "data": { 1125 | "text/plain": [ 1126 | "[92, 87, 70, 66, 60, 50, 5]" 1127 | ] 1128 | }, 1129 | "execution_count": 75, 1130 | "metadata": {}, 1131 | "output_type": "execute_result" 1132 | } 1133 | ], 1134 | "source": [ 1135 | "list2" 1136 | ] 1137 | }, 1138 | { 1139 | "cell_type": "code", 1140 | "execution_count": 76, 1141 | "id": "7d9e6ca8", 1142 | "metadata": {}, 1143 | "outputs": [ 1144 | { 1145 | "data": { 1146 | "text/plain": [ 1147 | "[50, 60, 70, 5, 66, 87, 92]" 1148 | ] 1149 | }, 1150 | "execution_count": 76, 1151 | "metadata": {}, 1152 | "output_type": "execute_result" 1153 | } 1154 | ], 1155 | "source": [ 1156 | "list3" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": 78, 1162 | "id": "e406908a", 1163 | "metadata": {}, 1164 | "outputs": [], 1165 | "source": [ 1166 | "sorted_list3 = sorted(list3)" 1167 | ] 1168 | }, 1169 | { 1170 | "cell_type": "code", 1171 | "execution_count": 79, 1172 | "id": "2b91b4f5", 1173 | "metadata": {}, 1174 | "outputs": [ 1175 | { 1176 | "data": { 1177 | "text/plain": [ 1178 | "[5, 50, 60, 66, 70, 87, 92]" 1179 | ] 1180 | }, 1181 | "execution_count": 79, 1182 | "metadata": {}, 1183 | "output_type": "execute_result" 1184 | } 1185 | ], 1186 | "source": [ 1187 | "sorted_list3" 1188 | ] 1189 | }, 1190 | { 1191 | "cell_type": "code", 1192 | "execution_count": 80, 1193 | "id": "feabac24", 1194 | "metadata": {}, 1195 | "outputs": [ 1196 | { 1197 | "data": { 1198 | "text/plain": [ 1199 | "[50, 60, 70, 5, 66, 87, 92]" 1200 | ] 1201 | }, 1202 | "execution_count": 80, 1203 | "metadata": {}, 1204 | "output_type": "execute_result" 1205 | } 1206 | ], 1207 | "source": [ 1208 | "list3" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "code", 1213 | "execution_count": 81, 1214 | "id": "bd8f9014", 1215 | "metadata": {}, 1216 | "outputs": [ 1217 | { 1218 | "data": { 1219 | "text/plain": [ 1220 | "[92, 87, 70, 66, 60, 50, 5]" 1221 | ] 1222 | }, 1223 | "execution_count": 81, 1224 | "metadata": {}, 1225 | "output_type": "execute_result" 1226 | } 1227 | ], 1228 | "source": [ 1229 | "sorted(list3, reverse = True)" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "execution_count": 82, 1235 | "id": "679e3e72", 1236 | "metadata": {}, 1237 | "outputs": [ 1238 | { 1239 | "data": { 1240 | "text/plain": [ 1241 | "[92, 87, 70, 66, 60, 50, 5]" 1242 | ] 1243 | }, 1244 | "execution_count": 82, 1245 | "metadata": {}, 1246 | "output_type": "execute_result" 1247 | } 1248 | ], 1249 | "source": [ 1250 | "list2" 1251 | ] 1252 | }, 1253 | { 1254 | "cell_type": "code", 1255 | "execution_count": 83, 1256 | "id": "43416161", 1257 | "metadata": {}, 1258 | "outputs": [], 1259 | "source": [ 1260 | "list2.reverse()" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": 84, 1266 | "id": "507ab437", 1267 | "metadata": {}, 1268 | "outputs": [ 1269 | { 1270 | "data": { 1271 | "text/plain": [ 1272 | "[5, 50, 60, 66, 70, 87, 92]" 1273 | ] 1274 | }, 1275 | "execution_count": 84, 1276 | "metadata": {}, 1277 | "output_type": "execute_result" 1278 | } 1279 | ], 1280 | "source": [ 1281 | "list2" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "code", 1286 | "execution_count": 85, 1287 | "id": "3c51a450", 1288 | "metadata": {}, 1289 | "outputs": [ 1290 | { 1291 | "data": { 1292 | "text/plain": [ 1293 | "[50, 60, 70, 5, 66, 87, 92]" 1294 | ] 1295 | }, 1296 | "execution_count": 85, 1297 | "metadata": {}, 1298 | "output_type": "execute_result" 1299 | } 1300 | ], 1301 | "source": [ 1302 | "list3" 1303 | ] 1304 | }, 1305 | { 1306 | "cell_type": "code", 1307 | "execution_count": 87, 1308 | "id": "d014e0cc", 1309 | "metadata": {}, 1310 | "outputs": [ 1311 | { 1312 | "data": { 1313 | "text/plain": [ 1314 | "[92, 87, 66, 5, 70, 60, 50]" 1315 | ] 1316 | }, 1317 | "execution_count": 87, 1318 | "metadata": {}, 1319 | "output_type": "execute_result" 1320 | } 1321 | ], 1322 | "source": [ 1323 | "list(reversed(list3))" 1324 | ] 1325 | }, 1326 | { 1327 | "cell_type": "code", 1328 | "execution_count": 88, 1329 | "id": "24371cb0", 1330 | "metadata": {}, 1331 | "outputs": [ 1332 | { 1333 | "data": { 1334 | "text/plain": [ 1335 | "[50, 60, 70, 5, 66, 87, 92]" 1336 | ] 1337 | }, 1338 | "execution_count": 88, 1339 | "metadata": {}, 1340 | "output_type": "execute_result" 1341 | } 1342 | ], 1343 | "source": [ 1344 | "list3" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "code", 1349 | "execution_count": 89, 1350 | "id": "8144fb1c", 1351 | "metadata": {}, 1352 | "outputs": [ 1353 | { 1354 | "data": { 1355 | "text/plain": [ 1356 | "1" 1357 | ] 1358 | }, 1359 | "execution_count": 89, 1360 | "metadata": {}, 1361 | "output_type": "execute_result" 1362 | } 1363 | ], 1364 | "source": [ 1365 | "list3.count(60)" 1366 | ] 1367 | }, 1368 | { 1369 | "cell_type": "code", 1370 | "execution_count": 90, 1371 | "id": "b9ab0d3d", 1372 | "metadata": {}, 1373 | "outputs": [ 1374 | { 1375 | "data": { 1376 | "text/plain": [ 1377 | "7" 1378 | ] 1379 | }, 1380 | "execution_count": 90, 1381 | "metadata": {}, 1382 | "output_type": "execute_result" 1383 | } 1384 | ], 1385 | "source": [ 1386 | "len(list3)" 1387 | ] 1388 | }, 1389 | { 1390 | "cell_type": "code", 1391 | "execution_count": 91, 1392 | "id": "d78a9078", 1393 | "metadata": {}, 1394 | "outputs": [ 1395 | { 1396 | "data": { 1397 | "text/plain": [ 1398 | "5" 1399 | ] 1400 | }, 1401 | "execution_count": 91, 1402 | "metadata": {}, 1403 | "output_type": "execute_result" 1404 | } 1405 | ], 1406 | "source": [ 1407 | "min(list3)" 1408 | ] 1409 | }, 1410 | { 1411 | "cell_type": "code", 1412 | "execution_count": 92, 1413 | "id": "451d07b1", 1414 | "metadata": {}, 1415 | "outputs": [ 1416 | { 1417 | "data": { 1418 | "text/plain": [ 1419 | "92" 1420 | ] 1421 | }, 1422 | "execution_count": 92, 1423 | "metadata": {}, 1424 | "output_type": "execute_result" 1425 | } 1426 | ], 1427 | "source": [ 1428 | "max(list3)" 1429 | ] 1430 | }, 1431 | { 1432 | "cell_type": "code", 1433 | "execution_count": 93, 1434 | "id": "3afc66db", 1435 | "metadata": {}, 1436 | "outputs": [ 1437 | { 1438 | "data": { 1439 | "text/plain": [ 1440 | "430" 1441 | ] 1442 | }, 1443 | "execution_count": 93, 1444 | "metadata": {}, 1445 | "output_type": "execute_result" 1446 | } 1447 | ], 1448 | "source": [ 1449 | "sum(list3)" 1450 | ] 1451 | }, 1452 | { 1453 | "cell_type": "code", 1454 | "execution_count": null, 1455 | "id": "56e43d48", 1456 | "metadata": {}, 1457 | "outputs": [], 1458 | "source": [] 1459 | }, 1460 | { 1461 | "cell_type": "markdown", 1462 | "id": "560de1a7", 1463 | "metadata": {}, 1464 | "source": [ 1465 | "## Tuples\n", 1466 | "\n", 1467 | "- It is a collection of mix data type.\n", 1468 | "- It is immutable." 1469 | ] 1470 | }, 1471 | { 1472 | "cell_type": "code", 1473 | "execution_count": 95, 1474 | "id": "0a89b85d", 1475 | "metadata": {}, 1476 | "outputs": [], 1477 | "source": [ 1478 | "tuple1 = (10,20,30,40,50,70.6, 44.3)" 1479 | ] 1480 | }, 1481 | { 1482 | "cell_type": "code", 1483 | "execution_count": 96, 1484 | "id": "b076eae4", 1485 | "metadata": {}, 1486 | "outputs": [ 1487 | { 1488 | "data": { 1489 | "text/plain": [ 1490 | "40" 1491 | ] 1492 | }, 1493 | "execution_count": 96, 1494 | "metadata": {}, 1495 | "output_type": "execute_result" 1496 | } 1497 | ], 1498 | "source": [ 1499 | "tuple1[3]" 1500 | ] 1501 | }, 1502 | { 1503 | "cell_type": "code", 1504 | "execution_count": 97, 1505 | "id": "cb062725", 1506 | "metadata": {}, 1507 | "outputs": [ 1508 | { 1509 | "data": { 1510 | "text/plain": [ 1511 | "(30, 50, 44.3)" 1512 | ] 1513 | }, 1514 | "execution_count": 97, 1515 | "metadata": {}, 1516 | "output_type": "execute_result" 1517 | } 1518 | ], 1519 | "source": [ 1520 | "tuple1[2:7:2]" 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "code", 1525 | "execution_count": 98, 1526 | "id": "d0db01e4", 1527 | "metadata": {}, 1528 | "outputs": [ 1529 | { 1530 | "ename": "TypeError", 1531 | "evalue": "'tuple' object does not support item assignment", 1532 | "output_type": "error", 1533 | "traceback": [ 1534 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1535 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 1536 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtuple1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m900\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1537 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 1538 | ] 1539 | } 1540 | ], 1541 | "source": [ 1542 | "tuple1[5] = 900" 1543 | ] 1544 | }, 1545 | { 1546 | "cell_type": "markdown", 1547 | "id": "fc173c64-d6f1-4cbc-ae63-3113fdbada01", 1548 | "metadata": {}, 1549 | "source": [ 1550 | "#### To make changes in the tuples we follow the below process" 1551 | ] 1552 | }, 1553 | { 1554 | "cell_type": "code", 1555 | "execution_count": 99, 1556 | "id": "c848a14d", 1557 | "metadata": {}, 1558 | "outputs": [], 1559 | "source": [ 1560 | "user_pins = (1001, 9987, 9943, 2234, 6676, 5543)" 1561 | ] 1562 | }, 1563 | { 1564 | "cell_type": "code", 1565 | "execution_count": 100, 1566 | "id": "4f2c1ae5", 1567 | "metadata": {}, 1568 | "outputs": [], 1569 | "source": [ 1570 | "temp = list(user_pins)" 1571 | ] 1572 | }, 1573 | { 1574 | "cell_type": "code", 1575 | "execution_count": 101, 1576 | "id": "0c47bd09", 1577 | "metadata": {}, 1578 | "outputs": [ 1579 | { 1580 | "data": { 1581 | "text/plain": [ 1582 | "[1001, 9987, 9943, 2234, 6676, 5543]" 1583 | ] 1584 | }, 1585 | "execution_count": 101, 1586 | "metadata": {}, 1587 | "output_type": "execute_result" 1588 | } 1589 | ], 1590 | "source": [ 1591 | "temp" 1592 | ] 1593 | }, 1594 | { 1595 | "cell_type": "code", 1596 | "execution_count": 102, 1597 | "id": "fd62d029", 1598 | "metadata": {}, 1599 | "outputs": [], 1600 | "source": [ 1601 | "temp.append(1100)" 1602 | ] 1603 | }, 1604 | { 1605 | "cell_type": "code", 1606 | "execution_count": 104, 1607 | "id": "72227e41", 1608 | "metadata": {}, 1609 | "outputs": [], 1610 | "source": [ 1611 | "user_pins = tuple(temp)" 1612 | ] 1613 | }, 1614 | { 1615 | "cell_type": "code", 1616 | "execution_count": 105, 1617 | "id": "5d3d7527", 1618 | "metadata": {}, 1619 | "outputs": [ 1620 | { 1621 | "data": { 1622 | "text/plain": [ 1623 | "(1001, 9987, 9943, 2234, 6676, 5543, 1100)" 1624 | ] 1625 | }, 1626 | "execution_count": 105, 1627 | "metadata": {}, 1628 | "output_type": "execute_result" 1629 | } 1630 | ], 1631 | "source": [ 1632 | "user_pins" 1633 | ] 1634 | }, 1635 | { 1636 | "cell_type": "markdown", 1637 | "id": "7c680898", 1638 | "metadata": {}, 1639 | "source": [ 1640 | "### Some Methods in tuple" 1641 | ] 1642 | }, 1643 | { 1644 | "cell_type": "code", 1645 | "execution_count": 106, 1646 | "id": "7529a3f5", 1647 | "metadata": {}, 1648 | "outputs": [ 1649 | { 1650 | "data": { 1651 | "text/plain": [ 1652 | "(10, 20, 30, 40, 50, 70.6, 44.3)" 1653 | ] 1654 | }, 1655 | "execution_count": 106, 1656 | "metadata": {}, 1657 | "output_type": "execute_result" 1658 | } 1659 | ], 1660 | "source": [ 1661 | "tuple1" 1662 | ] 1663 | }, 1664 | { 1665 | "cell_type": "code", 1666 | "execution_count": 107, 1667 | "id": "609fbeb3", 1668 | "metadata": {}, 1669 | "outputs": [ 1670 | { 1671 | "ename": "AttributeError", 1672 | "evalue": "'tuple' object has no attribute 'sort'", 1673 | "output_type": "error", 1674 | "traceback": [ 1675 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1676 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 1677 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtuple1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1678 | "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'sort'" 1679 | ] 1680 | } 1681 | ], 1682 | "source": [ 1683 | "tuple1.sort()" 1684 | ] 1685 | }, 1686 | { 1687 | "cell_type": "code", 1688 | "execution_count": 109, 1689 | "id": "676e26af", 1690 | "metadata": {}, 1691 | "outputs": [ 1692 | { 1693 | "data": { 1694 | "text/plain": [ 1695 | "(10, 20, 30, 40, 44.3, 50, 70.6)" 1696 | ] 1697 | }, 1698 | "execution_count": 109, 1699 | "metadata": {}, 1700 | "output_type": "execute_result" 1701 | } 1702 | ], 1703 | "source": [ 1704 | "tuple(sorted(tuple1))" 1705 | ] 1706 | }, 1707 | { 1708 | "cell_type": "code", 1709 | "execution_count": 111, 1710 | "id": "e5ec70b4", 1711 | "metadata": {}, 1712 | "outputs": [ 1713 | { 1714 | "data": { 1715 | "text/plain": [ 1716 | "(70.6, 50, 44.3, 40, 30, 20, 10)" 1717 | ] 1718 | }, 1719 | "execution_count": 111, 1720 | "metadata": {}, 1721 | "output_type": "execute_result" 1722 | } 1723 | ], 1724 | "source": [ 1725 | "tuple(sorted(tuple1, reverse = True))" 1726 | ] 1727 | }, 1728 | { 1729 | "cell_type": "code", 1730 | "execution_count": 113, 1731 | "id": "2f95f86f", 1732 | "metadata": {}, 1733 | "outputs": [ 1734 | { 1735 | "data": { 1736 | "text/plain": [ 1737 | "0" 1738 | ] 1739 | }, 1740 | "execution_count": 113, 1741 | "metadata": {}, 1742 | "output_type": "execute_result" 1743 | } 1744 | ], 1745 | "source": [ 1746 | "tuple1.count(100)" 1747 | ] 1748 | }, 1749 | { 1750 | "cell_type": "code", 1751 | "execution_count": 114, 1752 | "id": "ffa1b452", 1753 | "metadata": {}, 1754 | "outputs": [ 1755 | { 1756 | "data": { 1757 | "text/plain": [ 1758 | "2" 1759 | ] 1760 | }, 1761 | "execution_count": 114, 1762 | "metadata": {}, 1763 | "output_type": "execute_result" 1764 | } 1765 | ], 1766 | "source": [ 1767 | "tuple1.index(30)" 1768 | ] 1769 | }, 1770 | { 1771 | "cell_type": "code", 1772 | "execution_count": 115, 1773 | "id": "9baf6cbb", 1774 | "metadata": {}, 1775 | "outputs": [ 1776 | { 1777 | "data": { 1778 | "text/plain": [ 1779 | "10" 1780 | ] 1781 | }, 1782 | "execution_count": 115, 1783 | "metadata": {}, 1784 | "output_type": "execute_result" 1785 | } 1786 | ], 1787 | "source": [ 1788 | "min(tuple1)" 1789 | ] 1790 | }, 1791 | { 1792 | "cell_type": "code", 1793 | "execution_count": 116, 1794 | "id": "560df9eb", 1795 | "metadata": {}, 1796 | "outputs": [ 1797 | { 1798 | "data": { 1799 | "text/plain": [ 1800 | "70.6" 1801 | ] 1802 | }, 1803 | "execution_count": 116, 1804 | "metadata": {}, 1805 | "output_type": "execute_result" 1806 | } 1807 | ], 1808 | "source": [ 1809 | "max(tuple1)" 1810 | ] 1811 | }, 1812 | { 1813 | "cell_type": "code", 1814 | "execution_count": 117, 1815 | "id": "8fe153da", 1816 | "metadata": {}, 1817 | "outputs": [ 1818 | { 1819 | "data": { 1820 | "text/plain": [ 1821 | "264.9" 1822 | ] 1823 | }, 1824 | "execution_count": 117, 1825 | "metadata": {}, 1826 | "output_type": "execute_result" 1827 | } 1828 | ], 1829 | "source": [ 1830 | "sum(tuple1)" 1831 | ] 1832 | }, 1833 | { 1834 | "cell_type": "code", 1835 | "execution_count": null, 1836 | "id": "28afbbf1", 1837 | "metadata": {}, 1838 | "outputs": [], 1839 | "source": [] 1840 | }, 1841 | { 1842 | "cell_type": "markdown", 1843 | "id": "d8171837", 1844 | "metadata": {}, 1845 | "source": [ 1846 | "## Strings" 1847 | ] 1848 | }, 1849 | { 1850 | "cell_type": "code", 1851 | "execution_count": 1, 1852 | "id": "cad6cb8b", 1853 | "metadata": {}, 1854 | "outputs": [], 1855 | "source": [ 1856 | "word1 = 'Python Programming'" 1857 | ] 1858 | }, 1859 | { 1860 | "cell_type": "code", 1861 | "execution_count": 2, 1862 | "id": "c5f6c694", 1863 | "metadata": {}, 1864 | "outputs": [], 1865 | "source": [ 1866 | "word2 = \"Python Programming\"" 1867 | ] 1868 | }, 1869 | { 1870 | "cell_type": "code", 1871 | "execution_count": 3, 1872 | "id": "b529383d", 1873 | "metadata": {}, 1874 | "outputs": [], 1875 | "source": [ 1876 | "word3 = 'Python is the most preferred language for Data Science and Machine Learning'" 1877 | ] 1878 | }, 1879 | { 1880 | "cell_type": "markdown", 1881 | "id": "aeb51946", 1882 | "metadata": {}, 1883 | "source": [ 1884 | "### Indexing and Slicing" 1885 | ] 1886 | }, 1887 | { 1888 | "cell_type": "code", 1889 | "execution_count": 4, 1890 | "id": "f4f48a20", 1891 | "metadata": {}, 1892 | "outputs": [ 1893 | { 1894 | "data": { 1895 | "text/plain": [ 1896 | "'Python is the most preferred language for Data Science and Machine Learning'" 1897 | ] 1898 | }, 1899 | "execution_count": 4, 1900 | "metadata": {}, 1901 | "output_type": "execute_result" 1902 | } 1903 | ], 1904 | "source": [ 1905 | "word3" 1906 | ] 1907 | }, 1908 | { 1909 | "cell_type": "code", 1910 | "execution_count": 5, 1911 | "id": "d4ded57c", 1912 | "metadata": {}, 1913 | "outputs": [ 1914 | { 1915 | "data": { 1916 | "text/plain": [ 1917 | "'y'" 1918 | ] 1919 | }, 1920 | "execution_count": 5, 1921 | "metadata": {}, 1922 | "output_type": "execute_result" 1923 | } 1924 | ], 1925 | "source": [ 1926 | "word3[1]" 1927 | ] 1928 | }, 1929 | { 1930 | "cell_type": "code", 1931 | "execution_count": 7, 1932 | "id": "c96a404c", 1933 | "metadata": {}, 1934 | "outputs": [ 1935 | { 1936 | "data": { 1937 | "text/plain": [ 1938 | "'i'" 1939 | ] 1940 | }, 1941 | "execution_count": 7, 1942 | "metadata": {}, 1943 | "output_type": "execute_result" 1944 | } 1945 | ], 1946 | "source": [ 1947 | "word3[-3]" 1948 | ] 1949 | }, 1950 | { 1951 | "cell_type": "code", 1952 | "execution_count": 9, 1953 | "id": "303c2e61", 1954 | "metadata": {}, 1955 | "outputs": [ 1956 | { 1957 | "data": { 1958 | "text/plain": [ 1959 | "'Python'" 1960 | ] 1961 | }, 1962 | "execution_count": 9, 1963 | "metadata": {}, 1964 | "output_type": "execute_result" 1965 | } 1966 | ], 1967 | "source": [ 1968 | "word3[0:6]" 1969 | ] 1970 | }, 1971 | { 1972 | "cell_type": "code", 1973 | "execution_count": 10, 1974 | "id": "e73e814b", 1975 | "metadata": {}, 1976 | "outputs": [ 1977 | { 1978 | "data": { 1979 | "text/plain": [ 1980 | "'Data Science'" 1981 | ] 1982 | }, 1983 | "execution_count": 10, 1984 | "metadata": {}, 1985 | "output_type": "execute_result" 1986 | } 1987 | ], 1988 | "source": [ 1989 | "word3[word3.index('Data') : word3.index('Data') + len('Data Science')]" 1990 | ] 1991 | }, 1992 | { 1993 | "cell_type": "markdown", 1994 | "id": "90fdf0a5", 1995 | "metadata": {}, 1996 | "source": [ 1997 | "### String Methods" 1998 | ] 1999 | }, 2000 | { 2001 | "cell_type": "code", 2002 | "execution_count": 11, 2003 | "id": "edbcb0e1", 2004 | "metadata": {}, 2005 | "outputs": [ 2006 | { 2007 | "data": { 2008 | "text/plain": [ 2009 | "42" 2010 | ] 2011 | }, 2012 | "execution_count": 11, 2013 | "metadata": {}, 2014 | "output_type": "execute_result" 2015 | } 2016 | ], 2017 | "source": [ 2018 | "word3.find('Data')" 2019 | ] 2020 | }, 2021 | { 2022 | "cell_type": "code", 2023 | "execution_count": 12, 2024 | "id": "d3184fb3", 2025 | "metadata": {}, 2026 | "outputs": [ 2027 | { 2028 | "data": { 2029 | "text/plain": [ 2030 | "-1" 2031 | ] 2032 | }, 2033 | "execution_count": 12, 2034 | "metadata": {}, 2035 | "output_type": "execute_result" 2036 | } 2037 | ], 2038 | "source": [ 2039 | "word3.find('Java')" 2040 | ] 2041 | }, 2042 | { 2043 | "cell_type": "code", 2044 | "execution_count": 13, 2045 | "id": "f8f05ef8", 2046 | "metadata": {}, 2047 | "outputs": [ 2048 | { 2049 | "data": { 2050 | "text/plain": [ 2051 | "0" 2052 | ] 2053 | }, 2054 | "execution_count": 13, 2055 | "metadata": {}, 2056 | "output_type": "execute_result" 2057 | } 2058 | ], 2059 | "source": [ 2060 | "word3.index('Python')" 2061 | ] 2062 | }, 2063 | { 2064 | "cell_type": "code", 2065 | "execution_count": 14, 2066 | "id": "010995c1", 2067 | "metadata": {}, 2068 | "outputs": [ 2069 | { 2070 | "data": { 2071 | "text/plain": [ 2072 | "'Java is the most preferred language for Data Science and Machine Learning'" 2073 | ] 2074 | }, 2075 | "execution_count": 14, 2076 | "metadata": {}, 2077 | "output_type": "execute_result" 2078 | } 2079 | ], 2080 | "source": [ 2081 | "word3.replace('Python', 'Java')" 2082 | ] 2083 | }, 2084 | { 2085 | "cell_type": "code", 2086 | "execution_count": 16, 2087 | "id": "06e7d4c3", 2088 | "metadata": {}, 2089 | "outputs": [ 2090 | { 2091 | "data": { 2092 | "text/plain": [ 2093 | "'PYTHON PROGRAMMING'" 2094 | ] 2095 | }, 2096 | "execution_count": 16, 2097 | "metadata": {}, 2098 | "output_type": "execute_result" 2099 | } 2100 | ], 2101 | "source": [ 2102 | "word1.upper()" 2103 | ] 2104 | }, 2105 | { 2106 | "cell_type": "code", 2107 | "execution_count": 17, 2108 | "id": "cf1e39cb", 2109 | "metadata": {}, 2110 | "outputs": [ 2111 | { 2112 | "data": { 2113 | "text/plain": [ 2114 | "'python programming'" 2115 | ] 2116 | }, 2117 | "execution_count": 17, 2118 | "metadata": {}, 2119 | "output_type": "execute_result" 2120 | } 2121 | ], 2122 | "source": [ 2123 | "word2.lower()" 2124 | ] 2125 | }, 2126 | { 2127 | "cell_type": "code", 2128 | "execution_count": 18, 2129 | "id": "0c2956d9", 2130 | "metadata": {}, 2131 | "outputs": [ 2132 | { 2133 | "data": { 2134 | "text/plain": [ 2135 | "'Python Programming'" 2136 | ] 2137 | }, 2138 | "execution_count": 18, 2139 | "metadata": {}, 2140 | "output_type": "execute_result" 2141 | } 2142 | ], 2143 | "source": [ 2144 | "word2.title()" 2145 | ] 2146 | }, 2147 | { 2148 | "cell_type": "code", 2149 | "execution_count": 20, 2150 | "id": "5707a6c5", 2151 | "metadata": {}, 2152 | "outputs": [ 2153 | { 2154 | "data": { 2155 | "text/plain": [ 2156 | "'Python is the most preferred language for data science and machine learning'" 2157 | ] 2158 | }, 2159 | "execution_count": 20, 2160 | "metadata": {}, 2161 | "output_type": "execute_result" 2162 | } 2163 | ], 2164 | "source": [ 2165 | "word3.capitalize()" 2166 | ] 2167 | }, 2168 | { 2169 | "cell_type": "code", 2170 | "execution_count": 22, 2171 | "id": "a801d901", 2172 | "metadata": {}, 2173 | "outputs": [ 2174 | { 2175 | "data": { 2176 | "text/plain": [ 2177 | "'Python'" 2178 | ] 2179 | }, 2180 | "execution_count": 22, 2181 | "metadata": {}, 2182 | "output_type": "execute_result" 2183 | } 2184 | ], 2185 | "source": [ 2186 | "' Python'.lstrip()" 2187 | ] 2188 | }, 2189 | { 2190 | "cell_type": "code", 2191 | "execution_count": 23, 2192 | "id": "3200e1e4", 2193 | "metadata": {}, 2194 | "outputs": [ 2195 | { 2196 | "data": { 2197 | "text/plain": [ 2198 | "'Python'" 2199 | ] 2200 | }, 2201 | "execution_count": 23, 2202 | "metadata": {}, 2203 | "output_type": "execute_result" 2204 | } 2205 | ], 2206 | "source": [ 2207 | "'Python '.rstrip()" 2208 | ] 2209 | }, 2210 | { 2211 | "cell_type": "code", 2212 | "execution_count": 24, 2213 | "id": "a14f297b", 2214 | "metadata": {}, 2215 | "outputs": [ 2216 | { 2217 | "data": { 2218 | "text/plain": [ 2219 | "'Python'" 2220 | ] 2221 | }, 2222 | "execution_count": 24, 2223 | "metadata": {}, 2224 | "output_type": "execute_result" 2225 | } 2226 | ], 2227 | "source": [ 2228 | "' Python '.strip()" 2229 | ] 2230 | }, 2231 | { 2232 | "cell_type": "code", 2233 | "execution_count": 25, 2234 | "id": "6ce4e964", 2235 | "metadata": {}, 2236 | "outputs": [ 2237 | { 2238 | "data": { 2239 | "text/plain": [ 2240 | "'Python Programming'" 2241 | ] 2242 | }, 2243 | "execution_count": 25, 2244 | "metadata": {}, 2245 | "output_type": "execute_result" 2246 | } 2247 | ], 2248 | "source": [ 2249 | "'Python Programming'.replace(' ', '')" 2250 | ] 2251 | }, 2252 | { 2253 | "cell_type": "code", 2254 | "execution_count": 27, 2255 | "id": "652a204d", 2256 | "metadata": {}, 2257 | "outputs": [ 2258 | { 2259 | "name": "stdout", 2260 | "output_type": "stream", 2261 | "text": [ 2262 | "['Python', 'is', 'the', 'most', 'preferred', 'language', 'for', 'Data', 'Science', 'and', 'Machine', 'Learning']\n" 2263 | ] 2264 | } 2265 | ], 2266 | "source": [ 2267 | "word3.split() #split a text into list of words" 2268 | ] 2269 | }, 2270 | { 2271 | "cell_type": "code", 2272 | "execution_count": 28, 2273 | "id": "40adba61", 2274 | "metadata": {}, 2275 | "outputs": [], 2276 | "source": [ 2277 | "list_of_words = word3.split()" 2278 | ] 2279 | }, 2280 | { 2281 | "cell_type": "code", 2282 | "execution_count": 29, 2283 | "id": "5ef3a6e7", 2284 | "metadata": {}, 2285 | "outputs": [ 2286 | { 2287 | "data": { 2288 | "text/plain": [ 2289 | "'Python is the most preferred language for Data Science and Machine Learning'" 2290 | ] 2291 | }, 2292 | "execution_count": 29, 2293 | "metadata": {}, 2294 | "output_type": "execute_result" 2295 | } 2296 | ], 2297 | "source": [ 2298 | "' '.join(list_of_words)" 2299 | ] 2300 | }, 2301 | { 2302 | "cell_type": "code", 2303 | "execution_count": 30, 2304 | "id": "fb2c85b6", 2305 | "metadata": {}, 2306 | "outputs": [ 2307 | { 2308 | "data": { 2309 | "text/plain": [ 2310 | "'Python-is-the-most-preferred-language-for-Data-Science-and-Machine-Learning'" 2311 | ] 2312 | }, 2313 | "execution_count": 30, 2314 | "metadata": {}, 2315 | "output_type": "execute_result" 2316 | } 2317 | ], 2318 | "source": [ 2319 | "'-'.join(list_of_words)" 2320 | ] 2321 | }, 2322 | { 2323 | "cell_type": "markdown", 2324 | "id": "6922f939", 2325 | "metadata": {}, 2326 | "source": [ 2327 | "#### Taking input from users and String Formatting" 2328 | ] 2329 | }, 2330 | { 2331 | "cell_type": "code", 2332 | "execution_count": 2, 2333 | "id": "9f14866e", 2334 | "metadata": {}, 2335 | "outputs": [ 2336 | { 2337 | "name": "stdout", 2338 | "output_type": "stream", 2339 | "text": [ 2340 | "Enter your age: 23\n" 2341 | ] 2342 | } 2343 | ], 2344 | "source": [ 2345 | "age = input('Enter your age: ')" 2346 | ] 2347 | }, 2348 | { 2349 | "cell_type": "code", 2350 | "execution_count": 3, 2351 | "id": "04bee960", 2352 | "metadata": {}, 2353 | "outputs": [ 2354 | { 2355 | "data": { 2356 | "text/plain": [ 2357 | "str" 2358 | ] 2359 | }, 2360 | "execution_count": 3, 2361 | "metadata": {}, 2362 | "output_type": "execute_result" 2363 | } 2364 | ], 2365 | "source": [ 2366 | "type(age)" 2367 | ] 2368 | }, 2369 | { 2370 | "cell_type": "code", 2371 | "execution_count": 4, 2372 | "id": "b6df2a5b", 2373 | "metadata": {}, 2374 | "outputs": [ 2375 | { 2376 | "name": "stdout", 2377 | "output_type": "stream", 2378 | "text": [ 2379 | "Enter your age: 23\n" 2380 | ] 2381 | } 2382 | ], 2383 | "source": [ 2384 | "age = int(input('Enter your age: '))" 2385 | ] 2386 | }, 2387 | { 2388 | "cell_type": "code", 2389 | "execution_count": 5, 2390 | "id": "db419d76", 2391 | "metadata": {}, 2392 | "outputs": [ 2393 | { 2394 | "data": { 2395 | "text/plain": [ 2396 | "int" 2397 | ] 2398 | }, 2399 | "execution_count": 5, 2400 | "metadata": {}, 2401 | "output_type": "execute_result" 2402 | } 2403 | ], 2404 | "source": [ 2405 | "type(age)" 2406 | ] 2407 | }, 2408 | { 2409 | "cell_type": "code", 2410 | "execution_count": 6, 2411 | "id": "57d8feb1", 2412 | "metadata": {}, 2413 | "outputs": [ 2414 | { 2415 | "name": "stdout", 2416 | "output_type": "stream", 2417 | "text": [ 2418 | "Enter your name: Shikhar\n" 2419 | ] 2420 | } 2421 | ], 2422 | "source": [ 2423 | "name = input('Enter your name: ')" 2424 | ] 2425 | }, 2426 | { 2427 | "cell_type": "code", 2428 | "execution_count": 7, 2429 | "id": "c5085986", 2430 | "metadata": {}, 2431 | "outputs": [ 2432 | { 2433 | "name": "stdout", 2434 | "output_type": "stream", 2435 | "text": [ 2436 | "Enter your place of stay: Delhi\n" 2437 | ] 2438 | } 2439 | ], 2440 | "source": [ 2441 | "place = input('Enter your place of stay: ')" 2442 | ] 2443 | }, 2444 | { 2445 | "cell_type": "code", 2446 | "execution_count": 8, 2447 | "id": "5433e5fc", 2448 | "metadata": {}, 2449 | "outputs": [ 2450 | { 2451 | "name": "stdout", 2452 | "output_type": "stream", 2453 | "text": [ 2454 | "My name is Shikhar, I am 23 years old and I stay in Delhi.\n" 2455 | ] 2456 | } 2457 | ], 2458 | "source": [ 2459 | "print('My name is Shikhar, I am 23 years old and I stay in Delhi.')" 2460 | ] 2461 | }, 2462 | { 2463 | "cell_type": "code", 2464 | "execution_count": 10, 2465 | "id": "5cebc5b1", 2466 | "metadata": {}, 2467 | "outputs": [ 2468 | { 2469 | "name": "stdout", 2470 | "output_type": "stream", 2471 | "text": [ 2472 | "My name is Shikhar, I am 23 years old and I stay in Delhi.\n" 2473 | ] 2474 | } 2475 | ], 2476 | "source": [ 2477 | "#string formatting\n", 2478 | "print(f'My name is {name}, I am {age} years old and I stay in {place}.')" 2479 | ] 2480 | }, 2481 | { 2482 | "cell_type": "code", 2483 | "execution_count": null, 2484 | "id": "01d55e62", 2485 | "metadata": {}, 2486 | "outputs": [], 2487 | "source": [] 2488 | } 2489 | ], 2490 | "metadata": { 2491 | "kernelspec": { 2492 | "display_name": "Python 3 (ipykernel)", 2493 | "language": "python", 2494 | "name": "python3" 2495 | }, 2496 | "language_info": { 2497 | "codemirror_mode": { 2498 | "name": "ipython", 2499 | "version": 3 2500 | }, 2501 | "file_extension": ".py", 2502 | "mimetype": "text/x-python", 2503 | "name": "python", 2504 | "nbconvert_exporter": "python", 2505 | "pygments_lexer": "ipython3", 2506 | "version": "3.11.7" 2507 | } 2508 | }, 2509 | "nbformat": 4, 2510 | "nbformat_minor": 5 2511 | } 2512 | -------------------------------------------------------------------------------- /Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Operators" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Operator Types" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "1. Arithmetic operators\n", 29 | " \n", 30 | "2. Comparison (Relational) operators\n", 31 | "\n", 32 | "3. Logical (Boolean) operators\n", 33 | "\n", 34 | "4. Bitwise operators\n", 35 | "\n", 36 | "5. Assignment operators\n", 37 | "\n", 38 | "6. Special operators" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "# Arithmetic Operators" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc." 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | " + , -, *, /, %, //, ** are arithmetic operators" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Example:" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 1, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "num1 = 10\n", 76 | "num2 = 20" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 2, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "30" 88 | ] 89 | }, 90 | "execution_count": 2, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "num1 + num2" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 4, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "-10" 108 | ] 109 | }, 110 | "execution_count": 4, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "num1 - num2" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "200" 128 | ] 129 | }, 130 | "execution_count": 5, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "num1 * num2" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 7, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "5.0" 148 | ] 149 | }, 150 | "execution_count": 7, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "num1 / 2" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 8, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "1" 168 | ] 169 | }, 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "# modulo operator\n", 177 | "9 % 2" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 10, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "4" 189 | ] 190 | }, 191 | "execution_count": 10, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "# floor division\n", 198 | "9 // 2" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 14, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "15218.966662792947" 210 | ] 211 | }, 212 | "execution_count": 14, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "# exponential operator\n", 219 | "8.5 ** 4.5" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "# Comparision Operators" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "* These operators will allow us to compare variables and output a Boolean value (True or False). \n", 234 | "\n", 235 | "\n", 236 | "* If you have any sort of background in Math, these operators should be very straight forward.\n", 237 | "\n", 238 | "\n", 239 | "* First we'll present a table of the comparison operators and then work through some examples:\n", 240 | "\n", 241 | "\n", 242 | "* In the table below, a=3 and b=4.\n", 243 | "\n", 244 | "\n", 245 | "\n", 246 | "\n", 247 | "\n", 248 | "\n", 249 | "\n", 250 | "\n", 251 | "\n", 252 | "\n", 253 | "\n", 254 | "\n", 255 | "\n", 256 | "\n", 257 | "\n", 258 | "\n", 259 | "\n", 260 | "\n", 261 | "\n", 262 | "\n", 263 | "\n", 264 | "\n", 265 | "\n", 266 | "\n", 267 | "\n", 268 | "\n", 269 | "\n", 270 | "\n", 271 | "\n", 272 | "\n", 273 | "\n", 274 | "\n", 275 | "\n", 276 | "\n", 277 | "\n", 278 | "
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.(a != b) is true
>If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.
" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 16, 284 | "metadata": {}, 285 | "outputs": [ 286 | { 287 | "data": { 288 | "text/plain": [ 289 | "False" 290 | ] 291 | }, 292 | "execution_count": 16, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "10 == 20" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 22, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/plain": [ 309 | "True" 310 | ] 311 | }, 312 | "execution_count": 22, 313 | "metadata": {}, 314 | "output_type": "execute_result" 315 | } 316 | ], 317 | "source": [ 318 | "num1 == 10" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 33, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "data": { 328 | "text/plain": [ 329 | "True" 330 | ] 331 | }, 332 | "execution_count": 33, 333 | "metadata": {}, 334 | "output_type": "execute_result" 335 | } 336 | ], 337 | "source": [ 338 | "10 != 20" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 24, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "data": { 348 | "text/plain": [ 349 | "False" 350 | ] 351 | }, 352 | "execution_count": 24, 353 | "metadata": {}, 354 | "output_type": "execute_result" 355 | } 356 | ], 357 | "source": [ 358 | "10 > 20" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 25, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "True" 370 | ] 371 | }, 372 | "execution_count": 25, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "10 < 20" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 27, 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "data": { 388 | "text/plain": [ 389 | "True" 390 | ] 391 | }, 392 | "execution_count": 27, 393 | "metadata": {}, 394 | "output_type": "execute_result" 395 | } 396 | ], 397 | "source": [ 398 | "10 >= 10" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 29, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "True" 410 | ] 411 | }, 412 | "execution_count": 29, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "10 <= 10" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 31, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "data": { 428 | "text/plain": [ 429 | "True" 430 | ] 431 | }, 432 | "execution_count": 31, 433 | "metadata": {}, 434 | "output_type": "execute_result" 435 | } 436 | ], 437 | "source": [ 438 | "num2 == 20" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "# Logical Operators" 446 | ] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "metadata": {}, 451 | "source": [ 452 | "Logical operators are **and, or, not** operators." 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 38, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "num = 15" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 39, 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "data": { 471 | "text/plain": [ 472 | "False" 473 | ] 474 | }, 475 | "execution_count": 39, 476 | "metadata": {}, 477 | "output_type": "execute_result" 478 | } 479 | ], 480 | "source": [ 481 | "num < 10" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 40, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "True" 493 | ] 494 | }, 495 | "execution_count": 40, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "num > 0" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 41, 507 | "metadata": {}, 508 | "outputs": [ 509 | { 510 | "data": { 511 | "text/plain": [ 512 | "False" 513 | ] 514 | }, 515 | "execution_count": 41, 516 | "metadata": {}, 517 | "output_type": "execute_result" 518 | } 519 | ], 520 | "source": [ 521 | "num < 10 and num > 0" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 42, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "data": { 531 | "text/plain": [ 532 | "True" 533 | ] 534 | }, 535 | "execution_count": 42, 536 | "metadata": {}, 537 | "output_type": "execute_result" 538 | } 539 | ], 540 | "source": [ 541 | "num < 10 or num > 0" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 44, 547 | "metadata": {}, 548 | "outputs": [ 549 | { 550 | "data": { 551 | "text/plain": [ 552 | "True" 553 | ] 554 | }, 555 | "execution_count": 44, 556 | "metadata": {}, 557 | "output_type": "execute_result" 558 | } 559 | ], 560 | "source": [ 561 | "not num > 20" 562 | ] 563 | }, 564 | { 565 | "cell_type": "markdown", 566 | "metadata": {}, 567 | "source": [ 568 | "# Assignment operators" 569 | ] 570 | }, 571 | { 572 | "cell_type": "markdown", 573 | "metadata": {}, 574 | "source": [ 575 | "Assignment operators are used in Python to assign values to variables.\n", 576 | "\n", 577 | "a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left." 578 | ] 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "metadata": {}, 583 | "source": [ 584 | " =, +=, -=, *=, /=, %=, //=, **= are Assignment operators" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 53, 590 | "metadata": {}, 591 | "outputs": [], 592 | "source": [ 593 | "num = 5" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": 54, 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [ 602 | "num = num + 5" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": null, 608 | "metadata": {}, 609 | "outputs": [], 610 | "source": [ 611 | "num += 5" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": null, 617 | "metadata": {}, 618 | "outputs": [], 619 | "source": [] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 55, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "num = num - 2" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 52, 633 | "metadata": {}, 634 | "outputs": [], 635 | "source": [ 636 | "num -= 2" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": null, 642 | "metadata": {}, 643 | "outputs": [], 644 | "source": [] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 56, 649 | "metadata": {}, 650 | "outputs": [], 651 | "source": [ 652 | "num = num * 4" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": null, 658 | "metadata": {}, 659 | "outputs": [], 660 | "source": [ 661 | "num *= 4" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": null, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [ 677 | "num = num / 5" 678 | ] 679 | }, 680 | { 681 | "cell_type": "code", 682 | "execution_count": null, 683 | "metadata": {}, 684 | "outputs": [], 685 | "source": [ 686 | "num /= 5" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": null, 692 | "metadata": {}, 693 | "outputs": [], 694 | "source": [] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": null, 699 | "metadata": {}, 700 | "outputs": [], 701 | "source": [ 702 | "num = num // 5" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": null, 708 | "metadata": {}, 709 | "outputs": [], 710 | "source": [ 711 | "num //= 5" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": null, 717 | "metadata": {}, 718 | "outputs": [], 719 | "source": [] 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": null, 724 | "metadata": {}, 725 | "outputs": [], 726 | "source": [ 727 | "num = num % 3" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": null, 733 | "metadata": {}, 734 | "outputs": [], 735 | "source": [ 736 | "num %= 3" 737 | ] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "execution_count": null, 742 | "metadata": {}, 743 | "outputs": [], 744 | "source": [] 745 | }, 746 | { 747 | "cell_type": "code", 748 | "execution_count": null, 749 | "metadata": {}, 750 | "outputs": [], 751 | "source": [ 752 | "num = num ** 10" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": null, 758 | "metadata": {}, 759 | "outputs": [], 760 | "source": [ 761 | "num **= 10" 762 | ] 763 | }, 764 | { 765 | "cell_type": "markdown", 766 | "metadata": {}, 767 | "source": [ 768 | "# Special Operators" 769 | ] 770 | }, 771 | { 772 | "cell_type": "markdown", 773 | "metadata": {}, 774 | "source": [ 775 | "# Identity Operators" 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "**is and is not** are the identity operators in Python. \n", 783 | "\n", 784 | "They are used to check if two values (or variables) are located on the same part of the memory." 785 | ] 786 | }, 787 | { 788 | "cell_type": "code", 789 | "execution_count": 57, 790 | "metadata": {}, 791 | "outputs": [], 792 | "source": [ 793 | "num1 = 10\n", 794 | "num2 = 10" 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": 58, 800 | "metadata": {}, 801 | "outputs": [ 802 | { 803 | "data": { 804 | "text/plain": [ 805 | "140704505735248" 806 | ] 807 | }, 808 | "execution_count": 58, 809 | "metadata": {}, 810 | "output_type": "execute_result" 811 | } 812 | ], 813 | "source": [ 814 | "id(num1)" 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 59, 820 | "metadata": {}, 821 | "outputs": [ 822 | { 823 | "data": { 824 | "text/plain": [ 825 | "140704505735248" 826 | ] 827 | }, 828 | "execution_count": 59, 829 | "metadata": {}, 830 | "output_type": "execute_result" 831 | } 832 | ], 833 | "source": [ 834 | "id(num2)" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 60, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "data": { 844 | "text/plain": [ 845 | "True" 846 | ] 847 | }, 848 | "execution_count": 60, 849 | "metadata": {}, 850 | "output_type": "execute_result" 851 | } 852 | ], 853 | "source": [ 854 | "num1 is num2" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 61, 860 | "metadata": {}, 861 | "outputs": [], 862 | "source": [ 863 | "word1 = 'Python'\n", 864 | "word2 = 'Python'" 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": 62, 870 | "metadata": {}, 871 | "outputs": [ 872 | { 873 | "data": { 874 | "text/plain": [ 875 | "True" 876 | ] 877 | }, 878 | "execution_count": 62, 879 | "metadata": {}, 880 | "output_type": "execute_result" 881 | } 882 | ], 883 | "source": [ 884 | "word1 is word2" 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": 63, 890 | "metadata": {}, 891 | "outputs": [], 892 | "source": [ 893 | "l1 = [10, 20, 30]\n", 894 | "l2 = [10, 20, 30]" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": 64, 900 | "metadata": {}, 901 | "outputs": [ 902 | { 903 | "data": { 904 | "text/plain": [ 905 | "False" 906 | ] 907 | }, 908 | "execution_count": 64, 909 | "metadata": {}, 910 | "output_type": "execute_result" 911 | } 912 | ], 913 | "source": [ 914 | "l1 is l2" 915 | ] 916 | }, 917 | { 918 | "cell_type": "code", 919 | "execution_count": 66, 920 | "metadata": {}, 921 | "outputs": [ 922 | { 923 | "data": { 924 | "text/plain": [ 925 | "2623799090688" 926 | ] 927 | }, 928 | "execution_count": 66, 929 | "metadata": {}, 930 | "output_type": "execute_result" 931 | } 932 | ], 933 | "source": [ 934 | "id(l1)" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": 67, 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "data": { 944 | "text/plain": [ 945 | "2623799037888" 946 | ] 947 | }, 948 | "execution_count": 67, 949 | "metadata": {}, 950 | "output_type": "execute_result" 951 | } 952 | ], 953 | "source": [ 954 | "id(l2)" 955 | ] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": 68, 960 | "metadata": {}, 961 | "outputs": [], 962 | "source": [ 963 | "l3 = [40 ,50, 60]\n", 964 | "l4 = l3" 965 | ] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": 69, 970 | "metadata": {}, 971 | "outputs": [ 972 | { 973 | "data": { 974 | "text/plain": [ 975 | "2623799449856" 976 | ] 977 | }, 978 | "execution_count": 69, 979 | "metadata": {}, 980 | "output_type": "execute_result" 981 | } 982 | ], 983 | "source": [ 984 | "id(l4)" 985 | ] 986 | }, 987 | { 988 | "cell_type": "code", 989 | "execution_count": 70, 990 | "metadata": {}, 991 | "outputs": [ 992 | { 993 | "data": { 994 | "text/plain": [ 995 | "2623799449856" 996 | ] 997 | }, 998 | "execution_count": 70, 999 | "metadata": {}, 1000 | "output_type": "execute_result" 1001 | } 1002 | ], 1003 | "source": [ 1004 | "id(l3)" 1005 | ] 1006 | }, 1007 | { 1008 | "cell_type": "code", 1009 | "execution_count": 71, 1010 | "metadata": {}, 1011 | "outputs": [ 1012 | { 1013 | "data": { 1014 | "text/plain": [ 1015 | "True" 1016 | ] 1017 | }, 1018 | "execution_count": 71, 1019 | "metadata": {}, 1020 | "output_type": "execute_result" 1021 | } 1022 | ], 1023 | "source": [ 1024 | "l3 is l4" 1025 | ] 1026 | }, 1027 | { 1028 | "cell_type": "code", 1029 | "execution_count": 72, 1030 | "metadata": {}, 1031 | "outputs": [], 1032 | "source": [ 1033 | "l3.append(70)" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "code", 1038 | "execution_count": 73, 1039 | "metadata": {}, 1040 | "outputs": [ 1041 | { 1042 | "data": { 1043 | "text/plain": [ 1044 | "[40, 50, 60, 70]" 1045 | ] 1046 | }, 1047 | "execution_count": 73, 1048 | "metadata": {}, 1049 | "output_type": "execute_result" 1050 | } 1051 | ], 1052 | "source": [ 1053 | "l3" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "code", 1058 | "execution_count": 74, 1059 | "metadata": {}, 1060 | "outputs": [ 1061 | { 1062 | "data": { 1063 | "text/plain": [ 1064 | "[40, 50, 60, 70]" 1065 | ] 1066 | }, 1067 | "execution_count": 74, 1068 | "metadata": {}, 1069 | "output_type": "execute_result" 1070 | } 1071 | ], 1072 | "source": [ 1073 | "l4" 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": null, 1079 | "metadata": {}, 1080 | "outputs": [], 1081 | "source": [] 1082 | }, 1083 | { 1084 | "cell_type": "code", 1085 | "execution_count": 19, 1086 | "metadata": {}, 1087 | "outputs": [ 1088 | { 1089 | "name": "stdout", 1090 | "output_type": "stream", 1091 | "text": [ 1092 | "True\n" 1093 | ] 1094 | } 1095 | ], 1096 | "source": [ 1097 | "num1 = 10\n", 1098 | "num2 = 10\n", 1099 | "\n", 1100 | "print(num1 is num2)" 1101 | ] 1102 | }, 1103 | { 1104 | "cell_type": "code", 1105 | "execution_count": 20, 1106 | "metadata": {}, 1107 | "outputs": [ 1108 | { 1109 | "name": "stdout", 1110 | "output_type": "stream", 1111 | "text": [ 1112 | "False\n" 1113 | ] 1114 | } 1115 | ], 1116 | "source": [ 1117 | "l1 = [1, 2, 3]\n", 1118 | "l2 = [1, 2, 3]\n", 1119 | "print(l1 is l2)" 1120 | ] 1121 | }, 1122 | { 1123 | "cell_type": "code", 1124 | "execution_count": 50, 1125 | "metadata": {}, 1126 | "outputs": [ 1127 | { 1128 | "name": "stdout", 1129 | "output_type": "stream", 1130 | "text": [ 1131 | "False\n" 1132 | ] 1133 | } 1134 | ], 1135 | "source": [ 1136 | "s1 = \"Shikhar\"\n", 1137 | "s2 = \"Shikhar\"\n", 1138 | "print(s1 is not s2)" 1139 | ] 1140 | }, 1141 | { 1142 | "cell_type": "markdown", 1143 | "metadata": {}, 1144 | "source": [ 1145 | "# MemberShip Operators" 1146 | ] 1147 | }, 1148 | { 1149 | "cell_type": "markdown", 1150 | "metadata": {}, 1151 | "source": [ 1152 | "**in and not in** are the membership operators in Python. \n", 1153 | " \n", 1154 | "They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary)." 1155 | ] 1156 | }, 1157 | { 1158 | "cell_type": "code", 1159 | "execution_count": 75, 1160 | "metadata": {}, 1161 | "outputs": [], 1162 | "source": [ 1163 | "list1 = [10,20,30,40,50,60]" 1164 | ] 1165 | }, 1166 | { 1167 | "cell_type": "code", 1168 | "execution_count": 78, 1169 | "metadata": {}, 1170 | "outputs": [ 1171 | { 1172 | "data": { 1173 | "text/plain": [ 1174 | "False" 1175 | ] 1176 | }, 1177 | "execution_count": 78, 1178 | "metadata": {}, 1179 | "output_type": "execute_result" 1180 | } 1181 | ], 1182 | "source": [ 1183 | "70 in list1" 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "execution_count": 79, 1189 | "metadata": {}, 1190 | "outputs": [ 1191 | { 1192 | "data": { 1193 | "text/plain": [ 1194 | "True" 1195 | ] 1196 | }, 1197 | "execution_count": 79, 1198 | "metadata": {}, 1199 | "output_type": "execute_result" 1200 | } 1201 | ], 1202 | "source": [ 1203 | "70 not in list1" 1204 | ] 1205 | }, 1206 | { 1207 | "cell_type": "markdown", 1208 | "metadata": {}, 1209 | "source": [ 1210 | "## Bitwise Operator" 1211 | ] 1212 | }, 1213 | { 1214 | "cell_type": "code", 1215 | "execution_count": 81, 1216 | "metadata": {}, 1217 | "outputs": [], 1218 | "source": [ 1219 | "num1 = 12\n", 1220 | "num2 = 25" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "code", 1225 | "execution_count": 82, 1226 | "metadata": {}, 1227 | "outputs": [ 1228 | { 1229 | "data": { 1230 | "text/plain": [ 1231 | "8" 1232 | ] 1233 | }, 1234 | "execution_count": 82, 1235 | "metadata": {}, 1236 | "output_type": "execute_result" 1237 | } 1238 | ], 1239 | "source": [ 1240 | "num1 & num2" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "execution_count": 83, 1246 | "metadata": {}, 1247 | "outputs": [ 1248 | { 1249 | "data": { 1250 | "text/plain": [ 1251 | "29" 1252 | ] 1253 | }, 1254 | "execution_count": 83, 1255 | "metadata": {}, 1256 | "output_type": "execute_result" 1257 | } 1258 | ], 1259 | "source": [ 1260 | "num1 | num2" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": 84, 1266 | "metadata": {}, 1267 | "outputs": [ 1268 | { 1269 | "data": { 1270 | "text/plain": [ 1271 | "21" 1272 | ] 1273 | }, 1274 | "execution_count": 84, 1275 | "metadata": {}, 1276 | "output_type": "execute_result" 1277 | } 1278 | ], 1279 | "source": [ 1280 | "num1 ^ num2" 1281 | ] 1282 | }, 1283 | { 1284 | "cell_type": "code", 1285 | "execution_count": null, 1286 | "metadata": {}, 1287 | "outputs": [], 1288 | "source": [] 1289 | } 1290 | ], 1291 | "metadata": { 1292 | "kernelspec": { 1293 | "display_name": "Python 3 (ipykernel)", 1294 | "language": "python", 1295 | "name": "python3" 1296 | }, 1297 | "language_info": { 1298 | "codemirror_mode": { 1299 | "name": "ipython", 1300 | "version": 3 1301 | }, 1302 | "file_extension": ".py", 1303 | "mimetype": "text/x-python", 1304 | "name": "python", 1305 | "nbconvert_exporter": "python", 1306 | "pygments_lexer": "ipython3", 1307 | "version": "3.11.7" 1308 | } 1309 | }, 1310 | "nbformat": 4, 1311 | "nbformat_minor": 4 1312 | } 1313 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Programming-Code -------------------------------------------------------------------------------- /Sets and Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "94d99c11-dc03-4cab-bae4-887155b7d278", 6 | "metadata": {}, 7 | "source": [ 8 | "## Sets\n", 9 | "\n", 10 | "- Sets are mutable data types.\n", 11 | "- Set is unordered collection of items.\n", 12 | "- Set only allow unique values to be stored." 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "id": "3c44f06f-4d49-43b1-83a5-3af4ed8c980e", 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "set1 = {10, 20, 30, 30, 40, 50, 50, 60, 70, 80, 90}" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "id": "27ef7d4d-7ba9-4ce0-9887-8d21125614af", 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "{70, 40, 10, 80, 50, 20, 90, 60, 30}\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "print(set1)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "59509fe4-6e34-4d21-a93b-7216003d7c99", 46 | "metadata": {}, 47 | "source": [ 48 | "###### See above that we have only unique values in the set and they are unordered by default." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "id": "af1d552b-57be-4c49-9624-48e331e257c1", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "ename": "TypeError", 59 | "evalue": "'set' object is not subscriptable", 60 | "output_type": "error", 61 | "traceback": [ 62 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 63 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 64 | "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m set1[\u001b[38;5;241m3\u001b[39m]\n", 65 | "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "#indexing and slicing doesn't work in set since they are unordered\n", 71 | "set1[3]" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "id": "1781af52-4ecb-4a88-a648-45c6a69828a1", 77 | "metadata": {}, 78 | "source": [ 79 | "##### We can use set to remove duplicates from a list" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 4, 85 | "id": "be263661-d1e6-4617-b6a8-a2677c5723d6", 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "list1 = [10, 20, 30, 30, 40, 50, 50, 60, 70, 80, 90]" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "id": "a138adbc-27af-45a8-a219-d3a9a5f7065b", 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "list1 = list(set(list1))" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "id": "1063ddd2-b30b-4dfa-9de2-0be0deafebab", 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "[70, 40, 10, 80, 50, 20, 90, 60, 30]" 112 | ] 113 | }, 114 | "execution_count": 6, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "#final result is a list with unique elements\n", 121 | "list1" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "id": "1eb96057-a154-4c44-9307-1e9da51039c8", 127 | "metadata": {}, 128 | "source": [ 129 | "#### Inserting values into a set" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 8, 135 | "id": "7cb5c27c-366c-4b6c-9083-ddf444c8f76f", 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "#to insert one element into a set at a time\n", 140 | "set1.add(100)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 10, 146 | "id": "9322cc3a-13cf-4cdd-9d35-ff19f34a653c", 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "{100, 70, 40, 10, 80, 50, 20, 90, 60, 30}\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "print(set1)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 11, 164 | "id": "80bd6439-d63d-4132-b182-0da11639a51f", 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "#to insert multiple values into the set\n", 169 | "set1.update({89, 45, 77})" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 12, 175 | "id": "ee856b19-4be0-4f12-a9b6-baf8705a87a8", 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "{100, 70, 40, 10, 45, 77, 80, 50, 20, 89, 90, 60, 30}\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "print(set1)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "id": "34fb62f9-7d48-4e65-b320-41fc7999e204", 193 | "metadata": {}, 194 | "source": [ 195 | "#### Delete Values from a set" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 13, 201 | "id": "99029e12-5a16-4cd1-b1b4-aefc4f79cd74", 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "100" 208 | ] 209 | }, 210 | "execution_count": 13, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "#remove an item from the set randomly\n", 217 | "set1.pop()" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 15, 223 | "id": "4f22a40b-4bfc-4b43-84d4-7be004464e38", 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "#remove a particular item from the set\n", 228 | "set1.remove(89)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 16, 234 | "id": "182ff0c7-8c59-4780-a6f0-a8a60e0b5ada", 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "#remove item using `discard` method\n", 239 | "set1.discard(90)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "id": "2ff5892c-92e5-473f-b8cd-470d9b3be779", 245 | "metadata": {}, 246 | "source": [ 247 | "##### Difference between remove and discard method is that remove gives error when the item we want to remove does not exist whereas discard does not throw any error in any case" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 17, 253 | "id": "4fc43c6f-82c9-49c1-8612-c6fbf6d24362", 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "ename": "KeyError", 258 | "evalue": "900", 259 | "output_type": "error", 260 | "traceback": [ 261 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 262 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 263 | "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m set1\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;241m900\u001b[39m)\n", 264 | "\u001b[1;31mKeyError\u001b[0m: 900" 265 | ] 266 | } 267 | ], 268 | "source": [ 269 | "set1.remove(900)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 18, 275 | "id": "38cb9494-6579-4902-a4fc-321a5da7c492", 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "set1.discard(900)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "id": "34eabe9b-42c9-4c7c-b4e8-7a02e6a1c6b0", 285 | "metadata": {}, 286 | "source": [ 287 | "#### Some Set Methods\n", 288 | "\n", 289 | "- Union \n", 290 | "- Intersection\n", 291 | "- Difference \n", 292 | "- Symmetric Difference\n", 293 | "- isdisjoint\n", 294 | "- issubset\n", 295 | "- issuperset" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 19, 301 | "id": "9f0b0452-b221-489c-bb03-9c93c941b88e", 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "set2 = {10,20,30,40,50,60,70}\n", 306 | "set3 = {60,70,80,90,100,110,120}" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 20, 312 | "id": "98cfff28-13d0-4c5c-9b5c-3527e6e68a67", 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "data": { 317 | "text/plain": [ 318 | "{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120}" 319 | ] 320 | }, 321 | "execution_count": 20, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "#set of items from the both the sets which must be unique\n", 328 | "set2.union(set3)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 21, 334 | "id": "9ed1e32e-8693-4b1d-b96b-b9de17e84fee", 335 | "metadata": {}, 336 | "outputs": [ 337 | { 338 | "data": { 339 | "text/plain": [ 340 | "{60, 70}" 341 | ] 342 | }, 343 | "execution_count": 21, 344 | "metadata": {}, 345 | "output_type": "execute_result" 346 | } 347 | ], 348 | "source": [ 349 | "#common elements in both the sets\n", 350 | "set2.intersection(set3)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 22, 356 | "id": "0ddd2e5d-e3cb-423e-872f-4ab364976318", 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "{10, 20, 30, 40, 50}" 363 | ] 364 | }, 365 | "execution_count": 22, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "#items which are purely in set2 and no items belonging to set3\n", 372 | "set2.difference(set3)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 23, 378 | "id": "81ec884a-c00b-4237-873e-e270ce89fb21", 379 | "metadata": {}, 380 | "outputs": [ 381 | { 382 | "data": { 383 | "text/plain": [ 384 | "{80, 90, 100, 110, 120}" 385 | ] 386 | }, 387 | "execution_count": 23, 388 | "metadata": {}, 389 | "output_type": "execute_result" 390 | } 391 | ], 392 | "source": [ 393 | "#items that belong to set3 and no items belonging to set2\n", 394 | "set3.difference(set2)" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 24, 400 | "id": "4d57ac34-59d2-4986-a966-66f2aa5b223d", 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "data": { 405 | "text/plain": [ 406 | "{10, 20, 30, 40, 50, 80, 90, 100, 110, 120}" 407 | ] 408 | }, 409 | "execution_count": 24, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "#set of items from set2 and set3 except the common ones\n", 416 | "set2.symmetric_difference(set3)" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 26, 422 | "id": "514222b4-518f-4e30-aaae-36904f27f2a1", 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "data": { 427 | "text/plain": [ 428 | "False" 429 | ] 430 | }, 431 | "execution_count": 26, 432 | "metadata": {}, 433 | "output_type": "execute_result" 434 | } 435 | ], 436 | "source": [ 437 | "#return True if no elements in two sets are common\n", 438 | "set2.isdisjoint(set3)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 27, 444 | "id": "cfc2329f-7b02-4e2e-9f2d-1f7b9288a11b", 445 | "metadata": {}, 446 | "outputs": [], 447 | "source": [ 448 | "set4 = {10, 30, 40, 50}" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 28, 454 | "id": "267225d2-9ee5-467e-b5cf-1fdfdf955843", 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "True" 461 | ] 462 | }, 463 | "execution_count": 28, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "#if every element of set4 is in set2 then set4 is subset of set2\n", 470 | "set4.issubset(set2)" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 29, 476 | "id": "8195dda8-8b61-4e3a-945d-257913db8e24", 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "data": { 481 | "text/plain": [ 482 | "True" 483 | ] 484 | }, 485 | "execution_count": 29, 486 | "metadata": {}, 487 | "output_type": "execute_result" 488 | } 489 | ], 490 | "source": [ 491 | "#reverse of subset where every element of set4 must be in set2\n", 492 | "set2.issuperset(set4)" 493 | ] 494 | }, 495 | { 496 | "cell_type": "markdown", 497 | "id": "492765b3-7ee8-43db-99f6-3fc9ab3aa125", 498 | "metadata": {}, 499 | "source": [ 500 | "## Dictionaries\n", 501 | "\n", 502 | "- Dictionaries are unordered collection of items.\n", 503 | "- It is a mutable data types.\n", 504 | "- It stores key-value pairs.\n", 505 | "- It only allow unique values." 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 30, 511 | "id": "0cf76950-3d23-40f2-b77a-4b0512fdde0b", 512 | "metadata": {}, 513 | "outputs": [], 514 | "source": [ 515 | "dict1 = {'name': 'Shikhar', 'place': 'India', 'roles': ['Data Scientist', 'Data Science Trainer'], \n", 516 | " 'hobbies': ('Fitness', 'Football'), 'education': {1: 'Masters', 2: 'Bachelors'}}" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 31, 522 | "id": "93bdc8b0-3471-45c9-8d6c-add07d58f440", 523 | "metadata": {}, 524 | "outputs": [ 525 | { 526 | "data": { 527 | "text/plain": [ 528 | "{'name': 'Shikhar',\n", 529 | " 'place': 'India',\n", 530 | " 'roles': ['Data Scientist', 'Data Science Trainer'],\n", 531 | " 'hobbies': ('Fitness', 'Football'),\n", 532 | " 'education': {1: 'Masters', 2: 'Bachelors'}}" 533 | ] 534 | }, 535 | "execution_count": 31, 536 | "metadata": {}, 537 | "output_type": "execute_result" 538 | } 539 | ], 540 | "source": [ 541 | "dict1" 542 | ] 543 | }, 544 | { 545 | "cell_type": "markdown", 546 | "id": "1bc1db8e-a09a-4d87-822f-8a4723ff2f58", 547 | "metadata": {}, 548 | "source": [ 549 | "#### Accessing the values of a dictionary" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 32, 555 | "id": "8ab1767d-5168-4c64-aae9-95d6a3588db0", 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "['Data Scientist', 'Data Science Trainer']" 562 | ] 563 | }, 564 | "execution_count": 32, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "#to access a value from a dictionary we access the keys related to that value\n", 571 | "dict1['roles']" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 33, 577 | "id": "9e8f18f3-a3f7-4f28-b2ba-52660cf7bd7a", 578 | "metadata": {}, 579 | "outputs": [ 580 | { 581 | "data": { 582 | "text/plain": [ 583 | "['Data Scientist', 'Data Science Trainer']" 584 | ] 585 | }, 586 | "execution_count": 33, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "#alternate way to access the values of a dictionary\n", 593 | "dict1.get('roles')" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": 34, 599 | "id": "5ef46d7c-b73e-40e6-a3c5-55289892f7eb", 600 | "metadata": {}, 601 | "outputs": [ 602 | { 603 | "data": { 604 | "text/plain": [ 605 | "'Data Science Trainer'" 606 | ] 607 | }, 608 | "execution_count": 34, 609 | "metadata": {}, 610 | "output_type": "execute_result" 611 | } 612 | ], 613 | "source": [ 614 | "#double indexing to access the data from inside the list of roles\n", 615 | "dict1['roles'][1]" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 35, 621 | "id": "d8f750d1-9e9f-496f-9cdd-ed42f490682f", 622 | "metadata": {}, 623 | "outputs": [ 624 | { 625 | "data": { 626 | "text/plain": [ 627 | "'Bachelors'" 628 | ] 629 | }, 630 | "execution_count": 35, 631 | "metadata": {}, 632 | "output_type": "execute_result" 633 | } 634 | ], 635 | "source": [ 636 | "#double indexing to access the data from inside the inner dictionary\n", 637 | "dict1['education'][2]" 638 | ] 639 | }, 640 | { 641 | "cell_type": "markdown", 642 | "id": "fd28a9c2-6b1f-41a1-a8c2-1c00e68867dd", 643 | "metadata": {}, 644 | "source": [ 645 | "#### Inserting values into a Dictionary" 646 | ] 647 | }, 648 | { 649 | "cell_type": "code", 650 | "execution_count": 36, 651 | "id": "574fa6b2-e9ce-4bdf-bdf4-90daceeab54a", 652 | "metadata": {}, 653 | "outputs": [], 654 | "source": [ 655 | "#to insert values in to a dictionary, we just need to provide new key and new value like dict[key] = value\n", 656 | "dict1['new_key'] = 45" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 37, 662 | "id": "0645e856-a3ac-4eb8-b91d-133bd6c8210f", 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "{'name': 'Shikhar',\n", 669 | " 'place': 'India',\n", 670 | " 'roles': ['Data Scientist', 'Data Science Trainer'],\n", 671 | " 'hobbies': ('Fitness', 'Football'),\n", 672 | " 'education': {1: 'Masters', 2: 'Bachelors'},\n", 673 | " 'new_key': 45}" 674 | ] 675 | }, 676 | "execution_count": 37, 677 | "metadata": {}, 678 | "output_type": "execute_result" 679 | } 680 | ], 681 | "source": [ 682 | "dict1" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 38, 688 | "id": "6bfaedc4-967c-4d06-94ee-c9f41af05ceb", 689 | "metadata": {}, 690 | "outputs": [], 691 | "source": [ 692 | "dict1.update({'new_key2': (12, 43)})" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 39, 698 | "id": "07592994-e7e8-497e-9d14-26e3d097b09b", 699 | "metadata": {}, 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/plain": [ 704 | "{'name': 'Shikhar',\n", 705 | " 'place': 'India',\n", 706 | " 'roles': ['Data Scientist', 'Data Science Trainer'],\n", 707 | " 'hobbies': ('Fitness', 'Football'),\n", 708 | " 'education': {1: 'Masters', 2: 'Bachelors'},\n", 709 | " 'new_key': 45,\n", 710 | " 'new_key2': (12, 43)}" 711 | ] 712 | }, 713 | "execution_count": 39, 714 | "metadata": {}, 715 | "output_type": "execute_result" 716 | } 717 | ], 718 | "source": [ 719 | "dict1" 720 | ] 721 | }, 722 | { 723 | "cell_type": "markdown", 724 | "id": "b1dcd119-0ee8-4027-80fa-ed8afa81d78d", 725 | "metadata": {}, 726 | "source": [ 727 | "#### Deleting values from a dictionary" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 40, 733 | "id": "e32ad085-79e5-4073-9e17-2258b7d7c61b", 734 | "metadata": {}, 735 | "outputs": [ 736 | { 737 | "data": { 738 | "text/plain": [ 739 | "('new_key2', (12, 43))" 740 | ] 741 | }, 742 | "execution_count": 40, 743 | "metadata": {}, 744 | "output_type": "execute_result" 745 | } 746 | ], 747 | "source": [ 748 | "#remove the last inserted pair from the dictionary\n", 749 | "dict1.popitem()" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": 42, 755 | "id": "126a2141-7385-4a32-8938-315dd73c8047", 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "data": { 760 | "text/plain": [ 761 | "45" 762 | ] 763 | }, 764 | "execution_count": 42, 765 | "metadata": {}, 766 | "output_type": "execute_result" 767 | } 768 | ], 769 | "source": [ 770 | "#remove a particular pair using its key\n", 771 | "dict1.pop('new_key')" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 43, 777 | "id": "c9952430-5a07-4fdb-8548-d104c29333e1", 778 | "metadata": {}, 779 | "outputs": [ 780 | { 781 | "data": { 782 | "text/plain": [ 783 | "{'name': 'Shikhar',\n", 784 | " 'place': 'India',\n", 785 | " 'roles': ['Data Scientist', 'Data Science Trainer'],\n", 786 | " 'hobbies': ('Fitness', 'Football'),\n", 787 | " 'education': {1: 'Masters', 2: 'Bachelors'}}" 788 | ] 789 | }, 790 | "execution_count": 43, 791 | "metadata": {}, 792 | "output_type": "execute_result" 793 | } 794 | ], 795 | "source": [ 796 | "dict1" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": 44, 802 | "id": "d0faa08e-6baf-411a-bacb-f27dd344bd56", 803 | "metadata": {}, 804 | "outputs": [], 805 | "source": [ 806 | "#alternate method of deleting a pair using del keyword\n", 807 | "del dict1['place']" 808 | ] 809 | }, 810 | { 811 | "cell_type": "markdown", 812 | "id": "74d615d9-2a36-44a0-ac51-82781c8e9346", 813 | "metadata": {}, 814 | "source": [ 815 | "#### Some Dictionary Methods" 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": 45, 821 | "id": "b683a94b-9efd-488d-98c6-c9e43230f668", 822 | "metadata": {}, 823 | "outputs": [], 824 | "source": [ 825 | "#returns a copy of the dictionary dict1\n", 826 | "dict1_copy = dict1.copy()" 827 | ] 828 | }, 829 | { 830 | "cell_type": "code", 831 | "execution_count": 46, 832 | "id": "b6bbc840-843a-4fbe-95cb-a0d96e6bc2a3", 833 | "metadata": {}, 834 | "outputs": [ 835 | { 836 | "data": { 837 | "text/plain": [ 838 | "dict_keys(['name', 'roles', 'hobbies', 'education'])" 839 | ] 840 | }, 841 | "execution_count": 46, 842 | "metadata": {}, 843 | "output_type": "execute_result" 844 | } 845 | ], 846 | "source": [ 847 | "#returns a list of keys of dictionary\n", 848 | "dict1.keys()" 849 | ] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": 47, 854 | "id": "8223ec7e-8d5d-421c-beb8-7e09de5156e3", 855 | "metadata": {}, 856 | "outputs": [ 857 | { 858 | "data": { 859 | "text/plain": [ 860 | "dict_values(['Shikhar', ['Data Scientist', 'Data Science Trainer'], ('Fitness', 'Football'), {1: 'Masters', 2: 'Bachelors'}])" 861 | ] 862 | }, 863 | "execution_count": 47, 864 | "metadata": {}, 865 | "output_type": "execute_result" 866 | } 867 | ], 868 | "source": [ 869 | "#returns a list of values of dictionary\n", 870 | "dict1.values()" 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "execution_count": 48, 876 | "id": "53c789e8-2893-4829-8bb7-1fa4ea45270c", 877 | "metadata": {}, 878 | "outputs": [ 879 | { 880 | "data": { 881 | "text/plain": [ 882 | "dict_items([('name', 'Shikhar'), ('roles', ['Data Scientist', 'Data Science Trainer']), ('hobbies', ('Fitness', 'Football')), ('education', {1: 'Masters', 2: 'Bachelors'})])" 883 | ] 884 | }, 885 | "execution_count": 48, 886 | "metadata": {}, 887 | "output_type": "execute_result" 888 | } 889 | ], 890 | "source": [ 891 | "#Returns a list containing a tuple for each key value pair\n", 892 | "dict1.items()" 893 | ] 894 | }, 895 | { 896 | "cell_type": "code", 897 | "execution_count": null, 898 | "id": "096df15b-a380-4077-88c7-d0b2230c419d", 899 | "metadata": {}, 900 | "outputs": [], 901 | "source": [] 902 | } 903 | ], 904 | "metadata": { 905 | "kernelspec": { 906 | "display_name": "Python 3 (ipykernel)", 907 | "language": "python", 908 | "name": "python3" 909 | }, 910 | "language_info": { 911 | "codemirror_mode": { 912 | "name": "ipython", 913 | "version": 3 914 | }, 915 | "file_extension": ".py", 916 | "mimetype": "text/x-python", 917 | "name": "python", 918 | "nbconvert_exporter": "python", 919 | "pygments_lexer": "ipython3", 920 | "version": "3.11.7" 921 | } 922 | }, 923 | "nbformat": 4, 924 | "nbformat_minor": 5 925 | } 926 | --------------------------------------------------------------------------------