├── Learn_Python_in_2_hr.ipynb └── README.md /Learn_Python_in_2_hr.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMPxeZBFgNzNUz+lcmtmj31", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "NgayMd7cK3U6" 33 | }, 34 | "source": [ 35 | "---\n", 36 | "# **Learn Python in 2 hr**\n", 37 | "---\n", 38 | "\n", 39 | "### There are 16 programs to explain the various concepts in python programming such as:\n", 40 | "\n", 41 | "- Syntex,\n", 42 | "- Loop,\n", 43 | "- if-else,\n", 44 | "- Data Structures,\n", 45 | "- Strings,\n", 46 | "- File Handaling,\n", 47 | "- Exception Handaling,\n", 48 | "- Random Numbers,\n", 49 | "- Command Line Argunment\n", 50 | "- Use of Libraries\n", 51 | "\n", 52 | "---\n", 53 | "### **Self learning resource**\n", 54 | "Tutorial on Python (Byte of Python) ** Click Here**\n", 55 | "\n", 56 | "\n", 57 | "```\n", 58 | "\n", 59 | "\n", 60 | "\n", 61 | "```\n", 62 | "\n", 63 | "\n", 64 | "\n", 65 | "\n", 66 | "---\n", 67 | "# **1 Hello World**\n", 68 | "---\n", 69 | "**Learning:** How to print and run python program" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "metadata": { 75 | "colab": { 76 | "base_uri": "https://localhost:8080/" 77 | }, 78 | "id": "e4JFo1WM8ZON", 79 | "outputId": "d273fb9b-8bda-4c9c-c9f8-70f05f146045" 80 | }, 81 | "source": [ 82 | "print (\"Hello World\")" 83 | ], 84 | "execution_count": 1, 85 | "outputs": [ 86 | { 87 | "output_type": "stream", 88 | "name": "stdout", 89 | "text": [ 90 | "Hello World\n" 91 | ] 92 | } 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "id": "oBs7uRGQPn3J" 99 | }, 100 | "source": [ 101 | "**Assingment 1.1**: WAP to print your name three times" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": { 107 | "id": "EOp1wf9hlyC2" 108 | }, 109 | "source": [ 110 | "---\n", 111 | "# **2 Add numbers and Concatinate strings**\n", 112 | "**Learning:** How to declare variable, add, concatenate and print the result.\n", 113 | "\n", 114 | "---\n", 115 | "### **2.1 Add two numbers**\n" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "metadata": { 121 | "id": "sk_6l4N6l-vH", 122 | "colab": { 123 | "base_uri": "https://localhost:8080/" 124 | }, 125 | "outputId": "ad8a9c94-8769-4539-fa7f-af7b7ba74a75" 126 | }, 127 | "source": [ 128 | "a = 10\n", 129 | "b = 220\n", 130 | "c = a + b # Add two numbers\n", 131 | "print (a, \" + \", b, \" --> \", c)" 132 | ], 133 | "execution_count": 2, 134 | "outputs": [ 135 | { 136 | "output_type": "stream", 137 | "name": "stdout", 138 | "text": [ 139 | "10 + 220 --> 230\n" 140 | ] 141 | } 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": { 147 | "id": "iGXiAQqI4AvC" 148 | }, 149 | "source": [ 150 | "\n", 151 | "---\n", 152 | "### **2.2 Concatinate two strings**\n", 153 | "---" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "metadata": { 159 | "id": "o-kCSsW7lxNF", 160 | "colab": { 161 | "base_uri": "https://localhost:8080/" 162 | }, 163 | "outputId": "24a5bcd7-9c0e-40cc-a40b-a7b8a8dc2a81" 164 | }, 165 | "source": [ 166 | "a = \"Bhagat\"\n", 167 | "b = \" Singh\"\n", 168 | "c = a + b\t\t# Concatinate two strings\n", 169 | "print (a, \" + \", b, \" --> \", c)" 170 | ], 171 | "execution_count": 3, 172 | "outputs": [ 173 | { 174 | "output_type": "stream", 175 | "name": "stdout", 176 | "text": [ 177 | "Bhagat + Singh --> Bhagat Singh\n" 178 | ] 179 | } 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": { 185 | "id": "DOZfIRp04ZvM" 186 | }, 187 | "source": [ 188 | "\n", 189 | "---\n", 190 | "### **2.3 Concatinate string with number**\n", 191 | "---" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "metadata": { 197 | "colab": { 198 | "base_uri": "https://localhost:8080/" 199 | }, 200 | "id": "BuJp15Fd4ZvS", 201 | "outputId": "007d503e-63ca-4a93-f7c0-6af6188f3916" 202 | }, 203 | "source": [ 204 | "a = \"Bhagat\"\n", 205 | "b = 100\n", 206 | "c = a + str(b)\t\t# Concatinate string with number\n", 207 | "print (a, \" + \", b, \" --> \", c)\n" 208 | ], 209 | "execution_count": 4, 210 | "outputs": [ 211 | { 212 | "output_type": "stream", 213 | "name": "stdout", 214 | "text": [ 215 | "Bhagat + 100 --> Bhagat100\n" 216 | ] 217 | } 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": { 223 | "id": "hA7PLi-m4V8s" 224 | }, 225 | "source": [ 226 | "**Assingment 2.1**: WAP to add three numbers and print the result.\n", 227 | "\n", 228 | "**Assingment 2.2**: WAP to concatinate three strings and print the result." 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "id": "z1uDaVtK5tX_" 235 | }, 236 | "source": [ 237 | "---\n", 238 | "# **3 Input from user**\n", 239 | "**Learning:** How to take input from user\n", 240 | "\n", 241 | "---\n", 242 | "### **3.1 Input two strings from user and concatinate them**\n", 243 | "---" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "metadata": { 249 | "id": "aDsOjDWs5tYB", 250 | "colab": { 251 | "base_uri": "https://localhost:8080/" 252 | }, 253 | "outputId": "5b98efbd-99c5-48bf-9e0d-6cf7a07dd4ae" 254 | }, 255 | "source": [ 256 | "a = input(\"Enter First String: \")\n", 257 | "b = input(\"Enter Second String: \")\n", 258 | "c = a + b\t\t# concatinate two strings\n", 259 | "print (a, \" + \", b, \" --> \", c)\n", 260 | "\n", 261 | "# Run the program with (1) Two strings and (2) Two numbers" 262 | ], 263 | "execution_count": 5, 264 | "outputs": [ 265 | { 266 | "output_type": "stream", 267 | "name": "stdout", 268 | "text": [ 269 | "Enter First String: Shyam\n", 270 | "Enter Second String: Patiala\n", 271 | "Shyam + Patiala --> ShyamPatiala\n" 272 | ] 273 | } 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": { 279 | "id": "4CAypM6A5Jmx" 280 | }, 281 | "source": [ 282 | "---\n", 283 | "### **3.2 Input two numbers from user and add them**\n", 284 | "---" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "metadata": { 290 | "id": "EzB8G_a95d4X", 291 | "colab": { 292 | "base_uri": "https://localhost:8080/" 293 | }, 294 | "outputId": "c0898fe6-c492-41b4-add8-f3506f39cdb2" 295 | }, 296 | "source": [ 297 | "a = int(input(\"Enter First No: \"))\n", 298 | "b = int(input(\"Enter Second No: \"))\n", 299 | "c = a + b\n", 300 | "print (a, \" + \", b, \" --> \", c)" 301 | ], 302 | "execution_count": 6, 303 | "outputs": [ 304 | { 305 | "output_type": "stream", 306 | "name": "stdout", 307 | "text": [ 308 | "Enter First No: 45\n", 309 | "Enter Second No: 87\n", 310 | "45 + 87 --> 132\n" 311 | ] 312 | } 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": { 318 | "id": "VCXIhMtP-puV" 319 | }, 320 | "source": [ 321 | "---\n", 322 | "# **4 Loop**\n", 323 | "**Learning:** Learn various loops.\n", 324 | "\n", 325 | "---\n", 326 | "### **4.1 While Loop**" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "metadata": { 332 | "colab": { 333 | "base_uri": "https://localhost:8080/" 334 | }, 335 | "id": "ESdyGeh9-puX", 336 | "outputId": "651c84be-37a5-4f32-efdd-0fcd9d622add" 337 | }, 338 | "source": [ 339 | "i=1\n", 340 | "while i <= 10:\n", 341 | "\tprint (i)\n", 342 | "\ti = i+1" 343 | ], 344 | "execution_count": 7, 345 | "outputs": [ 346 | { 347 | "output_type": "stream", 348 | "name": "stdout", 349 | "text": [ 350 | "1\n", 351 | "2\n", 352 | "3\n", 353 | "4\n", 354 | "5\n", 355 | "6\n", 356 | "7\n", 357 | "8\n", 358 | "9\n", 359 | "10\n" 360 | ] 361 | } 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "id": "rd_F8ksi-puZ" 368 | }, 369 | "source": [ 370 | "---\n", 371 | "### **4.2 Range Function**\n", 372 | "---" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "metadata": { 378 | "colab": { 379 | "base_uri": "https://localhost:8080/" 380 | }, 381 | "id": "_U_Of2tu-puZ", 382 | "outputId": "71ed3d34-c176-4e99-ae1c-8fb590df276e" 383 | }, 384 | "source": [ 385 | "print (\"range(10) --> \", list(range(10)))\n", 386 | "print (\"range(10,20) --> \", list(range(10,20)))\n", 387 | "print (\"range(0,20,2) --> \", list(range(2,20,2)))\n", 388 | "print (\"range(-10,-20,2) --> \", list(range(-10,-20,2)))\n", 389 | "print (\"range(-10,-20,-2)--> \", list(range(-10,-20,-2)))" 390 | ], 391 | "execution_count": 8, 392 | "outputs": [ 393 | { 394 | "output_type": "stream", 395 | "name": "stdout", 396 | "text": [ 397 | "range(10) --> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 398 | "range(10,20) --> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", 399 | "range(0,20,2) --> [2, 4, 6, 8, 10, 12, 14, 16, 18]\n", 400 | "range(-10,-20,2) --> []\n", 401 | "range(-10,-20,-2)--> [-10, -12, -14, -16, -18]\n" 402 | ] 403 | } 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": { 409 | "id": "Xny977qN_f_f" 410 | }, 411 | "source": [ 412 | "---\n", 413 | "### **4.3 For loop**\n", 414 | "---\n", 415 | "### **4.3.1 For loop - Version 1**" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "metadata": { 421 | "colab": { 422 | "base_uri": "https://localhost:8080/" 423 | }, 424 | "id": "JXsAol2L_f_h", 425 | "outputId": "d1356079-e874-469f-e42d-161b122646ee" 426 | }, 427 | "source": [ 428 | "for i in range(0,10):\n", 429 | "\tprint (i)" 430 | ], 431 | "execution_count": 9, 432 | "outputs": [ 433 | { 434 | "output_type": "stream", 435 | "name": "stdout", 436 | "text": [ 437 | "0\n", 438 | "1\n", 439 | "2\n", 440 | "3\n", 441 | "4\n", 442 | "5\n", 443 | "6\n", 444 | "7\n", 445 | "8\n", 446 | "9\n" 447 | ] 448 | } 449 | ] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": { 454 | "id": "Xk5XRRV8_gMq" 455 | }, 456 | "source": [ 457 | "---\n", 458 | "### **4.3.2 For loop - Version 2**\n", 459 | "---" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "metadata": { 465 | "colab": { 466 | "base_uri": "https://localhost:8080/" 467 | }, 468 | "id": "w8scyduB_gMr", 469 | "outputId": "67c2a0bd-b5da-4fb1-ef96-f2203d355a05" 470 | }, 471 | "source": [ 472 | "for i in range(0,20,2):\n", 473 | "\tprint (i)" 474 | ], 475 | "execution_count": 10, 476 | "outputs": [ 477 | { 478 | "output_type": "stream", 479 | "name": "stdout", 480 | "text": [ 481 | "0\n", 482 | "2\n", 483 | "4\n", 484 | "6\n", 485 | "8\n", 486 | "10\n", 487 | "12\n", 488 | "14\n", 489 | "16\n", 490 | "18\n" 491 | ] 492 | } 493 | ] 494 | }, 495 | { 496 | "cell_type": "markdown", 497 | "metadata": { 498 | "id": "_NNQrHRXAZ8a" 499 | }, 500 | "source": [ 501 | "---\n", 502 | "### **4.3.3 For loop - Version 3**\n", 503 | "---" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "metadata": { 509 | "colab": { 510 | "base_uri": "https://localhost:8080/" 511 | }, 512 | "id": "18Im8yX7AZ8a", 513 | "outputId": "aaf4774f-0544-40f6-843b-619d00382cbd" 514 | }, 515 | "source": [ 516 | "for i in range(0,-10,-1):\n", 517 | "\tprint (i)" 518 | ], 519 | "execution_count": 11, 520 | "outputs": [ 521 | { 522 | "output_type": "stream", 523 | "name": "stdout", 524 | "text": [ 525 | "0\n", 526 | "-1\n", 527 | "-2\n", 528 | "-3\n", 529 | "-4\n", 530 | "-5\n", 531 | "-6\n", 532 | "-7\n", 533 | "-8\n", 534 | "-9\n" 535 | ] 536 | } 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "metadata": { 542 | "id": "Meq_Hv4RAaHS" 543 | }, 544 | "source": [ 545 | "---\n", 546 | "### **4.4 Print table of 5**\n", 547 | "---" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "metadata": { 553 | "colab": { 554 | "base_uri": "https://localhost:8080/" 555 | }, 556 | "id": "EvPN2wvOAaHT", 557 | "outputId": "f7dd6db3-17e6-4be9-c5db-8a6dccc5a685" 558 | }, 559 | "source": [ 560 | "for i in range(1,11):\n", 561 | "\tprint (5,\" * \", i , \" = \", i * 5)" 562 | ], 563 | "execution_count": 12, 564 | "outputs": [ 565 | { 566 | "output_type": "stream", 567 | "name": "stdout", 568 | "text": [ 569 | "5 * 1 = 5\n", 570 | "5 * 2 = 10\n", 571 | "5 * 3 = 15\n", 572 | "5 * 4 = 20\n", 573 | "5 * 5 = 25\n", 574 | "5 * 6 = 30\n", 575 | "5 * 7 = 35\n", 576 | "5 * 8 = 40\n", 577 | "5 * 9 = 45\n", 578 | "5 * 10 = 50\n" 579 | ] 580 | } 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": { 586 | "id": "aZErlsXYAaPu" 587 | }, 588 | "source": [ 589 | "---\n", 590 | "### **4.5 Sum all numbers from 1 to 10**\n", 591 | "---\n", 592 | "### **4.5.1 Version 1**" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "metadata": { 598 | "colab": { 599 | "base_uri": "https://localhost:8080/" 600 | }, 601 | "id": "ddCg6qxQAaPv", 602 | "outputId": "0747add6-95f0-47e9-ef94-3cfbf5bf3596" 603 | }, 604 | "source": [ 605 | "s=0\n", 606 | "for i in range(1,11):\n", 607 | "\ts=s+i\n", 608 | "print (\"Sum is --> \",s)" 609 | ], 610 | "execution_count": 13, 611 | "outputs": [ 612 | { 613 | "output_type": "stream", 614 | "name": "stdout", 615 | "text": [ 616 | "Sum is --> 55\n" 617 | ] 618 | } 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": { 624 | "id": "UucWFIAt_gV_" 625 | }, 626 | "source": [ 627 | "---\n", 628 | "### **4.5.2 Version 2**\n", 629 | "---" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "metadata": { 635 | "colab": { 636 | "base_uri": "https://localhost:8080/" 637 | }, 638 | "id": "cD3y-1lM_gWB", 639 | "outputId": "923a950b-06f3-40ea-b0ca-c06f4c7e990e" 640 | }, 641 | "source": [ 642 | "print (\"Sum is --> \", sum(range(1,11)))" 643 | ], 644 | "execution_count": 14, 645 | "outputs": [ 646 | { 647 | "output_type": "stream", 648 | "name": "stdout", 649 | "text": [ 650 | "Sum is --> 55\n" 651 | ] 652 | } 653 | ] 654 | }, 655 | { 656 | "cell_type": "markdown", 657 | "metadata": { 658 | "id": "DnUEH0WQ-pua" 659 | }, 660 | "source": [ 661 | "**Assingment 4.1:** WAP to print the table of 7, 9.\n", 662 | "\n", 663 | "**Assingment 4.2:** WAP to print the table of n and n is given by user.\n", 664 | "\n", 665 | "**Assingment 4.3:** WAP to add all the numbers from 1 to n and n is given by user." 666 | ] 667 | }, 668 | { 669 | "cell_type": "markdown", 670 | "metadata": { 671 | "id": "6eraxEhZCJRU" 672 | }, 673 | "source": [ 674 | "---\n", 675 | "# **5 If-Else - Conditional Checking**\n", 676 | "**Learning:** if-else Condition\n", 677 | "\n", 678 | "---\n", 679 | "### **5.1 Input two numbers from user and compare them**" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "metadata": { 685 | "colab": { 686 | "base_uri": "https://localhost:8080/" 687 | }, 688 | "id": "63a8CKQ9CJRV", 689 | "outputId": "9d304c28-608b-41f3-d342-61ab3856192b" 690 | }, 691 | "source": [ 692 | "a = int(input(\"Enter First No: \"))\n", 693 | "b = int(input(\"Enter Second No: \"))\n", 694 | "if a > b:\n", 695 | "\tprint (a,\" > \",b)\n", 696 | "else:\n", 697 | "\tprint (a,\" < \",b)" 698 | ], 699 | "execution_count": 15, 700 | "outputs": [ 701 | { 702 | "output_type": "stream", 703 | "name": "stdout", 704 | "text": [ 705 | "Enter First No: 43\n", 706 | "Enter Second No: 21\n", 707 | "43 > 21\n" 708 | ] 709 | } 710 | ] 711 | }, 712 | { 713 | "cell_type": "markdown", 714 | "metadata": { 715 | "id": "zPayD7lwCJRW" 716 | }, 717 | "source": [ 718 | "---\n", 719 | "### **5.2 Check weather a number is odd or even**\n", 720 | "---" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "metadata": { 726 | "colab": { 727 | "base_uri": "https://localhost:8080/" 728 | }, 729 | "id": "I2ohfxlRCJRW", 730 | "outputId": "0eb1d424-269e-4fcc-94a2-be692a2fd971" 731 | }, 732 | "source": [ 733 | "n = int(input(\"Enter a No: \"))\n", 734 | "if n % 2 == 0:\n", 735 | "\tprint (n,\" is even\")\n", 736 | "else:\n", 737 | "\tprint (n,\" is odd\")" 738 | ], 739 | "execution_count": 16, 740 | "outputs": [ 741 | { 742 | "output_type": "stream", 743 | "name": "stdout", 744 | "text": [ 745 | "Enter a No: 23\n", 746 | "23 is odd\n" 747 | ] 748 | } 749 | ] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "metadata": { 754 | "id": "861tynfFCJRX" 755 | }, 756 | "source": [ 757 | "---\n", 758 | "### **5.3 Check weather a number is prime of not**\n", 759 | "---" 760 | ] 761 | }, 762 | { 763 | "cell_type": "code", 764 | "metadata": { 765 | "colab": { 766 | "base_uri": "https://localhost:8080/" 767 | }, 768 | "id": "q37Sy99JCJRZ", 769 | "outputId": "ba1dbaa4-4de1-4a39-b2cd-5494bdf06eac" 770 | }, 771 | "source": [ 772 | "n = int(input(\"Enter a No: \"))\n", 773 | "f=0\n", 774 | "for i in range(2, n//2 + 1):\n", 775 | "\tif n % i == 0:\n", 776 | "\t\tf=1\n", 777 | "\t\tbreak\n", 778 | "\n", 779 | "if f==0:\n", 780 | "\tprint (\"Prime\")\n", 781 | "else:\n", 782 | "\tprint (\"Not Prime\")" 783 | ], 784 | "execution_count": 17, 785 | "outputs": [ 786 | { 787 | "output_type": "stream", 788 | "name": "stdout", 789 | "text": [ 790 | "Enter a No: 45\n", 791 | "Not Prime\n" 792 | ] 793 | } 794 | ] 795 | }, 796 | { 797 | "cell_type": "markdown", 798 | "metadata": { 799 | "id": "YCf466JQCJRa" 800 | }, 801 | "source": [ 802 | "---\n", 803 | "### **5.4 Conditional Checking - Compare strings**\n", 804 | "---" 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "metadata": { 810 | "colab": { 811 | "base_uri": "https://localhost:8080/" 812 | }, 813 | "id": "YSTLUkUVCJRb", 814 | "outputId": "f2e0d94d-fcb3-4e56-9656-11731a1ea696" 815 | }, 816 | "source": [ 817 | "a = input(\"Enter First String : \")\n", 818 | "b = input(\"Enter Second String: \")\n", 819 | "\n", 820 | "if a == b:\n", 821 | "\tprint (\"a == b\")\n", 822 | "elif a >= b:\n", 823 | "\tprint (\"a > b\")\n", 824 | "else:\n", 825 | "\tprint (\"a < b\")" 826 | ], 827 | "execution_count": 18, 828 | "outputs": [ 829 | { 830 | "output_type": "stream", 831 | "name": "stdout", 832 | "text": [ 833 | "Enter First String : User\n", 834 | "Enter Second String: Friend\n", 835 | "a > b\n" 836 | ] 837 | } 838 | ] 839 | }, 840 | { 841 | "cell_type": "markdown", 842 | "metadata": { 843 | "id": "bPPQtf1ACJRg" 844 | }, 845 | "source": [ 846 | "**Assingment 5.1:** WAP to find max amoung three numbers and input from user. [Try max() function]\n", 847 | "\n", 848 | "**Assingment 5.2:** WAP to add all numbers divisible by 7 and 9 from 1 to n and n is given by the user.\n", 849 | "\n", 850 | "**Assingment 5.3:** WAP to add all prime numbers from 1 to n and n is given by the user.\n" 851 | ] 852 | }, 853 | { 854 | "cell_type": "markdown", 855 | "metadata": { 856 | "id": "_dC1CuVnDipZ" 857 | }, 858 | "source": [ 859 | "---\n", 860 | "# **6 Functions**\n", 861 | "**Learning:** How to declare and call function\n", 862 | "\n", 863 | "---\n", 864 | "### **6.1 Add two numbers**" 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "metadata": { 870 | "colab": { 871 | "base_uri": "https://localhost:8080/" 872 | }, 873 | "id": "_Vw-ZYlDDipb", 874 | "outputId": "79091235-9b36-4bf2-f969-c9d63f28987b" 875 | }, 876 | "source": [ 877 | "def Add(a,b):\n", 878 | "\tc=a+b\n", 879 | "\treturn c\n", 880 | "\n", 881 | "print (\"Add(10,20) -->\", Add(10,20))\n", 882 | "print (\"Add(20,50) -->\", Add(20,50))\n", 883 | "print (\"Add(80,200) -->\", Add(80,200))" 884 | ], 885 | "execution_count": 19, 886 | "outputs": [ 887 | { 888 | "output_type": "stream", 889 | "name": "stdout", 890 | "text": [ 891 | "Add(10,20) --> 30\n", 892 | "Add(20,50) --> 70\n", 893 | "Add(80,200) --> 280\n" 894 | ] 895 | } 896 | ] 897 | }, 898 | { 899 | "cell_type": "markdown", 900 | "metadata": { 901 | "id": "ygKAg2QjDipc" 902 | }, 903 | "source": [ 904 | "---\n", 905 | "### **6.2 Prime number**\n", 906 | "---" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "metadata": { 912 | "colab": { 913 | "base_uri": "https://localhost:8080/" 914 | }, 915 | "id": "sg8feM_YDipc", 916 | "outputId": "aedb6493-e8ef-4360-f6af-f401c0861dbc" 917 | }, 918 | "source": [ 919 | "def IsPrime(n):\n", 920 | "\tfor i in range(2, n//2 + 1):\n", 921 | "\t\tif n%i==0:\n", 922 | "\t\t\treturn 0\n", 923 | "\treturn 1\n", 924 | "\n", 925 | "print (\"IsPrime(20) --> \", IsPrime(20))\n", 926 | "print (\"IsPrime(23) --> \", IsPrime(23))\n", 927 | "print (\"IsPrime(200) --> \", IsPrime(200))\n", 928 | "print (\"IsPrime(37) --> \", IsPrime(37))" 929 | ], 930 | "execution_count": 20, 931 | "outputs": [ 932 | { 933 | "output_type": "stream", 934 | "name": "stdout", 935 | "text": [ 936 | "IsPrime(20) --> 0\n", 937 | "IsPrime(23) --> 1\n", 938 | "IsPrime(200) --> 0\n", 939 | "IsPrime(37) --> 1\n" 940 | ] 941 | } 942 | ] 943 | }, 944 | { 945 | "cell_type": "markdown", 946 | "metadata": { 947 | "id": "cMBI2zEJDipd" 948 | }, 949 | "source": [ 950 | "---\n", 951 | "### **6.3 Add 1 to n**\n", 952 | "---" 953 | ] 954 | }, 955 | { 956 | "cell_type": "code", 957 | "metadata": { 958 | "colab": { 959 | "base_uri": "https://localhost:8080/" 960 | }, 961 | "id": "OdcwPL2BDipe", 962 | "outputId": "15ca7726-c737-4d8d-bcc4-8d041c555308" 963 | }, 964 | "source": [ 965 | "def AddN(n):\n", 966 | "\ts= sum(range(n+1))\n", 967 | "\treturn s\n", 968 | "\n", 969 | "print (\"AddN(10) --> \", AddN(10))\n", 970 | "print (\"AddN(20) --> \", AddN(20))\n", 971 | "print (\"AddN(50) --> \", AddN(50))\n", 972 | "print (\"AddN(200) --> \", AddN(200))" 973 | ], 974 | "execution_count": 21, 975 | "outputs": [ 976 | { 977 | "output_type": "stream", 978 | "name": "stdout", 979 | "text": [ 980 | "AddN(10) --> 55\n", 981 | "AddN(20) --> 210\n", 982 | "AddN(50) --> 1275\n", 983 | "AddN(200) --> 20100\n" 984 | ] 985 | } 986 | ] 987 | }, 988 | { 989 | "cell_type": "markdown", 990 | "metadata": { 991 | "id": "bWcn3nSlDipg" 992 | }, 993 | "source": [ 994 | "\n", 995 | "**Assingment 6.1:** WAP using function that add all odd numbers from 1 to n, n is given by the user.\n", 996 | "\n", 997 | "**Assingment 6.2:** WAP using function that add all prime numbers from 1 to n, n given by the user." 998 | ] 999 | }, 1000 | { 1001 | "cell_type": "markdown", 1002 | "metadata": { 1003 | "id": "n6nFmz3zEfuq" 1004 | }, 1005 | "source": [ 1006 | "---\n", 1007 | "# **7 Math library**\n", 1008 | "**Learning:** Use math library\n", 1009 | "\n", 1010 | "---" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "metadata": { 1016 | "colab": { 1017 | "base_uri": "https://localhost:8080/" 1018 | }, 1019 | "id": "qLiy6UxaEfus", 1020 | "outputId": "429b9892-fc68-4232-aafa-8249d6b55d22" 1021 | }, 1022 | "source": [ 1023 | "import math as m\n", 1024 | "print (\"exp(-200) --> \", m.exp(-200)) # Exponential function\n", 1025 | "print (\"log(100,2) --> \", m.log(100,2)) # Log\n", 1026 | "print (\"log(100,10) --> \", m.log(100,10))# Log\n", 1027 | "print (\"log10(100) --> \", m.log10(100)) # Log 10\n", 1028 | "print (\"m.cos(30) --> \", m.cos(30)) # cos\n", 1029 | "print (\"m.sin(30) --> \", m.sin(30)) # sin\n", 1030 | "print (\"m.tan(30) --> \", m.tan(30)) # tan\n", 1031 | "print (\"m.sqrt(324) --> \", m.sqrt(324))\n", 1032 | "print (\"m.ceil(89.9) --> \", m.ceil(89.9))\n", 1033 | "print (\"m.floor(89.9)--> \", m.floor(89.9))" 1034 | ], 1035 | "execution_count": 22, 1036 | "outputs": [ 1037 | { 1038 | "output_type": "stream", 1039 | "name": "stdout", 1040 | "text": [ 1041 | "exp(-200) --> 1.3838965267367376e-87\n", 1042 | "log(100,2) --> 6.643856189774725\n", 1043 | "log(100,10) --> 2.0\n", 1044 | "log10(100) --> 2.0\n", 1045 | "m.cos(30) --> 0.15425144988758405\n", 1046 | "m.sin(30) --> -0.9880316240928618\n", 1047 | "m.tan(30) --> -6.405331196646276\n", 1048 | "m.sqrt(324) --> 18.0\n", 1049 | "m.ceil(89.9) --> 90\n", 1050 | "m.floor(89.9)--> 89\n" 1051 | ] 1052 | } 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "markdown", 1057 | "metadata": { 1058 | "id": "uuGWj5Q9E7na" 1059 | }, 1060 | "source": [ 1061 | "---\n", 1062 | "# **8 Strings**\n", 1063 | "**Learning:** How to handle string\n", 1064 | "\n", 1065 | "---\n", 1066 | "### **8.1 Indexing in string**" 1067 | ] 1068 | }, 1069 | { 1070 | "cell_type": "code", 1071 | "metadata": { 1072 | "colab": { 1073 | "base_uri": "https://localhost:8080/" 1074 | }, 1075 | "id": "1OvIOp21E7nc", 1076 | "outputId": "cf487bb9-6d52-4a62-c7fe-fd676cbe4e85" 1077 | }, 1078 | "source": [ 1079 | "var = 'Hello World!'\n", 1080 | "print (\"var --> \", var)\n", 1081 | "print (\"var[0] --> \", var[0])\n", 1082 | "print (\"var[1:5] --> \", var[1:5])\n", 1083 | "print (\"var[:-5] --> \", var[:-5])" 1084 | ], 1085 | "execution_count": 23, 1086 | "outputs": [ 1087 | { 1088 | "output_type": "stream", 1089 | "name": "stdout", 1090 | "text": [ 1091 | "var --> Hello World!\n", 1092 | "var[0] --> H\n", 1093 | "var[1:5] --> ello\n", 1094 | "var[:-5] --> Hello W\n" 1095 | ] 1096 | } 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "markdown", 1101 | "metadata": { 1102 | "id": "X9V-dQU-E7nd" 1103 | }, 1104 | "source": [ 1105 | "---\n", 1106 | "### **8.2 String length, upper, lower**\n", 1107 | "---" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "code", 1112 | "metadata": { 1113 | "colab": { 1114 | "base_uri": "https://localhost:8080/" 1115 | }, 1116 | "id": "y8HSuWkEE7ne", 1117 | "outputId": "ee360fd3-9fe9-4358-cd10-9d6e5082e726" 1118 | }, 1119 | "source": [ 1120 | "var = 'Hello World!'\n", 1121 | "print (\"String --> \", var)\n", 1122 | "print (\"Length --> : \", len(var))\n", 1123 | "print (\"Upper --> : \", var.upper())\n", 1124 | "print (\"Lower --> : \", var.lower())" 1125 | ], 1126 | "execution_count": 24, 1127 | "outputs": [ 1128 | { 1129 | "output_type": "stream", 1130 | "name": "stdout", 1131 | "text": [ 1132 | "String --> Hello World!\n", 1133 | "Length --> : 12\n", 1134 | "Upper --> : HELLO WORLD!\n", 1135 | "Lower --> : hello world!\n" 1136 | ] 1137 | } 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "markdown", 1142 | "metadata": { 1143 | "id": "4ySbLzf1Fpi5" 1144 | }, 1145 | "source": [ 1146 | "---\n", 1147 | "### **8.3 String formatting**\n", 1148 | "---" 1149 | ] 1150 | }, 1151 | { 1152 | "cell_type": "code", 1153 | "metadata": { 1154 | "colab": { 1155 | "base_uri": "https://localhost:8080/" 1156 | }, 1157 | "id": "RDB15138Fpi6", 1158 | "outputId": "0b06a86a-6a3d-428c-880d-818449b0c27b" 1159 | }, 1160 | "source": [ 1161 | "name=input(\"Enter your name: \")\n", 1162 | "age=int(input(\"Enter your age : \"))\n", 1163 | "price=float(input(\"Enter the book price: \"))\n", 1164 | "s=\"\\nYour name is %s, age is %d and book price is %f\" %(name.upper(),age,price)\n", 1165 | "print (s)" 1166 | ], 1167 | "execution_count": 25, 1168 | "outputs": [ 1169 | { 1170 | "output_type": "stream", 1171 | "name": "stdout", 1172 | "text": [ 1173 | "Enter your name: Suresh\n", 1174 | "Enter your age : 43\n", 1175 | "Enter the book price: 39.90\n", 1176 | "\n", 1177 | "Your name is SURESH, age is 43 and book price is 39.900000\n" 1178 | ] 1179 | } 1180 | ] 1181 | }, 1182 | { 1183 | "cell_type": "markdown", 1184 | "metadata": { 1185 | "id": "27Y8Dk7XFp1F" 1186 | }, 1187 | "source": [ 1188 | "---\n", 1189 | "### **8.4 String in Triple Quotes**\n", 1190 | "---" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "metadata": { 1196 | "colab": { 1197 | "base_uri": "https://localhost:8080/" 1198 | }, 1199 | "id": "jiOlVj37Fp1H", 1200 | "outputId": "b60ce422-8520-43f0-99bc-8ec6ba6c2f86" 1201 | }, 1202 | "source": [ 1203 | "para_str = \"\"\"This is a long string that is made up of\n", 1204 | "several lines and non-printable characters such as\n", 1205 | "TAB ( \\t ) and they will show up that way when displayed.\n", 1206 | "NEWLINEs within the string, whether explicitly given like\n", 1207 | "this within the brackets [ \\n ], or just a NEWLINE within\n", 1208 | "the variable assignment will also show up.\n", 1209 | "\"\"\"\n", 1210 | "print (para_str)" 1211 | ], 1212 | "execution_count": 26, 1213 | "outputs": [ 1214 | { 1215 | "output_type": "stream", 1216 | "name": "stdout", 1217 | "text": [ 1218 | "This is a long string that is made up of\n", 1219 | "several lines and non-printable characters such as\n", 1220 | "TAB ( \t ) and they will show up that way when displayed.\n", 1221 | "NEWLINEs within the string, whether explicitly given like\n", 1222 | "this within the brackets [ \n", 1223 | " ], or just a NEWLINE within\n", 1224 | "the variable assignment will also show up.\n", 1225 | "\n" 1226 | ] 1227 | } 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "markdown", 1232 | "metadata": { 1233 | "id": "WK4GTOeIFp-I" 1234 | }, 1235 | "source": [ 1236 | "---\n", 1237 | "### **8.5 String strip**\n", 1238 | "---" 1239 | ] 1240 | }, 1241 | { 1242 | "cell_type": "code", 1243 | "metadata": { 1244 | "colab": { 1245 | "base_uri": "https://localhost:8080/" 1246 | }, 1247 | "id": "tXJQREI3Fp-J", 1248 | "outputId": "9dac494c-98e8-45be-b1a4-bf881401b1da" 1249 | }, 1250 | "source": [ 1251 | "var =\" Indian Army \"\n", 1252 | "\n", 1253 | "print(\"String --> \", var)\n", 1254 | "print(\"Length --> \", len(var))\n", 1255 | "print(\"var strip --> \", var.strip())\n", 1256 | "print(\"Length of var after strip --> \", len(var.strip()))" 1257 | ], 1258 | "execution_count": 27, 1259 | "outputs": [ 1260 | { 1261 | "output_type": "stream", 1262 | "name": "stdout", 1263 | "text": [ 1264 | "String --> Indian Army \n", 1265 | "Length --> 18\n", 1266 | "var strip --> Indian Army\n", 1267 | "Length of var after strip --> 13\n" 1268 | ] 1269 | } 1270 | ] 1271 | }, 1272 | { 1273 | "cell_type": "markdown", 1274 | "metadata": { 1275 | "id": "_aSibOwQFqFC" 1276 | }, 1277 | "source": [ 1278 | "---\n", 1279 | "### **8.6 String split**\n", 1280 | "---" 1281 | ] 1282 | }, 1283 | { 1284 | "cell_type": "code", 1285 | "metadata": { 1286 | "colab": { 1287 | "base_uri": "https://localhost:8080/" 1288 | }, 1289 | "id": "02uwsbaQFqFD", 1290 | "outputId": "8ec20ab9-042a-45a0-f367-238a0ec93f47" 1291 | }, 1292 | "source": [ 1293 | "var =\" Indian, Army \"\n", 1294 | "\n", 1295 | "print(\"String --> \", var)\n", 1296 | "print(\"Length --> \", len(var))\n", 1297 | "print(\"var split --> \", var.split())\n", 1298 | "print(\"var split --> \", var.split(' '))\n", 1299 | "print(\"var split --> \", var.split(','))\n", 1300 | "\n", 1301 | "# Strip + Split\n", 1302 | "print(\"var split --> \", var.strip().split(','))\n" 1303 | ], 1304 | "execution_count": 28, 1305 | "outputs": [ 1306 | { 1307 | "output_type": "stream", 1308 | "name": "stdout", 1309 | "text": [ 1310 | "String --> Indian, Army \n", 1311 | "Length --> 19\n", 1312 | "var split --> ['Indian,', 'Army']\n", 1313 | "var split --> ['', 'Indian,', '', '', 'Army', '', '', '', '']\n", 1314 | "var split --> [' Indian', ' Army ']\n", 1315 | "var split --> ['Indian', ' Army']\n" 1316 | ] 1317 | } 1318 | ] 1319 | }, 1320 | { 1321 | "cell_type": "markdown", 1322 | "metadata": { 1323 | "id": "TgsO9gfoFqMV" 1324 | }, 1325 | "source": [ 1326 | "---\n", 1327 | "### **8.7 Count in string**\n", 1328 | "---" 1329 | ] 1330 | }, 1331 | { 1332 | "cell_type": "code", 1333 | "metadata": { 1334 | "colab": { 1335 | "base_uri": "https://localhost:8080/" 1336 | }, 1337 | "id": "lwrJzwy6FqMV", 1338 | "outputId": "793a7051-3e1b-4778-8a16-9cb392a5501d" 1339 | }, 1340 | "source": [ 1341 | "var=\" Indian Army \"\n", 1342 | "print (\"String --> \", var)\n", 1343 | "print (\"Count of ' ' --> \", var.count(' '))\n", 1344 | "print (\"Count of 'a' --> \", var.count('a'))\n", 1345 | "print (\"Count of 'n' --> \", var.count('an'))" 1346 | ], 1347 | "execution_count": 29, 1348 | "outputs": [ 1349 | { 1350 | "output_type": "stream", 1351 | "name": "stdout", 1352 | "text": [ 1353 | "String --> Indian Army \n", 1354 | "Count of ' ' --> 6\n", 1355 | "Count of 'a' --> 1\n", 1356 | "Count of 'n' --> 1\n" 1357 | ] 1358 | } 1359 | ] 1360 | }, 1361 | { 1362 | "cell_type": "markdown", 1363 | "metadata": { 1364 | "id": "RYP11CU5FqUJ" 1365 | }, 1366 | "source": [ 1367 | "---\n", 1368 | "### **8.8 Reverse a String**\n", 1369 | "---" 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "code", 1374 | "metadata": { 1375 | "colab": { 1376 | "base_uri": "https://localhost:8080/" 1377 | }, 1378 | "id": "QSXd_wfOFqUK", 1379 | "outputId": "186e72a0-999d-45f6-8614-1950b6cf6546" 1380 | }, 1381 | "source": [ 1382 | "var=\"Indian Army\"\n", 1383 | "print (\"String --> \", var)\n", 1384 | "print (\"var[::1] --> \", var[::1])\n", 1385 | "print (\"var[::2] --> \", var[::2])\n", 1386 | "print (\"var[::-1] --> \", var[::-1])\n", 1387 | "print (\"var[::-2] --> \", var[::-2])\n", 1388 | "\n", 1389 | "var=var[::-1]\n", 1390 | "print (\"var after reverse --> \", var)" 1391 | ], 1392 | "execution_count": 30, 1393 | "outputs": [ 1394 | { 1395 | "output_type": "stream", 1396 | "name": "stdout", 1397 | "text": [ 1398 | "String --> Indian Army\n", 1399 | "var[::1] --> Indian Army\n", 1400 | "var[::2] --> Ida ry\n", 1401 | "var[::-1] --> ymrA naidnI\n", 1402 | "var[::-2] --> yr adI\n", 1403 | "var after reverse --> ymrA naidnI\n" 1404 | ] 1405 | } 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "markdown", 1410 | "metadata": { 1411 | "id": "E1Ie2pJRGiNz" 1412 | }, 1413 | "source": [ 1414 | "---\n", 1415 | "### **8.9 Palindrome**\n", 1416 | "---" 1417 | ] 1418 | }, 1419 | { 1420 | "cell_type": "code", 1421 | "metadata": { 1422 | "colab": { 1423 | "base_uri": "https://localhost:8080/" 1424 | }, 1425 | "id": "-t2FMiSTGiN0", 1426 | "outputId": "1b7f3d89-4785-4e9a-e867-dea20969eedb" 1427 | }, 1428 | "source": [ 1429 | "s1=\"Indian Army\"\n", 1430 | "s2=\"malayalam\"\n", 1431 | "s3=\"madam\"\n", 1432 | "s4=\"teacher\"\n", 1433 | "print (\"s1 --> \", s1==s1[::-1])\n", 1434 | "print (\"s2 --> \", s2==s2[::-1])\n", 1435 | "print (\"s3 --> \", s3==s3[::-1])\n", 1436 | "print (\"s4 --> \", s4==s4[::-1])" 1437 | ], 1438 | "execution_count": 31, 1439 | "outputs": [ 1440 | { 1441 | "output_type": "stream", 1442 | "name": "stdout", 1443 | "text": [ 1444 | "s1 --> False\n", 1445 | "s2 --> True\n", 1446 | "s3 --> True\n", 1447 | "s4 --> False\n" 1448 | ] 1449 | } 1450 | ] 1451 | }, 1452 | { 1453 | "cell_type": "markdown", 1454 | "metadata": { 1455 | "id": "MpgFLAABG0Nn" 1456 | }, 1457 | "source": [ 1458 | "---\n", 1459 | "# **9 Random Numbers/String**\n", 1460 | "**Learning:** Generate Random Numbers/String\n", 1461 | "\n", 1462 | "---\n", 1463 | "### **9.1 Generate random number between 0 and 1**" 1464 | ] 1465 | }, 1466 | { 1467 | "cell_type": "code", 1468 | "metadata": { 1469 | "colab": { 1470 | "base_uri": "https://localhost:8080/" 1471 | }, 1472 | "id": "B2jZpOnRG0Nn", 1473 | "outputId": "c2620dd4-10c7-4a88-a925-315e0108880c" 1474 | }, 1475 | "source": [ 1476 | "import random as r\n", 1477 | "print (r.random())\n", 1478 | "print (r.random())\n", 1479 | "print (round(r.random(),4))" 1480 | ], 1481 | "execution_count": 32, 1482 | "outputs": [ 1483 | { 1484 | "output_type": "stream", 1485 | "name": "stdout", 1486 | "text": [ 1487 | "0.0836581959930992\n", 1488 | "0.5332908563946105\n", 1489 | "0.7008\n" 1490 | ] 1491 | } 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "markdown", 1496 | "metadata": { 1497 | "id": "wBI9otLPG0Nn" 1498 | }, 1499 | "source": [ 1500 | "---\n", 1501 | "### **9.2 Generate random integer number**\n", 1502 | "---" 1503 | ] 1504 | }, 1505 | { 1506 | "cell_type": "code", 1507 | "metadata": { 1508 | "colab": { 1509 | "base_uri": "https://localhost:8080/" 1510 | }, 1511 | "id": "72NOmyH-G0No", 1512 | "outputId": "e8d51f6b-666b-4f46-95ae-d1f217efba95" 1513 | }, 1514 | "source": [ 1515 | "import random as r\n", 1516 | "print (r.randint(1, 100))\n", 1517 | "print (r.randint(1, 100))\n", 1518 | "print (r.randint(-10, 10))\n", 1519 | "print (r.randint(-10, 10))" 1520 | ], 1521 | "execution_count": 33, 1522 | "outputs": [ 1523 | { 1524 | "output_type": "stream", 1525 | "name": "stdout", 1526 | "text": [ 1527 | "58\n", 1528 | "1\n", 1529 | "5\n", 1530 | "1\n" 1531 | ] 1532 | } 1533 | ] 1534 | }, 1535 | { 1536 | "cell_type": "markdown", 1537 | "metadata": { 1538 | "id": "gFTecYJvG0No" 1539 | }, 1540 | "source": [ 1541 | "---\n", 1542 | "### **9.3 Generate random real number**\n", 1543 | "---" 1544 | ] 1545 | }, 1546 | { 1547 | "cell_type": "code", 1548 | "metadata": { 1549 | "colab": { 1550 | "base_uri": "https://localhost:8080/" 1551 | }, 1552 | "id": "vs3jmtqtG0No", 1553 | "outputId": "534b24b5-e497-423c-803a-85aece872d51" 1554 | }, 1555 | "source": [ 1556 | "import random as r\n", 1557 | "print (r.uniform(1, 100))\n", 1558 | "print (r.uniform(1, 100))\n", 1559 | "print (r.uniform (-10, 10))\n", 1560 | "print (r.uniform (-10, 10))\n", 1561 | "print (round(r.uniform (-10, 10),2))" 1562 | ], 1563 | "execution_count": 34, 1564 | "outputs": [ 1565 | { 1566 | "output_type": "stream", 1567 | "name": "stdout", 1568 | "text": [ 1569 | "47.44499207264831\n", 1570 | "83.6664852763471\n", 1571 | "1.430478777556715\n", 1572 | "-4.218801688238902\n", 1573 | "7.84\n" 1574 | ] 1575 | } 1576 | ] 1577 | }, 1578 | { 1579 | "cell_type": "markdown", 1580 | "metadata": { 1581 | "id": "DJ_9Kt4XG0Np" 1582 | }, 1583 | "source": [ 1584 | "---\n", 1585 | "### **9.4 Select sample from a list of elements**\n", 1586 | "---" 1587 | ] 1588 | }, 1589 | { 1590 | "cell_type": "code", 1591 | "metadata": { 1592 | "colab": { 1593 | "base_uri": "https://localhost:8080/" 1594 | }, 1595 | "id": "ZVDp33YQG0Np", 1596 | "outputId": "f28cb676-dbf6-499c-9b2e-bcc195caeaf7" 1597 | }, 1598 | "source": [ 1599 | "import random as r\n", 1600 | "\n", 1601 | "A=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 1602 | "\n", 1603 | "print (r.sample(A, 4))\n", 1604 | "print (r.sample(A, 2))\n", 1605 | "print (r.sample(range(0,100), 2))\n", 1606 | "print (r.sample(range(-100,100), 5))" 1607 | ], 1608 | "execution_count": 35, 1609 | "outputs": [ 1610 | { 1611 | "output_type": "stream", 1612 | "name": "stdout", 1613 | "text": [ 1614 | "[5, 1, 7, 3]\n", 1615 | "[10, 3]\n", 1616 | "[20, 9]\n", 1617 | "[-16, -45, 62, -68, -91]\n" 1618 | ] 1619 | } 1620 | ] 1621 | }, 1622 | { 1623 | "cell_type": "markdown", 1624 | "metadata": { 1625 | "id": "cgEXGBtsG0Np" 1626 | }, 1627 | "source": [ 1628 | "---\n", 1629 | "### **9.5 Generate random string**\n", 1630 | "---" 1631 | ] 1632 | }, 1633 | { 1634 | "cell_type": "code", 1635 | "metadata": { 1636 | "colab": { 1637 | "base_uri": "https://localhost:8080/" 1638 | }, 1639 | "id": "T-FHSfgfG0Nq", 1640 | "outputId": "ca72fdc5-69ff-489e-f7c3-4be9cd62bb9c" 1641 | }, 1642 | "source": [ 1643 | "import string as s\n", 1644 | "import random as r\n", 1645 | "print (\"String --> \",s.ascii_letters)\n", 1646 | "\n", 1647 | "passwd=r.sample(s.ascii_letters, 6)\n", 1648 | "print (\"Selected Char --> \",passwd)\n", 1649 | "\n", 1650 | "passwd1=\"\".join(passwd)\n", 1651 | "print (\"passwd1 --> \",passwd1)\n", 1652 | "\n", 1653 | "passwd2=\"+\".join(passwd)\n", 1654 | "print (\"passwd2 --> \",passwd2)\n", 1655 | "\n", 1656 | "passwd3=\"*\".join(passwd)\n", 1657 | "print (\"passwd3 --> \",passwd3)\n" 1658 | ], 1659 | "execution_count": 36, 1660 | "outputs": [ 1661 | { 1662 | "output_type": "stream", 1663 | "name": "stdout", 1664 | "text": [ 1665 | "String --> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 1666 | "Selected Char --> ['U', 'P', 'v', 'n', 'I', 'h']\n", 1667 | "passwd1 --> UPvnIh\n", 1668 | "passwd2 --> U+P+v+n+I+h\n", 1669 | "passwd3 --> U*P*v*n*I*h\n" 1670 | ] 1671 | } 1672 | ] 1673 | }, 1674 | { 1675 | "cell_type": "markdown", 1676 | "metadata": { 1677 | "id": "9NFexXG1G0Nq" 1678 | }, 1679 | "source": [ 1680 | "---\n", 1681 | "### **9.6 Generate random digits**\n", 1682 | "---" 1683 | ] 1684 | }, 1685 | { 1686 | "cell_type": "code", 1687 | "metadata": { 1688 | "colab": { 1689 | "base_uri": "https://localhost:8080/" 1690 | }, 1691 | "id": "mH0sHuuHG0Nq", 1692 | "outputId": "03939f6d-7e09-44e2-d498-db1e6a6c14de" 1693 | }, 1694 | "source": [ 1695 | "import string as s\n", 1696 | "import random as r\n", 1697 | "print (\"Digits --> \",s.digits)\n", 1698 | "\n", 1699 | "otp=r.sample(s.digits, 5)\n", 1700 | "print (\"Selected num1 --> \",otp)\n", 1701 | "otp=\"\".join(otp)\n", 1702 | "print (\"otp1 --> \",otp)\n", 1703 | "\n", 1704 | "otp=r.sample(s.digits, 5)\n", 1705 | "print (\"Selected num2 --> \",otp)\n", 1706 | "otp=\"\".join(otp)\n", 1707 | "print (\"otp2 --> \",otp)\n", 1708 | "\n", 1709 | "otp=r.sample(s.digits, 5)\n", 1710 | "print (\"Selected num2 --> \",otp)\n", 1711 | "otp=\"\".join(otp)\n", 1712 | "print (\"otp3 --> \",otp)" 1713 | ], 1714 | "execution_count": 37, 1715 | "outputs": [ 1716 | { 1717 | "output_type": "stream", 1718 | "name": "stdout", 1719 | "text": [ 1720 | "Digits --> 0123456789\n", 1721 | "Selected num1 --> ['9', '3', '1', '4', '5']\n", 1722 | "otp1 --> 93145\n", 1723 | "Selected num2 --> ['3', '5', '7', '2', '8']\n", 1724 | "otp2 --> 35728\n", 1725 | "Selected num2 --> ['4', '1', '9', '5', '8']\n", 1726 | "otp3 --> 41958\n" 1727 | ] 1728 | } 1729 | ] 1730 | }, 1731 | { 1732 | "cell_type": "markdown", 1733 | "metadata": { 1734 | "id": "1bcIKPDpG0Nr" 1735 | }, 1736 | "source": [ 1737 | "---\n", 1738 | "### **9.7 Generate random string + digits**\n", 1739 | "---" 1740 | ] 1741 | }, 1742 | { 1743 | "cell_type": "code", 1744 | "metadata": { 1745 | "colab": { 1746 | "base_uri": "https://localhost:8080/" 1747 | }, 1748 | "id": "BG-ACfp0G0Nr", 1749 | "outputId": "791215e2-2f14-4bb3-b04f-25137b64bfae" 1750 | }, 1751 | "source": [ 1752 | "import string as s\n", 1753 | "import random as r\n", 1754 | "print (\"String + Digits --> \",s.ascii_letters + s.digits)\n", 1755 | "\n", 1756 | "mixPasswd=r.sample(s.ascii_letters + s.digits, 5)\n", 1757 | "print (\"\\nSelected Str1 --> \",mixPasswd)\n", 1758 | "mixPasswd=\"\".join(mixPasswd)\n", 1759 | "print (\"mixPasswd1 --> \",mixPasswd)\n", 1760 | "\n", 1761 | "mixPasswd=r.sample(s.ascii_letters + s.digits, 6)\n", 1762 | "print (\"\\nSelected Str2 --> \",mixPasswd)\n", 1763 | "mixPasswd=\"\".join(mixPasswd)\n", 1764 | "print (\"mixPasswd2 --> \",mixPasswd)\n", 1765 | "\n", 1766 | "splChar=\"#@!~%^&*()_+=-[]{}|\"\n", 1767 | "mixPasswd=r.sample(splChar + s.ascii_letters + s.digits, 8)\n", 1768 | "print (\"\\nSelected Str3 --> \",mixPasswd)\n", 1769 | "mixPasswd=\"\".join(mixPasswd)\n", 1770 | "print (\"mixPasswd3 --> \",mixPasswd)" 1771 | ], 1772 | "execution_count": 38, 1773 | "outputs": [ 1774 | { 1775 | "output_type": "stream", 1776 | "name": "stdout", 1777 | "text": [ 1778 | "String + Digits --> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n", 1779 | "\n", 1780 | "Selected Str1 --> ['O', 'k', 'A', 'N', 'i']\n", 1781 | "mixPasswd1 --> OkANi\n", 1782 | "\n", 1783 | "Selected Str2 --> ['t', 'Q', 'b', 'N', 'm', 'i']\n", 1784 | "mixPasswd2 --> tQbNmi\n", 1785 | "\n", 1786 | "Selected Str3 --> ['_', '~', 'j', 'B', '*', 'J', 'x', 'r']\n", 1787 | "mixPasswd3 --> _~jB*Jxr\n" 1788 | ] 1789 | } 1790 | ] 1791 | }, 1792 | { 1793 | "cell_type": "markdown", 1794 | "metadata": { 1795 | "id": "WcdyFjhzJo9j" 1796 | }, 1797 | "source": [ 1798 | "---\n", 1799 | "# **10 Exception Handaling**\n", 1800 | "**Learning:** How to handle exceptions\n", 1801 | "\n", 1802 | "---\n", 1803 | "### **10.1 Error Generation**" 1804 | ] 1805 | }, 1806 | { 1807 | "cell_type": "code", 1808 | "metadata": { 1809 | "colab": { 1810 | "base_uri": "https://localhost:8080/", 1811 | "height": 283 1812 | }, 1813 | "id": "vKJEy_SYJo9k", 1814 | "outputId": "c5a3832b-867e-419f-c1c0-67ac72796c8d" 1815 | }, 1816 | "source": [ 1817 | "for i in range(-5,6):\n", 1818 | "\tprint (\"100/\",i,\" --> \", 100/i)" 1819 | ], 1820 | "execution_count": 39, 1821 | "outputs": [ 1822 | { 1823 | "output_type": "stream", 1824 | "name": "stdout", 1825 | "text": [ 1826 | "100/ -5 --> -20.0\n", 1827 | "100/ -4 --> -25.0\n", 1828 | "100/ -3 --> -33.333333333333336\n", 1829 | "100/ -2 --> -50.0\n", 1830 | "100/ -1 --> -100.0\n" 1831 | ] 1832 | }, 1833 | { 1834 | "output_type": "error", 1835 | "ename": "ZeroDivisionError", 1836 | "evalue": "division by zero", 1837 | "traceback": [ 1838 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1839 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 1840 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"100/\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\" --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1841 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 1842 | ] 1843 | } 1844 | ] 1845 | }, 1846 | { 1847 | "cell_type": "markdown", 1848 | "metadata": { 1849 | "id": "ulPZfl8rJo9l" 1850 | }, 1851 | "source": [ 1852 | "---\n", 1853 | "### **10.2 Exception handaling for division by zero**\n", 1854 | "---" 1855 | ] 1856 | }, 1857 | { 1858 | "cell_type": "code", 1859 | "metadata": { 1860 | "colab": { 1861 | "base_uri": "https://localhost:8080/" 1862 | }, 1863 | "id": "GY4t0EqjJo9m", 1864 | "outputId": "62cf8737-2081-4ad1-8228-4aeed8b62fde" 1865 | }, 1866 | "source": [ 1867 | "for i in range(-5,6):\n", 1868 | "\ttry:\n", 1869 | "\t\tprint (\"100/\",i,\" --> \", 100/i)\n", 1870 | "\texcept:\n", 1871 | "\t\tprint (\"error\")" 1872 | ], 1873 | "execution_count": 40, 1874 | "outputs": [ 1875 | { 1876 | "output_type": "stream", 1877 | "name": "stdout", 1878 | "text": [ 1879 | "100/ -5 --> -20.0\n", 1880 | "100/ -4 --> -25.0\n", 1881 | "100/ -3 --> -33.333333333333336\n", 1882 | "100/ -2 --> -50.0\n", 1883 | "100/ -1 --> -100.0\n", 1884 | "error\n", 1885 | "100/ 1 --> 100.0\n", 1886 | "100/ 2 --> 50.0\n", 1887 | "100/ 3 --> 33.333333333333336\n", 1888 | "100/ 4 --> 25.0\n", 1889 | "100/ 5 --> 20.0\n" 1890 | ] 1891 | } 1892 | ] 1893 | }, 1894 | { 1895 | "cell_type": "markdown", 1896 | "metadata": { 1897 | "id": "g1cY_zT3Jo9m" 1898 | }, 1899 | "source": [ 1900 | "---\n", 1901 | "### **10.3 Exception handaling for array out of index**\n", 1902 | "---" 1903 | ] 1904 | }, 1905 | { 1906 | "cell_type": "code", 1907 | "metadata": { 1908 | "colab": { 1909 | "base_uri": "https://localhost:8080/" 1910 | }, 1911 | "id": "m1rBOUB3Jo9n", 1912 | "outputId": "939c4604-242b-42f3-d943-d58ca87cf9a6" 1913 | }, 1914 | "source": [ 1915 | "L=[1,2,3,4,5]\n", 1916 | "\n", 1917 | "for i in range(8):\n", 1918 | "\ttry:\n", 1919 | "\t\tprint (i,\" --> \",L[i])\n", 1920 | "\texcept:\n", 1921 | "\t\tprint (\"error\")" 1922 | ], 1923 | "execution_count": 41, 1924 | "outputs": [ 1925 | { 1926 | "output_type": "stream", 1927 | "name": "stdout", 1928 | "text": [ 1929 | "0 --> 1\n", 1930 | "1 --> 2\n", 1931 | "2 --> 3\n", 1932 | "3 --> 4\n", 1933 | "4 --> 5\n", 1934 | "error\n", 1935 | "error\n", 1936 | "error\n" 1937 | ] 1938 | } 1939 | ] 1940 | }, 1941 | { 1942 | "cell_type": "markdown", 1943 | "metadata": { 1944 | "id": "kH8CATzbJo9o" 1945 | }, 1946 | "source": [ 1947 | "---\n", 1948 | "### **10.4 Exception handaling for file not found**\n", 1949 | "---" 1950 | ] 1951 | }, 1952 | { 1953 | "cell_type": "code", 1954 | "metadata": { 1955 | "colab": { 1956 | "base_uri": "https://localhost:8080/", 1957 | "height": 248 1958 | }, 1959 | "id": "zZ7ns4K3Jo9o", 1960 | "outputId": "757d8937-dbf4-46e7-fe21-30207a468fc0" 1961 | }, 1962 | "source": [ 1963 | "fileName=input(\"Enter File Name: \")\n", 1964 | "fp=open(fileName)\t# Open the file in reading mode\n", 1965 | "fp.close()\n", 1966 | "print (\"Done\")" 1967 | ], 1968 | "execution_count": 42, 1969 | "outputs": [ 1970 | { 1971 | "name": "stdout", 1972 | "output_type": "stream", 1973 | "text": [ 1974 | "Enter File Name: test.txt\n" 1975 | ] 1976 | }, 1977 | { 1978 | "output_type": "error", 1979 | "ename": "FileNotFoundError", 1980 | "evalue": "[Errno 2] No such file or directory: 'test.txt'", 1981 | "traceback": [ 1982 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1983 | "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 1984 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfileName\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter File Name: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfileName\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Open the file in reading mode\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Done\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1985 | "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'test.txt'" 1986 | ] 1987 | } 1988 | ] 1989 | }, 1990 | { 1991 | "cell_type": "markdown", 1992 | "metadata": { 1993 | "id": "D4bffbZQJo9p" 1994 | }, 1995 | "source": [ 1996 | "---\n", 1997 | "### **10.5 Exception handaling for file not found**\n", 1998 | "---" 1999 | ] 2000 | }, 2001 | { 2002 | "cell_type": "code", 2003 | "metadata": { 2004 | "colab": { 2005 | "base_uri": "https://localhost:8080/" 2006 | }, 2007 | "id": "dt3X92CyJo9q", 2008 | "outputId": "c5afd7eb-8c83-4e02-e8e5-b45c4487cc5f" 2009 | }, 2010 | "source": [ 2011 | "fileName=input(\"Enter File Name: \")\n", 2012 | "try:\n", 2013 | "\tfp=open(fileName)\t# Open the file in reading mode\n", 2014 | "\tfp.close()\n", 2015 | "except:\n", 2016 | "\tprint (\"Error !! \\\"%s\\\" File Not Found\"%(fileName))\n", 2017 | "\n", 2018 | "print (\"Done\")" 2019 | ], 2020 | "execution_count": 43, 2021 | "outputs": [ 2022 | { 2023 | "output_type": "stream", 2024 | "name": "stdout", 2025 | "text": [ 2026 | "Enter File Name: test.txt\n", 2027 | "Error !! \"test.txt\" File Not Found\n", 2028 | "Done\n" 2029 | ] 2030 | } 2031 | ] 2032 | }, 2033 | { 2034 | "cell_type": "markdown", 2035 | "metadata": { 2036 | "id": "F5w6mKSaPcaj" 2037 | }, 2038 | "source": [ 2039 | "---\n", 2040 | "# **11 Data Structure 1 - List**\n", 2041 | "**Learning:** How to use list, add, delete and search in the list.\n", 2042 | "\n", 2043 | "**Note:** Read more about list and try yourself\n", 2044 | "\n", 2045 | "---\n", 2046 | "### **11.1 List Declaration**\n" 2047 | ] 2048 | }, 2049 | { 2050 | "cell_type": "code", 2051 | "metadata": { 2052 | "colab": { 2053 | "base_uri": "https://localhost:8080/" 2054 | }, 2055 | "id": "nYnqBFRzPcas", 2056 | "outputId": "26028d7f-41d4-4edb-b8f9-77dba6f69b18" 2057 | }, 2058 | "source": [ 2059 | "L = [\"Pratham\", 'Sharma', 3.14, 3 ]\n", 2060 | "print (\"Original List: \", L)\n", 2061 | "print (\"Number of elements in list: \", len(L))" 2062 | ], 2063 | "execution_count": 44, 2064 | "outputs": [ 2065 | { 2066 | "output_type": "stream", 2067 | "name": "stdout", 2068 | "text": [ 2069 | "Original List: ['Pratham', 'Sharma', 3.14, 3]\n", 2070 | "Number of elements in list: 4\n" 2071 | ] 2072 | } 2073 | ] 2074 | }, 2075 | { 2076 | "cell_type": "markdown", 2077 | "metadata": { 2078 | "id": "xyP5tZ7IPcat" 2079 | }, 2080 | "source": [ 2081 | "---\n", 2082 | "### **11.2 List Iteration**\n", 2083 | "---" 2084 | ] 2085 | }, 2086 | { 2087 | "cell_type": "code", 2088 | "metadata": { 2089 | "colab": { 2090 | "base_uri": "https://localhost:8080/" 2091 | }, 2092 | "id": "TIalxne5Pcat", 2093 | "outputId": "e1e42691-581b-49a3-ce69-f8cbb1cb22ee" 2094 | }, 2095 | "source": [ 2096 | "L = [\"Pratham\", 'Sharma', 3.14, 3 ]\n", 2097 | "print (\"Original List: \", L)\n", 2098 | "i=0\n", 2099 | "while i < len(L):\n", 2100 | "\tprint (L[i])\n", 2101 | "\ti+=1" 2102 | ], 2103 | "execution_count": 45, 2104 | "outputs": [ 2105 | { 2106 | "output_type": "stream", 2107 | "name": "stdout", 2108 | "text": [ 2109 | "Original List: ['Pratham', 'Sharma', 3.14, 3]\n", 2110 | "Pratham\n", 2111 | "Sharma\n", 2112 | "3.14\n", 2113 | "3\n" 2114 | ] 2115 | } 2116 | ] 2117 | }, 2118 | { 2119 | "cell_type": "markdown", 2120 | "metadata": { 2121 | "id": "fpkfQhktPcau" 2122 | }, 2123 | "source": [ 2124 | "---\n", 2125 | "### **11.3 List Iteration using for loop**\n", 2126 | "---" 2127 | ] 2128 | }, 2129 | { 2130 | "cell_type": "code", 2131 | "metadata": { 2132 | "colab": { 2133 | "base_uri": "https://localhost:8080/" 2134 | }, 2135 | "id": "p6mVPS1pPcau", 2136 | "outputId": "90db2e6f-3152-4fbf-c2c3-dd5bd7fd6beb" 2137 | }, 2138 | "source": [ 2139 | "L = [\"Pratham\", 'Sharma', 3.14, 3 ]\n", 2140 | "print (\"Original List: \", L)\n", 2141 | "for i in range(0, len(L)):\n", 2142 | "\tprint (L[i])" 2143 | ], 2144 | "execution_count": 46, 2145 | "outputs": [ 2146 | { 2147 | "output_type": "stream", 2148 | "name": "stdout", 2149 | "text": [ 2150 | "Original List: ['Pratham', 'Sharma', 3.14, 3]\n", 2151 | "Pratham\n", 2152 | "Sharma\n", 2153 | "3.14\n", 2154 | "3\n" 2155 | ] 2156 | } 2157 | ] 2158 | }, 2159 | { 2160 | "cell_type": "markdown", 2161 | "metadata": { 2162 | "id": "CouKDMO1Pcav" 2163 | }, 2164 | "source": [ 2165 | "---\n", 2166 | "### **11.4 List Iteration using for loop**\n", 2167 | "---" 2168 | ] 2169 | }, 2170 | { 2171 | "cell_type": "code", 2172 | "metadata": { 2173 | "colab": { 2174 | "base_uri": "https://localhost:8080/" 2175 | }, 2176 | "id": "yT0MDFBJPcaw", 2177 | "outputId": "c0a798a5-4d7c-468f-b597-6363e1135987" 2178 | }, 2179 | "source": [ 2180 | "L = [\"Pratham\", 'Sharma', 3.14, 3 ]\n", 2181 | "print (\"Original List --> \", L)\n", 2182 | "for s in L:\n", 2183 | "\tprint (s)" 2184 | ], 2185 | "execution_count": 47, 2186 | "outputs": [ 2187 | { 2188 | "output_type": "stream", 2189 | "name": "stdout", 2190 | "text": [ 2191 | "Original List --> ['Pratham', 'Sharma', 3.14, 3]\n", 2192 | "Pratham\n", 2193 | "Sharma\n", 2194 | "3.14\n", 2195 | "3\n" 2196 | ] 2197 | } 2198 | ] 2199 | }, 2200 | { 2201 | "cell_type": "markdown", 2202 | "metadata": { 2203 | "id": "XeroS2dsPcay" 2204 | }, 2205 | "source": [ 2206 | "---\n", 2207 | "### **11.5 Adding and deleting from list**\n", 2208 | "---" 2209 | ] 2210 | }, 2211 | { 2212 | "cell_type": "code", 2213 | "metadata": { 2214 | "colab": { 2215 | "base_uri": "https://localhost:8080/" 2216 | }, 2217 | "id": "rWUOXQitPcay", 2218 | "outputId": "620362e0-c20e-41d8-952d-f6825d4a2e6b" 2219 | }, 2220 | "source": [ 2221 | "L = [\"Pratham\", 'Sharma', 3.14, 3 ]\n", 2222 | "print (\"Original List --> \", L)\n", 2223 | "\n", 2224 | "L.append(\"Rahul\")\n", 2225 | "print (\"List After Adding --> \", L)\n", 2226 | "\n", 2227 | "del L[1]\n", 2228 | "print (\"List After Deleting --> \", L)" 2229 | ], 2230 | "execution_count": 48, 2231 | "outputs": [ 2232 | { 2233 | "output_type": "stream", 2234 | "name": "stdout", 2235 | "text": [ 2236 | "Original List --> ['Pratham', 'Sharma', 3.14, 3]\n", 2237 | "List After Adding --> ['Pratham', 'Sharma', 3.14, 3, 'Rahul']\n", 2238 | "List After Deleting --> ['Pratham', 3.14, 3, 'Rahul']\n" 2239 | ] 2240 | } 2241 | ] 2242 | }, 2243 | { 2244 | "cell_type": "markdown", 2245 | "metadata": { 2246 | "id": "_DxAr9ZfPcaz" 2247 | }, 2248 | "source": [ 2249 | "---\n", 2250 | "### **11.6 Sum/Average of List**\n", 2251 | "---" 2252 | ] 2253 | }, 2254 | { 2255 | "cell_type": "code", 2256 | "metadata": { 2257 | "colab": { 2258 | "base_uri": "https://localhost:8080/" 2259 | }, 2260 | "id": "taKMcEajPca1", 2261 | "outputId": "397e67a7-1fec-4b7c-f703-41973b37e6eb" 2262 | }, 2263 | "source": [ 2264 | "L=[3, 6, 9, 12, 5, 3, 2]\n", 2265 | "print (\"Original List --> \", L)\n", 2266 | "\n", 2267 | "print (\"Sum --> \", sum(L))\n", 2268 | "print (\"Average --> \", sum(L)/len(L))\n", 2269 | "print (\"Average --> \", sum(L)//len(L))\n", 2270 | "\n", 2271 | "print (\"L * 3 --> \", L * 3) # Every element get tripled\n", 2272 | "print (\"L + L --> \", L + L) # Every element get doubled" 2273 | ], 2274 | "execution_count": 49, 2275 | "outputs": [ 2276 | { 2277 | "output_type": "stream", 2278 | "name": "stdout", 2279 | "text": [ 2280 | "Original List --> [3, 6, 9, 12, 5, 3, 2]\n", 2281 | "Sum --> 40\n", 2282 | "Average --> 5.714285714285714\n", 2283 | "Average --> 5\n", 2284 | "L * 3 --> [3, 6, 9, 12, 5, 3, 2, 3, 6, 9, 12, 5, 3, 2, 3, 6, 9, 12, 5, 3, 2]\n", 2285 | "L + L --> [3, 6, 9, 12, 5, 3, 2, 3, 6, 9, 12, 5, 3, 2]\n" 2286 | ] 2287 | } 2288 | ] 2289 | }, 2290 | { 2291 | "cell_type": "markdown", 2292 | "metadata": { 2293 | "id": "2SIEcqKePca1" 2294 | }, 2295 | "source": [ 2296 | "---\n", 2297 | "### **11.7 Min/Max/Sort the list**\n", 2298 | "---" 2299 | ] 2300 | }, 2301 | { 2302 | "cell_type": "code", 2303 | "metadata": { 2304 | "colab": { 2305 | "base_uri": "https://localhost:8080/" 2306 | }, 2307 | "id": "NUsOwblSPca1", 2308 | "outputId": "7faf4863-1f44-4d33-952f-e305e65196ee" 2309 | }, 2310 | "source": [ 2311 | "L=[3, 6, 9, 12, 5, 3, 2]\n", 2312 | "print (\"Original List --> \", L)\n", 2313 | "\n", 2314 | "print (\"max --> \", max(L))\n", 2315 | "print (\"min --> \", min(L))\n", 2316 | "\n", 2317 | "print (\"\\nBefore Sort --> \", L)\n", 2318 | "L.sort()\n", 2319 | "\n", 2320 | "print (\"After Sort (Asending) --> \", L)\n", 2321 | "\n", 2322 | "L.sort(reverse=True)\n", 2323 | "print (\"After Sort (Desending) --> \", L)" 2324 | ], 2325 | "execution_count": 50, 2326 | "outputs": [ 2327 | { 2328 | "output_type": "stream", 2329 | "name": "stdout", 2330 | "text": [ 2331 | "Original List --> [3, 6, 9, 12, 5, 3, 2]\n", 2332 | "max --> 12\n", 2333 | "min --> 2\n", 2334 | "\n", 2335 | "Before Sort --> [3, 6, 9, 12, 5, 3, 2]\n", 2336 | "After Sort (Asending) --> [2, 3, 3, 5, 6, 9, 12]\n", 2337 | "After Sort (Desending) --> [12, 9, 6, 5, 3, 3, 2]\n" 2338 | ] 2339 | } 2340 | ] 2341 | }, 2342 | { 2343 | "cell_type": "markdown", 2344 | "metadata": { 2345 | "id": "pX_P07ODQoQM" 2346 | }, 2347 | "source": [ 2348 | "---\n", 2349 | "### **11.8 Merge lists and select elements**\n", 2350 | "---" 2351 | ] 2352 | }, 2353 | { 2354 | "cell_type": "code", 2355 | "metadata": { 2356 | "colab": { 2357 | "base_uri": "https://localhost:8080/" 2358 | }, 2359 | "id": "pf8csu4yQoQO", 2360 | "outputId": "7b458b47-dd7e-4276-f811-f24c7b9bb79c" 2361 | }, 2362 | "source": [ 2363 | "L1 = [3, 6, 9]\n", 2364 | "L2 = [12, 5, 3, 2]\n", 2365 | "L3 = L1 + L2\n", 2366 | "print (\"L1 --> \",L1)\n", 2367 | "print (\"L2 --> \",L2)\n", 2368 | "print (\"L3 --> \",L3)\n", 2369 | "\n", 2370 | "print (\"\\nL3[2:] --> \",L3[2:])\n", 2371 | "print (\"L3[2:5] --> \",L3[2:5])\n", 2372 | "print (\"L3[:-1] --> \",L3[:-1])\n", 2373 | "print (\"L3[::2] --> \",L3[::2])" 2374 | ], 2375 | "execution_count": 51, 2376 | "outputs": [ 2377 | { 2378 | "output_type": "stream", 2379 | "name": "stdout", 2380 | "text": [ 2381 | "L1 --> [3, 6, 9]\n", 2382 | "L2 --> [12, 5, 3, 2]\n", 2383 | "L3 --> [3, 6, 9, 12, 5, 3, 2]\n", 2384 | "\n", 2385 | "L3[2:] --> [9, 12, 5, 3, 2]\n", 2386 | "L3[2:5] --> [9, 12, 5]\n", 2387 | "L3[:-1] --> [3, 6, 9, 12, 5, 3]\n", 2388 | "L3[::2] --> [3, 9, 5, 2]\n" 2389 | ] 2390 | } 2391 | ] 2392 | }, 2393 | { 2394 | "cell_type": "markdown", 2395 | "metadata": { 2396 | "id": "2fxU9VLhQpGB" 2397 | }, 2398 | "source": [ 2399 | "---\n", 2400 | "### **11.9 Multiply all elements of the list by a constant**\n", 2401 | "---" 2402 | ] 2403 | }, 2404 | { 2405 | "cell_type": "code", 2406 | "metadata": { 2407 | "colab": { 2408 | "base_uri": "https://localhost:8080/" 2409 | }, 2410 | "id": "Karxlcp6QpGC", 2411 | "outputId": "24d19176-713a-4578-db87-4dd10363bbb2" 2412 | }, 2413 | "source": [ 2414 | "L = [12, 5, 3, 2, 7]\n", 2415 | "print (\"Original List --> \", L)\n", 2416 | "\n", 2417 | "newL = [ i * 5 for i in L ]\n", 2418 | "print (\"After Multiply with constant --> \", newL)" 2419 | ], 2420 | "execution_count": 52, 2421 | "outputs": [ 2422 | { 2423 | "output_type": "stream", 2424 | "name": "stdout", 2425 | "text": [ 2426 | "Original List --> [12, 5, 3, 2, 7]\n", 2427 | "After Multiply with constant --> [60, 25, 15, 10, 35]\n" 2428 | ] 2429 | } 2430 | ] 2431 | }, 2432 | { 2433 | "cell_type": "markdown", 2434 | "metadata": { 2435 | "id": "S9_QezrcQpQ9" 2436 | }, 2437 | "source": [ 2438 | "---\n", 2439 | "### **11.10 Searching in the list**\n", 2440 | "---" 2441 | ] 2442 | }, 2443 | { 2444 | "cell_type": "code", 2445 | "metadata": { 2446 | "colab": { 2447 | "base_uri": "https://localhost:8080/" 2448 | }, 2449 | "id": "ZptMehxhQpQ9", 2450 | "outputId": "3c122b95-2181-497f-df7f-4b384b1ebcda" 2451 | }, 2452 | "source": [ 2453 | "L=[3, 6, 9, 12, 5, 3, 2]\n", 2454 | "print (\"Original List --> \", 6 in L)\n", 2455 | "print (\"Original List --> \", 10 in L)\n", 2456 | "print (\"Original List --> \", 12 in L)\n", 2457 | "\n", 2458 | "if (6 in L) == True:\n", 2459 | "\tprint (\"Present\")\n", 2460 | "else:\n", 2461 | "\tprint (\"Not Present\")\n", 2462 | "\n", 2463 | "if 10 in L == False:\n", 2464 | "\tprint (\"Not Present\")\n", 2465 | "else:\n", 2466 | "\tprint (\"Present\")" 2467 | ], 2468 | "execution_count": 53, 2469 | "outputs": [ 2470 | { 2471 | "output_type": "stream", 2472 | "name": "stdout", 2473 | "text": [ 2474 | "Original List --> True\n", 2475 | "Original List --> False\n", 2476 | "Original List --> True\n", 2477 | "Present\n", 2478 | "Present\n" 2479 | ] 2480 | } 2481 | ] 2482 | }, 2483 | { 2484 | "cell_type": "markdown", 2485 | "metadata": { 2486 | "id": "zHAa4y1BScFQ" 2487 | }, 2488 | "source": [ 2489 | "---\n", 2490 | "# **12 Data Structure 2 - Dictionary**\n", 2491 | "**Learning:** How to use Dictionary, add, delete, search in Dictionary\n", 2492 | "\n", 2493 | "**Note:** Read more about Dictionary and try yourself\n", 2494 | "\n", 2495 | "---\n", 2496 | "### **12.1 Declare Dictionary**" 2497 | ] 2498 | }, 2499 | { 2500 | "cell_type": "code", 2501 | "metadata": { 2502 | "colab": { 2503 | "base_uri": "https://localhost:8080/", 2504 | "height": 301 2505 | }, 2506 | "id": "T7Q4GKPaScFS", 2507 | "outputId": "11e79454-bbfc-4ae4-de35-f2dfd91d143f" 2508 | }, 2509 | "source": [ 2510 | "CGPA={1:8.9, 2:5.6, 4:6.7, 7:9.1, 8:5.3}\n", 2511 | "print (\"Dictionary --> \", CGPA)\n", 2512 | "print (\"Num of elements --> \", len(CGPA))\n", 2513 | "\n", 2514 | "print (\"CGPA of 1 --> \", CGPA[1])\n", 2515 | "print (\"CGPA of 4 --> \", CGPA[4])\n", 2516 | "print (\"CGPA of 7 --> \", CGPA[7])\n", 2517 | "print (\"CGPA of 3 --> \", CGPA[3])" 2518 | ], 2519 | "execution_count": 54, 2520 | "outputs": [ 2521 | { 2522 | "output_type": "stream", 2523 | "name": "stdout", 2524 | "text": [ 2525 | "Dictionary --> {1: 8.9, 2: 5.6, 4: 6.7, 7: 9.1, 8: 5.3}\n", 2526 | "Num of elements --> 5\n", 2527 | "CGPA of 1 --> 8.9\n", 2528 | "CGPA of 4 --> 6.7\n", 2529 | "CGPA of 7 --> 9.1\n" 2530 | ] 2531 | }, 2532 | { 2533 | "output_type": "error", 2534 | "ename": "KeyError", 2535 | "evalue": "3", 2536 | "traceback": [ 2537 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2538 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 2539 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"CGPA of 4 --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mCGPA\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"CGPA of 7 --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mCGPA\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"CGPA of 3 --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mCGPA\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2540 | "\u001b[0;31mKeyError\u001b[0m: 3" 2541 | ] 2542 | } 2543 | ] 2544 | }, 2545 | { 2546 | "cell_type": "markdown", 2547 | "metadata": { 2548 | "id": "BxbeipLlScFS" 2549 | }, 2550 | "source": [ 2551 | "---\n", 2552 | "### **12.2 Triverse dictionary**\n", 2553 | "---" 2554 | ] 2555 | }, 2556 | { 2557 | "cell_type": "code", 2558 | "metadata": { 2559 | "colab": { 2560 | "base_uri": "https://localhost:8080/" 2561 | }, 2562 | "id": "iWXBcVNTScFT", 2563 | "outputId": "20ecfbb0-5481-4f77-e3b3-7ede153b0cf6" 2564 | }, 2565 | "source": [ 2566 | "CGPA={1:8.9, 2:5.6, 4:6.7, 7:9.1, 8:5.3}\n", 2567 | "for k in CGPA:\n", 2568 | "\tprint (\"CGPA of \", k, \" --> \", CGPA[k])" 2569 | ], 2570 | "execution_count": 55, 2571 | "outputs": [ 2572 | { 2573 | "output_type": "stream", 2574 | "name": "stdout", 2575 | "text": [ 2576 | "CGPA of 1 --> 8.9\n", 2577 | "CGPA of 2 --> 5.6\n", 2578 | "CGPA of 4 --> 6.7\n", 2579 | "CGPA of 7 --> 9.1\n", 2580 | "CGPA of 8 --> 5.3\n" 2581 | ] 2582 | } 2583 | ] 2584 | }, 2585 | { 2586 | "cell_type": "markdown", 2587 | "metadata": { 2588 | "id": "O721iFiOScFT" 2589 | }, 2590 | "source": [ 2591 | "---\n", 2592 | "### **12.3 Getting Keys and Values**\n", 2593 | "---" 2594 | ] 2595 | }, 2596 | { 2597 | "cell_type": "code", 2598 | "metadata": { 2599 | "colab": { 2600 | "base_uri": "https://localhost:8080/" 2601 | }, 2602 | "id": "dnPH-GCyScFU", 2603 | "outputId": "73defcba-0cd7-4230-809c-273a55ea2e47" 2604 | }, 2605 | "source": [ 2606 | "CGPA={1:8.9, 2:5.6, 4:6.7, 7:9.1, 8:5.3}\n", 2607 | "print (\"Dictionary --> \", CGPA)\n", 2608 | "print (\"Keys --> \", list(CGPA.keys()))\n", 2609 | "print (\"Values --> \", list(CGPA.values()))" 2610 | ], 2611 | "execution_count": 56, 2612 | "outputs": [ 2613 | { 2614 | "output_type": "stream", 2615 | "name": "stdout", 2616 | "text": [ 2617 | "Dictionary --> {1: 8.9, 2: 5.6, 4: 6.7, 7: 9.1, 8: 5.3}\n", 2618 | "Keys --> [1, 2, 4, 7, 8]\n", 2619 | "Values --> [8.9, 5.6, 6.7, 9.1, 5.3]\n" 2620 | ] 2621 | } 2622 | ] 2623 | }, 2624 | { 2625 | "cell_type": "markdown", 2626 | "metadata": { 2627 | "id": "v2ooUfguScFV" 2628 | }, 2629 | "source": [ 2630 | "---\n", 2631 | "### **12.4 Updating, Adding and Deleting from Dictionary**\n", 2632 | "---" 2633 | ] 2634 | }, 2635 | { 2636 | "cell_type": "code", 2637 | "metadata": { 2638 | "colab": { 2639 | "base_uri": "https://localhost:8080/", 2640 | "height": 301 2641 | }, 2642 | "id": "zO_QA0GNScFV", 2643 | "outputId": "66e5feb1-782a-401d-f926-78d22b790cd9" 2644 | }, 2645 | "source": [ 2646 | "CGPA={1:8.9,2:5.6,4:6.7,7:9.1,8:5.3}\n", 2647 | "print (\"Original Dictionary --> \", CGPA)\n", 2648 | "\n", 2649 | "CGPA[4] = 9.2\n", 2650 | "print (\"After Updating (4) --> \", CGPA)\n", 2651 | "\n", 2652 | "CGPA[3] = 8.6\n", 2653 | "print (\"After Adding (3) --> \", CGPA)\n", 2654 | "\n", 2655 | "del CGPA[1]\n", 2656 | "print (\"After Deleting (1) --> \", CGPA)\n", 2657 | "\n", 2658 | "CGPA.clear()\n", 2659 | "print (\"After Clear --> \", CGPA)\n", 2660 | "\n", 2661 | "del CGPA\n", 2662 | "print (\"After Delete --> \", CGPA)" 2663 | ], 2664 | "execution_count": 57, 2665 | "outputs": [ 2666 | { 2667 | "output_type": "stream", 2668 | "name": "stdout", 2669 | "text": [ 2670 | "Original Dictionary --> {1: 8.9, 2: 5.6, 4: 6.7, 7: 9.1, 8: 5.3}\n", 2671 | "After Updating (4) --> {1: 8.9, 2: 5.6, 4: 9.2, 7: 9.1, 8: 5.3}\n", 2672 | "After Adding (3) --> {1: 8.9, 2: 5.6, 4: 9.2, 7: 9.1, 8: 5.3, 3: 8.6}\n", 2673 | "After Deleting (1) --> {2: 5.6, 4: 9.2, 7: 9.1, 8: 5.3, 3: 8.6}\n", 2674 | "After Clear --> {}\n" 2675 | ] 2676 | }, 2677 | { 2678 | "output_type": "error", 2679 | "ename": "NameError", 2680 | "evalue": "name 'CGPA' is not defined", 2681 | "traceback": [ 2682 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2683 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 2684 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mCGPA\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"After Delete --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mCGPA\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2685 | "\u001b[0;31mNameError\u001b[0m: name 'CGPA' is not defined" 2686 | ] 2687 | } 2688 | ] 2689 | }, 2690 | { 2691 | "cell_type": "markdown", 2692 | "metadata": { 2693 | "id": "8SPV0FVyScFW" 2694 | }, 2695 | "source": [ 2696 | "---\n", 2697 | "### **12.5 Checking for Key in Dictionary**\n", 2698 | "---" 2699 | ] 2700 | }, 2701 | { 2702 | "cell_type": "code", 2703 | "metadata": { 2704 | "colab": { 2705 | "base_uri": "https://localhost:8080/" 2706 | }, 2707 | "id": "Bwt-yAtpScFW", 2708 | "outputId": "97f058de-9302-42b5-cf81-e18c9e3818f8" 2709 | }, 2710 | "source": [ 2711 | "CGPA={1:8.9, 2:5.6, 4:6.7, 7:9.1, 8:5.3}\n", 2712 | "print (\"Original Dictionary --> \", CGPA)\n", 2713 | "print (\"Is Key 2 Present --> \", 2 in CGPA)\n", 2714 | "print (\"Is Key 9 Present --> \", 9 in CGPA)" 2715 | ], 2716 | "execution_count": 63, 2717 | "outputs": [ 2718 | { 2719 | "output_type": "stream", 2720 | "name": "stdout", 2721 | "text": [ 2722 | "Original Dictionary --> {1: 8.9, 2: 5.6, 4: 6.7, 7: 9.1, 8: 5.3}\n", 2723 | "Is Key 2 Present --> True\n", 2724 | "Is Key 9 Present --> False\n" 2725 | ] 2726 | } 2727 | ] 2728 | }, 2729 | { 2730 | "cell_type": "markdown", 2731 | "metadata": { 2732 | "id": "_KifQnHnScFX" 2733 | }, 2734 | "source": [ 2735 | "---\n", 2736 | "### **12.6 More example1**\n", 2737 | "---" 2738 | ] 2739 | }, 2740 | { 2741 | "cell_type": "code", 2742 | "metadata": { 2743 | "colab": { 2744 | "base_uri": "https://localhost:8080/" 2745 | }, 2746 | "id": "tM4fQro9ScFZ", 2747 | "outputId": "0196f56f-82e1-4060-b72b-ea240b7bc625" 2748 | }, 2749 | "source": [ 2750 | "HomeTown={\"Prashant\":\"Delhi\", \"Govind\":\"Gwalior\", \"Anil\":\"Morena\", \"Pankaj\":\"Agra\"}\n", 2751 | "print (\"Original Dictionary --> \", HomeTown)\n", 2752 | "print (\"Home Town of Prashant is --> \", HomeTown[\"Prashant\"])\n", 2753 | "print (\"Home Town of Govind is --> \", HomeTown[\"Govind\"])\n", 2754 | "print (\"Home Town of Anil is --> \", HomeTown[\"Anil\"])\n", 2755 | "print (\"Home Town of Pankaj is --> \", HomeTown[\"Pankaj\"])\n" 2756 | ], 2757 | "execution_count": 64, 2758 | "outputs": [ 2759 | { 2760 | "output_type": "stream", 2761 | "name": "stdout", 2762 | "text": [ 2763 | "Original Dictionary --> {'Prashant': 'Delhi', 'Govind': 'Gwalior', 'Anil': 'Morena', 'Pankaj': 'Agra'}\n", 2764 | "Home Town of Prashant is --> Delhi\n", 2765 | "Home Town of Govind is --> Gwalior\n", 2766 | "Home Town of Anil is --> Morena\n", 2767 | "Home Town of Pankaj is --> Agra\n" 2768 | ] 2769 | } 2770 | ] 2771 | }, 2772 | { 2773 | "cell_type": "markdown", 2774 | "metadata": { 2775 | "id": "ZQIFxo4GScFa" 2776 | }, 2777 | "source": [ 2778 | "---\n", 2779 | "### **12.7 More example2**\n", 2780 | "---" 2781 | ] 2782 | }, 2783 | { 2784 | "cell_type": "code", 2785 | "metadata": { 2786 | "colab": { 2787 | "base_uri": "https://localhost:8080/" 2788 | }, 2789 | "id": "hOADMSpvScFa", 2790 | "outputId": "b6b0c285-eeb3-4954-ea37-554599f0f852" 2791 | }, 2792 | "source": [ 2793 | "HomeTown={\"Prashant\":\"Delhi\", \"Govind\":\"Gwalior\", \"Anil\":\"Morena\", \"Pankaj\":\"Agra\"}\n", 2794 | "print (\"Original Dictionary --> \", HomeTown)\n", 2795 | "\n", 2796 | "for d in HomeTown:\n", 2797 | "\tprint (\"Home Town of \", d, \" is --> \", HomeTown[d])" 2798 | ], 2799 | "execution_count": 65, 2800 | "outputs": [ 2801 | { 2802 | "output_type": "stream", 2803 | "name": "stdout", 2804 | "text": [ 2805 | "Original Dictionary --> {'Prashant': 'Delhi', 'Govind': 'Gwalior', 'Anil': 'Morena', 'Pankaj': 'Agra'}\n", 2806 | "Home Town of Prashant is --> Delhi\n", 2807 | "Home Town of Govind is --> Gwalior\n", 2808 | "Home Town of Anil is --> Morena\n", 2809 | "Home Town of Pankaj is --> Agra\n" 2810 | ] 2811 | } 2812 | ] 2813 | }, 2814 | { 2815 | "cell_type": "markdown", 2816 | "metadata": { 2817 | "id": "Hom72Xe6Z1sJ" 2818 | }, 2819 | "source": [ 2820 | "---\n", 2821 | "# **13 Data Structure 3 - Tuple**\n", 2822 | "\n", 2823 | "**Learning:** How to use Tuple, add, delete, search in Tuple\n", 2824 | "\n", 2825 | "**Note:** Read more about Tuple and try yourself\n", 2826 | "\n", 2827 | "---\n", 2828 | "### **13.1 Declare Tuple**" 2829 | ] 2830 | }, 2831 | { 2832 | "cell_type": "code", 2833 | "metadata": { 2834 | "colab": { 2835 | "base_uri": "https://localhost:8080/" 2836 | }, 2837 | "id": "au4DzzGyFysT", 2838 | "outputId": "260f9e1c-961e-4c2a-b106-2a5bcb0efaa1" 2839 | }, 2840 | "source": [ 2841 | "# Method 1\n", 2842 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 2843 | "\n", 2844 | "print (\"T -->\", T)\n", 2845 | "print (\"Num of elements -->\", len(T))\n", 2846 | "print (\"Type of Object -->\", type(T))" 2847 | ], 2848 | "execution_count": 66, 2849 | "outputs": [ 2850 | { 2851 | "output_type": "stream", 2852 | "name": "stdout", 2853 | "text": [ 2854 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 2855 | "Num of elements --> 4\n", 2856 | "Type of Object --> \n" 2857 | ] 2858 | } 2859 | ] 2860 | }, 2861 | { 2862 | "cell_type": "code", 2863 | "metadata": { 2864 | "colab": { 2865 | "base_uri": "https://localhost:8080/" 2866 | }, 2867 | "id": "6w09RiHeF-Io", 2868 | "outputId": "2e1e55ef-cc60-413c-c008-71f70ffddf11" 2869 | }, 2870 | "source": [ 2871 | "# Method 2\n", 2872 | "T = tuple([\"Pratham\", 'Sharma', 3.14, 3]) # Convert list to tuple\n", 2873 | "#T = tuple((\"Pratham\", 'Sharma', 3.14, 3)) # Also Works\n", 2874 | "\n", 2875 | "print (\"T -->\", T)\n", 2876 | "print (\"Num of elements -->\", len(T))\n", 2877 | "print (\"Type of Object -->\", type(T))" 2878 | ], 2879 | "execution_count": 67, 2880 | "outputs": [ 2881 | { 2882 | "output_type": "stream", 2883 | "name": "stdout", 2884 | "text": [ 2885 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 2886 | "Num of elements --> 4\n", 2887 | "Type of Object --> \n" 2888 | ] 2889 | } 2890 | ] 2891 | }, 2892 | { 2893 | "cell_type": "markdown", 2894 | "metadata": { 2895 | "id": "sC3XhTJtFysW" 2896 | }, 2897 | "source": [ 2898 | "---\n", 2899 | "### **13.2 Tuple Iteration**\n", 2900 | "---" 2901 | ] 2902 | }, 2903 | { 2904 | "cell_type": "code", 2905 | "metadata": { 2906 | "colab": { 2907 | "base_uri": "https://localhost:8080/" 2908 | }, 2909 | "id": "pama9SlbFysX", 2910 | "outputId": "7369d42f-1f4f-4ee9-a6c7-6ab75e36a151" 2911 | }, 2912 | "source": [ 2913 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 2914 | "print (\"T -->\", T)\n", 2915 | "\n", 2916 | "i = 0\n", 2917 | "while i < len(T):\n", 2918 | " print (T[i])\n", 2919 | " i += 1" 2920 | ], 2921 | "execution_count": 68, 2922 | "outputs": [ 2923 | { 2924 | "output_type": "stream", 2925 | "name": "stdout", 2926 | "text": [ 2927 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 2928 | "Pratham\n", 2929 | "Sharma\n", 2930 | "3.14\n", 2931 | "3\n" 2932 | ] 2933 | } 2934 | ] 2935 | }, 2936 | { 2937 | "cell_type": "markdown", 2938 | "metadata": { 2939 | "id": "Xk0P6u4BFysY" 2940 | }, 2941 | "source": [ 2942 | "---\n", 2943 | "### **13.3 Tuple iteration using for loop**\n", 2944 | "---" 2945 | ] 2946 | }, 2947 | { 2948 | "cell_type": "code", 2949 | "metadata": { 2950 | "colab": { 2951 | "base_uri": "https://localhost:8080/" 2952 | }, 2953 | "id": "N73bUXRDFysZ", 2954 | "outputId": "80bac8c3-8c97-4c51-d151-814abf38637e" 2955 | }, 2956 | "source": [ 2957 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 2958 | "print (\"T -->\", T)\n", 2959 | "\n", 2960 | "for i in range(0, len(T)):\n", 2961 | " print (T[i])" 2962 | ], 2963 | "execution_count": 69, 2964 | "outputs": [ 2965 | { 2966 | "output_type": "stream", 2967 | "name": "stdout", 2968 | "text": [ 2969 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 2970 | "Pratham\n", 2971 | "Sharma\n", 2972 | "3.14\n", 2973 | "3\n" 2974 | ] 2975 | } 2976 | ] 2977 | }, 2978 | { 2979 | "cell_type": "markdown", 2980 | "metadata": { 2981 | "id": "kGZWqnebFysa" 2982 | }, 2983 | "source": [ 2984 | "---\n", 2985 | "### **13.4 Tuple iteration using for loop**\n", 2986 | "---" 2987 | ] 2988 | }, 2989 | { 2990 | "cell_type": "code", 2991 | "metadata": { 2992 | "colab": { 2993 | "base_uri": "https://localhost:8080/" 2994 | }, 2995 | "id": "Y6L2BeBcFysb", 2996 | "outputId": "5dfd0fa0-0644-43e8-c032-f905e56a17a5" 2997 | }, 2998 | "source": [ 2999 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3000 | "print (\"T -->\", T)\n", 3001 | "\n", 3002 | "for s in T:\n", 3003 | " print (s)" 3004 | ], 3005 | "execution_count": 70, 3006 | "outputs": [ 3007 | { 3008 | "output_type": "stream", 3009 | "name": "stdout", 3010 | "text": [ 3011 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 3012 | "Pratham\n", 3013 | "Sharma\n", 3014 | "3.14\n", 3015 | "3\n" 3016 | ] 3017 | } 3018 | ] 3019 | }, 3020 | { 3021 | "cell_type": "markdown", 3022 | "metadata": { 3023 | "id": "TsjuXQqfFysc" 3024 | }, 3025 | "source": [ 3026 | "---\n", 3027 | "### **13.5 Accessing/Selecting in Tuple**\n", 3028 | "---" 3029 | ] 3030 | }, 3031 | { 3032 | "cell_type": "code", 3033 | "metadata": { 3034 | "colab": { 3035 | "base_uri": "https://localhost:8080/" 3036 | }, 3037 | "id": "FgB4s6OtFysc", 3038 | "outputId": "6affd56f-1b52-4e24-c1d3-5507ae509605" 3039 | }, 3040 | "source": [ 3041 | "# Example 1:\n", 3042 | "T = (3, 6, 9, 12, 5, 3, 2)\n", 3043 | "print (\"T -->\", T)\n", 3044 | "\n", 3045 | "print (\"T[1] -->\", T[1])\n", 3046 | "print (\"T[2] -->\", T[2])\n", 3047 | "print (\"T[-1] -->\", T[-1])\n", 3048 | "print (\"T[-2] -->\", T[-2])" 3049 | ], 3050 | "execution_count": 71, 3051 | "outputs": [ 3052 | { 3053 | "output_type": "stream", 3054 | "name": "stdout", 3055 | "text": [ 3056 | "T --> (3, 6, 9, 12, 5, 3, 2)\n", 3057 | "T[1] --> 6\n", 3058 | "T[2] --> 9\n", 3059 | "T[-1] --> 2\n", 3060 | "T[-2] --> 3\n" 3061 | ] 3062 | } 3063 | ] 3064 | }, 3065 | { 3066 | "cell_type": "code", 3067 | "metadata": { 3068 | "colab": { 3069 | "base_uri": "https://localhost:8080/" 3070 | }, 3071 | "id": "ex2S0y2tGzSH", 3072 | "outputId": "e76adfbc-97c7-476d-fe3d-4fcbeb43dc82" 3073 | }, 3074 | "source": [ 3075 | "# Example 2:\n", 3076 | "T = (3, 6, 9, 12, 5, 3, 2)\n", 3077 | "print (\"T -->\", T)\n", 3078 | "\n", 3079 | "print (\"T[1:3] -->\", T[1:3])\n", 3080 | "print (\"T[2:] -->\", T[2:])\n", 3081 | "print (\"T[2:5] -->\", T[2:5])\n", 3082 | "print (\"T[:2] -->\", T[:2])\n", 3083 | "print (\"T[:-1] -->\", T[:-1])\n", 3084 | "print (\"T[-4:-1] -->\", T[-4:-1])" 3085 | ], 3086 | "execution_count": 72, 3087 | "outputs": [ 3088 | { 3089 | "output_type": "stream", 3090 | "name": "stdout", 3091 | "text": [ 3092 | "T --> (3, 6, 9, 12, 5, 3, 2)\n", 3093 | "T[1:3] --> (6, 9)\n", 3094 | "T[2:] --> (9, 12, 5, 3, 2)\n", 3095 | "T[2:5] --> (9, 12, 5)\n", 3096 | "T[:2] --> (3, 6)\n", 3097 | "T[:-1] --> (3, 6, 9, 12, 5, 3)\n", 3098 | "T[-4:-1] --> (12, 5, 3)\n" 3099 | ] 3100 | } 3101 | ] 3102 | }, 3103 | { 3104 | "cell_type": "markdown", 3105 | "metadata": { 3106 | "id": "IYCHgXGZFyse" 3107 | }, 3108 | "source": [ 3109 | "---\n", 3110 | "### **13.6 Sum/Average of Tuple**\n", 3111 | "---" 3112 | ] 3113 | }, 3114 | { 3115 | "cell_type": "code", 3116 | "metadata": { 3117 | "colab": { 3118 | "base_uri": "https://localhost:8080/" 3119 | }, 3120 | "id": "1sSPfUTGFysf", 3121 | "outputId": "e2072c8d-2a37-4197-da72-5bedbcf087d2" 3122 | }, 3123 | "source": [ 3124 | "T = (3, 6, 9, 12, 5, 3, 2)\n", 3125 | "print (\"T -->\", T)\n", 3126 | "print (\"Sum -->\", sum(T))\n", 3127 | "print (\"Average -->\", sum(T)/len(T))\n", 3128 | "print (\"Average -->\", sum(T)//len(T))" 3129 | ], 3130 | "execution_count": 73, 3131 | "outputs": [ 3132 | { 3133 | "output_type": "stream", 3134 | "name": "stdout", 3135 | "text": [ 3136 | "T --> (3, 6, 9, 12, 5, 3, 2)\n", 3137 | "Sum --> 40\n", 3138 | "Average --> 5.714285714285714\n", 3139 | "Average --> 5\n" 3140 | ] 3141 | } 3142 | ] 3143 | }, 3144 | { 3145 | "cell_type": "markdown", 3146 | "metadata": { 3147 | "id": "E_Je9dVxFysg" 3148 | }, 3149 | "source": [ 3150 | "---\n", 3151 | "### **13.7 Min/Max in Tuple**\n", 3152 | "---" 3153 | ] 3154 | }, 3155 | { 3156 | "cell_type": "code", 3157 | "metadata": { 3158 | "colab": { 3159 | "base_uri": "https://localhost:8080/" 3160 | }, 3161 | "id": "tNsc-7xEFysi", 3162 | "outputId": "16783cc5-1839-4de8-ef68-6a613f3427ae" 3163 | }, 3164 | "source": [ 3165 | "# Example 1\n", 3166 | "T = (3, 6, 9, 12, 5, 3, 2) # Integer Tuple\n", 3167 | "print (\"T -->\", T)\n", 3168 | "print (\"Max -->\", max(T))\n", 3169 | "print (\"Min -->\", min(T))" 3170 | ], 3171 | "execution_count": 74, 3172 | "outputs": [ 3173 | { 3174 | "output_type": "stream", 3175 | "name": "stdout", 3176 | "text": [ 3177 | "T --> (3, 6, 9, 12, 5, 3, 2)\n", 3178 | "Max --> 12\n", 3179 | "Min --> 2\n" 3180 | ] 3181 | } 3182 | ] 3183 | }, 3184 | { 3185 | "cell_type": "code", 3186 | "metadata": { 3187 | "colab": { 3188 | "base_uri": "https://localhost:8080/" 3189 | }, 3190 | "id": "mwCtFSUDHL50", 3191 | "outputId": "9e3ce257-05c8-4908-859a-350321f925e7" 3192 | }, 3193 | "source": [ 3194 | "# Example 2\n", 3195 | "T = (\"Ram\", \"Shyam\", \"Human\", \"Ant\") # String Tuple\n", 3196 | "print (\"T -->\", T)\n", 3197 | "print (\"Max -->\", max(T))\n", 3198 | "print (\"Min -->\", min(T))" 3199 | ], 3200 | "execution_count": 75, 3201 | "outputs": [ 3202 | { 3203 | "output_type": "stream", 3204 | "name": "stdout", 3205 | "text": [ 3206 | "T --> ('Ram', 'Shyam', 'Human', 'Ant')\n", 3207 | "Max --> Shyam\n", 3208 | "Min --> Ant\n" 3209 | ] 3210 | } 3211 | ] 3212 | }, 3213 | { 3214 | "cell_type": "markdown", 3215 | "metadata": { 3216 | "id": "WUNgIs62Fysi" 3217 | }, 3218 | "source": [ 3219 | "---\n", 3220 | "### **13.8 Merging Tuples**\n", 3221 | "---" 3222 | ] 3223 | }, 3224 | { 3225 | "cell_type": "code", 3226 | "metadata": { 3227 | "colab": { 3228 | "base_uri": "https://localhost:8080/" 3229 | }, 3230 | "id": "zqz9Xp0tFysj", 3231 | "outputId": "c8c38f50-f212-43ba-a251-e46ab471341b" 3232 | }, 3233 | "source": [ 3234 | "T1 = (3, 6, 9)\n", 3235 | "T2 = (12, 5, 3, 2)\n", 3236 | "\n", 3237 | "print (\"T1 -->\", T1)\n", 3238 | "print (\"T2 -->\", T2)\n", 3239 | "\n", 3240 | "T3 = T1 + T2\n", 3241 | "print (\"T3 -->\", T3)\n", 3242 | "\n", 3243 | "T4 = T1 + T2 + T1 + T2\n", 3244 | "print (\"T4 -->\", T4)" 3245 | ], 3246 | "execution_count": 76, 3247 | "outputs": [ 3248 | { 3249 | "output_type": "stream", 3250 | "name": "stdout", 3251 | "text": [ 3252 | "T1 --> (3, 6, 9)\n", 3253 | "T2 --> (12, 5, 3, 2)\n", 3254 | "T3 --> (3, 6, 9, 12, 5, 3, 2)\n", 3255 | "T4 --> (3, 6, 9, 12, 5, 3, 2, 3, 6, 9, 12, 5, 3, 2)\n" 3256 | ] 3257 | } 3258 | ] 3259 | }, 3260 | { 3261 | "cell_type": "markdown", 3262 | "metadata": { 3263 | "id": "K2lF7bjeFysj" 3264 | }, 3265 | "source": [ 3266 | "---\n", 3267 | "### **13.9 Merging part of Tuples**\n", 3268 | "---" 3269 | ] 3270 | }, 3271 | { 3272 | "cell_type": "code", 3273 | "metadata": { 3274 | "colab": { 3275 | "base_uri": "https://localhost:8080/" 3276 | }, 3277 | "id": "1ChXppaZFysk", 3278 | "outputId": "08dcbe12-e275-408f-e41e-05607b9b92dd" 3279 | }, 3280 | "source": [ 3281 | "T1 = (3, 6, 9)\n", 3282 | "T2 = (12, 5, 3, 2)\n", 3283 | "\n", 3284 | "print (\"T1 -->\", T1)\n", 3285 | "print (\"T2 -->\", T2)\n", 3286 | "\n", 3287 | "T3 = T1[1:2] + T2[1:3]\n", 3288 | "print (\"T3 -->\", T3)\n", 3289 | "\n", 3290 | "T4 = T1[:-2] + T2[:-3]\n", 3291 | "print (\"T4 -->\", T4)" 3292 | ], 3293 | "execution_count": 77, 3294 | "outputs": [ 3295 | { 3296 | "output_type": "stream", 3297 | "name": "stdout", 3298 | "text": [ 3299 | "T1 --> (3, 6, 9)\n", 3300 | "T2 --> (12, 5, 3, 2)\n", 3301 | "T3 --> (6, 5, 3)\n", 3302 | "T4 --> (3, 12)\n" 3303 | ] 3304 | } 3305 | ] 3306 | }, 3307 | { 3308 | "cell_type": "markdown", 3309 | "metadata": { 3310 | "id": "3frpbJ4RFysl" 3311 | }, 3312 | "source": [ 3313 | "---\n", 3314 | "### **13.10 Searching in the tuple**\n", 3315 | "---" 3316 | ] 3317 | }, 3318 | { 3319 | "cell_type": "code", 3320 | "metadata": { 3321 | "colab": { 3322 | "base_uri": "https://localhost:8080/" 3323 | }, 3324 | "id": "cY6y818WFysl", 3325 | "outputId": "928a6f2c-644e-4722-f547-503103189a00" 3326 | }, 3327 | "source": [ 3328 | "T = (3, 6, 9, 12, 5, 3, 2)\n", 3329 | "print (\"T -->\", T)\n", 3330 | "\n", 3331 | "print (\"6 in T -->\", 6 in T)\n", 3332 | "print (\"10 in T -->\", 10 in T)\n", 3333 | "print (\"12 in T -->\", 12 in T)" 3334 | ], 3335 | "execution_count": 78, 3336 | "outputs": [ 3337 | { 3338 | "output_type": "stream", 3339 | "name": "stdout", 3340 | "text": [ 3341 | "T --> (3, 6, 9, 12, 5, 3, 2)\n", 3342 | "6 in T --> True\n", 3343 | "10 in T --> False\n", 3344 | "12 in T --> True\n" 3345 | ] 3346 | } 3347 | ] 3348 | }, 3349 | { 3350 | "cell_type": "markdown", 3351 | "metadata": { 3352 | "id": "vJg6l4N_E4IG" 3353 | }, 3354 | "source": [ 3355 | "---\n", 3356 | "### **13.11 Adding element to Tuple (Error)**\n", 3357 | "---" 3358 | ] 3359 | }, 3360 | { 3361 | "cell_type": "code", 3362 | "metadata": { 3363 | "id": "RM0E_sSoE4IH", 3364 | "colab": { 3365 | "base_uri": "https://localhost:8080/", 3366 | "height": 266 3367 | }, 3368 | "outputId": "ed125c83-37c3-446f-ed12-355982d6c4d8" 3369 | }, 3370 | "source": [ 3371 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3372 | "print (\"T -->\", T)\n", 3373 | "\n", 3374 | "T[2] = 900 # Error; 'tuple' object does not support item assignment\n", 3375 | "print (\"T -->\", T)\n", 3376 | "\n", 3377 | "#Tuples are unchangeable. We cannot add items to it." 3378 | ], 3379 | "execution_count": 79, 3380 | "outputs": [ 3381 | { 3382 | "output_type": "stream", 3383 | "name": "stdout", 3384 | "text": [ 3385 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n" 3386 | ] 3387 | }, 3388 | { 3389 | "output_type": "error", 3390 | "ename": "TypeError", 3391 | "evalue": "'tuple' object does not support item assignment", 3392 | "traceback": [ 3393 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3394 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 3395 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"T -->\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mT\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m900\u001b[0m \u001b[0;31m# Error; 'tuple' object does not support item assignment\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"T -->\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 3396 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 3397 | ] 3398 | } 3399 | ] 3400 | }, 3401 | { 3402 | "cell_type": "markdown", 3403 | "metadata": { 3404 | "id": "KocwDH8QE4IH" 3405 | }, 3406 | "source": [ 3407 | "---\n", 3408 | "### **13.12 Adding element to Tuple - (Jugaad)**\n", 3409 | "---" 3410 | ] 3411 | }, 3412 | { 3413 | "cell_type": "code", 3414 | "metadata": { 3415 | "id": "TWSdjWFfE4II", 3416 | "colab": { 3417 | "base_uri": "https://localhost:8080/" 3418 | }, 3419 | "outputId": "d66b8590-e033-42f6-d6a7-5b55c3e16d93" 3420 | }, 3421 | "source": [ 3422 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3423 | "print (\"T -->\", T)\n", 3424 | "\n", 3425 | "T1 = list(T)\n", 3426 | "T1.append(9.8)\n", 3427 | "T = tuple(T1)\n", 3428 | "\n", 3429 | "print (\"After Add -->\", T)" 3430 | ], 3431 | "execution_count": 80, 3432 | "outputs": [ 3433 | { 3434 | "output_type": "stream", 3435 | "name": "stdout", 3436 | "text": [ 3437 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 3438 | "After Add --> ('Pratham', 'Sharma', 3.14, 3, 9.8)\n" 3439 | ] 3440 | } 3441 | ] 3442 | }, 3443 | { 3444 | "cell_type": "markdown", 3445 | "metadata": { 3446 | "id": "D8fE_Al0E4II" 3447 | }, 3448 | "source": [ 3449 | "---\n", 3450 | "### **13.13 Inserting element in Tuple - (Jugaad)**\n", 3451 | "---" 3452 | ] 3453 | }, 3454 | { 3455 | "cell_type": "code", 3456 | "metadata": { 3457 | "id": "bkvZ4s4yE4IJ", 3458 | "colab": { 3459 | "base_uri": "https://localhost:8080/" 3460 | }, 3461 | "outputId": "7c830181-8551-4e4c-f443-af3b52efa202" 3462 | }, 3463 | "source": [ 3464 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3465 | "print (\"T -->\", T)\n", 3466 | "\n", 3467 | "T1 = list(T)\n", 3468 | "T1.insert(2, \"Rahul\")\n", 3469 | "T = tuple(T1)\n", 3470 | "\n", 3471 | "print (\"After Insert -->\", T)" 3472 | ], 3473 | "execution_count": 81, 3474 | "outputs": [ 3475 | { 3476 | "output_type": "stream", 3477 | "name": "stdout", 3478 | "text": [ 3479 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 3480 | "After Insert --> ('Pratham', 'Sharma', 'Rahul', 3.14, 3)\n" 3481 | ] 3482 | } 3483 | ] 3484 | }, 3485 | { 3486 | "cell_type": "markdown", 3487 | "metadata": { 3488 | "id": "UvpIX1nsE4IK" 3489 | }, 3490 | "source": [ 3491 | "---\n", 3492 | "### **13.14 Deleting from Tuple (Error)**\n", 3493 | "---" 3494 | ] 3495 | }, 3496 | { 3497 | "cell_type": "code", 3498 | "metadata": { 3499 | "id": "mex45HHQE4IL", 3500 | "colab": { 3501 | "base_uri": "https://localhost:8080/", 3502 | "height": 266 3503 | }, 3504 | "outputId": "f18734ef-42fc-45eb-f44f-07dd5f9bae18" 3505 | }, 3506 | "source": [ 3507 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3508 | "print (\"T -->\", T)\n", 3509 | "\n", 3510 | "del T[1]\n", 3511 | "\n", 3512 | "print (\"After Delete -->\", T)" 3513 | ], 3514 | "execution_count": 82, 3515 | "outputs": [ 3516 | { 3517 | "output_type": "stream", 3518 | "name": "stdout", 3519 | "text": [ 3520 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n" 3521 | ] 3522 | }, 3523 | { 3524 | "output_type": "error", 3525 | "ename": "TypeError", 3526 | "evalue": "'tuple' object doesn't support item deletion", 3527 | "traceback": [ 3528 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3529 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 3530 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"T -->\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mT\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"After Delete -->\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 3531 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object doesn't support item deletion" 3532 | ] 3533 | } 3534 | ] 3535 | }, 3536 | { 3537 | "cell_type": "markdown", 3538 | "metadata": { 3539 | "id": "mUlLkF7xK4ta" 3540 | }, 3541 | "source": [ 3542 | "---\n", 3543 | "### **13.15 Deleting from Tuple - (Jugaad)**\n", 3544 | "---" 3545 | ] 3546 | }, 3547 | { 3548 | "cell_type": "code", 3549 | "metadata": { 3550 | "colab": { 3551 | "base_uri": "https://localhost:8080/" 3552 | }, 3553 | "id": "Ey7gyq_JK4tc", 3554 | "outputId": "0f8b9a23-4964-40e4-fadd-33b3dfd3a8f3" 3555 | }, 3556 | "source": [ 3557 | "T = (\"Pratham\", 'Sharma', 3.14, 3)\n", 3558 | "print (\"T -->\", T)\n", 3559 | "\n", 3560 | "T1 = list(T)\n", 3561 | "del T1[1]\n", 3562 | "T = tuple(T1)\n", 3563 | "\n", 3564 | "print (\"After Delete -->\", T)" 3565 | ], 3566 | "execution_count": 83, 3567 | "outputs": [ 3568 | { 3569 | "output_type": "stream", 3570 | "name": "stdout", 3571 | "text": [ 3572 | "T --> ('Pratham', 'Sharma', 3.14, 3)\n", 3573 | "After Delete --> ('Pratham', 3.14, 3)\n" 3574 | ] 3575 | } 3576 | ] 3577 | }, 3578 | { 3579 | "cell_type": "markdown", 3580 | "metadata": { 3581 | "id": "nP1BXJvpaSMR" 3582 | }, 3583 | "source": [ 3584 | "---\n", 3585 | "# **14 Data Structure 4 - Set**\n", 3586 | "\n", 3587 | "**Learning:** How to use Set, add, delete, search in Set\n", 3588 | "\n", 3589 | "**Note:** Read more about Set and try yourself\n", 3590 | "\n", 3591 | "---\n", 3592 | "### **14.1 Declare Set**" 3593 | ] 3594 | }, 3595 | { 3596 | "cell_type": "code", 3597 | "metadata": { 3598 | "colab": { 3599 | "base_uri": "https://localhost:8080/" 3600 | }, 3601 | "id": "q7k_3HyiaWwa", 3602 | "outputId": "88fe0b5f-7a77-4d73-eb67-b242be3ae8a8" 3603 | }, 3604 | "source": [ 3605 | "s = set(['A', 'B', 'E', 'F','E', 'F' ])\n", 3606 | "print (\"Original set --> \", s)\n", 3607 | "print (\"Num of elements in set --> \", len(s))" 3608 | ], 3609 | "execution_count": 84, 3610 | "outputs": [ 3611 | { 3612 | "output_type": "stream", 3613 | "name": "stdout", 3614 | "text": [ 3615 | "Original set --> {'F', 'E', 'A', 'B'}\n", 3616 | "Num of elements in set --> 4\n" 3617 | ] 3618 | } 3619 | ] 3620 | }, 3621 | { 3622 | "cell_type": "markdown", 3623 | "metadata": { 3624 | "id": "ltLfKXfKardd" 3625 | }, 3626 | "source": [ 3627 | "---\n", 3628 | "### **14.2 Opertions on Sets**\n", 3629 | "---" 3630 | ] 3631 | }, 3632 | { 3633 | "cell_type": "code", 3634 | "metadata": { 3635 | "colab": { 3636 | "base_uri": "https://localhost:8080/" 3637 | }, 3638 | "id": "s8KiiSKSardf", 3639 | "outputId": "82b6aa99-fcc3-42ba-df47-5e5cf31e1ec4" 3640 | }, 3641 | "source": [ 3642 | "a = set(['A', 'B', 'E', 'F' ])\n", 3643 | "b = set([\"A\", \"C\", \"D\", \"E\"])\n", 3644 | "print (\"Original set a --> \", a)\n", 3645 | "print (\"Original set b --> \", b)\n", 3646 | "print (\"Union of a and b --> \", a.union(b))\n", 3647 | "print (\"Intersection of a,b --> \", a.intersection(b))\n", 3648 | "print (\"Difference a - b --> \", a - b)\n", 3649 | "print (\"Difference a - b --> \", a.difference(b))\n", 3650 | "print (\"Difference b - a --> \", b - a)\n", 3651 | "print (\"Difference b - a --> \", b.difference(a))\n", 3652 | "print (\"Symetric Diff a - b --> \", a.symmetric_difference(b))\n", 3653 | "print (\"Symetric Diff b - a --> \", b.symmetric_difference(a))" 3654 | ], 3655 | "execution_count": 85, 3656 | "outputs": [ 3657 | { 3658 | "output_type": "stream", 3659 | "name": "stdout", 3660 | "text": [ 3661 | "Original set a --> {'F', 'E', 'A', 'B'}\n", 3662 | "Original set b --> {'E', 'D', 'A', 'C'}\n", 3663 | "Union of a and b --> {'F', 'E', 'D', 'A', 'C', 'B'}\n", 3664 | "Intersection of a,b --> {'E', 'A'}\n", 3665 | "Difference a - b --> {'F', 'B'}\n", 3666 | "Difference a - b --> {'F', 'B'}\n", 3667 | "Difference b - a --> {'D', 'C'}\n", 3668 | "Difference b - a --> {'D', 'C'}\n", 3669 | "Symetric Diff a - b --> {'F', 'B', 'D', 'C'}\n", 3670 | "Symetric Diff b - a --> {'F', 'D', 'C', 'B'}\n" 3671 | ] 3672 | } 3673 | ] 3674 | }, 3675 | { 3676 | "cell_type": "markdown", 3677 | "metadata": { 3678 | "id": "oGG-LZJhars5" 3679 | }, 3680 | "source": [ 3681 | "---\n", 3682 | "### **14.3 Add, delete, pop element from set**\n", 3683 | "---" 3684 | ] 3685 | }, 3686 | { 3687 | "cell_type": "code", 3688 | "metadata": { 3689 | "colab": { 3690 | "base_uri": "https://localhost:8080/" 3691 | }, 3692 | "id": "6MfSdtaUars6", 3693 | "outputId": "7bfd5c10-b400-48d7-976f-1e4e13342cf3" 3694 | }, 3695 | "source": [ 3696 | "a = set(['A', 'B', 'E', 'F' ])\n", 3697 | "print (\"Original set a --> \", a)\n", 3698 | "a.add(\"D\")\n", 3699 | "print (\"Set After Adding (D) --> \", a)\n", 3700 | "a.add(\"D\")\n", 3701 | "print (\"Set After Adding (D) --> \", a)\n", 3702 | "a.remove(\"D\")\n", 3703 | "print (\"Set After Deleting(D)--> \", a)\n", 3704 | "a.pop()\n", 3705 | "print (\"Set After pop --> \", a)\n", 3706 | "a.pop()\n", 3707 | "print (\"Set After pop --> \", a)" 3708 | ], 3709 | "execution_count": 86, 3710 | "outputs": [ 3711 | { 3712 | "output_type": "stream", 3713 | "name": "stdout", 3714 | "text": [ 3715 | "Original set a --> {'F', 'E', 'A', 'B'}\n", 3716 | "Set After Adding (D) --> {'F', 'E', 'D', 'A', 'B'}\n", 3717 | "Set After Adding (D) --> {'F', 'E', 'D', 'A', 'B'}\n", 3718 | "Set After Deleting(D)--> {'F', 'E', 'A', 'B'}\n", 3719 | "Set After pop --> {'E', 'A', 'B'}\n", 3720 | "Set After pop --> {'A', 'B'}\n" 3721 | ] 3722 | } 3723 | ] 3724 | }, 3725 | { 3726 | "cell_type": "markdown", 3727 | "metadata": { 3728 | "id": "spvefWIWbNAR" 3729 | }, 3730 | "source": [ 3731 | "---\n", 3732 | "# **15 Command Line Argument**\n", 3733 | "\n", 3734 | "**Learning:** How to Take input from command line and process it\n", 3735 | "\n", 3736 | "**Note:** Run the program at cmd line\n", 3737 | "\n", 3738 | "---\n", 3739 | "### **15.1 Add two numbers given at cmd line**\n", 3740 | "**Note:** To run the program at cmd line\n", 3741 | "* python Program.py 10 20" 3742 | ] 3743 | }, 3744 | { 3745 | "cell_type": "code", 3746 | "metadata": { 3747 | "colab": { 3748 | "base_uri": "https://localhost:8080/", 3749 | "height": 286 3750 | }, 3751 | "id": "0ieaM9uebNAS", 3752 | "outputId": "6e92856d-f29a-4ee7-c243-0ef1007636c8" 3753 | }, 3754 | "source": [ 3755 | "import sys\n", 3756 | "print (sys.argv)\n", 3757 | "a = int(sys.argv[1]) \t# First Number\n", 3758 | "b = int(sys.argv[2])\t# Second Number\n", 3759 | "c = a + b\n", 3760 | "print (a, \" + \", b, \" --> \", c)" 3761 | ], 3762 | "execution_count": 87, 3763 | "outputs": [ 3764 | { 3765 | "output_type": "stream", 3766 | "name": "stdout", 3767 | "text": [ 3768 | "['/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py', '-f', '/root/.local/share/jupyter/runtime/kernel-ec77039d-b3a9-4c52-a78e-ae484bab0b4b.json']\n" 3769 | ] 3770 | }, 3771 | { 3772 | "output_type": "error", 3773 | "ename": "ValueError", 3774 | "evalue": "invalid literal for int() with base 10: '-f'", 3775 | "traceback": [ 3776 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3777 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 3778 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# First Number\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Second Number\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 3779 | "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '-f'" 3780 | ] 3781 | } 3782 | ] 3783 | }, 3784 | { 3785 | "cell_type": "markdown", 3786 | "metadata": { 3787 | "id": "jGBXdT5CbNAT" 3788 | }, 3789 | "source": [ 3790 | "---\n", 3791 | "### **15.2 Concatinate two strings given at cmd line**\n", 3792 | "**Note:** To run the program at cmd line\n", 3793 | "* python Program.py FirstString SecondString\n", 3794 | "\n", 3795 | "---" 3796 | ] 3797 | }, 3798 | { 3799 | "cell_type": "code", 3800 | "metadata": { 3801 | "colab": { 3802 | "base_uri": "https://localhost:8080/" 3803 | }, 3804 | "id": "dg117QSIbNAU", 3805 | "outputId": "4a6f2509-c246-4ad8-8091-865b5a080990" 3806 | }, 3807 | "source": [ 3808 | "import sys\n", 3809 | "print (sys.argv)\n", 3810 | "s = sys.argv[1] + \" \" + sys.argv[2]\n", 3811 | "print (sys.argv[1], \" + \", sys.argv[2], \" --> \", s)" 3812 | ], 3813 | "execution_count": 88, 3814 | "outputs": [ 3815 | { 3816 | "output_type": "stream", 3817 | "name": "stdout", 3818 | "text": [ 3819 | "['/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py', '-f', '/root/.local/share/jupyter/runtime/kernel-ec77039d-b3a9-4c52-a78e-ae484bab0b4b.json']\n", 3820 | "-f + /root/.local/share/jupyter/runtime/kernel-ec77039d-b3a9-4c52-a78e-ae484bab0b4b.json --> -f /root/.local/share/jupyter/runtime/kernel-ec77039d-b3a9-4c52-a78e-ae484bab0b4b.json\n" 3821 | ] 3822 | } 3823 | ] 3824 | }, 3825 | { 3826 | "cell_type": "markdown", 3827 | "metadata": { 3828 | "id": "Gtu3hMwabNAW" 3829 | }, 3830 | "source": [ 3831 | "---\n", 3832 | "### **15.3 Add all the numbers given at cmd line**\n", 3833 | "**Note:** To run the program at cmd line\n", 3834 | "* python Program.py\n", 3835 | "* python Program.py 10\n", 3836 | "* python Program.py 10 20 30 40\n", 3837 | "\n", 3838 | "---" 3839 | ] 3840 | }, 3841 | { 3842 | "cell_type": "code", 3843 | "metadata": { 3844 | "colab": { 3845 | "base_uri": "https://localhost:8080/", 3846 | "height": 286 3847 | }, 3848 | "id": "QSHWbDHmbNAX", 3849 | "outputId": "eba914e4-c7bf-4182-f34a-942c5a8d0722" 3850 | }, 3851 | "source": [ 3852 | "import sys\n", 3853 | "print (sys.argv)\n", 3854 | "sum=0\n", 3855 | "for s in sys.argv[1:]:\n", 3856 | "\tsum += int(s)\n", 3857 | "\n", 3858 | "print (\"Sum is --> \", sum)" 3859 | ], 3860 | "execution_count": 89, 3861 | "outputs": [ 3862 | { 3863 | "output_type": "stream", 3864 | "name": "stdout", 3865 | "text": [ 3866 | "['/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py', '-f', '/root/.local/share/jupyter/runtime/kernel-ec77039d-b3a9-4c52-a78e-ae484bab0b4b.json']\n" 3867 | ] 3868 | }, 3869 | { 3870 | "output_type": "error", 3871 | "ename": "ValueError", 3872 | "evalue": "invalid literal for int() with base 10: '-f'", 3873 | "traceback": [ 3874 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3875 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 3876 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0msum\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Sum is --> \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 3877 | "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '-f'" 3878 | ] 3879 | } 3880 | ] 3881 | }, 3882 | { 3883 | "cell_type": "markdown", 3884 | "metadata": { 3885 | "id": "-wicdMoyfLoZ" 3886 | }, 3887 | "source": [ 3888 | "---\n", 3889 | "# **16 File Handling**\n", 3890 | "\n", 3891 | "**Learning:** How to open the file, read the file and write in the file\n", 3892 | "\n", 3893 | "---\n", 3894 | "### **16.1 Writing 1 to 10 in file**" 3895 | ] 3896 | }, 3897 | { 3898 | "cell_type": "code", 3899 | "metadata": { 3900 | "colab": { 3901 | "base_uri": "https://localhost:8080/" 3902 | }, 3903 | "id": "oX-fBKbpfLob", 3904 | "outputId": "f86790db-5563-4e0b-fdda-4103e36d9688" 3905 | }, 3906 | "source": [ 3907 | "fp=open('result.txt','w')\t# Open the file in writing mode\n", 3908 | "for i in range(1,11):\n", 3909 | "\tfp.write(str(i) + \"\\n\")\t# Writing to the file line by line\n", 3910 | "fp.close()\n", 3911 | "\n", 3912 | "print (\"Writing done !! \\nOpen result.txt to view the content\")" 3913 | ], 3914 | "execution_count": 90, 3915 | "outputs": [ 3916 | { 3917 | "output_type": "stream", 3918 | "name": "stdout", 3919 | "text": [ 3920 | "Writing done !! \n", 3921 | "Open result.txt to view the content\n" 3922 | ] 3923 | } 3924 | ] 3925 | }, 3926 | { 3927 | "cell_type": "markdown", 3928 | "metadata": { 3929 | "id": "-6OU-QD_fLoc" 3930 | }, 3931 | "source": [ 3932 | "---\n", 3933 | "### **16.2 Read a file and print its content**\n", 3934 | "---\n" 3935 | ] 3936 | }, 3937 | { 3938 | "cell_type": "code", 3939 | "metadata": { 3940 | "colab": { 3941 | "base_uri": "https://localhost:8080/" 3942 | }, 3943 | "id": "jiPQizlEfLod", 3944 | "outputId": "55440594-62bf-463a-ad37-80f26494e765" 3945 | }, 3946 | "source": [ 3947 | "fp=open('result.txt')\t\t# Open the file in reading mode\n", 3948 | "for line in fp: \t\t # print line by line\n", 3949 | "\tprint (line.strip())\n", 3950 | "fp.close()" 3951 | ], 3952 | "execution_count": 91, 3953 | "outputs": [ 3954 | { 3955 | "output_type": "stream", 3956 | "name": "stdout", 3957 | "text": [ 3958 | "1\n", 3959 | "2\n", 3960 | "3\n", 3961 | "4\n", 3962 | "5\n", 3963 | "6\n", 3964 | "7\n", 3965 | "8\n", 3966 | "9\n", 3967 | "10\n" 3968 | ] 3969 | } 3970 | ] 3971 | }, 3972 | { 3973 | "cell_type": "markdown", 3974 | "metadata": { 3975 | "id": "Sby32ykwfLof" 3976 | }, 3977 | "source": [ 3978 | "---\n", 3979 | "### **16.3 Read from one file, Convert it to upper case and write to other file**\n", 3980 | "---" 3981 | ] 3982 | }, 3983 | { 3984 | "cell_type": "code", 3985 | "metadata": { 3986 | "colab": { 3987 | "base_uri": "https://localhost:8080/" 3988 | }, 3989 | "id": "rjvJZNv_fLoh", 3990 | "outputId": "82e27035-cee3-4651-bf7e-94073a155e33" 3991 | }, 3992 | "source": [ 3993 | "Readfp=open('result.txt')\t\t# Open the file in reading mode\n", 3994 | "Writefp=open('abc.txt','w')\t# Open the file in writing mode\n", 3995 | "for line in Readfp:\n", 3996 | "\tWritefp.write(line.upper())\n", 3997 | "\n", 3998 | "Writefp.close()\n", 3999 | "Readfp.close()\n", 4000 | "\n", 4001 | "print (\"Writing done !! \\nOpen result.txt to view the content\")" 4002 | ], 4003 | "execution_count": 92, 4004 | "outputs": [ 4005 | { 4006 | "output_type": "stream", 4007 | "name": "stdout", 4008 | "text": [ 4009 | "Writing done !! \n", 4010 | "Open result.txt to view the content\n" 4011 | ] 4012 | } 4013 | ] 4014 | } 4015 | ] 4016 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Python in 2 hr 2 | **There are 16 programs to explain the various concepts in python programming** 3 | - Syntex, Loop, if-else, Data Structures, Strings, File Handaling, Exception Handaling, Random Numbers, Use of Libraries 4 | 5 | ## **Python Assignment** 6 | - https://github.com/psrana/Assignment-Python 7 | --------------------------------------------------------------------------------