├── .DS_Store ├── README.md ├── Week 01_ Introduction.pdf ├── Week_01__Introduction(F_21)_Friday.ipynb ├── Week_01__Introduction(F_21)_Thursday.ipynb ├── Week_01__Introduction(F_21)_Wednesday.ipynb └── task-week1 └── Identify.txt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NYU-DataScienceBootCamp/Week_1-Introduction/b1a61855f03311a297df5f98453dd9619570b6fd/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Week 01: Introduction 2 | 3 | This repository contains all the resources from the first session of the NYU Data Science Bootcamp. In the first week, we covered the basics of Python and Git to help everyone get started. 4 | 5 | **Instructor:** Sagar Patel 6 | 7 | The recording of the session can be found here: 8 | - [Section A](https://nyu.zoom.us/rec/share/lIkedbLvTHqQ_lOZiTLgzcW1sakCoXFgLXHM2EwpsfyeNt4pMq29qgE2cINGeBuL.9-pLWjNv_PPtxKe9) (Wednesday) 9 | - [Section B](https://nyu.zoom.us/rec/share/MHkZcZeqIpKn9RpUkNGvdY0r5A08J_PohXHvyDZzYPMnuNzvI4ckWTb6UFl6rVL_.fW2u93wCBXEYkxIS) (Thursday) 10 | - [Section C](https://nyu.zoom.us/rec/share/81J2ZHR6L7JUkFz--xcPE80FRUqeJKfecMgWrWx-4jsYVkNQnZGV9j5BRT5v913b.2QIeXN7dF_P85yrQ) (Friday) 11 | 12 | --- 13 | 14 | ## Practice tasks for Week 01 (Optional) 15 | 16 | **NOTE:** These tasks are optional and only for your practice. They will not be graded but they can be shared with the instructor for feedback. 17 | 18 | 1. Clone the repository to the local machine. 19 | 2. Create a repository on your GitHub profile and name it `data-science-bootcamp`. The repository should have the name `/data-science-bootcamp`. 20 | 1. Clone that repository to the local machine as well. 21 | 3. From the current repository, move the folder `task-week1` to your repository.
*Hint: you can simply drag and drop or copy/paste the directory* 22 | 4. Identify yourself: Open the `Identify.txt` file and enter your name, netid, and a brief summary of your favorite book with the title and author. 23 | 5. Add/move files: 24 | 1. Create a new file named hello-world.txt and commit it to the repo. 25 | 2. Create a new directory named `Hello` and move `hello-world.txt` into that directory. 26 | 3. Finally commit the changes in the previous step. 27 | 28 | Once you are done with this and would like the intsructor to review it, share the URL of your repository with the Instructor or on the `#tasks` channel on Slack. 29 | 30 | --- 31 | 32 | ## Online Resources: 33 | 1. [Installing git on Windows](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) 34 | 2. [Git tutorial by Colt Steele](https://www.youtube.com/watch?v=USjZcfj8yxE&t=339s&ab_channel=TraversyMediaTraversyMediaVerified) 35 | 3. [Markdown Cheat Sheet](https://www.markdownguide.org/cheat-sheet/) 36 | 37 | If you have any questions regarding the bootcamp, feel free to email [datasciencebootcamp@nyu.edu](mailto:datasciencebootcamp@nyu.edu) 38 | -------------------------------------------------------------------------------- /Week 01_ Introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NYU-DataScienceBootCamp/Week_1-Introduction/b1a61855f03311a297df5f98453dd9619570b6fd/Week 01_ Introduction.pdf -------------------------------------------------------------------------------- /Week_01__Introduction(F_21)_Friday.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Week_01:_Introduction(F-21)-Friday.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "7SjRpRssC2e-" 22 | }, 23 | "source": [ 24 | "# Introduction to Python" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "metadata": { 30 | "id": "B3-JWyWzCwrm", 31 | "colab": { 32 | "base_uri": "https://localhost:8080/" 33 | }, 34 | "outputId": "60282d14-3931-4454-9096-660de548530b" 35 | }, 36 | "source": [ 37 | "print('Hello, world!')" 38 | ], 39 | "execution_count": null, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "name": "stdout", 44 | "text": [ 45 | "Hello, world!\n" 46 | ] 47 | } 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "id": "TI2v4cRlDXaK" 54 | }, 55 | "source": [ 56 | "## Just for fun!\n", 57 | "\n", 58 | "Did you know that entering `import this` prints the 'Zen of Python'?\n", 59 | "\n", 60 | "The Zen of Python is a collection of 19 \"guiding principles\" for writing computer programs that influence the design of the Python programming language.\n", 61 | "\n", 62 | "It is always good to follow the zen even when you have an opportunity to show off your Python skills! :)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "metadata": { 68 | "id": "wx9lQCM0DfmX", 69 | "colab": { 70 | "base_uri": "https://localhost:8080/" 71 | }, 72 | "outputId": "f44f3b16-def9-4db9-9bfd-26caafc544c9" 73 | }, 74 | "source": [ 75 | "import this" 76 | ], 77 | "execution_count": null, 78 | "outputs": [ 79 | { 80 | "output_type": "stream", 81 | "name": "stdout", 82 | "text": [ 83 | "The Zen of Python, by Tim Peters\n", 84 | "\n", 85 | "Beautiful is better than ugly.\n", 86 | "Explicit is better than implicit.\n", 87 | "Simple is better than complex.\n", 88 | "Complex is better than complicated.\n", 89 | "Flat is better than nested.\n", 90 | "Sparse is better than dense.\n", 91 | "Readability counts.\n", 92 | "Special cases aren't special enough to break the rules.\n", 93 | "Although practicality beats purity.\n", 94 | "Errors should never pass silently.\n", 95 | "Unless explicitly silenced.\n", 96 | "In the face of ambiguity, refuse the temptation to guess.\n", 97 | "There should be one-- and preferably only one --obvious way to do it.\n", 98 | "Although that way may not be obvious at first unless you're Dutch.\n", 99 | "Now is better than never.\n", 100 | "Although never is often better than *right* now.\n", 101 | "If the implementation is hard to explain, it's a bad idea.\n", 102 | "If the implementation is easy to explain, it may be a good idea.\n", 103 | "Namespaces are one honking great idea -- let's do more of those!\n" 104 | ] 105 | } 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": { 111 | "id": "ZPcJ_rESHwum" 112 | }, 113 | "source": [ 114 | "## The `help()` function\n", 115 | "\n", 116 | "The `help()` function in python is used to display the documentation of modules, functions, classes, etc.\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "metadata": { 122 | "id": "44SQH6V9DgVV", 123 | "colab": { 124 | "base_uri": "https://localhost:8080/" 125 | }, 126 | "outputId": "6aa78644-bd41-4395-ebc2-7fca0a43e096" 127 | }, 128 | "source": [ 129 | "# help([object])\n", 130 | "\n", 131 | "help(print)" 132 | ], 133 | "execution_count": null, 134 | "outputs": [ 135 | { 136 | "output_type": "stream", 137 | "name": "stdout", 138 | "text": [ 139 | "Help on built-in function print in module builtins:\n", 140 | "\n", 141 | "print(...)\n", 142 | " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 143 | " \n", 144 | " Prints the values to a stream, or to sys.stdout by default.\n", 145 | " Optional keyword arguments:\n", 146 | " file: a file-like object (stream); defaults to the current sys.stdout.\n", 147 | " sep: string inserted between values, default a space.\n", 148 | " end: string appended after the last value, default a newline.\n", 149 | " flush: whether to forcibly flush the stream.\n", 150 | "\n" 151 | ] 152 | } 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": { 158 | "id": "AypvTz1OJgEv" 159 | }, 160 | "source": [ 161 | "**NOTE:** If the `help()` function is passed without an argument,then the interactive help utility starts up on the console." 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": { 167 | "id": "U2Gx61gZJ5oj" 168 | }, 169 | "source": [ 170 | "## Accessing the Attributes (/Methods) of any object\n", 171 | "\n", 172 | "In order to understand the pre-loaded attributes of an object, we use the `dir()` function." 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "metadata": { 178 | "id": "HVLY2TTyJuYy", 179 | "colab": { 180 | "base_uri": "https://localhost:8080/" 181 | }, 182 | "outputId": "68513fd1-f540-4123-8ed4-3712d850c8bc" 183 | }, 184 | "source": [ 185 | "# import [library]\n", 186 | "# dir([library])\n", 187 | "\n", 188 | "import math\n", 189 | "dir(math)" 190 | ], 191 | "execution_count": null, 192 | "outputs": [ 193 | { 194 | "output_type": "execute_result", 195 | "data": { 196 | "text/plain": [ 197 | "['__doc__',\n", 198 | " '__loader__',\n", 199 | " '__name__',\n", 200 | " '__package__',\n", 201 | " '__spec__',\n", 202 | " 'acos',\n", 203 | " 'acosh',\n", 204 | " 'asin',\n", 205 | " 'asinh',\n", 206 | " 'atan',\n", 207 | " 'atan2',\n", 208 | " 'atanh',\n", 209 | " 'ceil',\n", 210 | " 'copysign',\n", 211 | " 'cos',\n", 212 | " 'cosh',\n", 213 | " 'degrees',\n", 214 | " 'e',\n", 215 | " 'erf',\n", 216 | " 'erfc',\n", 217 | " 'exp',\n", 218 | " 'expm1',\n", 219 | " 'fabs',\n", 220 | " 'factorial',\n", 221 | " 'floor',\n", 222 | " 'fmod',\n", 223 | " 'frexp',\n", 224 | " 'fsum',\n", 225 | " 'gamma',\n", 226 | " 'gcd',\n", 227 | " 'hypot',\n", 228 | " 'inf',\n", 229 | " 'isclose',\n", 230 | " 'isfinite',\n", 231 | " 'isinf',\n", 232 | " 'isnan',\n", 233 | " 'ldexp',\n", 234 | " 'lgamma',\n", 235 | " 'log',\n", 236 | " 'log10',\n", 237 | " 'log1p',\n", 238 | " 'log2',\n", 239 | " 'modf',\n", 240 | " 'nan',\n", 241 | " 'pi',\n", 242 | " 'pow',\n", 243 | " 'radians',\n", 244 | " 'remainder',\n", 245 | " 'sin',\n", 246 | " 'sinh',\n", 247 | " 'sqrt',\n", 248 | " 'tan',\n", 249 | " 'tanh',\n", 250 | " 'tau',\n", 251 | " 'trunc']" 252 | ] 253 | }, 254 | "metadata": {}, 255 | "execution_count": 4 256 | } 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": { 262 | "id": "RKJPIBVeQ-Rr" 263 | }, 264 | "source": [ 265 | "## Understanding data types in Python\n", 266 | "\n", 267 | "Python has the following data types built-in by default:\n", 268 | "\n", 269 | "Text: `str`\n", 270 | "\n", 271 | "Numeric: `int`, `float`, `complex`\n", 272 | "\n", 273 | "Sequence: `list`, `tuple`, `range`\n", 274 | "\n", 275 | "Mapping: `dict`\n", 276 | "\n", 277 | "Set: `set`, `frozenset`\n", 278 | "\n", 279 | "Boolean: `bool`\n", 280 | "\n", 281 | "Binary: `bytes`, `bytearray`, `memoryview`" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "metadata": { 287 | "id": "jWGwNMZ3SfpC", 288 | "colab": { 289 | "base_uri": "https://localhost:8080/" 290 | }, 291 | "outputId": "90b92dee-1a13-477a-accc-55d145c191b7" 292 | }, 293 | "source": [ 294 | "# type(variable) prints the data type of the variable. In this case, string\n", 295 | "\n", 296 | "name = 'Jimmy ' + 'Fallon'\n", 297 | "\n", 298 | "print(name)\n", 299 | "print(type(name))" 300 | ], 301 | "execution_count": null, 302 | "outputs": [ 303 | { 304 | "output_type": "stream", 305 | "name": "stdout", 306 | "text": [ 307 | "Jimmy Fallon\n", 308 | "\n" 309 | ] 310 | } 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": { 316 | "id": "c9H2KIZrTP8x" 317 | }, 318 | "source": [ 319 | "**NOTE:** The initialization of datat type to a particular variable happens automaticaly when we assign a value to a variable." 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "metadata": { 325 | "id": "zd7DOnqVSyNv", 326 | "colab": { 327 | "base_uri": "https://localhost:8080/" 328 | }, 329 | "outputId": "40ea8821-3508-4d60-f166-50d19594e9b4" 330 | }, 331 | "source": [ 332 | "# class integer\n", 333 | "\n", 334 | "x = 10\n", 335 | "print(x)\n", 336 | "\n", 337 | "print(type(x))" 338 | ], 339 | "execution_count": null, 340 | "outputs": [ 341 | { 342 | "output_type": "stream", 343 | "name": "stdout", 344 | "text": [ 345 | "10\n", 346 | "\n" 347 | ] 348 | } 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "metadata": { 354 | "id": "li1jP3axTX-l", 355 | "colab": { 356 | "base_uri": "https://localhost:8080/" 357 | }, 358 | "outputId": "7c75027e-308b-48bf-cca9-2ff05f7469b3" 359 | }, 360 | "source": [ 361 | "# class float\n", 362 | "\n", 363 | "decimal = 0.690000\n", 364 | "print(decimal)\n", 365 | "\n", 366 | "print(type(decimal))" 367 | ], 368 | "execution_count": null, 369 | "outputs": [ 370 | { 371 | "output_type": "stream", 372 | "name": "stdout", 373 | "text": [ 374 | "0.69\n", 375 | "\n" 376 | ] 377 | } 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "metadata": { 383 | "id": "IUflSg0FTjcG", 384 | "colab": { 385 | "base_uri": "https://localhost:8080/" 386 | }, 387 | "outputId": "ac14f54e-554a-4b2a-e950-5cfe70f49edb" 388 | }, 389 | "source": [ 390 | "# class list\n", 391 | "\n", 392 | "groceries = ['apples', 20, 'cake mix', 'kitchen roll']\n", 393 | "print(groceries)\n", 394 | "\n", 395 | "print(type(groceries))" 396 | ], 397 | "execution_count": null, 398 | "outputs": [ 399 | { 400 | "output_type": "stream", 401 | "name": "stdout", 402 | "text": [ 403 | "['apples', 20, 'cake mix', 'kitchen roll']\n", 404 | "\n" 405 | ] 406 | } 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "metadata": { 412 | "id": "ZqWeUPXVT2J4", 413 | "colab": { 414 | "base_uri": "https://localhost:8080/" 415 | }, 416 | "outputId": "b0722f58-cd08-42c1-bfba-3ab8d26b3133" 417 | }, 418 | "source": [ 419 | "# class list, but tuples\n", 420 | "\n", 421 | "a = [(1, 2), (3, 4, 5), (6, 7)]\n", 422 | "print(type(a))" 423 | ], 424 | "execution_count": null, 425 | "outputs": [ 426 | { 427 | "output_type": "stream", 428 | "name": "stdout", 429 | "text": [ 430 | "\n" 431 | ] 432 | } 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "metadata": { 438 | "colab": { 439 | "base_uri": "https://localhost:8080/" 440 | }, 441 | "id": "0rQYNh1esJmN", 442 | "outputId": "30bf436d-0f5c-4671-abe5-1dfa39e15e4e" 443 | }, 444 | "source": [ 445 | "x = ('Apple', 'Tim', 'iPhone')\n", 446 | "print(type(x))" 447 | ], 448 | "execution_count": null, 449 | "outputs": [ 450 | { 451 | "output_type": "stream", 452 | "name": "stdout", 453 | "text": [ 454 | "\n" 455 | ] 456 | } 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "metadata": { 462 | "id": "JqynSwC0ULb_", 463 | "colab": { 464 | "base_uri": "https://localhost:8080/" 465 | }, 466 | "outputId": "9ec45924-2b73-4ad5-e3df-feae8651c726" 467 | }, 468 | "source": [ 469 | "# class set\n", 470 | "\n", 471 | "sets = set((10, 2, 3, 6876))\n", 472 | "print(sets)\n", 473 | "\n", 474 | "print(type(sets))" 475 | ], 476 | "execution_count": null, 477 | "outputs": [ 478 | { 479 | "output_type": "stream", 480 | "name": "stdout", 481 | "text": [ 482 | "{3, 10, 2, 6876}\n", 483 | "\n" 484 | ] 485 | } 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "metadata": { 491 | "id": "tzzxfQLIUTc5", 492 | "colab": { 493 | "base_uri": "https://localhost:8080/" 494 | }, 495 | "outputId": "303d621b-8e2a-410b-d167-7a0d32b8057c" 496 | }, 497 | "source": [ 498 | "# class dictionary\n", 499 | "\n", 500 | "hpc = {'Harry': '10', 'Ronald':'Weasley', 'Snape':8}\n", 501 | "\n", 502 | "print(hpc)\n", 503 | "print(type(hpc))" 504 | ], 505 | "execution_count": null, 506 | "outputs": [ 507 | { 508 | "output_type": "stream", 509 | "name": "stdout", 510 | "text": [ 511 | "{'Harry': '10', 'Ronald': 'Weasley', 'Snape': 8}\n", 512 | "\n" 513 | ] 514 | } 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "metadata": { 520 | "id": "lE-8ouBSUySo", 521 | "colab": { 522 | "base_uri": "https://localhost:8080/" 523 | }, 524 | "outputId": "137eef5c-179e-4000-bc60-e7effacfd1b0" 525 | }, 526 | "source": [ 527 | "# class boolean\n", 528 | "\n", 529 | "boolean = True\n", 530 | "print(type(boolean))\n", 531 | "\n", 532 | "print(bool(-2.3))" 533 | ], 534 | "execution_count": null, 535 | "outputs": [ 536 | { 537 | "output_type": "stream", 538 | "name": "stdout", 539 | "text": [ 540 | "\n", 541 | "True\n" 542 | ] 543 | } 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "metadata": { 549 | "id": "K6lHW02cVC6f", 550 | "colab": { 551 | "base_uri": "https://localhost:8080/" 552 | }, 553 | "outputId": "bbcf2603-68fb-442b-95a7-a468008eac31" 554 | }, 555 | "source": [ 556 | "#None type\n", 557 | "\n", 558 | "z = None\n", 559 | "print(type(z))" 560 | ], 561 | "execution_count": null, 562 | "outputs": [ 563 | { 564 | "output_type": "stream", 565 | "name": "stdout", 566 | "text": [ 567 | "\n" 568 | ] 569 | } 570 | ] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": { 575 | "id": "43k3bdBWVLdl" 576 | }, 577 | "source": [ 578 | "## Commonly used operators in Python" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "metadata": { 584 | "id": "hRgFK9ZzVFrp", 585 | "colab": { 586 | "base_uri": "https://localhost:8080/" 587 | }, 588 | "outputId": "4fdc4a48-8933-41db-8201-d12fc668280f" 589 | }, 590 | "source": [ 591 | "a = 6.3\n", 592 | "b = 5.2\n", 593 | "\n", 594 | "print('Add: ', a+b)\n", 595 | "print('Subtract: ', a-b)\n", 596 | "print('Divide: ', a/b)\n", 597 | "print('Multiply: ', a*b)\n", 598 | "print('Floor Division: ', a//b)\n", 599 | "print('Mod: ', a%b) \n", 600 | "print('Power: ', a**b)" 601 | ], 602 | "execution_count": null, 603 | "outputs": [ 604 | { 605 | "output_type": "stream", 606 | "name": "stdout", 607 | "text": [ 608 | "Add: 11.5\n", 609 | "Subtract: 1.0999999999999996\n", 610 | "Divide: 1.2115384615384615\n", 611 | "Multiply: 32.76\n", 612 | "Floor Division: 1.0\n", 613 | "Mod: 1.0999999999999996\n", 614 | "Power: 14340.716724131638\n" 615 | ] 616 | } 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": { 622 | "id": "epgvbOtWVuId" 623 | }, 624 | "source": [ 625 | "### A few tricks" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "metadata": { 631 | "id": "NB9-F6tCVpMO", 632 | "colab": { 633 | "base_uri": "https://localhost:8080/" 634 | }, 635 | "outputId": "00f749af-76fe-46b4-f22d-e3cea2258f04" 636 | }, 637 | "source": [ 638 | "a = 10\n", 639 | "b = '10&2'\n", 640 | "\n", 641 | "print('Before swap: ',a , b)\n", 642 | "\n", 643 | "a,b = b,a\n", 644 | "print('After swap: ', a, b)" 645 | ], 646 | "execution_count": null, 647 | "outputs": [ 648 | { 649 | "output_type": "stream", 650 | "name": "stdout", 651 | "text": [ 652 | "Before swap: 10 10&2\n", 653 | "After swap: 10&2 10\n" 654 | ] 655 | } 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "metadata": { 661 | "id": "SyWaqfHhV6L5", 662 | "colab": { 663 | "base_uri": "https://localhost:8080/" 664 | }, 665 | "outputId": "be460c80-1b59-42ea-d7c2-e3c575f622d1" 666 | }, 667 | "source": [ 668 | "a, *b = 34, 23, 10\n", 669 | "\n", 670 | "print(type(a))\n", 671 | "print(type(b))" 672 | ], 673 | "execution_count": null, 674 | "outputs": [ 675 | { 676 | "output_type": "stream", 677 | "name": "stdout", 678 | "text": [ 679 | "\n", 680 | "\n" 681 | ] 682 | } 683 | ] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": { 688 | "id": "8ttnUtpLWg5W" 689 | }, 690 | "source": [ 691 | "## Type casting in Python\n", 692 | "\n", 693 | "Type Casting is the method to convert the variable data type into a certain data type in order for the operation required to be performed by users." 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "metadata": { 699 | "id": "SzT49t-NWMuS", 700 | "colab": { 701 | "base_uri": "https://localhost:8080/" 702 | }, 703 | "outputId": "1c9edc13-694c-4351-f302-76be840214d1" 704 | }, 705 | "source": [ 706 | "# switching from string to integer\n", 707 | "\n", 708 | "x = '10'\n", 709 | "print(type(x))\n", 710 | "\n", 711 | "print('String to Integer: ', int(x))\n", 712 | "\n", 713 | "x = int(x)\n", 714 | "print(type(x))" 715 | ], 716 | "execution_count": null, 717 | "outputs": [ 718 | { 719 | "output_type": "stream", 720 | "name": "stdout", 721 | "text": [ 722 | "\n", 723 | "String to Integer: 10\n", 724 | "\n" 725 | ] 726 | } 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "metadata": { 732 | "id": "bDZE0cGjW2RG", 733 | "colab": { 734 | "base_uri": "https://localhost:8080/" 735 | }, 736 | "outputId": "15651624-596b-45c1-8990-1738859f44e0" 737 | }, 738 | "source": [ 739 | "# switching from string to float\n", 740 | "\n", 741 | "print(float(x))\n", 742 | "x = float(x)\n", 743 | "print(type(x))" 744 | ], 745 | "execution_count": null, 746 | "outputs": [ 747 | { 748 | "output_type": "stream", 749 | "name": "stdout", 750 | "text": [ 751 | "10.0\n", 752 | "\n" 753 | ] 754 | } 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "metadata": { 760 | "id": "ByWyHb2yXGGh", 761 | "colab": { 762 | "base_uri": "https://localhost:8080/" 763 | }, 764 | "outputId": "a7f4cd15-f562-425b-8e75-152518a9aedc" 765 | }, 766 | "source": [ 767 | "# switching from integer to string\n", 768 | "\n", 769 | "x = str(1) + '0'\n", 770 | "x = int(x)\n", 771 | "print(type(x))\n", 772 | "print(x)" 773 | ], 774 | "execution_count": null, 775 | "outputs": [ 776 | { 777 | "output_type": "stream", 778 | "name": "stdout", 779 | "text": [ 780 | "\n", 781 | "10\n" 782 | ] 783 | } 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "metadata": { 789 | "id": "-tbNz0ceXS4O", 790 | "colab": { 791 | "base_uri": "https://localhost:8080/" 792 | }, 793 | "outputId": "c3f0295f-82ea-4ba3-ccf3-7d05ee766bb2" 794 | }, 795 | "source": [ 796 | "# switching from set to list\n", 797 | "\n", 798 | "s = set((1, 2, 3, 5, 8))\n", 799 | "\n", 800 | "list_s = list(s)\n", 801 | "print(list_s)\n", 802 | "print(type(list_s))" 803 | ], 804 | "execution_count": null, 805 | "outputs": [ 806 | { 807 | "output_type": "stream", 808 | "name": "stdout", 809 | "text": [ 810 | "[1, 2, 3, 5, 8]\n", 811 | "\n" 812 | ] 813 | } 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "metadata": { 819 | "id": "92FyE9vyXwgX", 820 | "colab": { 821 | "base_uri": "https://localhost:8080/" 822 | }, 823 | "outputId": "af29d11f-f31f-4692-b6c3-759641cdecef" 824 | }, 825 | "source": [ 826 | "# switching from a tuple to list\n", 827 | "\n", 828 | "tuples = ((1, 2), (4, 'Ginny'), (7, 'Fred'))\n", 829 | "\n", 830 | "list_tup = list(tuples)\n", 831 | "\n", 832 | "print(list_tup)" 833 | ], 834 | "execution_count": null, 835 | "outputs": [ 836 | { 837 | "output_type": "stream", 838 | "name": "stdout", 839 | "text": [ 840 | "[(1, 2), (4, 'Ginny'), (7, 'Fred')]\n" 841 | ] 842 | } 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "metadata": { 848 | "id": "667dU9Hgennd" 849 | }, 850 | "source": [ 851 | "s = set((1,2,3,4))\n" 852 | ], 853 | "execution_count": null, 854 | "outputs": [] 855 | }, 856 | { 857 | "cell_type": "code", 858 | "metadata": { 859 | "id": "fI4hrIYfYLYK", 860 | "colab": { 861 | "base_uri": "https://localhost:8080/" 862 | }, 863 | "outputId": "7ba1ac51-a3bd-488e-df42-2c6345c3d3d1" 864 | }, 865 | "source": [ 866 | "# ascii value of alpha numeric characters\n", 867 | "\n", 868 | "print(chr(120))" 869 | ], 870 | "execution_count": null, 871 | "outputs": [ 872 | { 873 | "output_type": "stream", 874 | "name": "stdout", 875 | "text": [ 876 | "x\n" 877 | ] 878 | } 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "metadata": { 884 | "id": "15IRjR0fYWuA", 885 | "colab": { 886 | "base_uri": "https://localhost:8080/" 887 | }, 888 | "outputId": "add66ee5-c353-4729-cee7-0a09b749f00a" 889 | }, 890 | "source": [ 891 | "# string to ascii\n", 892 | "\n", 893 | "print(ord('x'))" 894 | ], 895 | "execution_count": null, 896 | "outputs": [ 897 | { 898 | "output_type": "stream", 899 | "name": "stdout", 900 | "text": [ 901 | "120\n" 902 | ] 903 | } 904 | ] 905 | }, 906 | { 907 | "cell_type": "markdown", 908 | "metadata": { 909 | "id": "FOOWQ-hpYfDj" 910 | }, 911 | "source": [ 912 | "## Using in-built functions" 913 | ] 914 | }, 915 | { 916 | "cell_type": "markdown", 917 | "metadata": { 918 | "id": "ptbOxtLzYkwt" 919 | }, 920 | "source": [ 921 | "### Using the `random` method\n", 922 | "\n", 923 | "This method is used to geenrate 'random' values or ranges depending on the requirement." 924 | ] 925 | }, 926 | { 927 | "cell_type": "code", 928 | "metadata": { 929 | "id": "Q2-bbLCQYYmo", 930 | "colab": { 931 | "base_uri": "https://localhost:8080/" 932 | }, 933 | "outputId": "14459e2c-59d5-4308-939c-829ec4fa108e" 934 | }, 935 | "source": [ 936 | "import random\n", 937 | "\n", 938 | "# adding the start and end paramaters\n", 939 | "\n", 940 | "print(random.randint(5, 10))" 941 | ], 942 | "execution_count": null, 943 | "outputs": [ 944 | { 945 | "output_type": "stream", 946 | "name": "stdout", 947 | "text": [ 948 | "5\n" 949 | ] 950 | } 951 | ] 952 | }, 953 | { 954 | "cell_type": "markdown", 955 | "metadata": { 956 | "id": "dOhlnJ3cZOG_" 957 | }, 958 | "source": [ 959 | "### Using the `math` method\n", 960 | "\n", 961 | "You can use this method(/library) to make mathematical operations much easier." 962 | ] 963 | }, 964 | { 965 | "cell_type": "code", 966 | "metadata": { 967 | "id": "OLn6RTezZAQo", 968 | "colab": { 969 | "base_uri": "https://localhost:8080/" 970 | }, 971 | "outputId": "21405fcd-a0cc-4bf2-b917-15afacfb4611" 972 | }, 973 | "source": [ 974 | "import math\n", 975 | "\n", 976 | "print('inbuilt value of pi: ', math.pi)\n", 977 | "\n", 978 | "print('Square root of 2209: ', math.sqrt(2209))\n", 979 | "print('Ceiling Value: ', math.floor(3.1415))" 980 | ], 981 | "execution_count": null, 982 | "outputs": [ 983 | { 984 | "output_type": "stream", 985 | "name": "stdout", 986 | "text": [ 987 | "inbuilt value of pi: 3.141592653589793\n", 988 | "Square root of 2209: 47.0\n", 989 | "Ceiling Value: 3\n" 990 | ] 991 | } 992 | ] 993 | }, 994 | { 995 | "cell_type": "markdown", 996 | "metadata": { 997 | "id": "AEeN-tSCaPun" 998 | }, 999 | "source": [ 1000 | "### Using the `datetime` method\n", 1001 | "\n", 1002 | "Yeah, nothing fancy about it. Just does what the name mentions." 1003 | ] 1004 | }, 1005 | { 1006 | "cell_type": "code", 1007 | "metadata": { 1008 | "id": "17f721kKZp2s", 1009 | "colab": { 1010 | "base_uri": "https://localhost:8080/" 1011 | }, 1012 | "outputId": "c6ee0798-3a36-4d1a-fb28-2782bad2b76b" 1013 | }, 1014 | "source": [ 1015 | "import datetime\n", 1016 | "\n", 1017 | "date = datetime.datetime.now()\n", 1018 | "\n", 1019 | "print(date)\n", 1020 | "print(type(date))" 1021 | ], 1022 | "execution_count": null, 1023 | "outputs": [ 1024 | { 1025 | "output_type": "stream", 1026 | "name": "stdout", 1027 | "text": [ 1028 | "2021-09-24 17:08:10.582879\n", 1029 | "\n" 1030 | ] 1031 | } 1032 | ] 1033 | }, 1034 | { 1035 | "cell_type": "markdown", 1036 | "metadata": { 1037 | "id": "Z-jk8S03aqlY" 1038 | }, 1039 | "source": [ 1040 | "## Lists!" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "metadata": { 1046 | "id": "_9wnAlTpae_u", 1047 | "colab": { 1048 | "base_uri": "https://localhost:8080/" 1049 | }, 1050 | "outputId": "7ade7bf8-5c80-4d6c-e694-a505cee5163c" 1051 | }, 1052 | "source": [ 1053 | "# initializing a list\n", 1054 | "list_1 = list()\n", 1055 | "print(list_1)\n", 1056 | "\n", 1057 | "# the data type of the variable\n", 1058 | "print(type(list_1))\n", 1059 | "\n", 1060 | "# a numeric list\n", 1061 | "list_num = list((1, 2, 3, 4, 5))\n", 1062 | "print(list_num)\n", 1063 | "\n", 1064 | "# list with objects\n", 1065 | "list_object = ['1', 81, '%', 'Fort Greene']\n", 1066 | "print(list_object)\n", 1067 | "\n", 1068 | "# The operations are automatically computed when mentioned inside the list\n", 1069 | "list_ops = [1*3, 'app'+'lication', 3//2]\n", 1070 | "print(list_ops)" 1071 | ], 1072 | "execution_count": null, 1073 | "outputs": [ 1074 | { 1075 | "output_type": "stream", 1076 | "name": "stdout", 1077 | "text": [ 1078 | "[]\n", 1079 | "\n", 1080 | "[1, 2, 3, 4, 5]\n", 1081 | "['1', 81, '%', 'Fort Greene']\n", 1082 | "[3, 'application', 1]\n" 1083 | ] 1084 | } 1085 | ] 1086 | }, 1087 | { 1088 | "cell_type": "markdown", 1089 | "metadata": { 1090 | "id": "UM8zUVyNboCx" 1091 | }, 1092 | "source": [ 1093 | "### Accessing the values from a list\n", 1094 | "\n", 1095 | "**Fun fact:** Python does allow negative indexing!" 1096 | ] 1097 | }, 1098 | { 1099 | "cell_type": "code", 1100 | "metadata": { 1101 | "id": "5gwYaHT0bZPC", 1102 | "colab": { 1103 | "base_uri": "https://localhost:8080/", 1104 | "height": 35 1105 | }, 1106 | "outputId": "a89480b4-10a5-41de-a043-d5c0fa4eb259" 1107 | }, 1108 | "source": [ 1109 | "list_object = ['1', 81, '%', 'Fort Greene']\n", 1110 | "\n", 1111 | "list_object[3]" 1112 | ], 1113 | "execution_count": null, 1114 | "outputs": [ 1115 | { 1116 | "output_type": "execute_result", 1117 | "data": { 1118 | "application/vnd.google.colaboratory.intrinsic+json": { 1119 | "type": "string" 1120 | }, 1121 | "text/plain": [ 1122 | "'Fort Greene'" 1123 | ] 1124 | }, 1125 | "metadata": {}, 1126 | "execution_count": 71 1127 | } 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "code", 1132 | "metadata": { 1133 | "id": "9ipofY0Fb0U1", 1134 | "colab": { 1135 | "base_uri": "https://localhost:8080/" 1136 | }, 1137 | "outputId": "c87d8d30-c401-428c-bba8-b110f40136b4" 1138 | }, 1139 | "source": [ 1140 | "# executing all the values from the beginning to the end\n", 1141 | "\n", 1142 | "print(list_object[1:])" 1143 | ], 1144 | "execution_count": null, 1145 | "outputs": [ 1146 | { 1147 | "output_type": "stream", 1148 | "name": "stdout", 1149 | "text": [ 1150 | "[81, '%', 'Fort Greene']\n" 1151 | ] 1152 | } 1153 | ] 1154 | }, 1155 | { 1156 | "cell_type": "code", 1157 | "metadata": { 1158 | "id": "Rpr8D7wSg_HV", 1159 | "colab": { 1160 | "base_uri": "https://localhost:8080/" 1161 | }, 1162 | "outputId": "4d3375e7-126f-4f7e-9bb1-529b92803866" 1163 | }, 1164 | "source": [ 1165 | "print(list_object[:3])" 1166 | ], 1167 | "execution_count": null, 1168 | "outputs": [ 1169 | { 1170 | "output_type": "stream", 1171 | "name": "stdout", 1172 | "text": [ 1173 | "['1', 81, '%']\n" 1174 | ] 1175 | } 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "code", 1180 | "metadata": { 1181 | "id": "xKJh-xDab_Bc", 1182 | "colab": { 1183 | "base_uri": "https://localhost:8080/" 1184 | }, 1185 | "outputId": "a1ebd609-24aa-402c-ed7e-1a9e56b929c4" 1186 | }, 1187 | "source": [ 1188 | "# let's say that we want we to skip a few values in between\n", 1189 | "\n", 1190 | "print(list_object[0:7687:2])" 1191 | ], 1192 | "execution_count": null, 1193 | "outputs": [ 1194 | { 1195 | "output_type": "stream", 1196 | "name": "stdout", 1197 | "text": [ 1198 | "['1', '%']\n" 1199 | ] 1200 | } 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "metadata": { 1206 | "id": "vZrPD263cJ5r", 1207 | "colab": { 1208 | "base_uri": "https://localhost:8080/" 1209 | }, 1210 | "outputId": "8c5ffc9a-0514-4d46-91fb-29957a855bd9" 1211 | }, 1212 | "source": [ 1213 | "# negative ranges\n", 1214 | "\n", 1215 | "print(list_object[-4:])" 1216 | ], 1217 | "execution_count": null, 1218 | "outputs": [ 1219 | { 1220 | "output_type": "stream", 1221 | "name": "stdout", 1222 | "text": [ 1223 | "['1', 81, '%', 'Fort Greene']\n" 1224 | ] 1225 | } 1226 | ] 1227 | }, 1228 | { 1229 | "cell_type": "markdown", 1230 | "metadata": { 1231 | "id": "L0V-j6SucW38" 1232 | }, 1233 | "source": [ 1234 | "### Modifying lists" 1235 | ] 1236 | }, 1237 | { 1238 | "cell_type": "code", 1239 | "metadata": { 1240 | "id": "vkWrEGdhcOaY", 1241 | "colab": { 1242 | "base_uri": "https://localhost:8080/" 1243 | }, 1244 | "outputId": "c7d2789a-2ae5-4fe9-fa1a-179a057ea9b6" 1245 | }, 1246 | "source": [ 1247 | "# make changes in the list\n", 1248 | "\n", 1249 | "list_object.extend([0,1.2])\n", 1250 | "print(list_object)\n", 1251 | "\n", 1252 | "\n", 1253 | "# make range of values changes\n", 1254 | "\n", 1255 | "subset_items = [1,52,798,8]\n", 1256 | "list_object[0:4] = subset_items\n", 1257 | "print(list_object)" 1258 | ], 1259 | "execution_count": null, 1260 | "outputs": [ 1261 | { 1262 | "output_type": "stream", 1263 | "name": "stdout", 1264 | "text": [ 1265 | "['1', 81, '%', 'Fort Greene', 0, 1.2]\n", 1266 | "[1, 52, 798, 8, 0, 1.2]\n" 1267 | ] 1268 | } 1269 | ] 1270 | }, 1271 | { 1272 | "cell_type": "code", 1273 | "metadata": { 1274 | "id": "gVXZXxmRc_h5", 1275 | "colab": { 1276 | "base_uri": "https://localhost:8080/" 1277 | }, 1278 | "outputId": "f41e536f-c3d8-4fb3-f177-a49b2843fe94" 1279 | }, 1280 | "source": [ 1281 | "# skipping changes\n", 1282 | "\n", 1283 | "subset = ['2','45555','12']\n", 1284 | "list_object[0:5:2] = subset\n", 1285 | "print(list_object)" 1286 | ], 1287 | "execution_count": null, 1288 | "outputs": [ 1289 | { 1290 | "output_type": "stream", 1291 | "name": "stdout", 1292 | "text": [ 1293 | "['2', 52, '45555', 8, '12', 1.2]\n" 1294 | ] 1295 | } 1296 | ] 1297 | }, 1298 | { 1299 | "cell_type": "code", 1300 | "metadata": { 1301 | "id": "sV2V_Y_qdD9I", 1302 | "colab": { 1303 | "base_uri": "https://localhost:8080/" 1304 | }, 1305 | "outputId": "e2c05358-95db-4dde-fdf7-d785d819c4e1" 1306 | }, 1307 | "source": [ 1308 | "# delete an element\n", 1309 | "\n", 1310 | "list_object = [1, 2, 12, 6, 112, 3, 4, 5, 7]\n", 1311 | "\n", 1312 | "# can be used as a stack and/or a queue\n", 1313 | "\n", 1314 | "print(list_object)\n", 1315 | "list_object.pop()\n", 1316 | "print(list_object)\n", 1317 | "\n", 1318 | "list_object.pop(2)\n", 1319 | "print(list_object)" 1320 | ], 1321 | "execution_count": null, 1322 | "outputs": [ 1323 | { 1324 | "output_type": "stream", 1325 | "name": "stdout", 1326 | "text": [ 1327 | "[1, 2, 12, 6, 112, 3, 4, 5, 7]\n", 1328 | "[1, 2, 12, 6, 112, 3, 4, 5]\n", 1329 | "[1, 2, 6, 112, 3, 4, 5]\n" 1330 | ] 1331 | } 1332 | ] 1333 | }, 1334 | { 1335 | "cell_type": "markdown", 1336 | "metadata": { 1337 | "id": "mEAUkcSHdd0b" 1338 | }, 1339 | "source": [ 1340 | "What do we do if there is an error while removing a value from the list and the list is empty?" 1341 | ] 1342 | }, 1343 | { 1344 | "cell_type": "code", 1345 | "metadata": { 1346 | "id": "0RVeKOizdQqL", 1347 | "colab": { 1348 | "base_uri": "https://localhost:8080/" 1349 | }, 1350 | "outputId": "75389a6f-fdad-4777-aa15-a482425e2ee3" 1351 | }, 1352 | "source": [ 1353 | "# we use the try except functionality\n", 1354 | "\n", 1355 | "try:\n", 1356 | " list_object.remove(10)\n", 1357 | "\n", 1358 | "except:\n", 1359 | " print('Element not in the list')" 1360 | ], 1361 | "execution_count": null, 1362 | "outputs": [ 1363 | { 1364 | "output_type": "stream", 1365 | "name": "stdout", 1366 | "text": [ 1367 | "Element not in the list\n" 1368 | ] 1369 | } 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "markdown", 1374 | "metadata": { 1375 | "id": "BC8M5ZqudzqK" 1376 | }, 1377 | "source": [ 1378 | "## The `for` loop" 1379 | ] 1380 | }, 1381 | { 1382 | "cell_type": "code", 1383 | "metadata": { 1384 | "id": "eVnWXhDfdq7V", 1385 | "colab": { 1386 | "base_uri": "https://localhost:8080/" 1387 | }, 1388 | "outputId": "37ecc0eb-2cd8-40f3-9b19-fd50666fea00" 1389 | }, 1390 | "source": [ 1391 | "for i in range(0,5):\n", 1392 | " print(i, end=' ')" 1393 | ], 1394 | "execution_count": null, 1395 | "outputs": [ 1396 | { 1397 | "output_type": "stream", 1398 | "name": "stdout", 1399 | "text": [ 1400 | "0 1 2 3 4 " 1401 | ] 1402 | } 1403 | ] 1404 | }, 1405 | { 1406 | "cell_type": "code", 1407 | "metadata": { 1408 | "id": "BDA6B-C9fNI7", 1409 | "colab": { 1410 | "base_uri": "https://localhost:8080/" 1411 | }, 1412 | "outputId": "7e6404f0-282d-4cf0-bd95-a7f9942f8303" 1413 | }, 1414 | "source": [ 1415 | "print('Adding the step parameter ')\n", 1416 | "\n", 1417 | "for i in range(10,2,-1):\n", 1418 | " print(i, end=' ')" 1419 | ], 1420 | "execution_count": null, 1421 | "outputs": [ 1422 | { 1423 | "output_type": "stream", 1424 | "name": "stdout", 1425 | "text": [ 1426 | "Adding the step parameter \n", 1427 | "10 9 8 7 6 5 4 3 " 1428 | ] 1429 | } 1430 | ] 1431 | }, 1432 | { 1433 | "cell_type": "code", 1434 | "metadata": { 1435 | "id": "-HopnRQRfSq8", 1436 | "colab": { 1437 | "base_uri": "https://localhost:8080/" 1438 | }, 1439 | "outputId": "e1a06fa7-8f62-4bc1-801b-ccd047eb2ac7" 1440 | }, 1441 | "source": [ 1442 | "print('Printing the values in the format of a list ')\n", 1443 | "\n", 1444 | "a = [1,2,3,4,5]\n", 1445 | "for i in a:\n", 1446 | " print(i)" 1447 | ], 1448 | "execution_count": null, 1449 | "outputs": [ 1450 | { 1451 | "output_type": "stream", 1452 | "name": "stdout", 1453 | "text": [ 1454 | "Printing the values in the format of a list \n", 1455 | "1\n", 1456 | "2\n", 1457 | "3\n", 1458 | "4\n", 1459 | "5\n" 1460 | ] 1461 | } 1462 | ] 1463 | }, 1464 | { 1465 | "cell_type": "code", 1466 | "metadata": { 1467 | "id": "ZyhGZlSufaUZ", 1468 | "colab": { 1469 | "base_uri": "https://localhost:8080/" 1470 | }, 1471 | "outputId": "8b881928-913e-4a33-9757-6324128df1c4" 1472 | }, 1473 | "source": [ 1474 | "print('One-line for loop')\n", 1475 | "\n", 1476 | "sum_upto_10 = [i for i in range(11)]\n", 1477 | "print(sum_upto_10)" 1478 | ], 1479 | "execution_count": null, 1480 | "outputs": [ 1481 | { 1482 | "output_type": "stream", 1483 | "name": "stdout", 1484 | "text": [ 1485 | "One-line for loop\n", 1486 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" 1487 | ] 1488 | } 1489 | ] 1490 | }, 1491 | { 1492 | "cell_type": "code", 1493 | "metadata": { 1494 | "id": "U8xDb2JEfhG_", 1495 | "colab": { 1496 | "base_uri": "https://localhost:8080/" 1497 | }, 1498 | "outputId": "c5403a0e-b14e-491d-93fa-f00f79f9c247" 1499 | }, 1500 | "source": [ 1501 | "print('One-linear nested for loop Matrix Multiplication')\n", 1502 | "\n", 1503 | "matrix_mul = [j*i for i in range(5) for j in range(5,10)]\n", 1504 | "print('Matrix Looks like: ', matrix_mul)" 1505 | ], 1506 | "execution_count": null, 1507 | "outputs": [ 1508 | { 1509 | "output_type": "stream", 1510 | "name": "stdout", 1511 | "text": [ 1512 | "One-linear nested for loop Matrix Multiplication\n", 1513 | "Matrix Looks like: [0, 0, 0, 0, 0, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 18, 21, 24, 27, 20, 24, 28, 32, 36]\n" 1514 | ] 1515 | } 1516 | ] 1517 | }, 1518 | { 1519 | "cell_type": "code", 1520 | "metadata": { 1521 | "id": "T3mYXwOpfuqX", 1522 | "colab": { 1523 | "base_uri": "https://localhost:8080/" 1524 | }, 1525 | "outputId": "38711635-3fc0-412a-8d62-40ec41fdbf01" 1526 | }, 1527 | "source": [ 1528 | "a = ['Hello', 'world', '!']\n", 1529 | "\n", 1530 | "# iterable method 1\n", 1531 | "print('Iterable Method 1: ')\n", 1532 | "for i in range(len(a)):\n", 1533 | " print(i, a[i])\n", 1534 | "\n", 1535 | "# iterable method 2\n", 1536 | "print('\\nIterable Method 2: ')\n", 1537 | "for i in a:\n", 1538 | " print(i)\n", 1539 | "\n", 1540 | "# (iterable?) method 3\n", 1541 | "print('\\nMethod 3: ')\n", 1542 | "print(list(enumerate(a)))" 1543 | ], 1544 | "execution_count": null, 1545 | "outputs": [ 1546 | { 1547 | "output_type": "stream", 1548 | "name": "stdout", 1549 | "text": [ 1550 | "Iterable Method 1: \n", 1551 | "0 Hello\n", 1552 | "1 world\n", 1553 | "2 !\n", 1554 | "\n", 1555 | "Iterable Method 2: \n", 1556 | "Hello\n", 1557 | "world\n", 1558 | "!\n", 1559 | "\n", 1560 | "Method 3: \n", 1561 | "[(0, 'Hello'), (1, 'world'), (2, '!')]\n" 1562 | ] 1563 | } 1564 | ] 1565 | }, 1566 | { 1567 | "cell_type": "markdown", 1568 | "metadata": { 1569 | "id": "Ak9F4uRrg0n4" 1570 | }, 1571 | "source": [ 1572 | "## Functions\n", 1573 | "\n", 1574 | "To define a function, use the `def` keyword followed by the name of the function with attributes." 1575 | ] 1576 | }, 1577 | { 1578 | "cell_type": "code", 1579 | "metadata": { 1580 | "id": "048vzN1Ggkbg", 1581 | "colab": { 1582 | "base_uri": "https://localhost:8080/" 1583 | }, 1584 | "outputId": "91b18836-b3c3-44d6-f141-702737ad5aba" 1585 | }, 1586 | "source": [ 1587 | "def fizzBuzz():\n", 1588 | " \n", 1589 | " '''\n", 1590 | " Given a range print Fizz if divisible by 3, Buzz if divisible by 5 else FizzBuzz if divisible by 3 and 5\n", 1591 | " '''\n", 1592 | "\n", 1593 | " n = int(input('Enter a number: '))\n", 1594 | " count = 1\n", 1595 | " while (count <= n):\n", 1596 | " string = ''\n", 1597 | " if (count %3 == 0):\n", 1598 | " string = 'Fizz'\n", 1599 | " if (count % 5 == 0):\n", 1600 | " string += 'Buzz'\n", 1601 | " print(count,string)\n", 1602 | " count +=1\n", 1603 | " \n", 1604 | "fizzBuzz()" 1605 | ], 1606 | "execution_count": null, 1607 | "outputs": [ 1608 | { 1609 | "output_type": "stream", 1610 | "name": "stdout", 1611 | "text": [ 1612 | "Enter a number: 15\n", 1613 | "1 \n", 1614 | "2 \n", 1615 | "3 Fizz\n", 1616 | "4 \n", 1617 | "5 Buzz\n", 1618 | "6 Fizz\n", 1619 | "7 \n", 1620 | "8 \n", 1621 | "9 Fizz\n", 1622 | "10 Buzz\n", 1623 | "11 \n", 1624 | "12 Fizz\n", 1625 | "13 \n", 1626 | "14 \n", 1627 | "15 FizzBuzz\n" 1628 | ] 1629 | } 1630 | ] 1631 | }, 1632 | { 1633 | "cell_type": "code", 1634 | "metadata": { 1635 | "id": "xKoVLslfhIwF", 1636 | "colab": { 1637 | "base_uri": "https://localhost:8080/" 1638 | }, 1639 | "outputId": "0ca186f2-4042-49fa-c91a-bfd7d84fa566" 1640 | }, 1641 | "source": [ 1642 | "def fizzBuzz():\n", 1643 | " \n", 1644 | " '''\n", 1645 | " Same function as above with more if else conditions.\n", 1646 | " '''\n", 1647 | " \n", 1648 | " n = int(input('Enter a number: '))\n", 1649 | " count = 1\n", 1650 | " while(count <= n):\n", 1651 | " if (count % 5 == 0 and count % 3 == 0):\n", 1652 | " print(count,'Fizz Buzz')\n", 1653 | " elif (count % 3 == 0):\n", 1654 | " print(count,'Fizz')\n", 1655 | " elif (count % 5 == 0):\n", 1656 | " print(count,'Buzz')\n", 1657 | " else:\n", 1658 | " print(count)\n", 1659 | " count +=1\n", 1660 | "\n", 1661 | "fizzBuzz()" 1662 | ], 1663 | "execution_count": null, 1664 | "outputs": [ 1665 | { 1666 | "output_type": "stream", 1667 | "name": "stdout", 1668 | "text": [ 1669 | "Enter a number: 15\n", 1670 | "1\n", 1671 | "2\n", 1672 | "3 Fizz\n", 1673 | "4\n", 1674 | "5 Buzz\n", 1675 | "6 Fizz\n", 1676 | "7\n", 1677 | "8\n", 1678 | "9 Fizz\n", 1679 | "10 Buzz\n", 1680 | "11\n", 1681 | "12 Fizz\n", 1682 | "13\n", 1683 | "14\n", 1684 | "15 Fizz Buzz\n" 1685 | ] 1686 | } 1687 | ] 1688 | }, 1689 | { 1690 | "cell_type": "markdown", 1691 | "metadata": { 1692 | "id": "Hej1NI9LjM_U" 1693 | }, 1694 | "source": [ 1695 | "## Dictionaries\n", 1696 | "\n", 1697 | "In the case of dictionaries, we use `key` and not indexes to refer elements in the dictionary." 1698 | ] 1699 | }, 1700 | { 1701 | "cell_type": "code", 1702 | "metadata": { 1703 | "id": "zuUqQDD4hVVI", 1704 | "colab": { 1705 | "base_uri": "https://localhost:8080/" 1706 | }, 1707 | "outputId": "b506be3a-4682-48af-f6b9-6dbdc7984607" 1708 | }, 1709 | "source": [ 1710 | "# creating an empty dictionary\n", 1711 | "dict_ = {}\n", 1712 | "\n", 1713 | "#dict_ = {key:value,key:value} \n", 1714 | "# keys have to be in a set i.e., they are to be unique\n", 1715 | "\n", 1716 | "string = 'iamdictionaryiteratemeandseethefun'\n", 1717 | "\n", 1718 | "for ch in string:\n", 1719 | " if (ch in dict_):\n", 1720 | " dict_[ch] +=1\n", 1721 | "\n", 1722 | " else:\n", 1723 | " dict_[ch] = 1\n", 1724 | "\n", 1725 | "for k,v in dict_.items():\n", 1726 | " # pass\n", 1727 | " print(f'Character {k} and its count {v}')" 1728 | ], 1729 | "execution_count": null, 1730 | "outputs": [ 1731 | { 1732 | "output_type": "stream", 1733 | "name": "stdout", 1734 | "text": [ 1735 | "Character i and its count 4\n", 1736 | "Character a and its count 4\n", 1737 | "Character m and its count 2\n", 1738 | "Character d and its count 2\n", 1739 | "Character c and its count 1\n", 1740 | "Character t and its count 4\n", 1741 | "Character o and its count 1\n", 1742 | "Character n and its count 3\n", 1743 | "Character r and its count 2\n", 1744 | "Character y and its count 1\n", 1745 | "Character e and its count 6\n", 1746 | "Character s and its count 1\n", 1747 | "Character h and its count 1\n", 1748 | "Character f and its count 1\n", 1749 | "Character u and its count 1\n" 1750 | ] 1751 | } 1752 | ] 1753 | }, 1754 | { 1755 | "cell_type": "markdown", 1756 | "metadata": { 1757 | "id": "6_elLpZ6kiK8" 1758 | }, 1759 | "source": [ 1760 | "**NOTE:** If we try updating for a key that is present the original value will be overwritten" 1761 | ] 1762 | }, 1763 | { 1764 | "cell_type": "code", 1765 | "metadata": { 1766 | "id": "sN67bR2IkhBg", 1767 | "colab": { 1768 | "base_uri": "https://localhost:8080/", 1769 | "height": 201 1770 | }, 1771 | "outputId": "557d16c8-5841-4c2f-fbf4-963c5f133e83" 1772 | }, 1773 | "source": [ 1774 | "# print value for some key\n", 1775 | "\n", 1776 | "# Hint: use .get or check for existence before access to avoid error like below\n", 1777 | "\n", 1778 | "print(dict_['z'])" 1779 | ], 1780 | "execution_count": null, 1781 | "outputs": [ 1782 | { 1783 | "output_type": "error", 1784 | "ename": "KeyError", 1785 | "evalue": "ignored", 1786 | "traceback": [ 1787 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1788 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 1789 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Hint: use .get or check for existence before access to avoid error like below\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'z'\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", 1790 | "\u001b[0;31mKeyError\u001b[0m: 'z'" 1791 | ] 1792 | } 1793 | ] 1794 | }, 1795 | { 1796 | "cell_type": "code", 1797 | "metadata": { 1798 | "id": "gkgH5jCblKe0", 1799 | "colab": { 1800 | "base_uri": "https://localhost:8080/" 1801 | }, 1802 | "outputId": "b60208fa-beca-49bb-bb03-2d0ec5f5b33e" 1803 | }, 1804 | "source": [ 1805 | "print(dict_.get('z'))" 1806 | ], 1807 | "execution_count": null, 1808 | "outputs": [ 1809 | { 1810 | "output_type": "stream", 1811 | "name": "stdout", 1812 | "text": [ 1813 | "None\n" 1814 | ] 1815 | } 1816 | ] 1817 | }, 1818 | { 1819 | "cell_type": "markdown", 1820 | "metadata": { 1821 | "id": "WKesksqVnpSm" 1822 | }, 1823 | "source": [ 1824 | "## Sorting\n", 1825 | "\n", 1826 | "For sorting a list, the data type of all the elements should be the same." 1827 | ] 1828 | }, 1829 | { 1830 | "cell_type": "code", 1831 | "metadata": { 1832 | "id": "bZQltNt8lN0s", 1833 | "colab": { 1834 | "base_uri": "https://localhost:8080/" 1835 | }, 1836 | "outputId": "55512bb5-5e6a-4cfc-951b-8c8f1f8850f9" 1837 | }, 1838 | "source": [ 1839 | "list_ = ['a','3232','21332','bsaa','87','3231']\n", 1840 | "print(list_)\n", 1841 | "\n", 1842 | "print('\\nAscending order: ')\n", 1843 | "\n", 1844 | "# the sorting is done only for the first character\n", 1845 | "result = []\n", 1846 | "for i in list_:\n", 1847 | " sum_ = 0\n", 1848 | " for j in i:\n", 1849 | " sum_ = ord(j)\n", 1850 | " break\n", 1851 | " result.append(sum_)\n", 1852 | "\n", 1853 | "# it checks the first character of the string\n", 1854 | "# if there is a match, it proceeds further\n", 1855 | "print('Before sorting')\n", 1856 | "print(list_)\n", 1857 | "print(result)\n", 1858 | "\n", 1859 | "print('---------------')\n", 1860 | "print('After sorting')\n", 1861 | "list_.sort()\n", 1862 | "result.sort()\n", 1863 | "print(list_)\n", 1864 | "print(result)" 1865 | ], 1866 | "execution_count": null, 1867 | "outputs": [ 1868 | { 1869 | "output_type": "stream", 1870 | "name": "stdout", 1871 | "text": [ 1872 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1873 | "\n", 1874 | "Ascending order: \n", 1875 | "Before sorting\n", 1876 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1877 | "[97, 51, 50, 98, 56, 51]\n", 1878 | "---------------\n", 1879 | "After sorting\n", 1880 | "['21332', '3231', '3232', '87', 'a', 'bsaa']\n", 1881 | "[50, 51, 51, 56, 97, 98]\n" 1882 | ] 1883 | } 1884 | ] 1885 | }, 1886 | { 1887 | "cell_type": "markdown", 1888 | "metadata": { 1889 | "id": "RHL7DdFyobhP" 1890 | }, 1891 | "source": [ 1892 | "### Difference between `sort()` and `sorted()`\n", 1893 | "\n", 1894 | "`sort()` function will modify the list it is called on. The function modifies/sorts the list in-place and has no return value.\n", 1895 | "\n", 1896 | "On the other hand, `sorted()` function will create a new list containing a sorted version of the list it is given." 1897 | ] 1898 | }, 1899 | { 1900 | "cell_type": "code", 1901 | "metadata": { 1902 | "id": "f62xRfAAoRsc", 1903 | "colab": { 1904 | "base_uri": "https://localhost:8080/" 1905 | }, 1906 | "outputId": "42dbb328-7fe5-4959-fcf1-13aeeb1589bc" 1907 | }, 1908 | "source": [ 1909 | "a = [1,3,2]\n", 1910 | "print('a: ', a)\n", 1911 | "b = sorted(a)\n", 1912 | "print('a after sorted(): ',a)\n", 1913 | "print('\\nb: ', b)\n", 1914 | "a.sort()\n", 1915 | "print('a after sort(): ', a)" 1916 | ], 1917 | "execution_count": null, 1918 | "outputs": [ 1919 | { 1920 | "output_type": "stream", 1921 | "name": "stdout", 1922 | "text": [ 1923 | "a: [1, 3, 2]\n", 1924 | "a after sorted(): [1, 3, 2]\n", 1925 | "\n", 1926 | "b: [1, 2, 3]\n", 1927 | "a after sort(): [1, 2, 3]\n" 1928 | ] 1929 | } 1930 | ] 1931 | }, 1932 | { 1933 | "cell_type": "markdown", 1934 | "metadata": { 1935 | "id": "Qc_uhmN7o9wf" 1936 | }, 1937 | "source": [ 1938 | "## `lambda` functions in Python" 1939 | ] 1940 | }, 1941 | { 1942 | "cell_type": "code", 1943 | "metadata": { 1944 | "id": "CuLsIVTpourk", 1945 | "colab": { 1946 | "base_uri": "https://localhost:8080/" 1947 | }, 1948 | "outputId": "ccaf06ac-adb8-4b9f-9466-d52883af6bfe" 1949 | }, 1950 | "source": [ 1951 | "add = lambda a, b, c: a+b+c\n", 1952 | "print(add(5, 6, 2))" 1953 | ], 1954 | "execution_count": null, 1955 | "outputs": [ 1956 | { 1957 | "output_type": "stream", 1958 | "name": "stdout", 1959 | "text": [ 1960 | "13\n" 1961 | ] 1962 | } 1963 | ] 1964 | }, 1965 | { 1966 | "cell_type": "code", 1967 | "metadata": { 1968 | "id": "NakEwD_bpFKh", 1969 | "colab": { 1970 | "base_uri": "https://localhost:8080/" 1971 | }, 1972 | "outputId": "1a62bb09-e1a5-4794-bf3c-3b695994eea4" 1973 | }, 1974 | "source": [ 1975 | "dictonary = {67:\"Ted\", 23:\"Marshall\", 12:\"Barney\", 45:\"Robin\", 11:\"Lily\", 73:\"Victoria\", 99:\"Tracy\"}\n", 1976 | "\n", 1977 | "# sort the tuple on values\n", 1978 | "tuples = sorted(dictonary.items(), key = lambda x:(x[1],x[0]), reverse=False)\n", 1979 | "print('\\nSorted on values')\n", 1980 | "for elements in tuples:\n", 1981 | " print(elements[0],\"::\",elements[1])" 1982 | ], 1983 | "execution_count": null, 1984 | "outputs": [ 1985 | { 1986 | "output_type": "stream", 1987 | "name": "stdout", 1988 | "text": [ 1989 | "\n", 1990 | "Sorted on values\n", 1991 | "12 :: Barney\n", 1992 | "11 :: Lily\n", 1993 | "23 :: Marshall\n", 1994 | "45 :: Robin\n", 1995 | "67 :: Ted\n", 1996 | "99 :: Tracy\n", 1997 | "73 :: Victoria\n" 1998 | ] 1999 | } 2000 | ] 2001 | }, 2002 | { 2003 | "cell_type": "code", 2004 | "metadata": { 2005 | "id": "zuIdrS6gpve2" 2006 | }, 2007 | "source": [ 2008 | "" 2009 | ], 2010 | "execution_count": null, 2011 | "outputs": [] 2012 | } 2013 | ] 2014 | } -------------------------------------------------------------------------------- /Week_01__Introduction(F_21)_Thursday.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Week_01:_Introduction(F-21)-Thursday.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "7SjRpRssC2e-" 22 | }, 23 | "source": [ 24 | "# Introduction to Python" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "metadata": { 30 | "id": "B3-JWyWzCwrm", 31 | "colab": { 32 | "base_uri": "https://localhost:8080/" 33 | }, 34 | "outputId": "e00d5d5e-3fbb-491d-baef-688634858cab" 35 | }, 36 | "source": [ 37 | "print('Hello, world!')" 38 | ], 39 | "execution_count": null, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "name": "stdout", 44 | "text": [ 45 | "Hello, world!\n" 46 | ] 47 | } 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "id": "TI2v4cRlDXaK" 54 | }, 55 | "source": [ 56 | "## Just for fun!\n", 57 | "\n", 58 | "Did you know that entering `import this` prints the 'Zen of Python'?\n", 59 | "\n", 60 | "The Zen of Python is a collection of 19 \"guiding principles\" for writing computer programs that influence the design of the Python programming language.\n", 61 | "\n", 62 | "It is always good to follow the zen even when you have an opportunity to show off your Python skills! :)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "metadata": { 68 | "id": "wx9lQCM0DfmX", 69 | "colab": { 70 | "base_uri": "https://localhost:8080/" 71 | }, 72 | "outputId": "d6e36cc9-db0f-457e-8928-9d6cb748b3a1" 73 | }, 74 | "source": [ 75 | "import this" 76 | ], 77 | "execution_count": null, 78 | "outputs": [ 79 | { 80 | "output_type": "stream", 81 | "name": "stdout", 82 | "text": [ 83 | "The Zen of Python, by Tim Peters\n", 84 | "\n", 85 | "Beautiful is better than ugly.\n", 86 | "Explicit is better than implicit.\n", 87 | "Simple is better than complex.\n", 88 | "Complex is better than complicated.\n", 89 | "Flat is better than nested.\n", 90 | "Sparse is better than dense.\n", 91 | "Readability counts.\n", 92 | "Special cases aren't special enough to break the rules.\n", 93 | "Although practicality beats purity.\n", 94 | "Errors should never pass silently.\n", 95 | "Unless explicitly silenced.\n", 96 | "In the face of ambiguity, refuse the temptation to guess.\n", 97 | "There should be one-- and preferably only one --obvious way to do it.\n", 98 | "Although that way may not be obvious at first unless you're Dutch.\n", 99 | "Now is better than never.\n", 100 | "Although never is often better than *right* now.\n", 101 | "If the implementation is hard to explain, it's a bad idea.\n", 102 | "If the implementation is easy to explain, it may be a good idea.\n", 103 | "Namespaces are one honking great idea -- let's do more of those!\n" 104 | ] 105 | } 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": { 111 | "id": "ZPcJ_rESHwum" 112 | }, 113 | "source": [ 114 | "## The `help()` function\n", 115 | "\n", 116 | "The `help()` function in python is used to display the documentation of modules, functions, classes, etc.\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "metadata": { 122 | "id": "44SQH6V9DgVV", 123 | "colab": { 124 | "base_uri": "https://localhost:8080/" 125 | }, 126 | "outputId": "d2e2eb1a-8024-4581-a064-317836a2c567" 127 | }, 128 | "source": [ 129 | "# help([object])\n", 130 | "\n", 131 | "help(print)" 132 | ], 133 | "execution_count": null, 134 | "outputs": [ 135 | { 136 | "output_type": "stream", 137 | "name": "stdout", 138 | "text": [ 139 | "Help on built-in function print in module builtins:\n", 140 | "\n", 141 | "print(...)\n", 142 | " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 143 | " \n", 144 | " Prints the values to a stream, or to sys.stdout by default.\n", 145 | " Optional keyword arguments:\n", 146 | " file: a file-like object (stream); defaults to the current sys.stdout.\n", 147 | " sep: string inserted between values, default a space.\n", 148 | " end: string appended after the last value, default a newline.\n", 149 | " flush: whether to forcibly flush the stream.\n", 150 | "\n" 151 | ] 152 | } 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": { 158 | "id": "AypvTz1OJgEv" 159 | }, 160 | "source": [ 161 | "**NOTE:** If the `help()` function is passed without an argument,then the interactive help utility starts up on the console." 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": { 167 | "id": "U2Gx61gZJ5oj" 168 | }, 169 | "source": [ 170 | "## Accessing the Attributes (/Methods) of any object\n", 171 | "\n", 172 | "In order to understand the pre-loaded attributes of an object, we use the `dir()` function." 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "metadata": { 178 | "id": "HVLY2TTyJuYy", 179 | "colab": { 180 | "base_uri": "https://localhost:8080/" 181 | }, 182 | "outputId": "e56e38b7-cd7b-48eb-870d-1b5cb4fd260c" 183 | }, 184 | "source": [ 185 | "# import [library]\n", 186 | "# dir([library])\n", 187 | "\n", 188 | "import math\n", 189 | "dir(math)" 190 | ], 191 | "execution_count": null, 192 | "outputs": [ 193 | { 194 | "output_type": "execute_result", 195 | "data": { 196 | "text/plain": [ 197 | "['__doc__',\n", 198 | " '__loader__',\n", 199 | " '__name__',\n", 200 | " '__package__',\n", 201 | " '__spec__',\n", 202 | " 'acos',\n", 203 | " 'acosh',\n", 204 | " 'asin',\n", 205 | " 'asinh',\n", 206 | " 'atan',\n", 207 | " 'atan2',\n", 208 | " 'atanh',\n", 209 | " 'ceil',\n", 210 | " 'copysign',\n", 211 | " 'cos',\n", 212 | " 'cosh',\n", 213 | " 'degrees',\n", 214 | " 'e',\n", 215 | " 'erf',\n", 216 | " 'erfc',\n", 217 | " 'exp',\n", 218 | " 'expm1',\n", 219 | " 'fabs',\n", 220 | " 'factorial',\n", 221 | " 'floor',\n", 222 | " 'fmod',\n", 223 | " 'frexp',\n", 224 | " 'fsum',\n", 225 | " 'gamma',\n", 226 | " 'gcd',\n", 227 | " 'hypot',\n", 228 | " 'inf',\n", 229 | " 'isclose',\n", 230 | " 'isfinite',\n", 231 | " 'isinf',\n", 232 | " 'isnan',\n", 233 | " 'ldexp',\n", 234 | " 'lgamma',\n", 235 | " 'log',\n", 236 | " 'log10',\n", 237 | " 'log1p',\n", 238 | " 'log2',\n", 239 | " 'modf',\n", 240 | " 'nan',\n", 241 | " 'pi',\n", 242 | " 'pow',\n", 243 | " 'radians',\n", 244 | " 'remainder',\n", 245 | " 'sin',\n", 246 | " 'sinh',\n", 247 | " 'sqrt',\n", 248 | " 'tan',\n", 249 | " 'tanh',\n", 250 | " 'tau',\n", 251 | " 'trunc']" 252 | ] 253 | }, 254 | "metadata": {}, 255 | "execution_count": 4 256 | } 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": { 262 | "id": "RKJPIBVeQ-Rr" 263 | }, 264 | "source": [ 265 | "## Understanding data types in Python\n", 266 | "\n", 267 | "Python has the following data types built-in by default:\n", 268 | "\n", 269 | "Text: `str`\n", 270 | "\n", 271 | "Numeric: `int`, `float`, `complex`\n", 272 | "\n", 273 | "Sequence: `list`, `tuple`, `range`\n", 274 | "\n", 275 | "Mapping: `dict`\n", 276 | "\n", 277 | "Set: `set`, `frozenset`\n", 278 | "\n", 279 | "Boolean: `bool`\n", 280 | "\n", 281 | "Binary: `bytes`, `bytearray`, `memoryview`" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "metadata": { 287 | "id": "jWGwNMZ3SfpC", 288 | "colab": { 289 | "base_uri": "https://localhost:8080/" 290 | }, 291 | "outputId": "fd0e74cf-972c-48ee-da32-adc3171e08c1" 292 | }, 293 | "source": [ 294 | "# type(variable) prints the data type of the variable. In this case, string\n", 295 | "\n", 296 | "name = 'Stephen ' + 'Colbert'\n", 297 | "print(name)\n", 298 | "\n", 299 | "print(type(name))\n", 300 | "type(name)" 301 | ], 302 | "execution_count": null, 303 | "outputs": [ 304 | { 305 | "output_type": "stream", 306 | "name": "stdout", 307 | "text": [ 308 | "Stephen Colbert\n", 309 | "\n" 310 | ] 311 | }, 312 | { 313 | "output_type": "execute_result", 314 | "data": { 315 | "text/plain": [ 316 | "str" 317 | ] 318 | }, 319 | "metadata": {}, 320 | "execution_count": 7 321 | } 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": { 327 | "id": "c9H2KIZrTP8x" 328 | }, 329 | "source": [ 330 | "**NOTE:** The initialization of datat type to a particular variable happens automaticaly when we assign a value to a variable." 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "metadata": { 336 | "id": "zd7DOnqVSyNv", 337 | "colab": { 338 | "base_uri": "https://localhost:8080/" 339 | }, 340 | "outputId": "f0d9001b-214e-441e-e9c1-089ade046f82" 341 | }, 342 | "source": [ 343 | "# class integer\n", 344 | "\n", 345 | "x = 10\n", 346 | "print(x)\n", 347 | "\n", 348 | "print(type(x))" 349 | ], 350 | "execution_count": null, 351 | "outputs": [ 352 | { 353 | "output_type": "stream", 354 | "name": "stdout", 355 | "text": [ 356 | "10\n", 357 | "\n" 358 | ] 359 | } 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "metadata": { 365 | "id": "li1jP3axTX-l", 366 | "colab": { 367 | "base_uri": "https://localhost:8080/" 368 | }, 369 | "outputId": "2c8a58b8-03f0-4949-96eb-bf1812bcdaa6" 370 | }, 371 | "source": [ 372 | "# class float\n", 373 | "\n", 374 | "decimal = 0.63000\n", 375 | "print(decimal)\n", 376 | "\n", 377 | "print(type(decimal))" 378 | ], 379 | "execution_count": null, 380 | "outputs": [ 381 | { 382 | "output_type": "stream", 383 | "name": "stdout", 384 | "text": [ 385 | "0.63\n", 386 | "\n" 387 | ] 388 | } 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "metadata": { 394 | "id": "IUflSg0FTjcG", 395 | "colab": { 396 | "base_uri": "https://localhost:8080/" 397 | }, 398 | "outputId": "41b57b6d-2e35-4f01-ad92-a5d995fd4cff" 399 | }, 400 | "source": [ 401 | "# class list\n", 402 | "\n", 403 | "groceries = ['apples', '20', 'cake mix', 'tp']\n", 404 | "print(groceries)\n", 405 | "\n", 406 | "print(type(groceries))" 407 | ], 408 | "execution_count": null, 409 | "outputs": [ 410 | { 411 | "output_type": "stream", 412 | "name": "stdout", 413 | "text": [ 414 | "['apples', '20', 'cake mix', 'tp']\n", 415 | "\n" 416 | ] 417 | } 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "metadata": { 423 | "id": "ZqWeUPXVT2J4", 424 | "colab": { 425 | "base_uri": "https://localhost:8080/" 426 | }, 427 | "outputId": "2d326837-9615-46a6-ad85-75b53c900f9f" 428 | }, 429 | "source": [ 430 | "# class list, but tuples\n", 431 | "\n", 432 | "a = [(1, 2), (3, 4, 5), (6, 7)]\n", 433 | "print(a)\n", 434 | "\n", 435 | "print(type(a))" 436 | ], 437 | "execution_count": null, 438 | "outputs": [ 439 | { 440 | "output_type": "stream", 441 | "name": "stdout", 442 | "text": [ 443 | "[(1, 2), (3, 4, 5), (6, 7)]\n", 444 | "\n" 445 | ] 446 | } 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "metadata": { 452 | "colab": { 453 | "base_uri": "https://localhost:8080/" 454 | }, 455 | "id": "4jvnDjzrjJt0", 456 | "outputId": "05aa4570-9c0b-4fa9-d3ab-85075551d682" 457 | }, 458 | "source": [ 459 | "x = ('Apple', 'Tim', 'Burns')\n", 460 | "\n", 461 | "print(type(x))" 462 | ], 463 | "execution_count": null, 464 | "outputs": [ 465 | { 466 | "output_type": "stream", 467 | "name": "stdout", 468 | "text": [ 469 | "\n" 470 | ] 471 | } 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "metadata": { 477 | "id": "JqynSwC0ULb_", 478 | "colab": { 479 | "base_uri": "https://localhost:8080/" 480 | }, 481 | "outputId": "d8aa4138-a0fa-4ee2-a0eb-3710095707d5" 482 | }, 483 | "source": [ 484 | "# class set\n", 485 | "\n", 486 | "sets = set((10, 2, 3, 6876))\n", 487 | "\n", 488 | "print(sets)\n", 489 | "print(type(sets))" 490 | ], 491 | "execution_count": null, 492 | "outputs": [ 493 | { 494 | "output_type": "stream", 495 | "name": "stdout", 496 | "text": [ 497 | "{3, 10, 2, 6876}\n", 498 | "\n" 499 | ] 500 | } 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "metadata": { 506 | "id": "tzzxfQLIUTc5", 507 | "colab": { 508 | "base_uri": "https://localhost:8080/" 509 | }, 510 | "outputId": "3a179f69-2c15-4c1a-9ec3-998320c85751" 511 | }, 512 | "source": [ 513 | "# class dictionary\n", 514 | "\n", 515 | "hpc = {'Harry':'10', None:'Weasley', 'Snape':5}\n", 516 | "\n", 517 | "print(hpc)\n", 518 | "print(type(hpc))" 519 | ], 520 | "execution_count": null, 521 | "outputs": [ 522 | { 523 | "output_type": "stream", 524 | "name": "stdout", 525 | "text": [ 526 | "{'Harry': '10', None: 'Weasley', 'Snape': 5}\n", 527 | "\n" 528 | ] 529 | } 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "metadata": { 535 | "id": "lE-8ouBSUySo", 536 | "colab": { 537 | "base_uri": "https://localhost:8080/" 538 | }, 539 | "outputId": "48ce8250-64cb-4e1c-96b8-62d79ab8250b" 540 | }, 541 | "source": [ 542 | "# class boolean\n", 543 | "\n", 544 | "boolean = True\n", 545 | "print(type(boolean))\n", 546 | "\n", 547 | "print(bool(-2.32))" 548 | ], 549 | "execution_count": null, 550 | "outputs": [ 551 | { 552 | "output_type": "stream", 553 | "name": "stdout", 554 | "text": [ 555 | "\n", 556 | "True\n" 557 | ] 558 | } 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "metadata": { 564 | "id": "K6lHW02cVC6f", 565 | "colab": { 566 | "base_uri": "https://localhost:8080/" 567 | }, 568 | "outputId": "392754b0-1753-4cc4-85ef-377c0f5a755f" 569 | }, 570 | "source": [ 571 | "#None type\n", 572 | "\n", 573 | "z = None\n", 574 | "print(type(z))" 575 | ], 576 | "execution_count": null, 577 | "outputs": [ 578 | { 579 | "output_type": "stream", 580 | "name": "stdout", 581 | "text": [ 582 | "\n" 583 | ] 584 | } 585 | ] 586 | }, 587 | { 588 | "cell_type": "markdown", 589 | "metadata": { 590 | "id": "43k3bdBWVLdl" 591 | }, 592 | "source": [ 593 | "## Commonly used operators in Python" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "metadata": { 599 | "id": "hRgFK9ZzVFrp", 600 | "colab": { 601 | "base_uri": "https://localhost:8080/" 602 | }, 603 | "outputId": "3fce2442-68c7-4931-9ec4-a4ec2ff11cde" 604 | }, 605 | "source": [ 606 | "a = 6.9\n", 607 | "b = 4.2\n", 608 | "\n", 609 | "print('Add: ', a+b)\n", 610 | "print('Subtract: ', a-b)\n", 611 | "print('Divide: ', a/b)\n", 612 | "print('Multiply: ', a*b)\n", 613 | "print('Floor Division: ', a//b)\n", 614 | "print('Mod: ', a%b) \n", 615 | "print('Power: ', a**b)" 616 | ], 617 | "execution_count": null, 618 | "outputs": [ 619 | { 620 | "output_type": "stream", 621 | "name": "stdout", 622 | "text": [ 623 | "Add: 11.100000000000001\n", 624 | "Subtract: 2.7\n", 625 | "Divide: 1.6428571428571428\n", 626 | "Multiply: 28.980000000000004\n", 627 | "Floor Division: 1.0\n", 628 | "Mod: 2.7\n", 629 | "Power: 3335.540214978119\n" 630 | ] 631 | } 632 | ] 633 | }, 634 | { 635 | "cell_type": "markdown", 636 | "metadata": { 637 | "id": "epgvbOtWVuId" 638 | }, 639 | "source": [ 640 | "### A few tricks" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "metadata": { 646 | "id": "NB9-F6tCVpMO", 647 | "colab": { 648 | "base_uri": "https://localhost:8080/" 649 | }, 650 | "outputId": "729b8da0-6ff1-4c86-8aa6-b9db98f544ef" 651 | }, 652 | "source": [ 653 | "a = 10\n", 654 | "b = 'Apple'\n", 655 | "\n", 656 | "print('Before swap: ', a, b)\n", 657 | "\n", 658 | "a, b = b, a\n", 659 | "print('After swap: ', a, b)" 660 | ], 661 | "execution_count": null, 662 | "outputs": [ 663 | { 664 | "output_type": "stream", 665 | "name": "stdout", 666 | "text": [ 667 | "Before swap: 10 Apple\n", 668 | "After swap: Apple 10\n" 669 | ] 670 | } 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "metadata": { 676 | "id": "SyWaqfHhV6L5", 677 | "colab": { 678 | "base_uri": "https://localhost:8080/" 679 | }, 680 | "outputId": "7b3cdef2-b75f-4f4a-a9cc-32371c3a9765" 681 | }, 682 | "source": [ 683 | "a, *b = 34, 23, 10\n", 684 | "\n", 685 | "print(type(a))\n", 686 | "print(type(b))" 687 | ], 688 | "execution_count": null, 689 | "outputs": [ 690 | { 691 | "output_type": "stream", 692 | "name": "stdout", 693 | "text": [ 694 | "\n", 695 | "\n" 696 | ] 697 | } 698 | ] 699 | }, 700 | { 701 | "cell_type": "markdown", 702 | "metadata": { 703 | "id": "8ttnUtpLWg5W" 704 | }, 705 | "source": [ 706 | "## Type casting in Python\n", 707 | "\n", 708 | "Type Casting is the method to convert the variable data type into a certain data type in order for the operation required to be performed by users." 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "metadata": { 714 | "id": "SzT49t-NWMuS", 715 | "colab": { 716 | "base_uri": "https://localhost:8080/" 717 | }, 718 | "outputId": "847b090f-2ad2-4286-88db-1f41a67d12c0" 719 | }, 720 | "source": [ 721 | "# switching from string to integer\n", 722 | "\n", 723 | "x = '10'\n", 724 | "print(type(x))\n", 725 | "\n", 726 | "print('String to Integer: ', int(x))\n", 727 | "\n", 728 | "x = int(x)\n", 729 | "print(type(x))" 730 | ], 731 | "execution_count": null, 732 | "outputs": [ 733 | { 734 | "output_type": "stream", 735 | "name": "stdout", 736 | "text": [ 737 | "\n", 738 | "String to Integer: 10\n", 739 | "\n" 740 | ] 741 | } 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "metadata": { 747 | "id": "bDZE0cGjW2RG", 748 | "colab": { 749 | "base_uri": "https://localhost:8080/" 750 | }, 751 | "outputId": "61fbd7b4-a022-4ac9-d49a-9e284ef98b76" 752 | }, 753 | "source": [ 754 | "# switching from string to float\n", 755 | "\n", 756 | "print(float(x))\n", 757 | "\n", 758 | "print(type(x))" 759 | ], 760 | "execution_count": null, 761 | "outputs": [ 762 | { 763 | "output_type": "stream", 764 | "name": "stdout", 765 | "text": [ 766 | "10.0\n", 767 | "\n" 768 | ] 769 | } 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "metadata": { 775 | "id": "ByWyHb2yXGGh", 776 | "colab": { 777 | "base_uri": "https://localhost:8080/" 778 | }, 779 | "outputId": "133d3da8-b641-4d86-d967-9921d66940c6" 780 | }, 781 | "source": [ 782 | "# switching from integer to string\n", 783 | "\n", 784 | "x = str(1) + '0'\n", 785 | "\n", 786 | "print(x)" 787 | ], 788 | "execution_count": null, 789 | "outputs": [ 790 | { 791 | "output_type": "stream", 792 | "name": "stdout", 793 | "text": [ 794 | "10\n" 795 | ] 796 | } 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "metadata": { 802 | "id": "-tbNz0ceXS4O", 803 | "colab": { 804 | "base_uri": "https://localhost:8080/" 805 | }, 806 | "outputId": "df101fcd-75a5-487b-e16d-e518574fc817" 807 | }, 808 | "source": [ 809 | "# switching from set to list\n", 810 | "\n", 811 | "s = set((1, 2, 3, 5, 8))\n", 812 | "\n", 813 | "list_s = list(s)\n", 814 | "print(list_s)" 815 | ], 816 | "execution_count": null, 817 | "outputs": [ 818 | { 819 | "output_type": "stream", 820 | "name": "stdout", 821 | "text": [ 822 | "[1, 2, 3, 5, 8]\n" 823 | ] 824 | } 825 | ] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "metadata": { 830 | "id": "92FyE9vyXwgX", 831 | "colab": { 832 | "base_uri": "https://localhost:8080/" 833 | }, 834 | "outputId": "cf851aec-997c-4bad-f32e-ab75314e2fc5" 835 | }, 836 | "source": [ 837 | "# switching from a tuple to list\n", 838 | "\n", 839 | "tuples = ((1, 2), (4, 'Ginny'), (7, 'Fred'))\n", 840 | "\n", 841 | "list_tup = list(tuples)\n", 842 | "print(list_tup)\n", 843 | "\n", 844 | "print(type(list_tup))" 845 | ], 846 | "execution_count": null, 847 | "outputs": [ 848 | { 849 | "output_type": "stream", 850 | "name": "stdout", 851 | "text": [ 852 | "[(1, 2), (4, 'Ginny'), (7, 'Fred')]\n", 853 | "\n" 854 | ] 855 | } 856 | ] 857 | }, 858 | { 859 | "cell_type": "code", 860 | "metadata": { 861 | "id": "667dU9Hgennd" 862 | }, 863 | "source": [ 864 | "s = set((1,2,3,4))\n" 865 | ], 866 | "execution_count": null, 867 | "outputs": [] 868 | }, 869 | { 870 | "cell_type": "code", 871 | "metadata": { 872 | "id": "fI4hrIYfYLYK", 873 | "colab": { 874 | "base_uri": "https://localhost:8080/" 875 | }, 876 | "outputId": "955e7c9d-aea9-48d4-b8a2-7963c5788115" 877 | }, 878 | "source": [ 879 | "# ascii value of alpha numeric characters\n", 880 | "\n", 881 | "print(chr(120))" 882 | ], 883 | "execution_count": null, 884 | "outputs": [ 885 | { 886 | "output_type": "stream", 887 | "name": "stdout", 888 | "text": [ 889 | "x\n" 890 | ] 891 | } 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "metadata": { 897 | "id": "15IRjR0fYWuA", 898 | "colab": { 899 | "base_uri": "https://localhost:8080/" 900 | }, 901 | "outputId": "26265510-62e9-472f-c076-06a6edbaa86a" 902 | }, 903 | "source": [ 904 | "# string to ascii\n", 905 | "\n", 906 | "print(ord('x'))" 907 | ], 908 | "execution_count": null, 909 | "outputs": [ 910 | { 911 | "output_type": "stream", 912 | "name": "stdout", 913 | "text": [ 914 | "120\n" 915 | ] 916 | } 917 | ] 918 | }, 919 | { 920 | "cell_type": "markdown", 921 | "metadata": { 922 | "id": "FOOWQ-hpYfDj" 923 | }, 924 | "source": [ 925 | "## Using in-built functions" 926 | ] 927 | }, 928 | { 929 | "cell_type": "markdown", 930 | "metadata": { 931 | "id": "ptbOxtLzYkwt" 932 | }, 933 | "source": [ 934 | "### Using the `random` method\n", 935 | "\n", 936 | "This method is used to geenrate 'random' values or ranges depending on the requirement." 937 | ] 938 | }, 939 | { 940 | "cell_type": "code", 941 | "metadata": { 942 | "id": "Q2-bbLCQYYmo", 943 | "colab": { 944 | "base_uri": "https://localhost:8080/" 945 | }, 946 | "outputId": "c80eb521-a701-4fb3-e086-84d57966bec2" 947 | }, 948 | "source": [ 949 | "import random\n", 950 | "\n", 951 | "# adding the start and end paramaters\n", 952 | "\n", 953 | "print(random.randint(5, 10))" 954 | ], 955 | "execution_count": null, 956 | "outputs": [ 957 | { 958 | "output_type": "stream", 959 | "name": "stdout", 960 | "text": [ 961 | "10\n" 962 | ] 963 | } 964 | ] 965 | }, 966 | { 967 | "cell_type": "markdown", 968 | "metadata": { 969 | "id": "dOhlnJ3cZOG_" 970 | }, 971 | "source": [ 972 | "### Using the `math` method\n", 973 | "\n", 974 | "You can use this method(/library) to make mathematical operations much easier." 975 | ] 976 | }, 977 | { 978 | "cell_type": "code", 979 | "metadata": { 980 | "id": "OLn6RTezZAQo", 981 | "colab": { 982 | "base_uri": "https://localhost:8080/" 983 | }, 984 | "outputId": "56eba09a-d6e0-446b-8319-61e725719689" 985 | }, 986 | "source": [ 987 | "import math\n", 988 | "print('Inbuilt value of pi: ', math.pi)\n", 989 | "print('Square root: ', math.sqrt(2209))\n", 990 | "\n", 991 | "print('Floor value: ', math.ceil(3.1415))" 992 | ], 993 | "execution_count": null, 994 | "outputs": [ 995 | { 996 | "output_type": "stream", 997 | "name": "stdout", 998 | "text": [ 999 | "Inbuilt value of pi: 3.141592653589793\n", 1000 | "Square root: 47.0\n", 1001 | "Floor value: 4\n" 1002 | ] 1003 | } 1004 | ] 1005 | }, 1006 | { 1007 | "cell_type": "markdown", 1008 | "metadata": { 1009 | "id": "AEeN-tSCaPun" 1010 | }, 1011 | "source": [ 1012 | "### Using the `datetime` method\n", 1013 | "\n", 1014 | "Yeah, nothing fancy about it. Just does what the name mentions." 1015 | ] 1016 | }, 1017 | { 1018 | "cell_type": "code", 1019 | "metadata": { 1020 | "id": "17f721kKZp2s", 1021 | "colab": { 1022 | "base_uri": "https://localhost:8080/" 1023 | }, 1024 | "outputId": "5c1ab180-003b-4aa9-c51e-f2178e495dae" 1025 | }, 1026 | "source": [ 1027 | "import datetime\n", 1028 | "\n", 1029 | "date = datetime.datetime.now()\n", 1030 | "\n", 1031 | "print(date)\n", 1032 | "print(type(date))" 1033 | ], 1034 | "execution_count": null, 1035 | "outputs": [ 1036 | { 1037 | "output_type": "stream", 1038 | "name": "stdout", 1039 | "text": [ 1040 | "2021-09-23 17:10:47.660093\n", 1041 | "\n" 1042 | ] 1043 | } 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "markdown", 1048 | "metadata": { 1049 | "id": "Z-jk8S03aqlY" 1050 | }, 1051 | "source": [ 1052 | "## Lists!" 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "code", 1057 | "metadata": { 1058 | "id": "_9wnAlTpae_u", 1059 | "colab": { 1060 | "base_uri": "https://localhost:8080/" 1061 | }, 1062 | "outputId": "3ce74d31-1bae-4b6a-89f1-b4960be49baf" 1063 | }, 1064 | "source": [ 1065 | "# initializing a list\n", 1066 | "list_1 = list()\n", 1067 | "print(list_1)\n", 1068 | "\n", 1069 | "# the data type of the variable\n", 1070 | "print(type(list_1))\n", 1071 | "\n", 1072 | "# a numeric list\n", 1073 | "list_num = [1, 2, 3, 4]\n", 1074 | "print(list_num)\n", 1075 | "\n", 1076 | "# list with objects\n", 1077 | "list_object = ['1', '6', 81, '%', 'Williamsburg']\n", 1078 | "print(list_object)\n", 1079 | "\n", 1080 | "# The operations are automatically computed when mentioned inside the list\n", 1081 | "list_ops = [1*3, 'apple'+'cheap', 10//2]\n", 1082 | "print(list_ops)" 1083 | ], 1084 | "execution_count": null, 1085 | "outputs": [ 1086 | { 1087 | "output_type": "stream", 1088 | "name": "stdout", 1089 | "text": [ 1090 | "[]\n", 1091 | "\n", 1092 | "[1, 2, 3, 4]\n", 1093 | "['1', '6', 81, '%', 'Williamsburg']\n", 1094 | "[3, 'applecheap', 5]\n" 1095 | ] 1096 | } 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "markdown", 1101 | "metadata": { 1102 | "id": "UM8zUVyNboCx" 1103 | }, 1104 | "source": [ 1105 | "### Accessing the values from a list\n", 1106 | "\n", 1107 | "**Fun fact:** Python does allow negative indexing!" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "code", 1112 | "metadata": { 1113 | "id": "5gwYaHT0bZPC", 1114 | "colab": { 1115 | "base_uri": "https://localhost:8080/", 1116 | "height": 35 1117 | }, 1118 | "outputId": "0df6b241-ddf1-4232-f0b3-cdda59956d67" 1119 | }, 1120 | "source": [ 1121 | "list_object = ['1', '6', 81, '%', 'Williamsburg']\n", 1122 | "\n", 1123 | "list_object[4]" 1124 | ], 1125 | "execution_count": null, 1126 | "outputs": [ 1127 | { 1128 | "output_type": "execute_result", 1129 | "data": { 1130 | "application/vnd.google.colaboratory.intrinsic+json": { 1131 | "type": "string" 1132 | }, 1133 | "text/plain": [ 1134 | "'Williamsburg'" 1135 | ] 1136 | }, 1137 | "metadata": {}, 1138 | "execution_count": 65 1139 | } 1140 | ] 1141 | }, 1142 | { 1143 | "cell_type": "code", 1144 | "metadata": { 1145 | "id": "9ipofY0Fb0U1", 1146 | "colab": { 1147 | "base_uri": "https://localhost:8080/" 1148 | }, 1149 | "outputId": "0074cff1-f9c7-40b4-a248-564f88b1f8cb" 1150 | }, 1151 | "source": [ 1152 | "# executing all the values from the beginning to the end\n", 1153 | "\n", 1154 | "print(list_object[2:4])" 1155 | ], 1156 | "execution_count": null, 1157 | "outputs": [ 1158 | { 1159 | "output_type": "stream", 1160 | "name": "stdout", 1161 | "text": [ 1162 | "[81, '%']\n" 1163 | ] 1164 | } 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "code", 1169 | "metadata": { 1170 | "id": "Rpr8D7wSg_HV", 1171 | "colab": { 1172 | "base_uri": "https://localhost:8080/" 1173 | }, 1174 | "outputId": "652b3605-bafa-4375-cdf1-d212ea67fd54" 1175 | }, 1176 | "source": [ 1177 | "print(list_object[:3])" 1178 | ], 1179 | "execution_count": null, 1180 | "outputs": [ 1181 | { 1182 | "output_type": "stream", 1183 | "name": "stdout", 1184 | "text": [ 1185 | "['1', '6', 81]\n" 1186 | ] 1187 | } 1188 | ] 1189 | }, 1190 | { 1191 | "cell_type": "code", 1192 | "metadata": { 1193 | "id": "xKJh-xDab_Bc", 1194 | "colab": { 1195 | "base_uri": "https://localhost:8080/" 1196 | }, 1197 | "outputId": "51c7e3d0-b272-43fd-9bad-c1a3b82bed54" 1198 | }, 1199 | "source": [ 1200 | "# let's say that we want we to skip a few values in between\n", 1201 | "\n", 1202 | "print(list_object[0:4:2])" 1203 | ], 1204 | "execution_count": null, 1205 | "outputs": [ 1206 | { 1207 | "output_type": "stream", 1208 | "name": "stdout", 1209 | "text": [ 1210 | "['1', 81]\n" 1211 | ] 1212 | } 1213 | ] 1214 | }, 1215 | { 1216 | "cell_type": "code", 1217 | "metadata": { 1218 | "id": "vZrPD263cJ5r", 1219 | "colab": { 1220 | "base_uri": "https://localhost:8080/" 1221 | }, 1222 | "outputId": "82920cfd-5838-450e-a630-732e2d341598" 1223 | }, 1224 | "source": [ 1225 | "# negative ranges\n", 1226 | "\n", 1227 | "print(list_object[-4:])" 1228 | ], 1229 | "execution_count": null, 1230 | "outputs": [ 1231 | { 1232 | "output_type": "stream", 1233 | "name": "stdout", 1234 | "text": [ 1235 | "['6', 81, '%', 'Williamsburg']\n" 1236 | ] 1237 | } 1238 | ] 1239 | }, 1240 | { 1241 | "cell_type": "markdown", 1242 | "metadata": { 1243 | "id": "L0V-j6SucW38" 1244 | }, 1245 | "source": [ 1246 | "### Modifying lists" 1247 | ] 1248 | }, 1249 | { 1250 | "cell_type": "code", 1251 | "metadata": { 1252 | "id": "vkWrEGdhcOaY", 1253 | "colab": { 1254 | "base_uri": "https://localhost:8080/" 1255 | }, 1256 | "outputId": "884721cc-9570-47ca-f45c-2b545e007d44" 1257 | }, 1258 | "source": [ 1259 | "# make changes in the list\n", 1260 | "\n", 1261 | "list_object.extend([0,1.2])\n", 1262 | "print(list_object)\n", 1263 | "\n", 1264 | "\n", 1265 | "# make range of values changes\n", 1266 | "\n", 1267 | "subset_items = [1,52,798,8]\n", 1268 | "list_object[0:4] = subset_items\n", 1269 | "print(list_object)" 1270 | ], 1271 | "execution_count": null, 1272 | "outputs": [ 1273 | { 1274 | "output_type": "stream", 1275 | "name": "stdout", 1276 | "text": [ 1277 | "['1', '6', 81, '%', 'Williamsburg', 0, 1.2]\n", 1278 | "[1, 52, 798, 8, 'Williamsburg', 0, 1.2]\n" 1279 | ] 1280 | } 1281 | ] 1282 | }, 1283 | { 1284 | "cell_type": "code", 1285 | "metadata": { 1286 | "id": "gVXZXxmRc_h5", 1287 | "colab": { 1288 | "base_uri": "https://localhost:8080/" 1289 | }, 1290 | "outputId": "80c4c481-5741-4360-e82d-28ff303f7f3e" 1291 | }, 1292 | "source": [ 1293 | "# skipping changes\n", 1294 | "\n", 1295 | "subset = ['2','45555','12']\n", 1296 | "list_object[0:5:2] = subset\n", 1297 | "print(list_object)" 1298 | ], 1299 | "execution_count": null, 1300 | "outputs": [ 1301 | { 1302 | "output_type": "stream", 1303 | "name": "stdout", 1304 | "text": [ 1305 | "['2', 52, '45555', 8, '12', 0, 1.2]\n" 1306 | ] 1307 | } 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "code", 1312 | "metadata": { 1313 | "id": "sV2V_Y_qdD9I", 1314 | "colab": { 1315 | "base_uri": "https://localhost:8080/" 1316 | }, 1317 | "outputId": "73c7352d-ac63-4b93-fa67-f3d5f6a1f394" 1318 | }, 1319 | "source": [ 1320 | "# delete an element\n", 1321 | "\n", 1322 | "list_object = [1, 2, 12, 12, 12, 3, 4, 5, 7]\n", 1323 | "\n", 1324 | "# can be used as a stack and/or a queue\n", 1325 | "\n", 1326 | "print(list_object)\n", 1327 | "list_object.pop()\n", 1328 | "print(list_object)\n", 1329 | "list_object.pop(2)\n", 1330 | "print(list_object)" 1331 | ], 1332 | "execution_count": null, 1333 | "outputs": [ 1334 | { 1335 | "output_type": "stream", 1336 | "name": "stdout", 1337 | "text": [ 1338 | "[1, 2, 12, 12, 12, 3, 4, 5, 7]\n", 1339 | "[1, 2, 12, 12, 12, 3, 4, 5]\n", 1340 | "[1, 2, 12, 12, 3, 4, 5]\n" 1341 | ] 1342 | } 1343 | ] 1344 | }, 1345 | { 1346 | "cell_type": "markdown", 1347 | "metadata": { 1348 | "id": "mEAUkcSHdd0b" 1349 | }, 1350 | "source": [ 1351 | "What do we do if there is an error while removing a value from the list and the list is empty?" 1352 | ] 1353 | }, 1354 | { 1355 | "cell_type": "code", 1356 | "metadata": { 1357 | "id": "0RVeKOizdQqL", 1358 | "colab": { 1359 | "base_uri": "https://localhost:8080/" 1360 | }, 1361 | "outputId": "6c3b45f4-5579-41ee-9649-2d469abcbcbb" 1362 | }, 1363 | "source": [ 1364 | "# we use the try except functionality\n", 1365 | "\n", 1366 | "try:\n", 1367 | " list_object.remove(10)\n", 1368 | "\n", 1369 | "except:\n", 1370 | " print('Element not in list')" 1371 | ], 1372 | "execution_count": null, 1373 | "outputs": [ 1374 | { 1375 | "output_type": "stream", 1376 | "name": "stdout", 1377 | "text": [ 1378 | "Element not in list\n" 1379 | ] 1380 | } 1381 | ] 1382 | }, 1383 | { 1384 | "cell_type": "markdown", 1385 | "metadata": { 1386 | "id": "BC8M5ZqudzqK" 1387 | }, 1388 | "source": [ 1389 | "## The `for` loop" 1390 | ] 1391 | }, 1392 | { 1393 | "cell_type": "code", 1394 | "metadata": { 1395 | "id": "eVnWXhDfdq7V", 1396 | "colab": { 1397 | "base_uri": "https://localhost:8080/" 1398 | }, 1399 | "outputId": "81b10b65-7b27-4c25-b711-ebd8cf01b385" 1400 | }, 1401 | "source": [ 1402 | "for i in range(0,5):\n", 1403 | " print(i, end=' ')" 1404 | ], 1405 | "execution_count": null, 1406 | "outputs": [ 1407 | { 1408 | "output_type": "stream", 1409 | "name": "stdout", 1410 | "text": [ 1411 | "0 1 2 3 4 " 1412 | ] 1413 | } 1414 | ] 1415 | }, 1416 | { 1417 | "cell_type": "code", 1418 | "metadata": { 1419 | "id": "BDA6B-C9fNI7", 1420 | "colab": { 1421 | "base_uri": "https://localhost:8080/" 1422 | }, 1423 | "outputId": "70d5b9df-e887-422b-a763-5cfb898869f2" 1424 | }, 1425 | "source": [ 1426 | "print('Adding the step parameter ')\n", 1427 | "\n", 1428 | "for i in range(10,2,-2):\n", 1429 | " print(i, end=' ')" 1430 | ], 1431 | "execution_count": null, 1432 | "outputs": [ 1433 | { 1434 | "output_type": "stream", 1435 | "name": "stdout", 1436 | "text": [ 1437 | "Adding the step parameter \n", 1438 | "10 8 6 4 " 1439 | ] 1440 | } 1441 | ] 1442 | }, 1443 | { 1444 | "cell_type": "code", 1445 | "metadata": { 1446 | "id": "-HopnRQRfSq8", 1447 | "colab": { 1448 | "base_uri": "https://localhost:8080/" 1449 | }, 1450 | "outputId": "59cd4d7b-0c79-494a-de8b-bf11150e0978" 1451 | }, 1452 | "source": [ 1453 | "print('Printing the values in the format of a list ')\n", 1454 | "\n", 1455 | "a = [1,2,3,4,5]\n", 1456 | "for i in a:\n", 1457 | " print(i)" 1458 | ], 1459 | "execution_count": null, 1460 | "outputs": [ 1461 | { 1462 | "output_type": "stream", 1463 | "name": "stdout", 1464 | "text": [ 1465 | "Printing the values in the format of a list \n", 1466 | "1\n", 1467 | "2\n", 1468 | "3\n", 1469 | "4\n", 1470 | "5\n" 1471 | ] 1472 | } 1473 | ] 1474 | }, 1475 | { 1476 | "cell_type": "code", 1477 | "metadata": { 1478 | "id": "ZyhGZlSufaUZ", 1479 | "colab": { 1480 | "base_uri": "https://localhost:8080/" 1481 | }, 1482 | "outputId": "15ffa4e9-bb9c-4595-970a-dec15a077c6d" 1483 | }, 1484 | "source": [ 1485 | "print('One-line for loop')\n", 1486 | "\n", 1487 | "sum_upto_10 = [i for i in range(11)]\n", 1488 | "print(sum_upto_10)" 1489 | ], 1490 | "execution_count": null, 1491 | "outputs": [ 1492 | { 1493 | "output_type": "stream", 1494 | "name": "stdout", 1495 | "text": [ 1496 | "One-line for loop\n", 1497 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" 1498 | ] 1499 | } 1500 | ] 1501 | }, 1502 | { 1503 | "cell_type": "code", 1504 | "metadata": { 1505 | "id": "U8xDb2JEfhG_", 1506 | "colab": { 1507 | "base_uri": "https://localhost:8080/" 1508 | }, 1509 | "outputId": "5837a8f6-a972-48f8-b1ee-14cb0428c048" 1510 | }, 1511 | "source": [ 1512 | "print('One-linear nested for loop Matrix Multiplication')\n", 1513 | "\n", 1514 | "matrix_mul = [j*i for i in range(5) for j in range(5,10)]\n", 1515 | "print('Matrix Looks like: ', matrix_mul)" 1516 | ], 1517 | "execution_count": null, 1518 | "outputs": [ 1519 | { 1520 | "output_type": "stream", 1521 | "name": "stdout", 1522 | "text": [ 1523 | "One-linear nested for loop Matrix Multiplication\n", 1524 | "Matrix Looks like: [0, 0, 0, 0, 0, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 18, 21, 24, 27, 20, 24, 28, 32, 36]\n" 1525 | ] 1526 | } 1527 | ] 1528 | }, 1529 | { 1530 | "cell_type": "code", 1531 | "metadata": { 1532 | "id": "T3mYXwOpfuqX", 1533 | "colab": { 1534 | "base_uri": "https://localhost:8080/" 1535 | }, 1536 | "outputId": "1979da2b-e7d9-44a8-8ce9-75f7446ae979" 1537 | }, 1538 | "source": [ 1539 | "a = ['Hello', 'world', '!']\n", 1540 | "\n", 1541 | "# iterable method 1\n", 1542 | "print('Iterable Method 1: ')\n", 1543 | "for i in range(len(a)):\n", 1544 | " print(i, a[i])\n", 1545 | "\n", 1546 | "# iterable method 2\n", 1547 | "print('\\nIterable Method 2: ')\n", 1548 | "for i in a:\n", 1549 | " print(i)\n", 1550 | "\n", 1551 | "# (iterable?) method 3\n", 1552 | "print('\\nMethod 3: ')\n", 1553 | "print(list(enumerate(a)))" 1554 | ], 1555 | "execution_count": null, 1556 | "outputs": [ 1557 | { 1558 | "output_type": "stream", 1559 | "name": "stdout", 1560 | "text": [ 1561 | "Iterable Method 1: \n", 1562 | "0 Hello\n", 1563 | "1 world\n", 1564 | "2 !\n", 1565 | "\n", 1566 | "Iterable Method 2: \n", 1567 | "Hello\n", 1568 | "world\n", 1569 | "!\n", 1570 | "\n", 1571 | "Method 3: \n", 1572 | "[(0, 'Hello'), (1, 'world'), (2, '!')]\n" 1573 | ] 1574 | } 1575 | ] 1576 | }, 1577 | { 1578 | "cell_type": "markdown", 1579 | "metadata": { 1580 | "id": "Ak9F4uRrg0n4" 1581 | }, 1582 | "source": [ 1583 | "## Functions\n", 1584 | "\n", 1585 | "To define a function, use the `def` keyword followed by the name of the function with attributes." 1586 | ] 1587 | }, 1588 | { 1589 | "cell_type": "code", 1590 | "metadata": { 1591 | "id": "048vzN1Ggkbg", 1592 | "colab": { 1593 | "base_uri": "https://localhost:8080/" 1594 | }, 1595 | "outputId": "131eb441-2128-4d36-cdde-079104d873f9" 1596 | }, 1597 | "source": [ 1598 | "def fizzBuzz():\n", 1599 | " \n", 1600 | " '''\n", 1601 | " Given a range print Fizz if divisible by 3, Buzz if divisible by 5 else FizzBuzz if divisible by 3 and 5\n", 1602 | " '''\n", 1603 | "\n", 1604 | " n = int(input('Enter a number: '))\n", 1605 | " count = 1\n", 1606 | " while (count <= n):\n", 1607 | " string = ''\n", 1608 | " if (count %3 == 0):\n", 1609 | " string = 'Fizz'\n", 1610 | " if (count % 5 == 0):\n", 1611 | " string += 'Buzz'\n", 1612 | " print(count,string)\n", 1613 | " count +=1\n", 1614 | " \n", 1615 | "fizzBuzz()" 1616 | ], 1617 | "execution_count": null, 1618 | "outputs": [ 1619 | { 1620 | "output_type": "stream", 1621 | "name": "stdout", 1622 | "text": [ 1623 | "Enter a number: 15\n", 1624 | "1 \n", 1625 | "2 \n", 1626 | "3 Fizz\n", 1627 | "4 \n", 1628 | "5 Buzz\n", 1629 | "6 Fizz\n", 1630 | "7 \n", 1631 | "8 \n", 1632 | "9 Fizz\n", 1633 | "10 Buzz\n", 1634 | "11 \n", 1635 | "12 Fizz\n", 1636 | "13 \n", 1637 | "14 \n", 1638 | "15 FizzBuzz\n" 1639 | ] 1640 | } 1641 | ] 1642 | }, 1643 | { 1644 | "cell_type": "code", 1645 | "metadata": { 1646 | "id": "xKoVLslfhIwF", 1647 | "colab": { 1648 | "base_uri": "https://localhost:8080/" 1649 | }, 1650 | "outputId": "adf74657-cb1c-4bb4-d810-e7a5bb7e0e3e" 1651 | }, 1652 | "source": [ 1653 | "def fizzBuzz():\n", 1654 | " \n", 1655 | " '''\n", 1656 | " Same function as above with more if else conditions.\n", 1657 | " '''\n", 1658 | " \n", 1659 | " n = int(input('Enter a number: '))\n", 1660 | " count = 1\n", 1661 | " while(count <= n):\n", 1662 | " if (count % 5 == 0 and count % 3 == 0):\n", 1663 | " print(count,'Fizz Buzz')\n", 1664 | " elif (count % 3 == 0):\n", 1665 | " print(count,'Fizz')\n", 1666 | " elif (count % 5 == 0):\n", 1667 | " print(count,'Buzz')\n", 1668 | " else:\n", 1669 | " print(count)\n", 1670 | " count +=1\n", 1671 | "\n", 1672 | "fizzBuzz()" 1673 | ], 1674 | "execution_count": null, 1675 | "outputs": [ 1676 | { 1677 | "output_type": "stream", 1678 | "name": "stdout", 1679 | "text": [ 1680 | "Enter a number: 15\n", 1681 | "1\n", 1682 | "2\n", 1683 | "3 Fizz\n", 1684 | "4\n", 1685 | "5 Buzz\n", 1686 | "6 Fizz\n", 1687 | "7\n", 1688 | "8\n", 1689 | "9 Fizz\n", 1690 | "10 Buzz\n", 1691 | "11\n", 1692 | "12 Fizz\n", 1693 | "13\n", 1694 | "14\n", 1695 | "15 Fizz Buzz\n" 1696 | ] 1697 | } 1698 | ] 1699 | }, 1700 | { 1701 | "cell_type": "markdown", 1702 | "metadata": { 1703 | "id": "Hej1NI9LjM_U" 1704 | }, 1705 | "source": [ 1706 | "## Dictionaries\n", 1707 | "\n", 1708 | "In the case of dictionaries, we use `key` and not indexes to refer elements in the dictionary." 1709 | ] 1710 | }, 1711 | { 1712 | "cell_type": "code", 1713 | "metadata": { 1714 | "id": "zuUqQDD4hVVI", 1715 | "colab": { 1716 | "base_uri": "https://localhost:8080/" 1717 | }, 1718 | "outputId": "e10b1823-afc9-4334-f697-45f33e538031" 1719 | }, 1720 | "source": [ 1721 | "# creating an empty dictionary\n", 1722 | "dict_ = {}\n", 1723 | "\n", 1724 | "#dict_ = {key:value,key:value} \n", 1725 | "# keys have to be in a set i.e., they are to be unique\n", 1726 | "\n", 1727 | "string = 'iamdictionaryiteratemeandseethefun'\n", 1728 | "\n", 1729 | "for ch in string:\n", 1730 | " if (ch in dict_):\n", 1731 | " dict_[ch] +=1\n", 1732 | "\n", 1733 | " else:\n", 1734 | " dict_[ch] = 1\n", 1735 | "\n", 1736 | "for k,v in dict_.items():\n", 1737 | " # pass\n", 1738 | " print(f'Character {k} and its count {v}')" 1739 | ], 1740 | "execution_count": null, 1741 | "outputs": [ 1742 | { 1743 | "output_type": "stream", 1744 | "name": "stdout", 1745 | "text": [ 1746 | "Character i and its count 4\n", 1747 | "Character a and its count 4\n", 1748 | "Character m and its count 2\n", 1749 | "Character d and its count 2\n", 1750 | "Character c and its count 1\n", 1751 | "Character t and its count 4\n", 1752 | "Character o and its count 1\n", 1753 | "Character n and its count 3\n", 1754 | "Character r and its count 2\n", 1755 | "Character y and its count 1\n", 1756 | "Character e and its count 6\n", 1757 | "Character s and its count 1\n", 1758 | "Character h and its count 1\n", 1759 | "Character f and its count 1\n", 1760 | "Character u and its count 1\n" 1761 | ] 1762 | } 1763 | ] 1764 | }, 1765 | { 1766 | "cell_type": "markdown", 1767 | "metadata": { 1768 | "id": "6_elLpZ6kiK8" 1769 | }, 1770 | "source": [ 1771 | "**NOTE:** If we try updating for a key that is present the original value will be overwritten" 1772 | ] 1773 | }, 1774 | { 1775 | "cell_type": "code", 1776 | "metadata": { 1777 | "id": "sN67bR2IkhBg", 1778 | "colab": { 1779 | "base_uri": "https://localhost:8080/", 1780 | "height": 196 1781 | }, 1782 | "outputId": "c711b6d1-50f5-41d2-df56-f89590caa069" 1783 | }, 1784 | "source": [ 1785 | "# print value for some key\n", 1786 | "\n", 1787 | "# Hint: use .get or check for existence before access to avoid error like below\n", 1788 | "\n", 1789 | "print(dict_['z'])" 1790 | ], 1791 | "execution_count": null, 1792 | "outputs": [ 1793 | { 1794 | "output_type": "error", 1795 | "ename": "KeyError", 1796 | "evalue": "ignored", 1797 | "traceback": [ 1798 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1799 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 1800 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Hint: use .get or check for existence before access to avoid error like below\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'z'\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", 1801 | "\u001b[0;31mKeyError\u001b[0m: 'z'" 1802 | ] 1803 | } 1804 | ] 1805 | }, 1806 | { 1807 | "cell_type": "code", 1808 | "metadata": { 1809 | "id": "gkgH5jCblKe0", 1810 | "colab": { 1811 | "base_uri": "https://localhost:8080/" 1812 | }, 1813 | "outputId": "02340ea6-2111-4cd5-bb0b-a68007eafcfa" 1814 | }, 1815 | "source": [ 1816 | "print(dict_.get('z'))" 1817 | ], 1818 | "execution_count": null, 1819 | "outputs": [ 1820 | { 1821 | "output_type": "stream", 1822 | "name": "stdout", 1823 | "text": [ 1824 | "None\n" 1825 | ] 1826 | } 1827 | ] 1828 | }, 1829 | { 1830 | "cell_type": "markdown", 1831 | "metadata": { 1832 | "id": "WKesksqVnpSm" 1833 | }, 1834 | "source": [ 1835 | "## Sorting\n", 1836 | "\n", 1837 | "For sorting a list, the data type of all the elements should be the same." 1838 | ] 1839 | }, 1840 | { 1841 | "cell_type": "code", 1842 | "metadata": { 1843 | "id": "bZQltNt8lN0s", 1844 | "colab": { 1845 | "base_uri": "https://localhost:8080/" 1846 | }, 1847 | "outputId": "1baa4cef-f355-4b2b-cc29-b58dfee25f54" 1848 | }, 1849 | "source": [ 1850 | "list_ = ['a','3232','21332','bsaa','87','3231']\n", 1851 | "print(list_)\n", 1852 | "\n", 1853 | "print('\\nAscending order: ')\n", 1854 | "\n", 1855 | "# the sorting is done only for the first character\n", 1856 | "result = []\n", 1857 | "for i in list_:\n", 1858 | " sum_ = 0\n", 1859 | " for j in i:\n", 1860 | " sum_ = ord(j)\n", 1861 | " break\n", 1862 | " result.append(sum_)\n", 1863 | "\n", 1864 | "# it checks the first character of the string\n", 1865 | "# if there is a match, it proceeds further\n", 1866 | "print('Before sorting')\n", 1867 | "print(list_)\n", 1868 | "print(result)\n", 1869 | "\n", 1870 | "print('---------------')\n", 1871 | "print('After sorting')\n", 1872 | "list_.sort()\n", 1873 | "result.sort()\n", 1874 | "print(list_)\n", 1875 | "print(result)" 1876 | ], 1877 | "execution_count": null, 1878 | "outputs": [ 1879 | { 1880 | "output_type": "stream", 1881 | "name": "stdout", 1882 | "text": [ 1883 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1884 | "\n", 1885 | "Ascending order: \n", 1886 | "Before sorting\n", 1887 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1888 | "[97, 51, 50, 98, 56, 51]\n", 1889 | "---------------\n", 1890 | "After sorting\n", 1891 | "['21332', '3231', '3232', '87', 'a', 'bsaa']\n", 1892 | "[50, 51, 51, 56, 97, 98]\n" 1893 | ] 1894 | } 1895 | ] 1896 | }, 1897 | { 1898 | "cell_type": "markdown", 1899 | "metadata": { 1900 | "id": "RHL7DdFyobhP" 1901 | }, 1902 | "source": [ 1903 | "### Difference between `sort()` and `sorted()`\n", 1904 | "\n", 1905 | "`sort()` function will modify the list it is called on. The function modifies/sorts the list in-place and has no return value.\n", 1906 | "\n", 1907 | "On the other hand, `sorted()` function will create a new list containing a sorted version of the list it is given." 1908 | ] 1909 | }, 1910 | { 1911 | "cell_type": "code", 1912 | "metadata": { 1913 | "id": "f62xRfAAoRsc", 1914 | "colab": { 1915 | "base_uri": "https://localhost:8080/" 1916 | }, 1917 | "outputId": "5573561a-842a-4e6b-c988-347c790d5f79" 1918 | }, 1919 | "source": [ 1920 | "a = [1,3,2]\n", 1921 | "print('a: ', a)\n", 1922 | "b = sorted(a)\n", 1923 | "print('a after sorted(): ',a)\n", 1924 | "print('\\nb: ', b)\n", 1925 | "a.sort()\n", 1926 | "print('a after sort(): ', a)" 1927 | ], 1928 | "execution_count": null, 1929 | "outputs": [ 1930 | { 1931 | "output_type": "stream", 1932 | "name": "stdout", 1933 | "text": [ 1934 | "a: [1, 3, 2]\n", 1935 | "a after sorted(): [1, 3, 2]\n", 1936 | "\n", 1937 | "b: [1, 2, 3]\n", 1938 | "a after sort(): [1, 2, 3]\n" 1939 | ] 1940 | } 1941 | ] 1942 | }, 1943 | { 1944 | "cell_type": "markdown", 1945 | "metadata": { 1946 | "id": "Qc_uhmN7o9wf" 1947 | }, 1948 | "source": [ 1949 | "## `lambda` functions in Python" 1950 | ] 1951 | }, 1952 | { 1953 | "cell_type": "code", 1954 | "metadata": { 1955 | "id": "CuLsIVTpourk", 1956 | "colab": { 1957 | "base_uri": "https://localhost:8080/" 1958 | }, 1959 | "outputId": "6878fe9f-1cb2-4416-974d-5c546c26e41c" 1960 | }, 1961 | "source": [ 1962 | "add = lambda a, b, c: a+b+c\n", 1963 | "print(add(5, 6, 2))" 1964 | ], 1965 | "execution_count": null, 1966 | "outputs": [ 1967 | { 1968 | "output_type": "stream", 1969 | "name": "stdout", 1970 | "text": [ 1971 | "13\n" 1972 | ] 1973 | } 1974 | ] 1975 | }, 1976 | { 1977 | "cell_type": "code", 1978 | "metadata": { 1979 | "id": "NakEwD_bpFKh", 1980 | "colab": { 1981 | "base_uri": "https://localhost:8080/" 1982 | }, 1983 | "outputId": "520fbb8c-b832-4bd2-bcd6-e6f231fe0a59" 1984 | }, 1985 | "source": [ 1986 | "dictonary = {67:\"Ted\", 23:\"Marshall\", 12:\"Barney\", 45:\"Robin\", 11:\"Lily\", 73:\"Victoria\", 99:\"Tracy\"}\n", 1987 | "\n", 1988 | "# sort the tuple on values\n", 1989 | "tuples = sorted(dictonary.items(), key = lambda x:(x[1],x[0]), reverse=False)\n", 1990 | "print('\\nSorted on values')\n", 1991 | "for elements in tuples:\n", 1992 | " print(elements[0],\"::\",elements[1])" 1993 | ], 1994 | "execution_count": null, 1995 | "outputs": [ 1996 | { 1997 | "output_type": "stream", 1998 | "name": "stdout", 1999 | "text": [ 2000 | "\n", 2001 | "Sorted on values\n", 2002 | "12 :: Barney\n", 2003 | "11 :: Lily\n", 2004 | "23 :: Marshall\n", 2005 | "45 :: Robin\n", 2006 | "67 :: Ted\n", 2007 | "99 :: Tracy\n", 2008 | "73 :: Victoria\n" 2009 | ] 2010 | } 2011 | ] 2012 | }, 2013 | { 2014 | "cell_type": "code", 2015 | "metadata": { 2016 | "id": "zuIdrS6gpve2" 2017 | }, 2018 | "source": [ 2019 | "" 2020 | ], 2021 | "execution_count": null, 2022 | "outputs": [] 2023 | } 2024 | ] 2025 | } -------------------------------------------------------------------------------- /Week_01__Introduction(F_21)_Wednesday.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Week_01:_Introduction(F-21)-Wednesday.ipynb", 7 | "provenance": [], 8 | "toc_visible": 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": "7SjRpRssC2e-" 23 | }, 24 | "source": [ 25 | "# Introduction to Python" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "metadata": { 31 | "id": "B3-JWyWzCwrm", 32 | "colab": { 33 | "base_uri": "https://localhost:8080/" 34 | }, 35 | "outputId": "925bcf85-24b4-4282-c428-b6bca67c8070" 36 | }, 37 | "source": [ 38 | "print('Hello, world!')" 39 | ], 40 | "execution_count": null, 41 | "outputs": [ 42 | { 43 | "output_type": "stream", 44 | "name": "stdout", 45 | "text": [ 46 | "Hello, world!\n" 47 | ] 48 | } 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": { 54 | "id": "TI2v4cRlDXaK" 55 | }, 56 | "source": [ 57 | "## Just for fun!\n", 58 | "\n", 59 | "Did you know that entering `import this` prints the 'Zen of Python'?\n", 60 | "\n", 61 | "The Zen of Python is a collection of 19 \"guiding principles\" for writing computer programs that influence the design of the Python programming language.\n", 62 | "\n", 63 | "It is always good to follow the zen even when you have an opportunity to show off your Python skills! :)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "metadata": { 69 | "id": "wx9lQCM0DfmX", 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | }, 73 | "outputId": "96390872-38b1-44f6-bd05-a08b7c1cda33" 74 | }, 75 | "source": [ 76 | "import this" 77 | ], 78 | "execution_count": null, 79 | "outputs": [ 80 | { 81 | "output_type": "stream", 82 | "name": "stdout", 83 | "text": [ 84 | "The Zen of Python, by Tim Peters\n", 85 | "\n", 86 | "Beautiful is better than ugly.\n", 87 | "Explicit is better than implicit.\n", 88 | "Simple is better than complex.\n", 89 | "Complex is better than complicated.\n", 90 | "Flat is better than nested.\n", 91 | "Sparse is better than dense.\n", 92 | "Readability counts.\n", 93 | "Special cases aren't special enough to break the rules.\n", 94 | "Although practicality beats purity.\n", 95 | "Errors should never pass silently.\n", 96 | "Unless explicitly silenced.\n", 97 | "In the face of ambiguity, refuse the temptation to guess.\n", 98 | "There should be one-- and preferably only one --obvious way to do it.\n", 99 | "Although that way may not be obvious at first unless you're Dutch.\n", 100 | "Now is better than never.\n", 101 | "Although never is often better than *right* now.\n", 102 | "If the implementation is hard to explain, it's a bad idea.\n", 103 | "If the implementation is easy to explain, it may be a good idea.\n", 104 | "Namespaces are one honking great idea -- let's do more of those!\n" 105 | ] 106 | } 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": { 112 | "id": "ZPcJ_rESHwum" 113 | }, 114 | "source": [ 115 | "## The `help()` function\n", 116 | "\n", 117 | "The `help()` function in python is used to display the documentation of modules, functions, classes, etc.\n" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "metadata": { 123 | "id": "44SQH6V9DgVV", 124 | "colab": { 125 | "base_uri": "https://localhost:8080/" 126 | }, 127 | "outputId": "f171558c-8229-4516-fba3-6d665bfe4518" 128 | }, 129 | "source": [ 130 | "# help([object])\n", 131 | "\n", 132 | "help(print)" 133 | ], 134 | "execution_count": null, 135 | "outputs": [ 136 | { 137 | "output_type": "stream", 138 | "name": "stdout", 139 | "text": [ 140 | "Help on built-in function print in module builtins:\n", 141 | "\n", 142 | "print(...)\n", 143 | " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 144 | " \n", 145 | " Prints the values to a stream, or to sys.stdout by default.\n", 146 | " Optional keyword arguments:\n", 147 | " file: a file-like object (stream); defaults to the current sys.stdout.\n", 148 | " sep: string inserted between values, default a space.\n", 149 | " end: string appended after the last value, default a newline.\n", 150 | " flush: whether to forcibly flush the stream.\n", 151 | "\n" 152 | ] 153 | } 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": { 159 | "id": "AypvTz1OJgEv" 160 | }, 161 | "source": [ 162 | "**NOTE:** If the `help()` function is passed without an argument,then the interactive help utility starts up on the console." 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": { 168 | "id": "U2Gx61gZJ5oj" 169 | }, 170 | "source": [ 171 | "## Accessing the Attributes (/Methods) of any object\n", 172 | "\n", 173 | "In order to understand the pre-loaded attributes of an object, we use the `dir()` function." 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "metadata": { 179 | "id": "HVLY2TTyJuYy", 180 | "colab": { 181 | "base_uri": "https://localhost:8080/" 182 | }, 183 | "outputId": "8826426c-aa55-4123-a812-732ab09ea2e2" 184 | }, 185 | "source": [ 186 | "# import [library]\n", 187 | "# dir([library])\n", 188 | "\n", 189 | "import math\n", 190 | "dir(math)" 191 | ], 192 | "execution_count": null, 193 | "outputs": [ 194 | { 195 | "output_type": "execute_result", 196 | "data": { 197 | "text/plain": [ 198 | "['__doc__',\n", 199 | " '__loader__',\n", 200 | " '__name__',\n", 201 | " '__package__',\n", 202 | " '__spec__',\n", 203 | " 'acos',\n", 204 | " 'acosh',\n", 205 | " 'asin',\n", 206 | " 'asinh',\n", 207 | " 'atan',\n", 208 | " 'atan2',\n", 209 | " 'atanh',\n", 210 | " 'ceil',\n", 211 | " 'copysign',\n", 212 | " 'cos',\n", 213 | " 'cosh',\n", 214 | " 'degrees',\n", 215 | " 'e',\n", 216 | " 'erf',\n", 217 | " 'erfc',\n", 218 | " 'exp',\n", 219 | " 'expm1',\n", 220 | " 'fabs',\n", 221 | " 'factorial',\n", 222 | " 'floor',\n", 223 | " 'fmod',\n", 224 | " 'frexp',\n", 225 | " 'fsum',\n", 226 | " 'gamma',\n", 227 | " 'gcd',\n", 228 | " 'hypot',\n", 229 | " 'inf',\n", 230 | " 'isclose',\n", 231 | " 'isfinite',\n", 232 | " 'isinf',\n", 233 | " 'isnan',\n", 234 | " 'ldexp',\n", 235 | " 'lgamma',\n", 236 | " 'log',\n", 237 | " 'log10',\n", 238 | " 'log1p',\n", 239 | " 'log2',\n", 240 | " 'modf',\n", 241 | " 'nan',\n", 242 | " 'pi',\n", 243 | " 'pow',\n", 244 | " 'radians',\n", 245 | " 'remainder',\n", 246 | " 'sin',\n", 247 | " 'sinh',\n", 248 | " 'sqrt',\n", 249 | " 'tan',\n", 250 | " 'tanh',\n", 251 | " 'tau',\n", 252 | " 'trunc']" 253 | ] 254 | }, 255 | "metadata": {}, 256 | "execution_count": 4 257 | } 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": { 263 | "id": "RKJPIBVeQ-Rr" 264 | }, 265 | "source": [ 266 | "## Understanding data types in Python\n", 267 | "\n", 268 | "Python has the following data types built-in by default:\n", 269 | "\n", 270 | "Text: `str`\n", 271 | "\n", 272 | "Numeric: `int`, `float`, `complex`\n", 273 | "\n", 274 | "Sequence: `list`, `tuple`, `range`\n", 275 | "\n", 276 | "Mapping: `dict`\n", 277 | "\n", 278 | "Set: `set`, `frozenset`\n", 279 | "\n", 280 | "Boolean: `bool`\n", 281 | "\n", 282 | "Binary: `bytes`, `bytearray`, `memoryview`" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "metadata": { 288 | "id": "jWGwNMZ3SfpC", 289 | "colab": { 290 | "base_uri": "https://localhost:8080/" 291 | }, 292 | "outputId": "4ef08c16-e57c-4c7d-97e3-7346f2f135aa" 293 | }, 294 | "source": [ 295 | "# type(variable) prints the data type of the variable. In this case, string\n", 296 | "\n", 297 | "name = 'John ' + 'Oliver'\n", 298 | "print(name)\n", 299 | "print(type(name))" 300 | ], 301 | "execution_count": null, 302 | "outputs": [ 303 | { 304 | "output_type": "stream", 305 | "name": "stdout", 306 | "text": [ 307 | "John Oliver\n", 308 | "\n" 309 | ] 310 | } 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": { 316 | "id": "c9H2KIZrTP8x" 317 | }, 318 | "source": [ 319 | "**NOTE:** The initialization of datat type to a particular variable happens automaticaly when we assign a value to a variable." 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "metadata": { 325 | "id": "zd7DOnqVSyNv", 326 | "colab": { 327 | "base_uri": "https://localhost:8080/" 328 | }, 329 | "outputId": "10ce82d4-6d27-47fa-a2b5-9797c4aaf2da" 330 | }, 331 | "source": [ 332 | "# class integer\n", 333 | "\n", 334 | "x = 10\n", 335 | "print(x)\n", 336 | "\n", 337 | "print(type(x))" 338 | ], 339 | "execution_count": null, 340 | "outputs": [ 341 | { 342 | "output_type": "stream", 343 | "name": "stdout", 344 | "text": [ 345 | "10\n", 346 | "\n" 347 | ] 348 | } 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "metadata": { 354 | "id": "li1jP3axTX-l", 355 | "colab": { 356 | "base_uri": "https://localhost:8080/" 357 | }, 358 | "outputId": "0a18359c-54ad-480c-b48b-960f70c1e2aa" 359 | }, 360 | "source": [ 361 | "# class float\n", 362 | "\n", 363 | "decimal = 0.420000\n", 364 | "print(decimal)\n", 365 | "\n", 366 | "print(type(decimal))" 367 | ], 368 | "execution_count": null, 369 | "outputs": [ 370 | { 371 | "output_type": "stream", 372 | "name": "stdout", 373 | "text": [ 374 | "0.42\n", 375 | "\n" 376 | ] 377 | } 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "metadata": { 383 | "id": "IUflSg0FTjcG", 384 | "colab": { 385 | "base_uri": "https://localhost:8080/" 386 | }, 387 | "outputId": "c312eb27-36be-4415-cea6-6828d756168e" 388 | }, 389 | "source": [ 390 | "# class list\n", 391 | "\n", 392 | "groceries = ['apples', 'bananas', 3, 3]\n", 393 | "print(groceries)\n", 394 | "\n", 395 | "print(type(groceries))\n", 396 | "\n", 397 | "type(groceries)" 398 | ], 399 | "execution_count": null, 400 | "outputs": [ 401 | { 402 | "output_type": "stream", 403 | "name": "stdout", 404 | "text": [ 405 | "['apples', 'bananas', 3, 3]\n", 406 | "\n" 407 | ] 408 | }, 409 | { 410 | "output_type": "execute_result", 411 | "data": { 412 | "text/plain": [ 413 | "list" 414 | ] 415 | }, 416 | "metadata": {}, 417 | "execution_count": 15 418 | } 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "metadata": { 424 | "id": "ZqWeUPXVT2J4", 425 | "colab": { 426 | "base_uri": "https://localhost:8080/" 427 | }, 428 | "outputId": "909d97f5-77c9-49f4-c7e8-9fbb4df766de" 429 | }, 430 | "source": [ 431 | "# class list, but tuples\n", 432 | "\n", 433 | "a = [(1,2),(3,4,5),(6,7)]\n", 434 | "print(a)\n", 435 | "\n", 436 | "print(type(a))" 437 | ], 438 | "execution_count": null, 439 | "outputs": [ 440 | { 441 | "output_type": "stream", 442 | "name": "stdout", 443 | "text": [ 444 | "[(1, 2), (3, 4, 5), (6, 7)]\n", 445 | "\n" 446 | ] 447 | } 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "metadata": { 453 | "colab": { 454 | "base_uri": "https://localhost:8080/" 455 | }, 456 | "id": "R7g76GVoYjfv", 457 | "outputId": "cd4cadb6-639f-4696-d378-a4dbce871061" 458 | }, 459 | "source": [ 460 | "x = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')\n", 461 | "print(x)\n", 462 | "\n", 463 | "print(type(x))" 464 | ], 465 | "execution_count": null, 466 | "outputs": [ 467 | { 468 | "output_type": "stream", 469 | "name": "stdout", 470 | "text": [ 471 | "('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')\n", 472 | "\n" 473 | ] 474 | } 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "metadata": { 480 | "id": "JqynSwC0ULb_", 481 | "colab": { 482 | "base_uri": "https://localhost:8080/" 483 | }, 484 | "outputId": "24256090-ef5a-4e88-d213-8127f5cca347" 485 | }, 486 | "source": [ 487 | "# class set\n", 488 | "\n", 489 | "sets = set((10,2,53,27))\n", 490 | "\n", 491 | "print(sets)\n", 492 | "print(type(sets))" 493 | ], 494 | "execution_count": null, 495 | "outputs": [ 496 | { 497 | "output_type": "stream", 498 | "name": "stdout", 499 | "text": [ 500 | "{27, 10, 2, 53}\n", 501 | "\n" 502 | ] 503 | } 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "metadata": { 509 | "id": "tzzxfQLIUTc5", 510 | "colab": { 511 | "base_uri": "https://localhost:8080/" 512 | }, 513 | "outputId": "ce5acabe-f8df-4cb6-e8f8-e7c0f6c1960c" 514 | }, 515 | "source": [ 516 | "# class dictionary\n", 517 | "\n", 518 | "hpc = {'Harry':'10', None:'Dumbeldore', 5:'Dark Arts'}\n", 519 | "\n", 520 | "print(hpc)\n", 521 | "print(type(hpc))" 522 | ], 523 | "execution_count": null, 524 | "outputs": [ 525 | { 526 | "output_type": "stream", 527 | "name": "stdout", 528 | "text": [ 529 | "{'Harry': '10', None: 'Dumbeldore', 5: 'Dark Arts'}\n", 530 | "\n" 531 | ] 532 | } 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "metadata": { 538 | "id": "lE-8ouBSUySo", 539 | "colab": { 540 | "base_uri": "https://localhost:8080/" 541 | }, 542 | "outputId": "82cfe786-8508-4ed1-9fe7-9ae0061e5e4e" 543 | }, 544 | "source": [ 545 | "# class boolean\n", 546 | "\n", 547 | "boolean = True\n", 548 | "print(boolean)\n", 549 | "\n", 550 | "print('Boolean form another data type: ', bool(-2.3))" 551 | ], 552 | "execution_count": null, 553 | "outputs": [ 554 | { 555 | "output_type": "stream", 556 | "name": "stdout", 557 | "text": [ 558 | "True\n", 559 | "Boolean form another data type: True\n" 560 | ] 561 | } 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "metadata": { 567 | "id": "K6lHW02cVC6f", 568 | "colab": { 569 | "base_uri": "https://localhost:8080/" 570 | }, 571 | "outputId": "ba5da361-4751-4c8b-bb9b-6e34cef22f04" 572 | }, 573 | "source": [ 574 | "#None type\n", 575 | "\n", 576 | "z = None\n", 577 | "print(type(z))" 578 | ], 579 | "execution_count": null, 580 | "outputs": [ 581 | { 582 | "output_type": "stream", 583 | "name": "stdout", 584 | "text": [ 585 | "\n" 586 | ] 587 | } 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": { 593 | "id": "43k3bdBWVLdl" 594 | }, 595 | "source": [ 596 | "## Commonly used operators in Python" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "metadata": { 602 | "id": "hRgFK9ZzVFrp", 603 | "colab": { 604 | "base_uri": "https://localhost:8080/" 605 | }, 606 | "outputId": "abace123-4358-4448-e25a-395a085f043b" 607 | }, 608 | "source": [ 609 | "a = 5.5\n", 610 | "b = 3.2\n", 611 | "\n", 612 | "print('Add: ', a+b)\n", 613 | "print('Subtract: ', a-b)\n", 614 | "print('Divide: ', a/b)\n", 615 | "print('Multiply: ', a*b)\n", 616 | "print('Floor Division: ', a//b)\n", 617 | "print('Mod: ', a%b) \n", 618 | "print('Power: ', a**b)" 619 | ], 620 | "execution_count": null, 621 | "outputs": [ 622 | { 623 | "output_type": "stream", 624 | "name": "stdout", 625 | "text": [ 626 | "Add: 8.7\n", 627 | "Subtract: 2.3\n", 628 | "Divide: 1.71875\n", 629 | "Multiply: 17.6\n", 630 | "Floor Division: 1.0\n", 631 | "Mod: 2.3\n", 632 | "Power: 233.97023236799288\n" 633 | ] 634 | } 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": { 640 | "id": "epgvbOtWVuId" 641 | }, 642 | "source": [ 643 | "### A few tricks" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "metadata": { 649 | "id": "NB9-F6tCVpMO", 650 | "colab": { 651 | "base_uri": "https://localhost:8080/" 652 | }, 653 | "outputId": "60a9186f-2f11-4ef0-c76d-bc76106bccb6" 654 | }, 655 | "source": [ 656 | "a = '10'\n", 657 | "b = 5\n", 658 | "c = 'Apple'\n", 659 | "\n", 660 | "print('Before swap: ', a, b, c)\n", 661 | "\n", 662 | "a, b, c = c, b, a\n", 663 | "print('After swap: ', a, b, c)" 664 | ], 665 | "execution_count": null, 666 | "outputs": [ 667 | { 668 | "output_type": "stream", 669 | "name": "stdout", 670 | "text": [ 671 | "Before swap: 10 5 Apple\n", 672 | "After swap: Apple 5 10\n" 673 | ] 674 | } 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "metadata": { 680 | "id": "SyWaqfHhV6L5", 681 | "colab": { 682 | "base_uri": "https://localhost:8080/" 683 | }, 684 | "outputId": "7084349c-4885-43c7-bd12-5b6f17b08ffd" 685 | }, 686 | "source": [ 687 | "a, *b = 6, 10, 21\n", 688 | "\n", 689 | "print(type(a))\n", 690 | "print(type(b))\n", 691 | "\n", 692 | "print(b)" 693 | ], 694 | "execution_count": null, 695 | "outputs": [ 696 | { 697 | "output_type": "stream", 698 | "name": "stdout", 699 | "text": [ 700 | "\n", 701 | "\n", 702 | "[10, 21]\n" 703 | ] 704 | } 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "metadata": { 710 | "id": "8ttnUtpLWg5W" 711 | }, 712 | "source": [ 713 | "## Type casting in Python\n", 714 | "\n", 715 | "Type Casting is the method to convert the variable data type into a certain data type in order for the operation required to be performed by users." 716 | ] 717 | }, 718 | { 719 | "cell_type": "code", 720 | "metadata": { 721 | "id": "SzT49t-NWMuS", 722 | "colab": { 723 | "base_uri": "https://localhost:8080/" 724 | }, 725 | "outputId": "e22a441d-11db-4e6f-f8fe-5daadcc9596a" 726 | }, 727 | "source": [ 728 | "# switching from string to integer\n", 729 | "\n", 730 | "x = '10'\n", 731 | "print(type(x))\n", 732 | "\n", 733 | "print('Typecasted X: ', int(x))\n", 734 | "\n", 735 | "x = int(x)\n", 736 | "print(type(x))" 737 | ], 738 | "execution_count": null, 739 | "outputs": [ 740 | { 741 | "output_type": "stream", 742 | "name": "stdout", 743 | "text": [ 744 | "\n", 745 | "Typecasted X: 10\n", 746 | "\n" 747 | ] 748 | } 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "metadata": { 754 | "id": "bDZE0cGjW2RG", 755 | "colab": { 756 | "base_uri": "https://localhost:8080/" 757 | }, 758 | "outputId": "b90ab8be-389a-4b96-b5a4-94b062061f46" 759 | }, 760 | "source": [ 761 | "# switching from string to float\n", 762 | "\n", 763 | "print(float(x))\n", 764 | "\n", 765 | "x = float(x)\n", 766 | "print(type(x))" 767 | ], 768 | "execution_count": null, 769 | "outputs": [ 770 | { 771 | "output_type": "stream", 772 | "name": "stdout", 773 | "text": [ 774 | "10.0\n", 775 | "\n" 776 | ] 777 | } 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "metadata": { 783 | "id": "fcs8xu3PW9xA" 784 | }, 785 | "source": [ 786 | "# switching from string to list\n", 787 | "\n" 788 | ], 789 | "execution_count": null, 790 | "outputs": [] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "metadata": { 795 | "id": "ByWyHb2yXGGh", 796 | "colab": { 797 | "base_uri": "https://localhost:8080/" 798 | }, 799 | "outputId": "ee2f3dbe-0531-4518-c185-12d4d227d604" 800 | }, 801 | "source": [ 802 | "# switching from integer to string\n", 803 | "\n", 804 | "x = str(1) + '0'\n", 805 | "\n", 806 | "print('Integer to String: ', x)" 807 | ], 808 | "execution_count": null, 809 | "outputs": [ 810 | { 811 | "output_type": "stream", 812 | "name": "stdout", 813 | "text": [ 814 | "Integer to String: 10\n" 815 | ] 816 | } 817 | ] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "metadata": { 822 | "id": "-tbNz0ceXS4O", 823 | "colab": { 824 | "base_uri": "https://localhost:8080/" 825 | }, 826 | "outputId": "4fae0caa-767e-4254-bcf0-f45481a0ac6e" 827 | }, 828 | "source": [ 829 | "# switching from set to list\n", 830 | "\n", 831 | "s = set((1, 2, 3, 5, 8))\n", 832 | "\n", 833 | "list_s = list(s)\n", 834 | "print(list_s)" 835 | ], 836 | "execution_count": null, 837 | "outputs": [ 838 | { 839 | "output_type": "stream", 840 | "name": "stdout", 841 | "text": [ 842 | "[1, 2, 3, 5, 8]\n" 843 | ] 844 | } 845 | ] 846 | }, 847 | { 848 | "cell_type": "code", 849 | "metadata": { 850 | "id": "92FyE9vyXwgX", 851 | "colab": { 852 | "base_uri": "https://localhost:8080/" 853 | }, 854 | "outputId": "7e730699-19bf-441a-932b-50cadcf71607" 855 | }, 856 | "source": [ 857 | "# switching from a tuple to list\n", 858 | "\n", 859 | "tuples = ((1, 2), (4, 'Ginny'), (7, 'Fred'))\n", 860 | "\n", 861 | "list_tup = list(tuples)\n", 862 | "\n", 863 | "print(list_tup)\n", 864 | "print(type(list_tup))" 865 | ], 866 | "execution_count": null, 867 | "outputs": [ 868 | { 869 | "output_type": "stream", 870 | "name": "stdout", 871 | "text": [ 872 | "[(1, 2), (4, 'Ginny'), (7, 'Fred')]\n", 873 | "\n" 874 | ] 875 | } 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "metadata": { 881 | "colab": { 882 | "base_uri": "https://localhost:8080/" 883 | }, 884 | "id": "667dU9Hgennd", 885 | "outputId": "98881da9-358d-4cbe-9287-20cd102dc5d0" 886 | }, 887 | "source": [ 888 | "s = set((1,2,3,4))\n", 889 | "list_s = list(s)\n", 890 | "type(list_s)" 891 | ], 892 | "execution_count": null, 893 | "outputs": [ 894 | { 895 | "output_type": "execute_result", 896 | "data": { 897 | "text/plain": [ 898 | "list" 899 | ] 900 | }, 901 | "metadata": {}, 902 | "execution_count": 55 903 | } 904 | ] 905 | }, 906 | { 907 | "cell_type": "code", 908 | "metadata": { 909 | "id": "fI4hrIYfYLYK", 910 | "colab": { 911 | "base_uri": "https://localhost:8080/" 912 | }, 913 | "outputId": "1b85dd5f-3e79-4d24-b3f4-0e761e5671f4" 914 | }, 915 | "source": [ 916 | "# ascii value of alpha numeric characters\n", 917 | "\n", 918 | "print(chr(120))" 919 | ], 920 | "execution_count": null, 921 | "outputs": [ 922 | { 923 | "output_type": "stream", 924 | "name": "stdout", 925 | "text": [ 926 | "x\n" 927 | ] 928 | } 929 | ] 930 | }, 931 | { 932 | "cell_type": "code", 933 | "metadata": { 934 | "id": "15IRjR0fYWuA", 935 | "colab": { 936 | "base_uri": "https://localhost:8080/" 937 | }, 938 | "outputId": "ca215731-8d29-403c-fcb4-6a94e42b9311" 939 | }, 940 | "source": [ 941 | "# string to ascii\n", 942 | "\n", 943 | "print(ord('x'))" 944 | ], 945 | "execution_count": null, 946 | "outputs": [ 947 | { 948 | "output_type": "stream", 949 | "name": "stdout", 950 | "text": [ 951 | "120\n" 952 | ] 953 | } 954 | ] 955 | }, 956 | { 957 | "cell_type": "markdown", 958 | "metadata": { 959 | "id": "FOOWQ-hpYfDj" 960 | }, 961 | "source": [ 962 | "## Using in-built functions" 963 | ] 964 | }, 965 | { 966 | "cell_type": "markdown", 967 | "metadata": { 968 | "id": "ptbOxtLzYkwt" 969 | }, 970 | "source": [ 971 | "### Using the `random` method\n", 972 | "\n", 973 | "This method is used to geenrate 'random' values or ranges depending on the requirement." 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "metadata": { 979 | "id": "Q2-bbLCQYYmo", 980 | "colab": { 981 | "base_uri": "https://localhost:8080/" 982 | }, 983 | "outputId": "3b10a631-50e2-40d2-a6d1-bbcd239e6960" 984 | }, 985 | "source": [ 986 | "import random\n", 987 | "\n", 988 | "# adding the start and end paramaters\n", 989 | "\n", 990 | "print(random.randint(5, 10))" 991 | ], 992 | "execution_count": null, 993 | "outputs": [ 994 | { 995 | "output_type": "stream", 996 | "name": "stdout", 997 | "text": [ 998 | "5\n" 999 | ] 1000 | } 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "markdown", 1005 | "metadata": { 1006 | "id": "dOhlnJ3cZOG_" 1007 | }, 1008 | "source": [ 1009 | "### Using the `math` method\n", 1010 | "\n", 1011 | "You can use this method(/library) to make mathematical operations much easier." 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "metadata": { 1017 | "id": "OLn6RTezZAQo", 1018 | "colab": { 1019 | "base_uri": "https://localhost:8080/" 1020 | }, 1021 | "outputId": "be1b6a47-4ed6-42b2-8a2e-978f39ce8da8" 1022 | }, 1023 | "source": [ 1024 | "import math\n", 1025 | "\n", 1026 | "print('Inbuilt value of pi: ', math.pi)\n", 1027 | "print('Square root: ', math.sqrt(2209))\n", 1028 | "print('Floor value: ', math.floor(3.426))\n" 1029 | ], 1030 | "execution_count": null, 1031 | "outputs": [ 1032 | { 1033 | "output_type": "stream", 1034 | "name": "stdout", 1035 | "text": [ 1036 | "Inbuilt value of pi: 3.141592653589793\n", 1037 | "Square root: 47.0\n", 1038 | "Floor value: 3\n" 1039 | ] 1040 | } 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "markdown", 1045 | "metadata": { 1046 | "id": "AEeN-tSCaPun" 1047 | }, 1048 | "source": [ 1049 | "### Using the `datetime` method\n", 1050 | "\n", 1051 | "Yeah, nothing fancy about it. Just does what the name mentions." 1052 | ] 1053 | }, 1054 | { 1055 | "cell_type": "code", 1056 | "metadata": { 1057 | "id": "17f721kKZp2s", 1058 | "colab": { 1059 | "base_uri": "https://localhost:8080/" 1060 | }, 1061 | "outputId": "f8d8fa81-483d-41be-9dc1-1cabc577d9cd" 1062 | }, 1063 | "source": [ 1064 | "import datetime\n", 1065 | "\n", 1066 | "date = datetime.datetime.now()\n", 1067 | "\n", 1068 | "print(date)\n", 1069 | "print(type(date))" 1070 | ], 1071 | "execution_count": null, 1072 | "outputs": [ 1073 | { 1074 | "output_type": "stream", 1075 | "name": "stdout", 1076 | "text": [ 1077 | "2021-09-22 17:12:57.981063\n", 1078 | "\n" 1079 | ] 1080 | } 1081 | ] 1082 | }, 1083 | { 1084 | "cell_type": "markdown", 1085 | "metadata": { 1086 | "id": "Z-jk8S03aqlY" 1087 | }, 1088 | "source": [ 1089 | "## Lists!" 1090 | ] 1091 | }, 1092 | { 1093 | "cell_type": "code", 1094 | "metadata": { 1095 | "id": "_9wnAlTpae_u", 1096 | "colab": { 1097 | "base_uri": "https://localhost:8080/" 1098 | }, 1099 | "outputId": "cefd3b2f-effa-47fe-d9ae-33b811728f2f" 1100 | }, 1101 | "source": [ 1102 | "# initializing a list\n", 1103 | "list_1 = list()\n", 1104 | "print()\n", 1105 | "\n", 1106 | "# the data type of the variable\n", 1107 | "print('List_1: ', type(list_1))\n", 1108 | "print('\\n')\n", 1109 | "\n", 1110 | "# a numeric list\n", 1111 | "list_num = [1, 2, 3, 4]\n", 1112 | "print(list_num)\n", 1113 | "print('\\n')\n", 1114 | "\n", 1115 | "# list with objects\n", 1116 | "list_object = ['1', '5', 347, '%', 'Williamsburg']\n", 1117 | "print(list_object)\n", 1118 | "\n", 1119 | "# The operations are automatically computed when mentioned inside the list\n", 1120 | "list_ops = [1*2, 'app'+'le', 10//2]\n", 1121 | "print(list_ops)" 1122 | ], 1123 | "execution_count": null, 1124 | "outputs": [ 1125 | { 1126 | "output_type": "stream", 1127 | "name": "stdout", 1128 | "text": [ 1129 | "\n", 1130 | "List_1: \n", 1131 | "\n", 1132 | "\n", 1133 | "[1, 2, 3, 4]\n", 1134 | "\n", 1135 | "\n", 1136 | "['1', '5', 347, '%', 'Williamsburg']\n", 1137 | "[2, 'apple', 5]\n" 1138 | ] 1139 | } 1140 | ] 1141 | }, 1142 | { 1143 | "cell_type": "markdown", 1144 | "metadata": { 1145 | "id": "UM8zUVyNboCx" 1146 | }, 1147 | "source": [ 1148 | "### Accessing the values from a list\n", 1149 | "\n", 1150 | "**Fun fact:** Python does allow negative indexing!" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "metadata": { 1156 | "id": "5gwYaHT0bZPC", 1157 | "colab": { 1158 | "base_uri": "https://localhost:8080/", 1159 | "height": 35 1160 | }, 1161 | "outputId": "59af94db-5ab5-4073-f56d-ff2f1e89a8d7" 1162 | }, 1163 | "source": [ 1164 | "list_object[4]" 1165 | ], 1166 | "execution_count": null, 1167 | "outputs": [ 1168 | { 1169 | "output_type": "execute_result", 1170 | "data": { 1171 | "application/vnd.google.colaboratory.intrinsic+json": { 1172 | "type": "string" 1173 | }, 1174 | "text/plain": [ 1175 | "'Williamsburg'" 1176 | ] 1177 | }, 1178 | "metadata": {}, 1179 | "execution_count": 96 1180 | } 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "code", 1185 | "metadata": { 1186 | "id": "9ipofY0Fb0U1", 1187 | "colab": { 1188 | "base_uri": "https://localhost:8080/" 1189 | }, 1190 | "outputId": "3d66ae6d-2ecb-472e-8d09-465e697830a6" 1191 | }, 1192 | "source": [ 1193 | "# executing all the values from the beginning to the end\n", 1194 | "\n", 1195 | "print(list_object[2:4])" 1196 | ], 1197 | "execution_count": null, 1198 | "outputs": [ 1199 | { 1200 | "output_type": "stream", 1201 | "name": "stdout", 1202 | "text": [ 1203 | "[347, '%']\n" 1204 | ] 1205 | } 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "code", 1210 | "metadata": { 1211 | "colab": { 1212 | "base_uri": "https://localhost:8080/" 1213 | }, 1214 | "id": "Rpr8D7wSg_HV", 1215 | "outputId": "9af3a6fa-39e9-43a1-8c27-230bd01f2550" 1216 | }, 1217 | "source": [ 1218 | "print(list_object[:2])" 1219 | ], 1220 | "execution_count": null, 1221 | "outputs": [ 1222 | { 1223 | "output_type": "stream", 1224 | "name": "stdout", 1225 | "text": [ 1226 | "['1', '5']\n" 1227 | ] 1228 | } 1229 | ] 1230 | }, 1231 | { 1232 | "cell_type": "code", 1233 | "metadata": { 1234 | "id": "xKJh-xDab_Bc", 1235 | "colab": { 1236 | "base_uri": "https://localhost:8080/" 1237 | }, 1238 | "outputId": "f8f9d6b4-9d36-4e05-b04d-d5a6bfe30e41" 1239 | }, 1240 | "source": [ 1241 | "# let's say that we want we to skip a few values in between\n", 1242 | "\n", 1243 | "print(list_object[0:8:3])" 1244 | ], 1245 | "execution_count": null, 1246 | "outputs": [ 1247 | { 1248 | "output_type": "stream", 1249 | "name": "stdout", 1250 | "text": [ 1251 | "['1', '%']\n" 1252 | ] 1253 | } 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "code", 1258 | "metadata": { 1259 | "id": "vZrPD263cJ5r", 1260 | "colab": { 1261 | "base_uri": "https://localhost:8080/" 1262 | }, 1263 | "outputId": "60ca72b9-7a27-4046-92ec-ea4dc79641c7" 1264 | }, 1265 | "source": [ 1266 | "# negative ranges\n", 1267 | "\n", 1268 | "print(list_object[-4:])" 1269 | ], 1270 | "execution_count": null, 1271 | "outputs": [ 1272 | { 1273 | "output_type": "stream", 1274 | "name": "stdout", 1275 | "text": [ 1276 | "['5', 347, '%', 'Williamsburg']\n" 1277 | ] 1278 | } 1279 | ] 1280 | }, 1281 | { 1282 | "cell_type": "markdown", 1283 | "metadata": { 1284 | "id": "L0V-j6SucW38" 1285 | }, 1286 | "source": [ 1287 | "### Modifying lists" 1288 | ] 1289 | }, 1290 | { 1291 | "cell_type": "code", 1292 | "metadata": { 1293 | "id": "vkWrEGdhcOaY", 1294 | "colab": { 1295 | "base_uri": "https://localhost:8080/" 1296 | }, 1297 | "outputId": "6893202c-7087-4605-8c10-8fc8556837e9" 1298 | }, 1299 | "source": [ 1300 | "# make changes in the list\n", 1301 | "\n", 1302 | "list_object.extend([0,1.2])\n", 1303 | "print(list_object)\n", 1304 | "\n", 1305 | "\n", 1306 | "# make range of values changes\n", 1307 | "\n", 1308 | "subset_items = [1,52,798,8]\n", 1309 | "list_object[0:4] = subset_items\n", 1310 | "print(list_object)" 1311 | ], 1312 | "execution_count": null, 1313 | "outputs": [ 1314 | { 1315 | "output_type": "stream", 1316 | "name": "stdout", 1317 | "text": [ 1318 | "['1', '5', 347, '%', 'Williamsburg', 0, 1.2]\n", 1319 | "[1, 52, 798, 8, 'Williamsburg', 0, 1.2]\n" 1320 | ] 1321 | } 1322 | ] 1323 | }, 1324 | { 1325 | "cell_type": "code", 1326 | "metadata": { 1327 | "id": "gVXZXxmRc_h5", 1328 | "colab": { 1329 | "base_uri": "https://localhost:8080/" 1330 | }, 1331 | "outputId": "a130bca6-f756-4648-bc67-452d998afe2a" 1332 | }, 1333 | "source": [ 1334 | "# skipping changes\n", 1335 | "\n", 1336 | "subset = ['2','45555','12']\n", 1337 | "list_object[0:5:2] = subset\n", 1338 | "print(list_object)" 1339 | ], 1340 | "execution_count": null, 1341 | "outputs": [ 1342 | { 1343 | "output_type": "stream", 1344 | "name": "stdout", 1345 | "text": [ 1346 | "['2', 52, '45555', 8, '12', 0, 1.2]\n" 1347 | ] 1348 | } 1349 | ] 1350 | }, 1351 | { 1352 | "cell_type": "code", 1353 | "metadata": { 1354 | "id": "sV2V_Y_qdD9I", 1355 | "colab": { 1356 | "base_uri": "https://localhost:8080/" 1357 | }, 1358 | "outputId": "7b5afd0b-ee5e-410c-fa7e-ec5f36f3a9ec" 1359 | }, 1360 | "source": [ 1361 | "# delete an element\n", 1362 | "\n", 1363 | "list_object = [1, 2, 12, 12, 12, 3, 4, 5, 7]\n", 1364 | "\n", 1365 | "# can be used as a stack and/or a queue\n", 1366 | "\n", 1367 | "list_object.pop(0)\n", 1368 | "print(list_object)\n", 1369 | "list_object.pop()\n", 1370 | "print(list_object)\n", 1371 | "list_object.pop(5)\n", 1372 | "print(list_object)" 1373 | ], 1374 | "execution_count": null, 1375 | "outputs": [ 1376 | { 1377 | "output_type": "stream", 1378 | "name": "stdout", 1379 | "text": [ 1380 | "[2, 12, 12, 12, 3, 4, 5, 7]\n", 1381 | "[2, 12, 12, 12, 3, 4, 5]\n", 1382 | "[2, 12, 12, 12, 3, 5]\n" 1383 | ] 1384 | } 1385 | ] 1386 | }, 1387 | { 1388 | "cell_type": "markdown", 1389 | "metadata": { 1390 | "id": "mEAUkcSHdd0b" 1391 | }, 1392 | "source": [ 1393 | "What do we do if there is an error while removing a value from the list and the list is empty?" 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "metadata": { 1399 | "id": "0RVeKOizdQqL", 1400 | "colab": { 1401 | "base_uri": "https://localhost:8080/" 1402 | }, 1403 | "outputId": "a200e839-080b-42d6-e104-000dc2d4d60b" 1404 | }, 1405 | "source": [ 1406 | "# we use the try except functionality\n", 1407 | "\n", 1408 | "try:\n", 1409 | " list_object.remove(10)\n", 1410 | "\n", 1411 | "except:\n", 1412 | " print('Element not in list')\n", 1413 | "\n", 1414 | "print(list_object)" 1415 | ], 1416 | "execution_count": null, 1417 | "outputs": [ 1418 | { 1419 | "output_type": "stream", 1420 | "name": "stdout", 1421 | "text": [ 1422 | "Element not in list\n", 1423 | "[2, 12, 12, 12, 3, 5]\n" 1424 | ] 1425 | } 1426 | ] 1427 | }, 1428 | { 1429 | "cell_type": "markdown", 1430 | "metadata": { 1431 | "id": "BC8M5ZqudzqK" 1432 | }, 1433 | "source": [ 1434 | "## The `for` loop" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "code", 1439 | "metadata": { 1440 | "id": "eVnWXhDfdq7V", 1441 | "colab": { 1442 | "base_uri": "https://localhost:8080/" 1443 | }, 1444 | "outputId": "e42b1692-b735-440a-fd84-a0fa49a8a8a2" 1445 | }, 1446 | "source": [ 1447 | "for i in range(0,5):\n", 1448 | " print(i, end=' ')" 1449 | ], 1450 | "execution_count": null, 1451 | "outputs": [ 1452 | { 1453 | "output_type": "stream", 1454 | "name": "stdout", 1455 | "text": [ 1456 | "0 1 2 3 4 " 1457 | ] 1458 | } 1459 | ] 1460 | }, 1461 | { 1462 | "cell_type": "code", 1463 | "metadata": { 1464 | "id": "BDA6B-C9fNI7", 1465 | "colab": { 1466 | "base_uri": "https://localhost:8080/" 1467 | }, 1468 | "outputId": "7ede781b-0584-4e48-c95d-10eb9ff82fd5" 1469 | }, 1470 | "source": [ 1471 | "print('Adding the step parameter ')\n", 1472 | "\n", 1473 | "for i in range(10,2,-2):\n", 1474 | " print(i, end=' ')" 1475 | ], 1476 | "execution_count": null, 1477 | "outputs": [ 1478 | { 1479 | "output_type": "stream", 1480 | "name": "stdout", 1481 | "text": [ 1482 | "Adding the step parameter \n", 1483 | "10 8 6 4 " 1484 | ] 1485 | } 1486 | ] 1487 | }, 1488 | { 1489 | "cell_type": "code", 1490 | "metadata": { 1491 | "id": "-HopnRQRfSq8", 1492 | "colab": { 1493 | "base_uri": "https://localhost:8080/" 1494 | }, 1495 | "outputId": "3741c087-8e96-4339-c06e-c267f97c10bc" 1496 | }, 1497 | "source": [ 1498 | "print('Printing the values in the format of a list ')\n", 1499 | "\n", 1500 | "a = [1, 2,3, 4, 5]\n", 1501 | "for i in a:\n", 1502 | " print(i)" 1503 | ], 1504 | "execution_count": null, 1505 | "outputs": [ 1506 | { 1507 | "output_type": "stream", 1508 | "name": "stdout", 1509 | "text": [ 1510 | "Printing the values in the format of a list \n", 1511 | "1\n", 1512 | "2\n", 1513 | "3\n", 1514 | "4\n", 1515 | "5\n" 1516 | ] 1517 | } 1518 | ] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "metadata": { 1523 | "id": "ZyhGZlSufaUZ", 1524 | "colab": { 1525 | "base_uri": "https://localhost:8080/" 1526 | }, 1527 | "outputId": "549fb78f-9a0b-417a-c128-678603a27fcb" 1528 | }, 1529 | "source": [ 1530 | "print('One-line for loop')\n", 1531 | "\n", 1532 | "sum_upto_10 = [i for i in range(11)]\n", 1533 | "print(sum_upto_10)" 1534 | ], 1535 | "execution_count": null, 1536 | "outputs": [ 1537 | { 1538 | "output_type": "stream", 1539 | "name": "stdout", 1540 | "text": [ 1541 | "One-line for loop\n", 1542 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" 1543 | ] 1544 | } 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "code", 1549 | "metadata": { 1550 | "id": "U8xDb2JEfhG_", 1551 | "colab": { 1552 | "base_uri": "https://localhost:8080/" 1553 | }, 1554 | "outputId": "a43a6590-7b90-4415-8ed6-e1d689315f45" 1555 | }, 1556 | "source": [ 1557 | "print('One-linear nested for loop Matrix Multiplication')\n", 1558 | "\n", 1559 | "matrix_mul = [j*i for i in range(5) for j in range(5,10)]\n", 1560 | "print('Matrix Looks like: ', matrix_mul)" 1561 | ], 1562 | "execution_count": null, 1563 | "outputs": [ 1564 | { 1565 | "output_type": "stream", 1566 | "name": "stdout", 1567 | "text": [ 1568 | "One-linear nested for loop Matrix Multiplication\n", 1569 | "Matrix Looks like: [0, 0, 0, 0, 0, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 18, 21, 24, 27, 20, 24, 28, 32, 36]\n" 1570 | ] 1571 | } 1572 | ] 1573 | }, 1574 | { 1575 | "cell_type": "code", 1576 | "metadata": { 1577 | "id": "T3mYXwOpfuqX", 1578 | "colab": { 1579 | "base_uri": "https://localhost:8080/" 1580 | }, 1581 | "outputId": "9bb47142-29b3-4978-a08b-0300e16cc8da" 1582 | }, 1583 | "source": [ 1584 | "a = ['Hello', 'world', '!']\n", 1585 | "\n", 1586 | "# iterable method 1\n", 1587 | "print('Iterable Method 1: ')\n", 1588 | "for i in range(len(a)):\n", 1589 | " print(i, a[i])\n", 1590 | "\n", 1591 | "# iterable method 2\n", 1592 | "print('\\nIterable Method 2: ')\n", 1593 | "for i in a:\n", 1594 | " print(i)\n", 1595 | "\n", 1596 | "# (iterable?) method 3\n", 1597 | "print('\\nMethod 3: ')\n", 1598 | "print(list(enumerate(a)))" 1599 | ], 1600 | "execution_count": null, 1601 | "outputs": [ 1602 | { 1603 | "output_type": "stream", 1604 | "name": "stdout", 1605 | "text": [ 1606 | "Iterable Method 1: \n", 1607 | "0 Hello\n", 1608 | "1 world\n", 1609 | "2 !\n", 1610 | "\n", 1611 | "Iterable Method 2: \n", 1612 | "Hello\n", 1613 | "world\n", 1614 | "!\n", 1615 | "\n", 1616 | "Method 3: \n", 1617 | "[(0, 'Hello'), (1, 'world'), (2, '!')]\n" 1618 | ] 1619 | } 1620 | ] 1621 | }, 1622 | { 1623 | "cell_type": "markdown", 1624 | "metadata": { 1625 | "id": "Ak9F4uRrg0n4" 1626 | }, 1627 | "source": [ 1628 | "## Functions\n", 1629 | "\n", 1630 | "To define a function, use the `def` keyword followed by the name of the function with attributes." 1631 | ] 1632 | }, 1633 | { 1634 | "cell_type": "code", 1635 | "metadata": { 1636 | "id": "048vzN1Ggkbg", 1637 | "colab": { 1638 | "base_uri": "https://localhost:8080/", 1639 | "height": 522 1640 | }, 1641 | "outputId": "3b0df29c-d62f-4df1-c1a6-149faee298f9" 1642 | }, 1643 | "source": [ 1644 | "def fizzBuzz():\n", 1645 | " \n", 1646 | " '''\n", 1647 | " Given a range print Fizz if divisible by 3, Buzz if divisible by 5 else FizzBuzz if divisible by 3 and 5\n", 1648 | " '''\n", 1649 | "\n", 1650 | " n = int(input('Enter a number: '))\n", 1651 | " count = 1\n", 1652 | " while (count <= n):\n", 1653 | " string = ''\n", 1654 | " if (count %3 == 0):\n", 1655 | " string = 'Fizz'\n", 1656 | " if (count % 5 == 0):\n", 1657 | " string += 'Buzz'\n", 1658 | " print(count,string)\n", 1659 | " count +=1\n", 1660 | " \n", 1661 | "fizzBuzz()" 1662 | ], 1663 | "execution_count": null, 1664 | "outputs": [ 1665 | { 1666 | "output_type": "error", 1667 | "ename": "KeyboardInterrupt", 1668 | "evalue": "ignored", 1669 | "traceback": [ 1670 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1671 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 1672 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 728\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\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 730\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1673 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\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 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1674 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 618\u001b[0m \"\"\"\n\u001b[0;32m--> 619\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\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\u001b[1;32m 620\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1675 | "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", 1676 | "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", 1677 | "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", 1678 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", 1679 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", 1680 | "\nDuring handling of the above exception, another exception occurred:\n", 1681 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 1682 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m+=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mfizzBuzz\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", 1683 | "\u001b[0;32m\u001b[0m in \u001b[0;36mfizzBuzz\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m '''\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Enter a number: '\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\u001b[1;32m 8\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mn\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", 1684 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\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 705\u001b[0m )\n\u001b[1;32m 706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 1685 | "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1686 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: " 1687 | ] 1688 | } 1689 | ] 1690 | }, 1691 | { 1692 | "cell_type": "code", 1693 | "metadata": { 1694 | "id": "xKoVLslfhIwF", 1695 | "colab": { 1696 | "base_uri": "https://localhost:8080/" 1697 | }, 1698 | "outputId": "6d2dd165-79e6-4041-a34f-993dfa89833f" 1699 | }, 1700 | "source": [ 1701 | "def fizzBuzz():\n", 1702 | " \n", 1703 | " '''\n", 1704 | " Same function as above with more if else conditions.\n", 1705 | " '''\n", 1706 | " \n", 1707 | " n = int(input('Enter a number: '))\n", 1708 | " count = 1\n", 1709 | " while(count <= n):\n", 1710 | " if (count % 5 == 0 and count % 3 == 0):\n", 1711 | " print(count,'Fizz Buzz')\n", 1712 | " elif (count % 3 == 0):\n", 1713 | " print(count,'Fizz')\n", 1714 | " elif (count % 5 == 0):\n", 1715 | " print(count,'Buzz')\n", 1716 | " else:\n", 1717 | " print(count)\n", 1718 | " count +=1\n", 1719 | "\n", 1720 | "fizzBuzz()" 1721 | ], 1722 | "execution_count": null, 1723 | "outputs": [ 1724 | { 1725 | "output_type": "stream", 1726 | "name": "stdout", 1727 | "text": [ 1728 | "Enter a number: 15\n", 1729 | "1\n", 1730 | "2\n", 1731 | "3 Fizz\n", 1732 | "4\n", 1733 | "5 Buzz\n", 1734 | "6 Fizz\n", 1735 | "7\n", 1736 | "8\n", 1737 | "9 Fizz\n", 1738 | "10 Buzz\n", 1739 | "11\n", 1740 | "12 Fizz\n", 1741 | "13\n", 1742 | "14\n", 1743 | "15 Fizz Buzz\n" 1744 | ] 1745 | } 1746 | ] 1747 | }, 1748 | { 1749 | "cell_type": "markdown", 1750 | "metadata": { 1751 | "id": "Hej1NI9LjM_U" 1752 | }, 1753 | "source": [ 1754 | "## Dictionaries\n", 1755 | "\n", 1756 | "In the case of dictionaries, we use `key` and not indexes to refer elements in the dictionary." 1757 | ] 1758 | }, 1759 | { 1760 | "cell_type": "code", 1761 | "metadata": { 1762 | "id": "zuUqQDD4hVVI", 1763 | "colab": { 1764 | "base_uri": "https://localhost:8080/" 1765 | }, 1766 | "outputId": "db6cdcbc-8186-4e88-9f17-e4bf4a7889ff" 1767 | }, 1768 | "source": [ 1769 | "# creating an empty dictionary\n", 1770 | "dict_ = {}\n", 1771 | "\n", 1772 | "#dict_ = {key:value,key:value} \n", 1773 | "# keys have to be in a set i.e., they are to be unique\n", 1774 | "\n", 1775 | "string = 'iamdictionaryiteratemeandseethefun'\n", 1776 | "\n", 1777 | "for ch in string:\n", 1778 | " if (ch in dict_):\n", 1779 | " dict_[ch] +=1\n", 1780 | "\n", 1781 | " else:\n", 1782 | " dict_[ch] = 1\n", 1783 | "\n", 1784 | "for k,v in dict_.items():\n", 1785 | " # pass\n", 1786 | " print(f'Character {k} and its count {v}')" 1787 | ], 1788 | "execution_count": null, 1789 | "outputs": [ 1790 | { 1791 | "output_type": "stream", 1792 | "name": "stdout", 1793 | "text": [ 1794 | "Character i and its count 4\n", 1795 | "Character a and its count 4\n", 1796 | "Character m and its count 2\n", 1797 | "Character d and its count 2\n", 1798 | "Character c and its count 1\n", 1799 | "Character t and its count 4\n", 1800 | "Character o and its count 1\n", 1801 | "Character n and its count 3\n", 1802 | "Character r and its count 2\n", 1803 | "Character y and its count 1\n", 1804 | "Character e and its count 6\n", 1805 | "Character s and its count 1\n", 1806 | "Character h and its count 1\n", 1807 | "Character f and its count 1\n", 1808 | "Character u and its count 1\n" 1809 | ] 1810 | } 1811 | ] 1812 | }, 1813 | { 1814 | "cell_type": "markdown", 1815 | "metadata": { 1816 | "id": "6_elLpZ6kiK8" 1817 | }, 1818 | "source": [ 1819 | "**NOTE:** If we try updating for a key that is present the original value will be overwritten" 1820 | ] 1821 | }, 1822 | { 1823 | "cell_type": "code", 1824 | "metadata": { 1825 | "id": "sN67bR2IkhBg", 1826 | "colab": { 1827 | "base_uri": "https://localhost:8080/", 1828 | "height": 196 1829 | }, 1830 | "outputId": "8dbf5718-9a1e-4c55-c669-843d21110ceb" 1831 | }, 1832 | "source": [ 1833 | "# print value for some key\n", 1834 | "\n", 1835 | "# Hint: use .get or check for existence before access to avoid error like below\n", 1836 | "\n", 1837 | "print(dict_['z'])" 1838 | ], 1839 | "execution_count": null, 1840 | "outputs": [ 1841 | { 1842 | "output_type": "error", 1843 | "ename": "KeyError", 1844 | "evalue": "ignored", 1845 | "traceback": [ 1846 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1847 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 1848 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Hint: use .get or check for existence before access to avoid error like below\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict_\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'z'\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", 1849 | "\u001b[0;31mKeyError\u001b[0m: 'z'" 1850 | ] 1851 | } 1852 | ] 1853 | }, 1854 | { 1855 | "cell_type": "code", 1856 | "metadata": { 1857 | "id": "gkgH5jCblKe0", 1858 | "colab": { 1859 | "base_uri": "https://localhost:8080/" 1860 | }, 1861 | "outputId": "148eafb3-4ea9-45ad-aba8-0157148a80b7" 1862 | }, 1863 | "source": [ 1864 | "print(dict_.get('z'))" 1865 | ], 1866 | "execution_count": null, 1867 | "outputs": [ 1868 | { 1869 | "output_type": "stream", 1870 | "name": "stdout", 1871 | "text": [ 1872 | "None\n" 1873 | ] 1874 | } 1875 | ] 1876 | }, 1877 | { 1878 | "cell_type": "markdown", 1879 | "metadata": { 1880 | "id": "WKesksqVnpSm" 1881 | }, 1882 | "source": [ 1883 | "## Sorting\n", 1884 | "\n", 1885 | "For sorting a list, the data type of all the elements should be the same." 1886 | ] 1887 | }, 1888 | { 1889 | "cell_type": "code", 1890 | "metadata": { 1891 | "id": "bZQltNt8lN0s", 1892 | "colab": { 1893 | "base_uri": "https://localhost:8080/" 1894 | }, 1895 | "outputId": "f05e805d-2a8a-4b77-9a8c-987eb7b9d9b9" 1896 | }, 1897 | "source": [ 1898 | "list_ = ['a','3232','21332','bsaa','87','3231']\n", 1899 | "print(list_)\n", 1900 | "\n", 1901 | "print('\\nAscending order: ')\n", 1902 | "\n", 1903 | "# the sorting is done only for the first character\n", 1904 | "result = []\n", 1905 | "for i in list_:\n", 1906 | " sum_ = 0\n", 1907 | " for j in i:\n", 1908 | " sum_ = ord(j)\n", 1909 | " break\n", 1910 | " result.append(sum_)\n", 1911 | "\n", 1912 | "# it checks the first character of the string\n", 1913 | "# if there is a match, it proceeds further\n", 1914 | "print('Before sorting')\n", 1915 | "print(list_)\n", 1916 | "print(result)\n", 1917 | "\n", 1918 | "print('---------------')\n", 1919 | "print('After sorting')\n", 1920 | "list_.sort()\n", 1921 | "result.sort()\n", 1922 | "print(list_)\n", 1923 | "print(result)" 1924 | ], 1925 | "execution_count": null, 1926 | "outputs": [ 1927 | { 1928 | "output_type": "stream", 1929 | "name": "stdout", 1930 | "text": [ 1931 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1932 | "\n", 1933 | "Ascending order: \n", 1934 | "Before sorting\n", 1935 | "['a', '3232', '21332', 'bsaa', '87', '3231']\n", 1936 | "[97, 51, 50, 98, 56, 51]\n", 1937 | "---------------\n", 1938 | "After sorting\n", 1939 | "['21332', '3231', '3232', '87', 'a', 'bsaa']\n", 1940 | "[50, 51, 51, 56, 97, 98]\n" 1941 | ] 1942 | } 1943 | ] 1944 | }, 1945 | { 1946 | "cell_type": "markdown", 1947 | "metadata": { 1948 | "id": "RHL7DdFyobhP" 1949 | }, 1950 | "source": [ 1951 | "### Difference between `sort()` and `sorted()`\n", 1952 | "\n", 1953 | "`sort()` function will modify the list it is called on. The function modifies/sorts the list in-place and has no return value.\n", 1954 | "\n", 1955 | "On the other hand, `sorted()` function will create a new list containing a sorted version of the list it is given." 1956 | ] 1957 | }, 1958 | { 1959 | "cell_type": "code", 1960 | "metadata": { 1961 | "id": "f62xRfAAoRsc", 1962 | "colab": { 1963 | "base_uri": "https://localhost:8080/" 1964 | }, 1965 | "outputId": "47e8d2e5-a209-43ab-91da-cda98f181128" 1966 | }, 1967 | "source": [ 1968 | "a = [1,3,2]\n", 1969 | "print('a: ', a)\n", 1970 | "b = sorted(a)\n", 1971 | "print('a after sorted(): ',a)\n", 1972 | "print('\\nb: ', b)\n", 1973 | "a.sort()\n", 1974 | "print('a after sort(): ', a)" 1975 | ], 1976 | "execution_count": null, 1977 | "outputs": [ 1978 | { 1979 | "output_type": "stream", 1980 | "name": "stdout", 1981 | "text": [ 1982 | "\n", 1983 | "b: [10, 21]\n" 1984 | ] 1985 | } 1986 | ] 1987 | }, 1988 | { 1989 | "cell_type": "markdown", 1990 | "metadata": { 1991 | "id": "Qc_uhmN7o9wf" 1992 | }, 1993 | "source": [ 1994 | "## `lambda` functions in Python" 1995 | ] 1996 | }, 1997 | { 1998 | "cell_type": "code", 1999 | "metadata": { 2000 | "colab": { 2001 | "base_uri": "https://localhost:8080/" 2002 | }, 2003 | "id": "CuLsIVTpourk", 2004 | "outputId": "ebc7bf2e-906d-4690-8276-3bd34b3580ea" 2005 | }, 2006 | "source": [ 2007 | "add = lambda a, b, c: a+b+c\n", 2008 | "print(add(5, 6, 2))" 2009 | ], 2010 | "execution_count": null, 2011 | "outputs": [ 2012 | { 2013 | "output_type": "stream", 2014 | "name": "stdout", 2015 | "text": [ 2016 | "13\n" 2017 | ] 2018 | } 2019 | ] 2020 | }, 2021 | { 2022 | "cell_type": "code", 2023 | "metadata": { 2024 | "id": "NakEwD_bpFKh", 2025 | "colab": { 2026 | "base_uri": "https://localhost:8080/" 2027 | }, 2028 | "outputId": "cea0ca12-d552-4c25-a690-5e8fb8765887" 2029 | }, 2030 | "source": [ 2031 | "dictonary = {67:\"Ted\", 23:\"Marshall\", 12:\"Barney\", 45:\"Robin\", 11:\"Lily\", 73:\"Victoria\", 99:\"Tracy\"}\n", 2032 | "\n", 2033 | "# sort the tuple on values\n", 2034 | "tuples = sorted(dictonary.items(), key = lambda x:(x[1],x[0]), reverse=False)\n", 2035 | "print('\\nSorted on values')\n", 2036 | "for elements in tuples:\n", 2037 | " print(elements[0],\"::\",elements[1])" 2038 | ], 2039 | "execution_count": null, 2040 | "outputs": [ 2041 | { 2042 | "output_type": "stream", 2043 | "name": "stdout", 2044 | "text": [ 2045 | "\n", 2046 | "Sorted on values\n", 2047 | "12 :: Barney\n", 2048 | "11 :: Lily\n", 2049 | "23 :: Marshall\n", 2050 | "45 :: Robin\n", 2051 | "67 :: Ted\n", 2052 | "99 :: Tracy\n", 2053 | "73 :: Victoria\n" 2054 | ] 2055 | } 2056 | ] 2057 | }, 2058 | { 2059 | "cell_type": "code", 2060 | "metadata": { 2061 | "id": "zuIdrS6gpve2" 2062 | }, 2063 | "source": [ 2064 | "" 2065 | ], 2066 | "execution_count": null, 2067 | "outputs": [] 2068 | } 2069 | ] 2070 | } -------------------------------------------------------------------------------- /task-week1/Identify.txt: -------------------------------------------------------------------------------- 1 | Please enter your details: 2 | 3 | Name: 4 | NETID: 5 | Favorite book and author: --------------------------------------------------------------------------------