├── 00 - Introduction To Python.ipynb ├── 01 - Python Variables and Basics.ipynb ├── 02 - Data Types.ipynb ├── 03 - Lists.ipynb ├── 04 - Dictionary.ipynb ├── 05 - Tuples.ipynb ├── 06 - Sets.ipynb ├── 07 - Operators in Python.ipynb ├── 08- IF, ELIF and ELSE.ipynb ├── 09 - Loops.ipynb ├── 10 - Functions.ipynb ├── 11 - Scope.ipynb ├── 12 - List Comprehensions.ipynb ├── 13 - Lambda,Map and Filter functions.ipynb ├── 14 - Exception Handling.ipynb ├── 15 - File Handling.ipynb ├── 16 - Classes and Objects.ipynb ├── 17 - NumPy1.ipynb ├── 18 - NumPy2.ipynb ├── 19 - Pandas1.ipynb ├── 20 - Pandas2.ipynb ├── 21 - Pandas_Pre_Processing.ipynb ├── 22 - Matplotlib.ipynb ├── OneHotEncodervsLabelEncoder ├── OneHotEncodervsLabelEncoder.ipynb └── carprices.csv ├── Other Py FIles └── Logging in Py.ipynb ├── img ├── data_types.png ├── img1.png ├── img2.png ├── indexing.png ├── rev_indexing.png └── truthTable.png ├── marks.csv └── train.csv /00 - Introduction To Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction To Python\n", 8 | "* It was created by **Guido van Rossum**, and released in 1991.\n", 9 | "* **Python** is a High Level, Interpreted and Object Oriented programming language. " 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## What is a Programming Language?\n", 17 | "* It is a set of instructions given to computer to get the desired output.\n", 18 | "\n", 19 | "* Programming languages can be broadly classified as 2 types:\n", 20 | " 1. **High Level programming language.**\n", 21 | " 2. **Low Level programming language.**\n", 22 | " \n", 23 | "### 1) High Level programming language\n", 24 | "* It is programmer friendly language.\n", 25 | "* It can be easily understood by the programmer.\n", 26 | "* It is simple to debug.\n", 27 | "* It is simple to maintain.\n", 28 | "* It needs compiler or interpreter for translation\n", 29 | "\n", 30 | "### 2) Low Level programming language\n", 31 | "* It is a machine friendly language (instructions will be in the form of 0s and 1s).\n", 32 | "* It is tough to understand by the programmer but it can be easily understood by the computers.\n", 33 | "* It is complex to debug comparatively.\n", 34 | "* It is complex to maintain comparatively." 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## How python executes?" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "![title](img/img2.png)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "![title](img/img1.png)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "## What can Python do?\n", 77 | "* Python can be used to create web applications.\n", 78 | "* Python can be used alongside software to create workflows.\n", 79 | "* Python can connect to database systems.\n", 80 | "* Python can also read and modify files.\n", 81 | "* Python can be used to handle big data and perform complex mathematics." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Why Python?\n", 89 | "* Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).\n", 90 | "* Python has a **simple syntax** similar to the English language.\n", 91 | "* Python has syntax that allows developers to write programs with fewer lines than some other programming languages.\n", 92 | "* Abundance of libraries and frameworks that facilitate coding and save development time.\n", 93 | "* It also boasts a **large and active community** of developers willing to provide advice and assistance through all stages of the development process." 94 | ] 95 | } 96 | ], 97 | "metadata": { 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.8.3" 114 | } 115 | }, 116 | "nbformat": 4, 117 | "nbformat_minor": 2 118 | } 119 | -------------------------------------------------------------------------------- /01 - Python Variables and Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python Variables\n", 8 | "* Variables are containers for storing data values\n", 9 | "* A variable is created the moment you first assign a value to it.\n", 10 | "* Unlike other programming languages,you dont have to declare the type of variable" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "5\n", 23 | "John\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "# Ex1)\n", 29 | "x = 5\n", 30 | "y = \"John\"\n", 31 | "\n", 32 | "print(x)\n", 33 | "print(y)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "5\n", 46 | "john\n" 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "# Ex2)\n", 52 | "# In Python, there is no need to declare the type of variable .i.e,\n", 53 | "# same varible can be used to hold different data types\n", 54 | "x = 5\n", 55 | "print(x)\n", 56 | "x = 'john'\n", 57 | "print(x)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### Convention for naming variables" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "* A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )\n", 72 | "* A variable name must start with a letter or the underscore character\n", 73 | "* A variable name cannot start with a number" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "25 30 35\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# Ex 3)\n", 91 | "# Variable names are case-sensitive (age, Age and AGE are three different variables)\n", 92 | "age = 25\n", 93 | "# print(age)\n", 94 | "Age = 30\n", 95 | "# print(age)\n", 96 | "AGE = 35\n", 97 | "print(age,Age,AGE)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "* Examples of legal and illegal variable names" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "#Legal variable names:\n", 114 | "myvar = \"Python\"\n", 115 | "my_var = \"Python\"\n", 116 | "_my_var = \"Python\"\n", 117 | "myVar = \"Python\"\n", 118 | "MYVAR = \"Python\"\n", 119 | "myvar2 = \"Python\"\n", 120 | "\n", 121 | "#Illegal variable names:\n", 122 | "2myvar = \"Python\" # starting with number\n", 123 | "my-var = \"Python\" # hyphen is not allowed \n", 124 | "my var = \"Python\" # space in between is not allowed" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## Python print() Function\n", 132 | "* The print() function prints the specified message to the screen\n", 133 | "* The message can be a string, or any other object, the object will be converted into a string before written to the screen." 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 4, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "Hello world\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "# Ex1)\n", 151 | "print(\"Hello world\")" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 5, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "Welcome to python\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "# Ex2)\n", 169 | "print(\"Welcome to python\")" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 6, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "Hello world\n" 182 | ] 183 | } 184 | ], 185 | "source": [ 186 | "# Ex3)\n", 187 | "a = \"Hello world\"\n", 188 | "print(a)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 7, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "Arun\n", 201 | "Balaji\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# Ex4)\n", 207 | "first_name = 'Arun'\n", 208 | "last_name = 'Balaji'\n", 209 | "\n", 210 | "print(first_name)\n", 211 | "print(last_name)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "# desired output is:\n", 221 | "# Arun-Balaji\n", 222 | "# by using 2 print() functions" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 8, 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "Arun-Balaji\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "# Ex5)\n", 240 | "# You can specify the end parameter\n", 241 | "print(first_name,end = '-')\n", 242 | "print(last_name)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 9, 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "I have an Apple\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "# Ex6)\n", 260 | "fruit = 'Apple'\n", 261 | "print(\"I have an \"+fruit)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 10, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "I have 2 fruits, one is Apple other one is Banana\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "# Ex7)\n", 279 | "fruit1 = 'Apple'\n", 280 | "fruit2 = 'Banana'\n", 281 | "print('I have 2 fruits, one is '+ fruit1+\" other one is \"+fruit2)" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 12, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | " I have 2 fruits, one is Apple other one is Banana\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "# Ex8) print format()\n", 299 | "print(\" I have 2 fruits, one is {} other one is {}\".format(fruit1,fruit2))" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 13, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "name": "stdout", 309 | "output_type": "stream", 310 | "text": [ 311 | " I have 2 fruits, one is Banana other one is Apple\n" 312 | ] 313 | } 314 | ], 315 | "source": [ 316 | "# Ex9) giving indexes in the placeholders\n", 317 | "print(\" I have 2 fruits, one is {1} other one is {0}\".format(fruit1,fruit2))" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "## Python Comments\n", 325 | "* Comments can be used to explain Python code.\n", 326 | "\n", 327 | "* Comments can be used to make the code more readable.\n", 328 | "\n", 329 | "* Comments can be used to prevent execution when testing code." 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "'#' is used for single line comment. Python will ignore the lines starting with '#'" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 15, 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "name": "stdout", 346 | "output_type": "stream", 347 | "text": [ 348 | "Hello world\n" 349 | ] 350 | } 351 | ], 352 | "source": [ 353 | "#this is a comment\n", 354 | "print('Hello world')" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 16, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "Hello world\n" 367 | ] 368 | } 369 | ], 370 | "source": [ 371 | "print('Hello world') #You can write comments here as well!" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 17, 377 | "metadata": {}, 378 | "outputs": [ 379 | { 380 | "name": "stdout", 381 | "output_type": "stream", 382 | "text": [ 383 | "Hello world\n", 384 | "This is our first class\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "print('Hello world')\n", 390 | "# print('Welcome to python')\n", 391 | "print('This is our first class')" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 18, 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "name": "stdout", 401 | "output_type": "stream", 402 | "text": [ 403 | "Hello-World\n" 404 | ] 405 | } 406 | ], 407 | "source": [ 408 | "# i am a \n", 409 | "# multiline \n", 410 | "# comment \n", 411 | "# using '#' \n", 412 | "# at the beginning of\n", 413 | "# each line\n", 414 | "print('Hello-World')" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "metadata": {}, 420 | "source": [ 421 | "\"\"\"\n", 422 | " ....\n", 423 | "\"\"\"\n", 424 | "is used to comment multiple lines of code" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 19, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "Hello world\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "\"\"\"\n", 442 | "i am a \n", 443 | "multiline \n", 444 | "comment \n", 445 | "using '#' \n", 446 | "at the beginning of\n", 447 | "each line\n", 448 | "\"\"\"\n", 449 | "print('Hello world')" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 20, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "Hello world\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "'''\n", 467 | "i am a \n", 468 | "multiline \n", 469 | "comment \n", 470 | "using '#' \n", 471 | "at the beginning of\n", 472 | "each line\n", 473 | "'''\n", 474 | "print('Hello world')" 475 | ] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": {}, 480 | "source": [ 481 | "## input()\n", 482 | "* The input() function allows us to take input from the user." 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 21, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "name": "stdout", 492 | "output_type": "stream", 493 | "text": [ 494 | "Enter your name : Arun\n", 495 | "hello Arun\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "# Ex1) ask name from the user\n", 501 | "name = input(\"Enter your name : \")\n", 502 | "print(\"hello \"+name)" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 22, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "name": "stdout", 512 | "output_type": "stream", 513 | "text": [ 514 | "enter the first number : 5\n", 515 | "enter the second number : 6\n", 516 | "56\n" 517 | ] 518 | } 519 | ], 520 | "source": [ 521 | "# Ex2) take two inputs(numbers) from user and add the numbers\n", 522 | "num1 = input(\"enter the first number : \")\n", 523 | "num2 = input(\"enter the second number : \")\n", 524 | "num3 = num1 + num2 \n", 525 | "print(num3)" 526 | ] 527 | }, 528 | { 529 | "cell_type": "markdown", 530 | "metadata": {}, 531 | "source": [ 532 | "#### remember, whatever value user inputs is converted to string by default" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 23, 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "name": "stdout", 542 | "output_type": "stream", 543 | "text": [ 544 | "type of num1 = and num2 = \n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "# check the type\n", 550 | "print(\"type of num1 = {} and num2 = {} \".format(type(num1),type(num2)))\n", 551 | "\n", 552 | "# convert to int\n", 553 | "num1 = int(num1)\n", 554 | "num2 = int(num2)" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 24, 560 | "metadata": {}, 561 | "outputs": [ 562 | { 563 | "name": "stdout", 564 | "output_type": "stream", 565 | "text": [ 566 | "11\n" 567 | ] 568 | } 569 | ], 570 | "source": [ 571 | "# add and print\n", 572 | "num3 = num1 + num2 \n", 573 | "print(num3)" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": null, 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [] 582 | } 583 | ], 584 | "metadata": { 585 | "kernelspec": { 586 | "display_name": "Python 3", 587 | "language": "python", 588 | "name": "python3" 589 | }, 590 | "language_info": { 591 | "codemirror_mode": { 592 | "name": "ipython", 593 | "version": 3 594 | }, 595 | "file_extension": ".py", 596 | "mimetype": "text/x-python", 597 | "name": "python", 598 | "nbconvert_exporter": "python", 599 | "pygments_lexer": "ipython3", 600 | "version": "3.7.6" 601 | } 602 | }, 603 | "nbformat": 4, 604 | "nbformat_minor": 2 605 | } 606 | -------------------------------------------------------------------------------- /04 - Dictionary.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Dictionaries\n", 8 | "In Python dictionaries are written with curly brackets and they have keys and values seperated by commas.\n", 9 | "\n", 10 | "#### Properties\n", 11 | "* Can store different data types\n", 12 | "* Order doesnt matter here\n", 13 | "* Keys should be unique\n", 14 | "* Mutable" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "data": { 24 | "text/plain": [ 25 | "dict" 26 | ] 27 | }, 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "output_type": "execute_result" 31 | } 32 | ], 33 | "source": [ 34 | "# Example\n", 35 | "dict1 = {\n", 36 | " \"Brand\":\"Bajaj\",\n", 37 | " \"model\":\"Pulsar\",\n", 38 | " \"year\":2015,\n", 39 | " \"cost\":85000\n", 40 | "}\n", 41 | "dict1\n", 42 | "type(dict1)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "4" 54 | ] 55 | }, 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "# Dictionary Length\n", 63 | "len(dict1)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "#### Accessing Items" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'Pulsar'" 82 | ] 83 | }, 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "# 1)\n", 91 | "x = dict1[\"model\"]\n", 92 | "x" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "'Pulsar'" 104 | ] 105 | }, 106 | "execution_count": 4, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "# 2) get(keyname)\n", 113 | "x = dict1.get(\"model\")\n", 114 | "x" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "#### Change/update Values" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "{'Brand': 'Bajaj', 'model': 'Pulsar', 'year': 2018, 'cost': 85000}" 133 | ] 134 | }, 135 | "execution_count": 5, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "#update the year\n", 142 | "dict1[\"year\"] = 2018\n", 143 | "dict1" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 6, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "['Brand', 'model', 'year', 'cost']\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "# keys() method to get the keys \n", 161 | "keys = list(dict1.keys())\n", 162 | "print(keys)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 7, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "['Bajaj', 'Pulsar', 2018, 85000]\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "# values() method to get the values\n", 180 | "vals = list(dict1.values())\n", 181 | "print(vals)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 8, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "[('Brand', 'Bajaj'), ('model', 'Pulsar'), ('year', 2018), ('cost', 85000)]" 193 | ] 194 | }, 195 | "execution_count": 8, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "# items() method to get both keys and values\n", 202 | "items = list(dict1.items())\n", 203 | "items" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "#### Adding items to an existing dictionary" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "{'Brand': 'Bajaj',\n", 222 | " 'model': 'Pulsar',\n", 223 | " 'year': 2018,\n", 224 | " 'cost': 85000,\n", 225 | " 'color': 'Black'}" 226 | ] 227 | }, 228 | "execution_count": 9, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "# add color of the bike\n", 235 | "dict1[\"color\"] = \"Black\"\n", 236 | "dict1" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 10, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "{'Brand': 'Bajaj',\n", 248 | " 'model': 'Pulsar',\n", 249 | " 'year': 2018,\n", 250 | " 'cost': 85000,\n", 251 | " 'color': 'Black',\n", 252 | " 'tyreType': 'Tubeless'}" 253 | ] 254 | }, 255 | "execution_count": 10, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "# add tyreType\n", 262 | "dict1['tyreType'] = 'Tubeless'\n", 263 | "dict1" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "#### Removing items" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 11, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "Pulsar\n", 283 | "{'Brand': 'Bajaj', 'year': 2018, 'cost': 85000, 'color': 'Black', 'tyreType': 'Tubeless'}\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "# 1) pop(key)\n", 289 | "rem = dict1.pop('model')\n", 290 | "print(rem)\n", 291 | "print(dict1)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 12, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "{'Brand': 'Bajaj',\n", 303 | " 'year': 2018,\n", 304 | " 'cost': 85000,\n", 305 | " 'color': 'Black',\n", 306 | " 'tyreType': 'Tubeless',\n", 307 | " 'model': 'Pulsar'}" 308 | ] 309 | }, 310 | "execution_count": 12, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "# adding again\n", 317 | "dict1['model'] = 'Pulsar'\n", 318 | "dict1" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 13, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "{'Brand': 'Bajaj', 'year': 2018, 'cost': 85000, 'color': 'Black', 'tyreType': 'Tubeless', 'model': 'Pulsar'}\n", 331 | "{'Brand': 'Bajaj', 'year': 2018, 'cost': 85000, 'color': 'Black', 'tyreType': 'Tubeless'}\n" 332 | ] 333 | } 334 | ], 335 | "source": [ 336 | "# 2) popitem() removes the last inserted item\n", 337 | "print(dict1)\n", 338 | "dict1.popitem()\n", 339 | "print(dict1)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 14, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "# 3) del keyword\n", 349 | "del dict1['year']" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 15, 355 | "metadata": {}, 356 | "outputs": [ 357 | { 358 | "data": { 359 | "text/plain": [ 360 | "{'Brand': 'Bajaj', 'cost': 85000, 'color': 'Black', 'tyreType': 'Tubeless'}" 361 | ] 362 | }, 363 | "execution_count": 15, 364 | "metadata": {}, 365 | "output_type": "execute_result" 366 | } 367 | ], 368 | "source": [ 369 | "dict1" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 16, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "# 4) clear() method\n", 379 | "dict1.clear()" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 17, 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "data": { 389 | "text/plain": [ 390 | "{}" 391 | ] 392 | }, 393 | "execution_count": 17, 394 | "metadata": {}, 395 | "output_type": "execute_result" 396 | } 397 | ], 398 | "source": [ 399 | "dict1" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "#### Copy a dictionary" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 18, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "name": "stdout", 416 | "output_type": "stream", 417 | "text": [ 418 | "{'brand': 'Bajaj', 'model': 'Pulsar', 'year': 2015, 'cost': 85000}\n" 419 | ] 420 | } 421 | ], 422 | "source": [ 423 | "# 1) copy() method\n", 424 | "dict1 = {\n", 425 | " \"brand\": \"Bajaj\",\n", 426 | " \"model\": \"Pulsar\",\n", 427 | " \"year\": 2015,\n", 428 | " \"cost\": 85000\n", 429 | "}\n", 430 | "mydict = dict1.copy()\n", 431 | "print(mydict)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "#### Nested Dictionaries" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 19, 444 | "metadata": {}, 445 | "outputs": [], 446 | "source": [ 447 | "child1 = {\n", 448 | " \"name\" : \"John\",\n", 449 | " \"year\" : 1995\n", 450 | "}\n", 451 | "child2 = {\n", 452 | " \"name\" : \"Sammy\",\n", 453 | " \"year\" : 2001\n", 454 | "}\n", 455 | "\n", 456 | "\n", 457 | "myfamily = {\n", 458 | " \"child1\" : child1,\n", 459 | " \"child2\" : child2\n", 460 | "}" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 20, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "{'child1': {'name': 'John', 'year': 1995},\n", 472 | " 'child2': {'name': 'Sammy', 'year': 2001}}" 473 | ] 474 | }, 475 | "execution_count": 20, 476 | "metadata": {}, 477 | "output_type": "execute_result" 478 | } 479 | ], 480 | "source": [ 481 | "myfamily" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 21, 487 | "metadata": {}, 488 | "outputs": [], 489 | "source": [ 490 | "myfamily = {\n", 491 | " \"child1\" : {\n", 492 | " \"name\" : \"John\",\n", 493 | " \"year\" : 1995\n", 494 | " },\n", 495 | " \"child2\" : {\n", 496 | " \"name\" : \"Sammy\",\n", 497 | " \"year\" : 2001\n", 498 | " }\n", 499 | "}" 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 22, 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "{'child1': {'name': 'John', 'year': 1995},\n", 511 | " 'child2': {'name': 'Sammy', 'year': 2001}}" 512 | ] 513 | }, 514 | "execution_count": 22, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "myfamily" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "#### Exercises on Dictionaries" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 23, 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "data": { 544 | "text/plain": [ 545 | "{'child1': {'name': 'John',\n", 546 | " 'year': 1995,\n", 547 | " 'hobbies': ['Music', 'Cricket', 'TV']},\n", 548 | " 'child2': {'name': 'Sammy', 'year': 2001}}" 549 | ] 550 | }, 551 | "execution_count": 23, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "myfamily = {\n", 558 | " \"child1\" : {\n", 559 | " \"name\" : \"John\",\n", 560 | " \"year\" : 1995,\n", 561 | " \"hobbies\":[\"Music\",\"Cricket\",\"TV\"]\n", 562 | " },\n", 563 | " \"child2\" : {\n", 564 | " \"name\" : \"Sammy\",\n", 565 | " \"year\" : 2001\n", 566 | " }\n", 567 | "}\n", 568 | "myfamily" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 24, 574 | "metadata": {}, 575 | "outputs": [ 576 | { 577 | "data": { 578 | "text/plain": [ 579 | "'John'" 580 | ] 581 | }, 582 | "execution_count": 24, 583 | "metadata": {}, 584 | "output_type": "execute_result" 585 | } 586 | ], 587 | "source": [ 588 | "#accessing \"John\"\n", 589 | "myfamily[\"child1\"][\"name\"]" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": 25, 595 | "metadata": {}, 596 | "outputs": [ 597 | { 598 | "data": { 599 | "text/plain": [ 600 | "1995" 601 | ] 602 | }, 603 | "execution_count": 25, 604 | "metadata": {}, 605 | "output_type": "execute_result" 606 | } 607 | ], 608 | "source": [ 609 | "#accessing 1995\n", 610 | "myfamily[\"child1\"][\"year\"]" 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 26, 616 | "metadata": {}, 617 | "outputs": [ 618 | { 619 | "data": { 620 | "text/plain": [ 621 | "'Sammy'" 622 | ] 623 | }, 624 | "execution_count": 26, 625 | "metadata": {}, 626 | "output_type": "execute_result" 627 | } 628 | ], 629 | "source": [ 630 | "#accessing \"Sammy\"\n", 631 | "myfamily[\"child2\"][\"name\"]" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": 27, 637 | "metadata": {}, 638 | "outputs": [ 639 | { 640 | "data": { 641 | "text/plain": [ 642 | "2001" 643 | ] 644 | }, 645 | "execution_count": 27, 646 | "metadata": {}, 647 | "output_type": "execute_result" 648 | } 649 | ], 650 | "source": [ 651 | "#accessing 2001\n", 652 | "myfamily[\"child2\"][\"year\"]" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 28, 658 | "metadata": {}, 659 | "outputs": [ 660 | { 661 | "data": { 662 | "text/plain": [ 663 | "{'child1': {'name': 'John',\n", 664 | " 'year': 1995,\n", 665 | " 'hobbies': ['Music', 'Cricket', 'TV']},\n", 666 | " 'child2': {'name': 'Sammy', 'year': 2001}}" 667 | ] 668 | }, 669 | "execution_count": 28, 670 | "metadata": {}, 671 | "output_type": "execute_result" 672 | } 673 | ], 674 | "source": [ 675 | "myfamily" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 29, 681 | "metadata": {}, 682 | "outputs": [ 683 | { 684 | "data": { 685 | "text/plain": [ 686 | "'Cricket'" 687 | ] 688 | }, 689 | "execution_count": 29, 690 | "metadata": {}, 691 | "output_type": "execute_result" 692 | } 693 | ], 694 | "source": [ 695 | "#accessing 'Cricket'\n", 696 | "myfamily['child1']['hobbies'][1]" 697 | ] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "execution_count": 30, 702 | "metadata": {}, 703 | "outputs": [ 704 | { 705 | "data": { 706 | "text/plain": [ 707 | "['Cricket', 'TV']" 708 | ] 709 | }, 710 | "execution_count": 30, 711 | "metadata": {}, 712 | "output_type": "execute_result" 713 | } 714 | ], 715 | "source": [ 716 | "#get ['Cricket','TV']\n", 717 | "myfamily['child1']['hobbies'][1:]" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 31, 723 | "metadata": {}, 724 | "outputs": [ 725 | { 726 | "data": { 727 | "text/plain": [ 728 | "['TV', 'Cricket', 'Music']" 729 | ] 730 | }, 731 | "execution_count": 31, 732 | "metadata": {}, 733 | "output_type": "execute_result" 734 | } 735 | ], 736 | "source": [ 737 | "#get ['Music','TV']\n", 738 | "myfamily['child1']['hobbies'][::-1]" 739 | ] 740 | }, 741 | { 742 | "cell_type": "markdown", 743 | "metadata": {}, 744 | "source": [ 745 | "### Task-5" 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "execution_count": 32, 751 | "metadata": {}, 752 | "outputs": [], 753 | "source": [ 754 | "dict1 = { \n", 755 | " \"april_batch\":{ \n", 756 | " \"student\":{ \n", 757 | " \"name\":\"Mike\",\n", 758 | " \"marks\":{ \n", 759 | " \"python\":80,\n", 760 | " \"maths\":70\n", 761 | " }\n", 762 | " }\n", 763 | " }\n", 764 | "}" 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": {}, 770 | "source": [ 771 | "From the above dictionary, do the following tasks\n", 772 | "1. access \"Mike\"\n", 773 | "2. access 80\n", 774 | "3. change \"Mike\" to \"Your name\"\n", 775 | "4. add ML = 80 and DL = 80 inside marks" 776 | ] 777 | } 778 | ], 779 | "metadata": { 780 | "kernelspec": { 781 | "display_name": "Python 3", 782 | "language": "python", 783 | "name": "python3" 784 | }, 785 | "language_info": { 786 | "codemirror_mode": { 787 | "name": "ipython", 788 | "version": 3 789 | }, 790 | "file_extension": ".py", 791 | "mimetype": "text/x-python", 792 | "name": "python", 793 | "nbconvert_exporter": "python", 794 | "pygments_lexer": "ipython3", 795 | "version": "3.8.3" 796 | } 797 | }, 798 | "nbformat": 4, 799 | "nbformat_minor": 2 800 | } 801 | -------------------------------------------------------------------------------- /05 - Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Tuples\n", 8 | "In Python Tuples are written with round brackets.\n", 9 | "#### Important properties of Tuples\n", 10 | "\n", 11 | "* Can store different data types\n", 12 | "* Duplicate values are allowed\n", 13 | "* Order is retained\n", 14 | "* Indexing and slicing is possible\n", 15 | "* Immutable. " 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "tup1 = (1,2,3,4.4,5.5,True,False,\"Maths\",\"Python\")" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "(1, 2, 3, 4.4, 5.5, True, False, 'Maths', 'Python')" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "tup1" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "tup2 = (1,2,3,4,5,1,1)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "(1, 2, 3, 4, 5, 1, 1)" 65 | ] 66 | }, 67 | "execution_count": 4, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "tup2" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 5, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "tuple" 85 | ] 86 | }, 87 | "execution_count": 5, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "# check the type using type()\n", 94 | "type(tup1)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 6, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "7" 106 | ] 107 | }, 108 | "execution_count": 6, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "# len()\n", 115 | "len(tup2)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "#### Create a tuple with one element" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 7, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "tup3 = (\"Python\")" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 8, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "'Python'" 143 | ] 144 | }, 145 | "execution_count": 8, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "tup3" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "str" 163 | ] 164 | }, 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "# String type\n", 172 | "type(tup3)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 10, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "# to create a tuple with one element make use of comma(,) after the value\n", 182 | "tup3 = (\"python\",)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 11, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "('python',)" 194 | ] 195 | }, 196 | "execution_count": 11, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "tup3" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 12, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "tuple" 214 | ] 215 | }, 216 | "execution_count": 12, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "type(tup3)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 13, 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "1" 234 | ] 235 | }, 236 | "execution_count": 13, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "len(tup3)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "#### Indexing\n", 250 | "Indexing starts from 0(zero). Very much similar to what we have learnt in our previous class" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 14, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "(1, 2, 3, 4.4, 5.5, True, False, 'Maths', 'Python')" 262 | ] 263 | }, 264 | "execution_count": 14, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "# to get the first element\n", 271 | "tup1" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 15, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "data": { 281 | "text/plain": [ 282 | "1" 283 | ] 284 | }, 285 | "execution_count": 15, 286 | "metadata": {}, 287 | "output_type": "execute_result" 288 | } 289 | ], 290 | "source": [ 291 | "tup1[0]" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 16, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "3" 303 | ] 304 | }, 305 | "execution_count": 16, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "# to get the third element\n", 312 | "tup1[2]" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 17, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "'Python'" 324 | ] 325 | }, 326 | "execution_count": 17, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "# to get the last element\n", 333 | "tup1[-1]" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "#### Slicing" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 18, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [ 349 | "# [start:stop:step]\n", 350 | "\n", 351 | "# start = starting position (default value is 0)\n", 352 | "# stop = end position(exclusive) (default value is end position)\n", 353 | "# step = step size or increment size (default value is 1)" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 19, 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "data": { 363 | "text/plain": [ 364 | "(1, 2, 3, 4.4, 5.5)" 365 | ] 366 | }, 367 | "execution_count": 19, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "# to get first 5 elements \n", 374 | "tup1[:5]" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 20, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "data": { 384 | "text/plain": [ 385 | "(5.5, True, False, 'Maths', 'Python')" 386 | ] 387 | }, 388 | "execution_count": 20, 389 | "metadata": {}, 390 | "output_type": "execute_result" 391 | } 392 | ], 393 | "source": [ 394 | "# to get last 5 elements \n", 395 | "tup1[-5:]" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 21, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "data": { 405 | "text/plain": [ 406 | "(1, 3, 5.5, False, 'Python')" 407 | ] 408 | }, 409 | "execution_count": 21, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "# to get alternate elements \n", 416 | "tup1[::2]" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "#### Immutable\n", 424 | "Once the tuple is created, you cannot add,remove or modify the elements" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 22, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "ename": "TypeError", 434 | "evalue": "'tuple' object does not support item assignment", 435 | "output_type": "error", 436 | "traceback": [ 437 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 438 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 439 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# try to add values\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mtup1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtup1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m6\u001b[0m \u001b[1;31m# Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 440 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 441 | ] 442 | } 443 | ], 444 | "source": [ 445 | "# try to add values\n", 446 | "tup1 = (1,2,3,4,5)\n", 447 | "tup1[5] = 6 # Error" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 23, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "ename": "AttributeError", 457 | "evalue": "'tuple' object has no attribute 'pop'", 458 | "output_type": "error", 459 | "traceback": [ 460 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 461 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 462 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# remove\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtup1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 463 | "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'pop'" 464 | ] 465 | } 466 | ], 467 | "source": [ 468 | "# remove \n", 469 | "tup1.pop()" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 24, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "data": { 479 | "text/plain": [ 480 | "(1, 2, 3, 4, 5)" 481 | ] 482 | }, 483 | "execution_count": 24, 484 | "metadata": {}, 485 | "output_type": "execute_result" 486 | } 487 | ], 488 | "source": [ 489 | "# modify the values\n", 490 | "tup1" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 25, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "ename": "TypeError", 500 | "evalue": "'tuple' object does not support item assignment", 501 | "output_type": "error", 502 | "traceback": [ 503 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 504 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 505 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m100\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 506 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 507 | ] 508 | } 509 | ], 510 | "source": [ 511 | "tup1[0] = 100 #Error" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "#### workaround to modify the values" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 26, 524 | "metadata": {}, 525 | "outputs": [ 526 | { 527 | "data": { 528 | "text/plain": [ 529 | "(1, 2, 3, 4, 5)" 530 | ] 531 | }, 532 | "execution_count": 26, 533 | "metadata": {}, 534 | "output_type": "execute_result" 535 | } 536 | ], 537 | "source": [ 538 | "# My original tuple\n", 539 | "tup1" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 27, 545 | "metadata": {}, 546 | "outputs": [ 547 | { 548 | "data": { 549 | "text/plain": [ 550 | "[1, 2, 3, 4, 5]" 551 | ] 552 | }, 553 | "execution_count": 27, 554 | "metadata": {}, 555 | "output_type": "execute_result" 556 | } 557 | ], 558 | "source": [ 559 | "#Convert to list \n", 560 | "list1 = list(tup1)\n", 561 | "list1" 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 28, 567 | "metadata": {}, 568 | "outputs": [ 569 | { 570 | "data": { 571 | "text/plain": [ 572 | "[100, 2, 3, 4, 5]" 573 | ] 574 | }, 575 | "execution_count": 28, 576 | "metadata": {}, 577 | "output_type": "execute_result" 578 | } 579 | ], 580 | "source": [ 581 | "#Modify the value\n", 582 | "list1[0] = 100\n", 583 | "list1" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 29, 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "data": { 593 | "text/plain": [ 594 | "(100, 2, 3, 4, 5)" 595 | ] 596 | }, 597 | "execution_count": 29, 598 | "metadata": {}, 599 | "output_type": "execute_result" 600 | } 601 | ], 602 | "source": [ 603 | "#again convert to tuple\n", 604 | "tup1 = tuple(list1)\n", 605 | "tup1" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "#### To check a perticular value is present " 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 30, 618 | "metadata": {}, 619 | "outputs": [ 620 | { 621 | "name": "stdout", 622 | "output_type": "stream", 623 | "text": [ 624 | "True\n" 625 | ] 626 | } 627 | ], 628 | "source": [ 629 | "#use 'in' operator\n", 630 | "#syntax ---> item in object\n", 631 | "tup1\n", 632 | "print(100 in tup1)" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 31, 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "name": "stdout", 642 | "output_type": "stream", 643 | "text": [ 644 | "False\n" 645 | ] 646 | } 647 | ], 648 | "source": [ 649 | "print(200 in tup1)" 650 | ] 651 | }, 652 | { 653 | "cell_type": "markdown", 654 | "metadata": {}, 655 | "source": [ 656 | "#### Joining tuples" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 32, 662 | "metadata": {}, 663 | "outputs": [ 664 | { 665 | "data": { 666 | "text/plain": [ 667 | "(1, 2, 3, 4, 'a', 'b', 'C', 'd')" 668 | ] 669 | }, 670 | "execution_count": 32, 671 | "metadata": {}, 672 | "output_type": "execute_result" 673 | } 674 | ], 675 | "source": [ 676 | "tup1 = (1,2,3,4)\n", 677 | "tup2 = (\"a\",\"b\",\"C\",\"d\")\n", 678 | "\n", 679 | "tup3 = tup1 + tup2\n", 680 | "tup3" 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "#### some inbuilt methods" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 33, 693 | "metadata": {}, 694 | "outputs": [], 695 | "source": [ 696 | "# tup\n", 697 | "tup = (1,2,3,4,1,1,2,1,2,3,6)" 698 | ] 699 | }, 700 | { 701 | "cell_type": "markdown", 702 | "metadata": {}, 703 | "source": [ 704 | "##### 1) count(item)\n", 705 | "Returns the number of times a specified item occurs in a tuple" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 34, 711 | "metadata": {}, 712 | "outputs": [ 713 | { 714 | "data": { 715 | "text/plain": [ 716 | "4" 717 | ] 718 | }, 719 | "execution_count": 34, 720 | "metadata": {}, 721 | "output_type": "execute_result" 722 | } 723 | ], 724 | "source": [ 725 | "tup.count(1)" 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": 35, 731 | "metadata": {}, 732 | "outputs": [ 733 | { 734 | "data": { 735 | "text/plain": [ 736 | "3" 737 | ] 738 | }, 739 | "execution_count": 35, 740 | "metadata": {}, 741 | "output_type": "execute_result" 742 | } 743 | ], 744 | "source": [ 745 | "tup.count(2)" 746 | ] 747 | }, 748 | { 749 | "cell_type": "markdown", 750 | "metadata": {}, 751 | "source": [ 752 | "##### 2) index(item)\n", 753 | "Returns the first index of specified item.\n", 754 | "If item is not present it will give error" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": 36, 760 | "metadata": {}, 761 | "outputs": [ 762 | { 763 | "data": { 764 | "text/plain": [ 765 | "2" 766 | ] 767 | }, 768 | "execution_count": 36, 769 | "metadata": {}, 770 | "output_type": "execute_result" 771 | } 772 | ], 773 | "source": [ 774 | "tup.index(3)" 775 | ] 776 | }, 777 | { 778 | "cell_type": "markdown", 779 | "metadata": {}, 780 | "source": [ 781 | "### Task6 - Add and remove the elements from a tuple. (Refer workaroud to modify the tuples)" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": null, 787 | "metadata": {}, 788 | "outputs": [], 789 | "source": [] 790 | } 791 | ], 792 | "metadata": { 793 | "kernelspec": { 794 | "display_name": "Python 3", 795 | "language": "python", 796 | "name": "python3" 797 | }, 798 | "language_info": { 799 | "codemirror_mode": { 800 | "name": "ipython", 801 | "version": 3 802 | }, 803 | "file_extension": ".py", 804 | "mimetype": "text/x-python", 805 | "name": "python", 806 | "nbconvert_exporter": "python", 807 | "pygments_lexer": "ipython3", 808 | "version": "3.7.6" 809 | } 810 | }, 811 | "nbformat": 4, 812 | "nbformat_minor": 2 813 | } 814 | -------------------------------------------------------------------------------- /06 - Sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Sets\n", 8 | "In Python sets are written with curly brackets.\n", 9 | "#### Important properties of sets\n", 10 | "\n", 11 | "* Can store different data types\n", 12 | "* Duplicate values are not allowed\n", 13 | "* Order is not retained\n", 14 | "* Indexing and slicing is not possible" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "set1 = {2,3,4.4,True,\"python\"}" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "{2, 3, 4.4, True, 'python'}" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "set1" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "set2 = {1,2,3,4,1,2,1,1,4,3}" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "{1, 2, 3, 4}" 64 | ] 65 | }, 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "set2" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "set3 = {\"a\",1,\"b\",2,3}" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 6, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "{1, 2, 3, 'a', 'b'}" 93 | ] 94 | }, 95 | "execution_count": 6, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "set3" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 7, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "set" 113 | ] 114 | }, 115 | "execution_count": 7, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "# check the type using type()\n", 122 | "type(set2)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 8, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "5" 134 | ] 135 | }, 136 | "execution_count": 8, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "# to check the length of the sets\n", 143 | "len(set1)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "#### Indexing and Slicing is not allowed in sets" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 9, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "ename": "TypeError", 160 | "evalue": "'set' object is not subscriptable", 161 | "output_type": "error", 162 | "traceback": [ 163 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 164 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 165 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# try to index\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mset1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 166 | "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "# try to index\n", 172 | "set1[0]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "#### empty set\n" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 10, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "data": { 189 | "text/plain": [ 190 | "{}" 191 | ] 192 | }, 193 | "execution_count": 10, 194 | "metadata": {}, 195 | "output_type": "execute_result" 196 | } 197 | ], 198 | "source": [ 199 | "set4 = {}\n", 200 | "set4" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 11, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "dict" 212 | ] 213 | }, 214 | "execution_count": 11, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "type(set4)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 12, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "set" 232 | ] 233 | }, 234 | "execution_count": 12, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "# to create empty sets\n", 241 | "set4 = set()\n", 242 | "type(set4)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "#### Mutablility\n", 250 | "Once set is created you can add and remove the elements from the set, but you cannot modify the existing elements in a set" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "#### In built methods.\n", 258 | "##### * To add the elements" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "##### 1) add(item)\n", 266 | "adds single item to the set" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 13, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "{1, 2, 3, 4, 6}\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "set1 = {1,2,3,4,6}\n", 284 | "# set1\n", 285 | "set1.add(6)\n", 286 | "print(set1)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 14, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "data": { 296 | "text/plain": [ 297 | "{1, 2, 3, 4, 6, 7}" 298 | ] 299 | }, 300 | "execution_count": 14, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "set1.add(7)\n", 307 | "set1" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "##### 2) .update(list)\n", 315 | "this method is used to add more than one items. All the items should be passed in the form of lists" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 15, 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [ 324 | "set1 = {1,2,3,4,5}\n", 325 | "set1.update([6,7,8]) # pass list of values to set1" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 16, 331 | "metadata": {}, 332 | "outputs": [ 333 | { 334 | "data": { 335 | "text/plain": [ 336 | "{1, 2, 3, 4, 5, 6, 7, 8}" 337 | ] 338 | }, 339 | "execution_count": 16, 340 | "metadata": {}, 341 | "output_type": "execute_result" 342 | } 343 | ], 344 | "source": [ 345 | "set1" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "#### 3) copy()\n", 353 | "to copy a set" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 17, 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "data": { 363 | "text/plain": [ 364 | "{1, 2, 3, 4, 5, 6, 7, 8}" 365 | ] 366 | }, 367 | "execution_count": 17, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "set2 = set1.copy()\n", 374 | "set2" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "##### * To remove the items" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "##### 4) remove(item)\n", 389 | "removes the specified item from the set. If specified item is not present it will raise error" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 18, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "{1, 2, 3, 4, 5, 6, 7, 8}" 401 | ] 402 | }, 403 | "execution_count": 18, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "set1" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 19, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "data": { 419 | "text/plain": [ 420 | "{2, 3, 4, 5, 6, 7, 8}" 421 | ] 422 | }, 423 | "execution_count": 19, 424 | "metadata": {}, 425 | "output_type": "execute_result" 426 | } 427 | ], 428 | "source": [ 429 | "set1.remove(1)\n", 430 | "set1" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 20, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "data": { 440 | "text/plain": [ 441 | "{3, 4, 5, 6, 7, 8}" 442 | ] 443 | }, 444 | "execution_count": 20, 445 | "metadata": {}, 446 | "output_type": "execute_result" 447 | } 448 | ], 449 | "source": [ 450 | "set1.remove(2)\n", 451 | "set1" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 21, 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "ename": "KeyError", 461 | "evalue": "2", 462 | "output_type": "error", 463 | "traceback": [ 464 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 465 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 466 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#Gives error because 2 is not present\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mset1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mset1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 467 | "\u001b[1;31mKeyError\u001b[0m: 2" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "#Gives error because 2 is not present\n", 473 | "set1.remove(2)\n", 474 | "set1" 475 | ] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": {}, 480 | "source": [ 481 | "##### 5) discard(item)\n", 482 | "removes the specified item from the set.But if the item is not present then it will not raise error" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 22, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "data": { 492 | "text/plain": [ 493 | "{1, 2, 3, 4, 5, 6}" 494 | ] 495 | }, 496 | "execution_count": 22, 497 | "metadata": {}, 498 | "output_type": "execute_result" 499 | } 500 | ], 501 | "source": [ 502 | "set1 = {1,2,3,4,5,6}\n", 503 | "set1" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 23, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "{2, 3, 4, 5, 6}" 515 | ] 516 | }, 517 | "execution_count": 23, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "set1.discard(1)\n", 524 | "set1" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 24, 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | "{3, 4, 5, 6}" 536 | ] 537 | }, 538 | "execution_count": 24, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "set1.discard(2)\n", 545 | "set1" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 25, 551 | "metadata": {}, 552 | "outputs": [ 553 | { 554 | "data": { 555 | "text/plain": [ 556 | "{3, 4, 5, 6}" 557 | ] 558 | }, 559 | "execution_count": 25, 560 | "metadata": {}, 561 | "output_type": "execute_result" 562 | } 563 | ], 564 | "source": [ 565 | "# will not give the error if 2 is not there\n", 566 | "set1.discard(2)\n", 567 | "set1" 568 | ] 569 | }, 570 | { 571 | "cell_type": "markdown", 572 | "metadata": {}, 573 | "source": [ 574 | "##### 6) pop()\n", 575 | "Sets are unordered, so when using the pop() method, you will not know which item that gets removed." 576 | ] 577 | }, 578 | { 579 | "cell_type": "code", 580 | "execution_count": 26, 581 | "metadata": {}, 582 | "outputs": [ 583 | { 584 | "data": { 585 | "text/plain": [ 586 | "1" 587 | ] 588 | }, 589 | "execution_count": 26, 590 | "metadata": {}, 591 | "output_type": "execute_result" 592 | } 593 | ], 594 | "source": [ 595 | "set1 = {\"a\",1,\"B\",2,3,\"c\",4}\n", 596 | "set1.pop()" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": 27, 602 | "metadata": {}, 603 | "outputs": [ 604 | { 605 | "data": { 606 | "text/plain": [ 607 | "2" 608 | ] 609 | }, 610 | "execution_count": 27, 611 | "metadata": {}, 612 | "output_type": "execute_result" 613 | } 614 | ], 615 | "source": [ 616 | "set1.pop()" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | "##### 7) clear()\n", 624 | "empties the set" 625 | ] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": 28, 630 | "metadata": {}, 631 | "outputs": [ 632 | { 633 | "data": { 634 | "text/plain": [ 635 | "{3, 4, 'B', 'a', 'c'}" 636 | ] 637 | }, 638 | "execution_count": 28, 639 | "metadata": {}, 640 | "output_type": "execute_result" 641 | } 642 | ], 643 | "source": [ 644 | "set1" 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": 29, 650 | "metadata": {}, 651 | "outputs": [ 652 | { 653 | "data": { 654 | "text/plain": [ 655 | "set()" 656 | ] 657 | }, 658 | "execution_count": 29, 659 | "metadata": {}, 660 | "output_type": "execute_result" 661 | } 662 | ], 663 | "source": [ 664 | "set1.clear()\n", 665 | "set1" 666 | ] 667 | }, 668 | { 669 | "cell_type": "markdown", 670 | "metadata": {}, 671 | "source": [ 672 | "##### * Other inbuilt methods" 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "metadata": {}, 678 | "source": [ 679 | "##### 8) union()\n", 680 | "\n", 681 | "returns a new set with all the elements from set1 and set2" 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": 30, 687 | "metadata": {}, 688 | "outputs": [ 689 | { 690 | "name": "stdout", 691 | "output_type": "stream", 692 | "text": [ 693 | "{1, 2, 3, 4, 5}\n", 694 | "{4, 5, 6, 7, 8}\n" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "set1 = {1,2,3,4,5}\n", 700 | "set2 = {4,5,6,7,8}\n", 701 | "print(set1)\n", 702 | "print(set2)" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": 31, 708 | "metadata": {}, 709 | "outputs": [ 710 | { 711 | "data": { 712 | "text/plain": [ 713 | "{1, 2, 3, 4, 5, 6, 7, 8}" 714 | ] 715 | }, 716 | "execution_count": 31, 717 | "metadata": {}, 718 | "output_type": "execute_result" 719 | } 720 | ], 721 | "source": [ 722 | "set3 = set1.union(set2)\n", 723 | "set3" 724 | ] 725 | }, 726 | { 727 | "cell_type": "markdown", 728 | "metadata": {}, 729 | "source": [ 730 | "##### 9) intersection()\n", 731 | "returns the common element from both the sets" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 32, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "data": { 741 | "text/plain": [ 742 | "{1, 2, 3, 4, 5}" 743 | ] 744 | }, 745 | "execution_count": 32, 746 | "metadata": {}, 747 | "output_type": "execute_result" 748 | } 749 | ], 750 | "source": [ 751 | "set1" 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 33, 757 | "metadata": {}, 758 | "outputs": [ 759 | { 760 | "data": { 761 | "text/plain": [ 762 | "{4, 5, 6, 7, 8}" 763 | ] 764 | }, 765 | "execution_count": 33, 766 | "metadata": {}, 767 | "output_type": "execute_result" 768 | } 769 | ], 770 | "source": [ 771 | "set2" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 34, 777 | "metadata": {}, 778 | "outputs": [ 779 | { 780 | "data": { 781 | "text/plain": [ 782 | "{4, 5}" 783 | ] 784 | }, 785 | "execution_count": 34, 786 | "metadata": {}, 787 | "output_type": "execute_result" 788 | } 789 | ], 790 | "source": [ 791 | "set3 = set1.intersection(set2)\n", 792 | "set3" 793 | ] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "execution_count": null, 798 | "metadata": {}, 799 | "outputs": [], 800 | "source": [] 801 | }, 802 | { 803 | "cell_type": "markdown", 804 | "metadata": {}, 805 | "source": [ 806 | "### Task7 - go through other inbuilt methods like\n", 807 | "1) difference()\n", 808 | "2) symmetric_difference()" 809 | ] 810 | }, 811 | { 812 | "cell_type": "markdown", 813 | "metadata": {}, 814 | "source": [ 815 | "### Task8 - Make a table of all the In-built data structures and Point out the differences one by one" 816 | ] 817 | } 818 | ], 819 | "metadata": { 820 | "kernelspec": { 821 | "display_name": "Python 3", 822 | "language": "python", 823 | "name": "python3" 824 | }, 825 | "language_info": { 826 | "codemirror_mode": { 827 | "name": "ipython", 828 | "version": 3 829 | }, 830 | "file_extension": ".py", 831 | "mimetype": "text/x-python", 832 | "name": "python", 833 | "nbconvert_exporter": "python", 834 | "pygments_lexer": "ipython3", 835 | "version": "3.7.6" 836 | } 837 | }, 838 | "nbformat": 4, 839 | "nbformat_minor": 2 840 | } 841 | -------------------------------------------------------------------------------- /07 - Operators in Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## We will discuss about some of the important operators in python\n", 8 | "* Arithmatic Operators\n", 9 | "* Assignment Operators\n", 10 | "* Comparision Operators\n", 11 | "* Logical Operators" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "### 1) Arithmatic Operators ( + , - , * , / , // , % , ** )\n", 19 | "These operators are used to do calculations " 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "x = 10\n", 29 | "y = 3" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "13" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "# addition\n", 50 | "x + y" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "7" 62 | ] 63 | }, 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "# subtraction\n", 71 | "x - y" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "3.3333333333333335" 83 | ] 84 | }, 85 | "execution_count": 4, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "# division\n", 92 | "x / y" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "30" 104 | ] 105 | }, 106 | "execution_count": 5, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "# Multiplication\n", 113 | "x * y" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "1" 125 | ] 126 | }, 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "# remainder\n", 134 | "x % y" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "3" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "# floor division\n", 155 | "x // y" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 8, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "1000" 167 | ] 168 | }, 169 | "execution_count": 8, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "# exponential\n", 176 | "x ** y" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "### 2) Assignment Operators ( = , += , -= , *= , /= )\n", 184 | "Assigns the values" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 9, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "5" 196 | ] 197 | }, 198 | "execution_count": 9, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "x = 5\n", 205 | "x" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 10, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "8" 217 | ] 218 | }, 219 | "execution_count": 10, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "x = x + 3\n", 226 | "x" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 11, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "11" 238 | ] 239 | }, 240 | "execution_count": 11, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "x += 3 # x = x + 3\n", 247 | "x" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 12, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "8" 259 | ] 260 | }, 261 | "execution_count": 12, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "x -= 3 # x = x- 3\n", 268 | "x" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 13, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "24" 280 | ] 281 | }, 282 | "execution_count": 13, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "x *= 3 #x = x*3\n", 289 | "x" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 14, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "data": { 299 | "text/plain": [ 300 | "8.0" 301 | ] 302 | }, 303 | "execution_count": 14, 304 | "metadata": {}, 305 | "output_type": "execute_result" 306 | } 307 | ], 308 | "source": [ 309 | "x /= 3 # x = x/3\n", 310 | "x" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": null, 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "### 3) Comparision Operators ( == , != , > , < , >= , <= )\n", 325 | "this operators returns the boolean values i.e., either True or False" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 15, 331 | "metadata": {}, 332 | "outputs": [], 333 | "source": [ 334 | "x = 5\n", 335 | "y = 2" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 16, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "False" 347 | ] 348 | }, 349 | "execution_count": 16, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "x == y" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 17, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "True" 367 | ] 368 | }, 369 | "execution_count": 17, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "x != y" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 18, 381 | "metadata": {}, 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | "True" 387 | ] 388 | }, 389 | "execution_count": 18, 390 | "metadata": {}, 391 | "output_type": "execute_result" 392 | } 393 | ], 394 | "source": [ 395 | "x > y" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 19, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "data": { 405 | "text/plain": [ 406 | "False" 407 | ] 408 | }, 409 | "execution_count": 19, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "x < y" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 20, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "True" 427 | ] 428 | }, 429 | "execution_count": 20, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "x >= y" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 21, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/plain": [ 446 | "False" 447 | ] 448 | }, 449 | "execution_count": 21, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [ 455 | "x <= y" 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": {}, 461 | "source": [ 462 | "### 4) Logical operators ( or , and , not )" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "![title](img/truthTable.png)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 22, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [ 478 | "x = True\n", 479 | "y = False" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 23, 485 | "metadata": {}, 486 | "outputs": [ 487 | { 488 | "data": { 489 | "text/plain": [ 490 | "False" 491 | ] 492 | }, 493 | "execution_count": 23, 494 | "metadata": {}, 495 | "output_type": "execute_result" 496 | } 497 | ], 498 | "source": [ 499 | "x and y" 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 24, 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "True" 511 | ] 512 | }, 513 | "execution_count": 24, 514 | "metadata": {}, 515 | "output_type": "execute_result" 516 | } 517 | ], 518 | "source": [ 519 | "x or y" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 25, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | "False" 531 | ] 532 | }, 533 | "execution_count": 25, 534 | "metadata": {}, 535 | "output_type": "execute_result" 536 | } 537 | ], 538 | "source": [ 539 | "not x" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": null, 545 | "metadata": {}, 546 | "outputs": [], 547 | "source": [] 548 | } 549 | ], 550 | "metadata": { 551 | "kernelspec": { 552 | "display_name": "Python 3", 553 | "language": "python", 554 | "name": "python3" 555 | }, 556 | "language_info": { 557 | "codemirror_mode": { 558 | "name": "ipython", 559 | "version": 3 560 | }, 561 | "file_extension": ".py", 562 | "mimetype": "text/x-python", 563 | "name": "python", 564 | "nbconvert_exporter": "python", 565 | "pygments_lexer": "ipython3", 566 | "version": "3.8.3" 567 | } 568 | }, 569 | "nbformat": 4, 570 | "nbformat_minor": 2 571 | } 572 | -------------------------------------------------------------------------------- /08- IF, ELIF and ELSE.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## if condition" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "\"\"\"\n", 17 | "syntax of if\n", 18 | "\n", 19 | "if(condition/s):\n", 20 | " statement1\n", 21 | " statement2\n", 22 | " ..........\n", 23 | "\"\"\"" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Both the numbers are same\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "#Ex1)\n", 41 | "num1 = 10\n", 42 | "num2 = 10\n", 43 | "\n", 44 | "if(num1 == num2):\n", 45 | " print(\"Both the numbers are same\")" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Numbers are diff\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "#Ex2)\n", 63 | "num1 = 11\n", 64 | "num2 = 10\n", 65 | "if(num1 != num2):\n", 66 | " print(\"Numbers are diff\")" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "## Indentation\n", 74 | "whitespace at the beginning of a line" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "ename": "IndentationError", 84 | "evalue": "expected an indented block (, line 3)", 85 | "output_type": "error", 86 | "traceback": [ 87 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m print(\"A\") #ERROR\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "#Ex1)\n", 93 | "if(4>2):\n", 94 | "print(\"A\") #ERROR" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "ename": "IndentationError", 104 | "evalue": "unexpected indent (, line 4)", 105 | "output_type": "error", 106 | "traceback": [ 107 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m print(\"B\") #Error\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "#Ex2)\n", 113 | "if(4>2):\n", 114 | " print(\"A\")\n", 115 | " print(\"B\") #Error" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 6, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "ename": "IndentationError", 125 | "evalue": "unindent does not match any outer indentation level (, line 4)", 126 | "output_type": "error", 127 | "traceback": [ 128 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m print(\"B\") #Error\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "#Ex3)\n", 134 | "if(4>2):\n", 135 | " print(\"A\")\n", 136 | " print(\"B\") #Error" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 7, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "A\n", 149 | "B\n", 150 | "C\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "#Ex4)\n", 156 | "if(4>2):\n", 157 | " print(\"A\")\n", 158 | " print(\"B\") #Valid\n", 159 | " print(\"C\") #Valid" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "## if.. else.." 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "\"\"\"\n", 176 | "syntax of if else\n", 177 | "\n", 178 | "if(condition/s):\n", 179 | " statement1\n", 180 | " statement2\n", 181 | " ..........\n", 182 | "else:\n", 183 | " statement1\n", 184 | " statement2\n", 185 | " ..........\n", 186 | "\"\"\"" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 8, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "Not equal\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "#Ex1) \n", 204 | "num1 = 10\n", 205 | "num2 = 5\n", 206 | "\n", 207 | "if(num1 == num2):\n", 208 | " print(\"equal\")\n", 209 | "else:\n", 210 | " print(\"Not equal\")" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "num1 is greater than num2\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "#Ex2)\n", 228 | "if(num1num2):\n", 286 | " print('num1 is greater than num2')\n", 287 | " \n", 288 | "else:\n", 289 | " print('num1 is less than num2')\n" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "## Multiple conditions\n", 297 | "by using or & and operators" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 11, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "Satisfied\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "#Ex1) or\n", 315 | "num1 =7\n", 316 | "# it should be positive\n", 317 | "# or it should be divisible by 2\n", 318 | "# or it should be divisible by 3\n", 319 | "\n", 320 | "if(num1>0 or num1%2 == 0 or num1%3 == 0):\n", 321 | " print(\"Satisfied\")\n", 322 | "else:\n", 323 | " print(\"Not satisfied\")" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 12, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "name": "stdout", 333 | "output_type": "stream", 334 | "text": [ 335 | "Satisfied\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "#Ex2) and\n", 341 | "num1 = 6\n", 342 | "if(num1>0 and num1%2 == 0 and num1%3 == 0):\n", 343 | " print(\"Satisfied\")\n", 344 | "else:\n", 345 | " print(\"Not satisfied\")\n" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "## Nested if else" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 13, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "name": "stdout", 362 | "output_type": "stream", 363 | "text": [ 364 | "positive number\n", 365 | "Divisible by 3\n" 366 | ] 367 | } 368 | ], 369 | "source": [ 370 | "#Ex1)\n", 371 | "num1 = 3\n", 372 | "if(num1>0):\n", 373 | " print(\"positive number\")\n", 374 | " if(num1%2 == 0):\n", 375 | " print(\"Divisible by 2\")\n", 376 | " elif(num1%3 == 0):\n", 377 | " print(\"Divisible by 3\")\n", 378 | " else:\n", 379 | " print(\"Not divisible by 2 or 3\")\n", 380 | "else:\n", 381 | " print(\"negative number\")" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": null, 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [] 390 | }, 391 | { 392 | "cell_type": "markdown", 393 | "metadata": {}, 394 | "source": [ 395 | "### Task9\n", 396 | "* Ask 2 numbers from users and store it in num1 and num2\n", 397 | "* Ask user to press 1 for addition,2 for subtraction,3 for multiplication and 4 for division\n", 398 | "* based on number given by user do the math operation" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": null, 404 | "metadata": {}, 405 | "outputs": [], 406 | "source": [] 407 | } 408 | ], 409 | "metadata": { 410 | "kernelspec": { 411 | "display_name": "Python 3", 412 | "language": "python", 413 | "name": "python3" 414 | }, 415 | "language_info": { 416 | "codemirror_mode": { 417 | "name": "ipython", 418 | "version": 3 419 | }, 420 | "file_extension": ".py", 421 | "mimetype": "text/x-python", 422 | "name": "python", 423 | "nbconvert_exporter": "python", 424 | "pygments_lexer": "ipython3", 425 | "version": "3.8.3" 426 | } 427 | }, 428 | "nbformat": 4, 429 | "nbformat_minor": 2 430 | } 431 | -------------------------------------------------------------------------------- /09 - Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# LOOPS\n", 8 | "The purpose of loops is to repeat the same, or similar, code a number of times. This number of times could be specified to a certain number, or the number of times could be dictated by a certain condition being met\n", 9 | "\n", 10 | "There are 2 types of Loops \n", 11 | "* 1) While Loops\n", 12 | "* 2) For loops" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "1\n", 25 | "2\n", 26 | "3\n", 27 | "4\n", 28 | "5\n", 29 | "6\n", 30 | "7\n", 31 | "8\n", 32 | "9\n", 33 | "10\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print(1)\n", 39 | "print(2)\n", 40 | "print(3)\n", 41 | "print(4)\n", 42 | "print(5)\n", 43 | "print(6)\n", 44 | "print(7)\n", 45 | "print(8)\n", 46 | "print(9)\n", 47 | "print(10)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## While loops" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "\"\"\"\n", 64 | "syntax\n", 65 | "\n", 66 | "while(condition/s):\n", 67 | " statement1\n", 68 | " statement2\n", 69 | " ..........\n", 70 | "\"\"\"" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "1\n", 83 | "2\n", 84 | "3\n", 85 | "4\n", 86 | "5\n", 87 | "6\n", 88 | "7\n", 89 | "8\n", 90 | "9\n", 91 | "10\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "# ex1)\n", 97 | "count = 1\n", 98 | "while(count<11):\n", 99 | " print(count)\n", 100 | " count = count + 1" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "## while - else\n", 108 | "if you want to know your while loop has finished its execution and you want to do some operations then you can make use of \"else\"" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 3, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "1\n", 121 | "2\n", 122 | "3\n", 123 | "4\n", 124 | "5\n", 125 | "6\n", 126 | "7\n", 127 | "8\n", 128 | "9\n", 129 | "10\n", 130 | "End of while loop\n", 131 | "11\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "#ex2) \n", 137 | "count = 1\n", 138 | "while(count<11):\n", 139 | " print(count)\n", 140 | " count = count + 1\n", 141 | "else:\n", 142 | " print(\"End of while loop\")\n", 143 | " print(count)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "## break\n", 151 | "break keyword is used to break out of the loop or come out of the loop" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 4, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "1\n", 164 | "2\n", 165 | "3\n", 166 | "4\n", 167 | "5\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "# if count has reached 5 then break\n", 173 | "count = 1\n", 174 | "while(count<11):\n", 175 | " if(count == 5):\n", 176 | " print(count)\n", 177 | " break\n", 178 | " else:\n", 179 | " print(count)\n", 180 | " count = count + 1" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "### continue\n", 188 | "continue keyword takes the execution to the begining of the loop.\n", 189 | "In other word it skips that perticular iteration" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 5, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "1\n", 202 | "3\n", 203 | "5\n", 204 | "7\n", 205 | "9\n", 206 | "11\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "# print only odd numbers\n", 212 | "count = 0\n", 213 | "while(count<11):\n", 214 | " count = count + 1\n", 215 | " if(count%2 == 0):\n", 216 | " continue\n", 217 | " print(count)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "## For loops\n", 225 | "* For loops are basically used to loop over a sequence.\n", 226 | "* Sequence can be anything like strings,Lists,Tuples,Dictionaries,Sets etc" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "\"\"\"\n", 236 | "syntax\n", 237 | "\n", 238 | "for item in seq:\n", 239 | " statements....\n", 240 | "\"\"\"" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 6, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "a\n", 253 | "b\n", 254 | "c\n", 255 | "d\n", 256 | "e\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "# looping over the strings\n", 262 | "str1 = \"abcde\"\n", 263 | "\n", 264 | "for item in str1:\n", 265 | " print(item)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 7, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "1\n", 278 | "2\n", 279 | "3\n", 280 | "4\n", 281 | "5\n" 282 | ] 283 | } 284 | ], 285 | "source": [ 286 | "# looping over lists\n", 287 | "\n", 288 | "list1 = [1,2,3,4,5]\n", 289 | "\n", 290 | "for item in list1:\n", 291 | " print(item)\n" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "### range(start,stop,step)\n", 299 | "* The range() function is used to generate a sequence of numbers over time" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "metadata": {}, 306 | "outputs": [], 307 | "source": [ 308 | "# start = starting position (default value is 0)\n", 309 | "# stop = end position(exclusive) (default value is end position)\n", 310 | "# step = step size or increment size (default value is 1)\n", 311 | "\n", 312 | "# range(10) -> 10 is the stop parameter\n", 313 | "# range(1,10) -> 1 is start,10 is stop\n", 314 | "# range(1,10,2) -> 1 is start,10 is stop,2 is step" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 8, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "0\n", 327 | "1\n", 328 | "2\n", 329 | "3\n", 330 | "4\n", 331 | "5\n", 332 | "6\n", 333 | "7\n", 334 | "8\n", 335 | "9\n", 336 | "10\n" 337 | ] 338 | } 339 | ], 340 | "source": [ 341 | "for i in range(11):\n", 342 | " print(i)" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 9, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "1\n", 355 | "2\n", 356 | "3\n", 357 | "4\n", 358 | "5\n", 359 | "6\n", 360 | "7\n", 361 | "8\n", 362 | "9\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "for i in range(1,10):\n", 368 | " print(i)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 10, 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "1\n", 381 | "3\n", 382 | "5\n", 383 | "7\n", 384 | "9\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "for i in range(1,10,2):\n", 390 | " print(i)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 11, 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "10\n", 403 | "9\n", 404 | "8\n", 405 | "7\n", 406 | "6\n", 407 | "5\n", 408 | "4\n", 409 | "3\n", 410 | "2\n", 411 | "1\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "#looping in reverse order from 10 to 1\n", 417 | "for i in range(10,0,-1):\n", 418 | " print(i)\n", 419 | " " 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 12, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "0\n", 432 | "1\n", 433 | "2\n", 434 | "3\n", 435 | "4\n", 436 | "5\n", 437 | "6\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "# using break in for loops\n", 443 | "for i in range(10):\n", 444 | " if ( i == 7):\n", 445 | " break\n", 446 | " print(i)" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": null, 452 | "metadata": {}, 453 | "outputs": [], 454 | "source": [ 455 | "# using continue in for loops\n", 456 | "# say you dont want to print 4,5,6\n", 457 | "for i in range(10):\n", 458 | " if(i==4 or i == 5 or i == 6):\n", 459 | " continue\n", 460 | " print(i)\n" 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "metadata": {}, 466 | "source": [ 467 | "## pass\n", 468 | "pass keyword does nothing but you can avoid getting an error when empty code is not allowed. Empty code is not allowed in loops,if else conditions,functions,Classes etc" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 14, 474 | "metadata": {}, 475 | "outputs": [], 476 | "source": [ 477 | "if(4>2):\n", 478 | " pass" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 15, 484 | "metadata": {}, 485 | "outputs": [ 486 | { 487 | "name": "stdout", 488 | "output_type": "stream", 489 | "text": [ 490 | "A\n" 491 | ] 492 | } 493 | ], 494 | "source": [ 495 | "if(4>2):\n", 496 | " print(\"A\")\n", 497 | "else:\n", 498 | " pass\n" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 16, 504 | "metadata": {}, 505 | "outputs": [], 506 | "source": [ 507 | "for i in range(10):\n", 508 | " pass" 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "### Task 10.1 = what is the uasge of continue wiith an example. " 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "### Task 10.2 What is pass keyword, with example." 523 | ] 524 | }, 525 | { 526 | "cell_type": "markdown", 527 | "metadata": {}, 528 | "source": [ 529 | "### Task10\n", 530 | "* using **while loop** print numbers from 10 to 1\n", 531 | "* try using **for loop** to loop over tuples,sets and dictionaries" 532 | ] 533 | } 534 | ], 535 | "metadata": { 536 | "kernelspec": { 537 | "display_name": "Python 3", 538 | "language": "python", 539 | "name": "python3" 540 | }, 541 | "language_info": { 542 | "codemirror_mode": { 543 | "name": "ipython", 544 | "version": 3 545 | }, 546 | "file_extension": ".py", 547 | "mimetype": "text/x-python", 548 | "name": "python", 549 | "nbconvert_exporter": "python", 550 | "pygments_lexer": "ipython3", 551 | "version": "3.8.3" 552 | } 553 | }, 554 | "nbformat": 4, 555 | "nbformat_minor": 2 556 | } 557 | -------------------------------------------------------------------------------- /10 - Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# FUNCTIONS\n", 8 | "Functions are the block of reusable code that will execute only when it is called." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "# syntax\n", 18 | "\"\"\"\n", 19 | "def function_name():\n", 20 | " statement 1\n", 21 | " statement 2\n", 22 | " ...\n", 23 | " ...\n", 24 | " ...\n", 25 | " statement n\n", 26 | "\"\"\"" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# defining the function\n", 36 | "def my_func():\n", 37 | " print(\"hello\")\n", 38 | " print(\"I am a function\")\n", 39 | " print(\"I will execute only when i get called\")\n", 40 | " " 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "hello\n", 53 | "I am a function\n", 54 | "I will execute only when i get called\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "# calling the function\n", 60 | "my_func()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "#Ex2) Adding two numbers\n", 70 | "def add():\n", 71 | " x = 10\n", 72 | " y = 20\n", 73 | " z = x + y\n", 74 | " print(z)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "30\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "add()" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "#### Passing parameters/arguments to function" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "#Ex1)you can pass numbers\n", 108 | "def add(x,y):\n", 109 | " z = x + y\n", 110 | " print(z)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "30\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "add(10,20)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "#Ex2) you can pass strings\n", 137 | "def func(name):\n", 138 | " print(\"Hello {}\".format(name))" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 8, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "Hello Himanshu\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "func(\"Himanshu\")" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 9, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "# Ex3) you can pass lists\n", 165 | "def func(list1):\n", 166 | " for i in list1:\n", 167 | " print(i)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 10, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "1\n", 180 | "2\n", 181 | "3\n", 182 | "4\n", 183 | "5\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "func([1,2,3,4,5])" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "#### return keyword\n", 196 | "* **return** keyword can be used to return values from a function\n", 197 | "* it can be used to end the execution of a function\n", 198 | "* statements below **return** keyword will not get executed" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 11, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "# Ex1) returning sum\n", 208 | "def add(x,y):\n", 209 | " z = x + y\n", 210 | " return z\n", 211 | " print(z) #will not be executed" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 12, 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "data": { 221 | "text/plain": [ 222 | "40" 223 | ] 224 | }, 225 | "execution_count": 12, 226 | "metadata": {}, 227 | "output_type": "execute_result" 228 | } 229 | ], 230 | "source": [ 231 | "my_sum = add(10,20)\n", 232 | "my_sum + 10" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 13, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "# Ex2) returning product\n", 242 | "def product(x,y):\n", 243 | " z = x * y\n", 244 | " return z\n", 245 | " print(z) #will not execute" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 14, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "data": { 255 | "text/plain": [ 256 | "20" 257 | ] 258 | }, 259 | "execution_count": 14, 260 | "metadata": {}, 261 | "output_type": "execute_result" 262 | } 263 | ], 264 | "source": [ 265 | "my_num = product(10,2)\n", 266 | "my_num" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "#### default values\n", 274 | "* to initialize the parameters with default values in order to avoid the errors" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 15, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "#Without default values\n", 284 | "def add(x,y,z):\n", 285 | " my_num = x + y + z\n", 286 | " print(my_num)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 16, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "60\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "# when value is passed\n", 304 | "add(10,20,30)" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 17, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "ename": "TypeError", 314 | "evalue": "add() missing 1 required positional argument: 'z'", 315 | "output_type": "error", 316 | "traceback": [ 317 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 318 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 319 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# when value is not passed\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#ERROR\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 320 | "\u001b[1;31mTypeError\u001b[0m: add() missing 1 required positional argument: 'z'" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "# when value is not passed\n", 326 | "add(10,20) #ERROR" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 18, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "# With default values\n", 336 | "\n", 337 | "def add(x,y,z = 0): #z is initilized to 0 (default)\n", 338 | " my_num = x + y + z\n", 339 | " print(my_num)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 19, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "30\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "# when value is not passed\n", 357 | "add(10,20)" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 20, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "60\n" 370 | ] 371 | } 372 | ], 373 | "source": [ 374 | "#when value is passed\n", 375 | "add(10,20,30)" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "metadata": {}, 381 | "source": [ 382 | "#### A function can call any other function" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 21, 388 | "metadata": {}, 389 | "outputs": [], 390 | "source": [ 391 | "def func1():\n", 392 | " print(\"func1\")\n", 393 | "\n", 394 | "def func2():\n", 395 | " print(\"func2\")\n", 396 | " func1()\n", 397 | " \n", 398 | "def func3():\n", 399 | " print(\"func3\")\n", 400 | " func2()\n" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 22, 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "name": "stdout", 410 | "output_type": "stream", 411 | "text": [ 412 | "func3\n", 413 | "func2\n", 414 | "func1\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "func3()" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "#### Recursion or Recursive Function :\n", 427 | "* A function calling itself\n", 428 | "* Possibility of infinite execution. Always handle this scenario" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 23, 434 | "metadata": {}, 435 | "outputs": [], 436 | "source": [ 437 | "# Printing Hello for 5 times using recursion\n", 438 | "def my_func(num):\n", 439 | " num = num + 1\n", 440 | " if(num>5):\n", 441 | " return\n", 442 | " else:\n", 443 | " print(\"Hello\")\n", 444 | " my_func(num)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 24, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "Hello\n", 457 | "Hello\n", 458 | "Hello\n", 459 | "Hello\n", 460 | "Hello\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "my_func(0)" 466 | ] 467 | }, 468 | { 469 | "cell_type": "markdown", 470 | "metadata": {}, 471 | "source": [ 472 | "### Task 11.1\n", 473 | "* Explain Return keyword with example." 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": {}, 479 | "source": [ 480 | "### Task11.2\n", 481 | "* Ask 2 numbers from users and store it in num1 and num2\n", 482 | "* Ask user to press 1 for addition,2 for subtraction,3 for multiplication and 4 for division\n", 483 | "* create 4 seperate functions for each operation (i.e. addition,subtraction,multiplication and division)\n", 484 | "* based on number given by user,call that perticular function and print the output" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": null, 490 | "metadata": {}, 491 | "outputs": [], 492 | "source": [] 493 | } 494 | ], 495 | "metadata": { 496 | "kernelspec": { 497 | "display_name": "Python 3", 498 | "language": "python", 499 | "name": "python3" 500 | }, 501 | "language_info": { 502 | "codemirror_mode": { 503 | "name": "ipython", 504 | "version": 3 505 | }, 506 | "file_extension": ".py", 507 | "mimetype": "text/x-python", 508 | "name": "python", 509 | "nbconvert_exporter": "python", 510 | "pygments_lexer": "ipython3", 511 | "version": "3.8.3" 512 | } 513 | }, 514 | "nbformat": 4, 515 | "nbformat_minor": 2 516 | } 517 | -------------------------------------------------------------------------------- /11 - Scope.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# SCOPE\n", 8 | "A variable is only available from inside the region it is created. This is called scope." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "### Local Scope\n", 16 | "A variable created inside a function belongs to the local scope of that function, and can only be used inside that function." 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "ename": "NameError", 26 | "evalue": "name 'x' is not defined", 27 | "output_type": "error", 28 | "traceback": [ 29 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 30 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 31 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;31m#outside the function\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Not accessable , ERROR\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 32 | "\u001b[1;31mNameError\u001b[0m: name 'x' is not defined" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "def func1():\n", 38 | " x = 10\n", 39 | " print(x)\n", 40 | "\n", 41 | "# func1()\n", 42 | "\n", 43 | "#outside the function\n", 44 | "print(x) # Not accessable , ERROR" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### Enclosing scope\n", 52 | "inner functions can have access to the outer or enclosing function's variables" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "Outer function 300\n", 65 | "Inner function 300\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "def outer_func():\n", 71 | " x = 300\n", 72 | " print('Outer function ',x)\n", 73 | " def inner_fun():\n", 74 | " print('Inner function ',x) \n", 75 | " \n", 76 | " inner_fun()\n", 77 | "\n", 78 | "outer_func()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "### Global Scope\n", 86 | "A variable created in the main body of the Python code is a global variable and belongs to the global scope.\n", 87 | "Global variables are available from within any scope, global and local." 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 3, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "Global : 300\n", 100 | "my_func : 300\n", 101 | "my_inner_fun : 300\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "x = 300\n", 107 | "\n", 108 | "def my_func():\n", 109 | " \n", 110 | " print('my_func : ',x)\n", 111 | " \n", 112 | " def my_inner_fun():\n", 113 | " print('my_inner_fun : ',x)\n", 114 | " \n", 115 | " my_inner_fun()\n", 116 | " \n", 117 | "print('Global : ',x)\n", 118 | "\n", 119 | "my_func()\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "### How to change global variables inside functions" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "local 200\n", 139 | "GLobal 300\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "x = 300\n", 145 | "\n", 146 | "def my_fun():\n", 147 | " x = 200\n", 148 | " print('local ',x)\n", 149 | "\n", 150 | "my_fun()\n", 151 | "print('GLobal',x)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "### Global Keyword" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 5, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "local 200\n", 171 | "GLobal 300\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "x1 = 300\n", 177 | "\n", 178 | "def my_fun():\n", 179 | " global x\n", 180 | " x = 200\n", 181 | " print('local ',x)\n", 182 | " \n", 183 | "my_fun()\n", 184 | "\n", 185 | "print('GLobal',x1)\n" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### Order of execution ---> Local,Enclosing,Global" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 6, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "Value of y global = 1000\n", 205 | "Value of y in func1= 100\n", 206 | "Value of y in func2= 10\n", 207 | "Value of y = 1\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "y = 1000\n", 213 | "\n", 214 | "def func1():\n", 215 | " y = 100\n", 216 | " print('Value of y in func1= ', y )\n", 217 | " \n", 218 | " def func2():\n", 219 | " y = 10\n", 220 | " print('Value of y in func2= ', y )\n", 221 | " \n", 222 | " def func3():\n", 223 | " y = 1\n", 224 | " print('Value of y = ', y )\n", 225 | " \n", 226 | " func3()\n", 227 | " \n", 228 | " func2()\n", 229 | " \n", 230 | "print('Value of y global = ', y )\n", 231 | "func1()" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [] 240 | } 241 | ], 242 | "metadata": { 243 | "kernelspec": { 244 | "display_name": "Python 3", 245 | "language": "python", 246 | "name": "python3" 247 | }, 248 | "language_info": { 249 | "codemirror_mode": { 250 | "name": "ipython", 251 | "version": 3 252 | }, 253 | "file_extension": ".py", 254 | "mimetype": "text/x-python", 255 | "name": "python", 256 | "nbconvert_exporter": "python", 257 | "pygments_lexer": "ipython3", 258 | "version": "3.8.3" 259 | } 260 | }, 261 | "nbformat": 4, 262 | "nbformat_minor": 1 263 | } 264 | -------------------------------------------------------------------------------- /12 - List Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# List Comprehensions\n", 10 | "* In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", 11 | "\n", 12 | "* It's just another way of creating the lists\n", 13 | "\n", 14 | "* syntax\n", 15 | "##### my_list = [item for item in sequence]" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## Example 1 " 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "['P', 'y', 't', 'h', 'o', 'n', 'L', 'i', 's', 't']\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# to convert a string to list of characters\n", 40 | "# normal method\n", 41 | "x = 'PythonList'\n", 42 | "\n", 43 | "x_list = [] # step 1\n", 44 | "\n", 45 | "for char in x: # step 2\n", 46 | " x_list.append(char) # step 3\n", 47 | " \n", 48 | "print(x_list)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 2, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "['P', 'y', 't', 'h', 'o', 'n', 'L', 'i', 's', 't']\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "# Using list comprehension\n", 66 | "x_list = [char for char in x]\n", 67 | "print(x_list)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Example 2" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "# create a list which contains the squares of 0 to 10\n", 92 | "# without list comprehension\n", 93 | "list1 = []\n", 94 | "\n", 95 | "for i in range(11):\n", 96 | " list1.append(i**2)\n", 97 | "\n", 98 | "print(list1)\n" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# with list comprehensions\n", 116 | "list1 = [i**2 for i in range(11)]\n", 117 | "print(list1)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## Example 3\n", 132 | "Let's see how to add in if statements:" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 5, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "[0, 2, 4, 6, 8, 10]\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "# normal method\n", 150 | "list1 = [] \n", 151 | "\n", 152 | "for i in range(11):\n", 153 | " if(i%2 == 0):\n", 154 | " list1.append(i)\n", 155 | " \n", 156 | "print(list1)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 6, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "[0, 2, 4, 6, 8, 10]" 168 | ] 169 | }, 170 | "execution_count": 6, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "# Check for even numbers in a range\n", 177 | "list1 = [i for i in range(11) if(i%2 == 0)] \n", 178 | "list1" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "## Example 4" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 7, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "[1000, 2000, 3000, 4000, 5000]\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "# Convert Kms to meters\n", 210 | "# 1km = 1000m\n", 211 | "km_list = [1,2,3,4,5]\n", 212 | "\n", 213 | "m_list = []\n", 214 | "\n", 215 | "for i in km_list:\n", 216 | " m_list.append(i*1000)\n", 217 | " \n", 218 | "print(m_list)\n" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 8, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "[1000, 2000, 3000, 4000, 5000]\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "m_list = [i*1000 for i in km_list]\n", 236 | "print(m_list)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | } 246 | ], 247 | "metadata": { 248 | "kernelspec": { 249 | "display_name": "Python 3", 250 | "language": "python", 251 | "name": "python3" 252 | }, 253 | "language_info": { 254 | "codemirror_mode": { 255 | "name": "ipython", 256 | "version": 3 257 | }, 258 | "file_extension": ".py", 259 | "mimetype": "text/x-python", 260 | "name": "python", 261 | "nbconvert_exporter": "python", 262 | "pygments_lexer": "ipython3", 263 | "version": "3.7.3" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 1 268 | } 269 | -------------------------------------------------------------------------------- /13 - Lambda,Map and Filter functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Lambda Functions\n", 8 | "* A lambda function is a small anonymous function.\n", 9 | "* A lambda function can take any number of arguments, but can only have one expression.\n", 10 | "* They are mostly used along with map and filter functions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "# Syntax\n", 20 | "# lambda arguments : expression" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "### Ex1) Squaring a number" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "25" 39 | ] 40 | }, 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "# with one argument\n", 48 | "\n", 49 | "# normal function\n", 50 | "def square(num):\n", 51 | " return num**2\n", 52 | "\n", 53 | "square(5)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "# lambda functions\n", 63 | "square = lambda num:num**2" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "25" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "square(5)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Ex2) adding multiple numbers" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "30" 102 | ] 103 | }, 104 | "execution_count": 4, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# with multiple arguments\n", 111 | "# normal function\n", 112 | "def add(x,y,z):\n", 113 | " return x + y + z\n", 114 | "\n", 115 | "add(5,10,15)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "# lambda function\n", 125 | "add = lambda x,y,z:x + y + z" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "30" 137 | ] 138 | }, 139 | "execution_count": 6, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "add(5,10,15)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "##### we wont use lambda functions like this. We will mostly use them along with map,filter functions" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": null, 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "## Map function\n", 167 | "\n", 168 | "* The **map** function allows you to \"map\" a function to an iterable object.\n", 169 | "* It returns map object" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "\"\"\"\n", 179 | "about map function\n", 180 | "\n", 181 | "syntax ---->\n", 182 | "map(function,sequence)\n", 183 | "\n", 184 | "it applies the function to all the elements in the sequences \n", 185 | "\n", 186 | "returns the map object\n", 187 | "\"\"\"" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "### Ex1) Square all the elements of a list using map function" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 7, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "# sequence\n", 204 | "my_nums = [1,2,3,4,5]" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 8, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "#function to square a number\n", 214 | "def square(num):\n", 215 | " return num**2" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 9, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "data": { 225 | "text/plain": [ 226 | "" 227 | ] 228 | }, 229 | "execution_count": 9, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "#map\n", 236 | "map(square,my_nums)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 10, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "1\n", 249 | "4\n", 250 | "9\n", 251 | "16\n", 252 | "25\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "# To get the results, either iterate through mapobj \n", 258 | "for item in map(square,my_nums):\n", 259 | " print(item)" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 11, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "[1, 4, 9, 16, 25]" 271 | ] 272 | }, 273 | "execution_count": 11, 274 | "metadata": {}, 275 | "output_type": "execute_result" 276 | } 277 | ], 278 | "source": [ 279 | "# or just convert to a list\n", 280 | "list(map(square,my_nums))" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "### Ex2) even or odd numbers" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 12, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "#sequence\n", 297 | "my_nums = [1,2,3,4,5]" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 13, 303 | "metadata": {}, 304 | "outputs": [], 305 | "source": [ 306 | "#function\n", 307 | "def even_odd(num):\n", 308 | " if(num%2==0):\n", 309 | " return 'even'\n", 310 | " else:\n", 311 | " return 'odd'" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 14, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "['odd', 'even', 'odd', 'even', 'odd']" 323 | ] 324 | }, 325 | "execution_count": 14, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "list(map(even_odd,my_nums)) #['odd','even','odd','even','odd']" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "## Filter function\n", 346 | "\n", 347 | "* The filter function allows you to filter the elements based on the condition given in the function.\n", 348 | "* returns an filter object" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "\"\"\"\n", 358 | "about filter function\n", 359 | "\n", 360 | "syntax ---->\n", 361 | "filter(function,sequence)\n", 362 | "\n", 363 | "it applies the function to all the elements in the sequences and \n", 364 | "retains only those elements which passes the specific conditions\n", 365 | "\n", 366 | "returns the filter object\n", 367 | "\"\"\"" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "### Ex1) To filter the words starting with 'a'" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 15, 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [ 383 | "#Sequence\n", 384 | "words_list = ['apple','banana','airport','ant','cat']" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 16, 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "# function\n", 394 | "def starts_with_a(word):\n", 395 | " if(word[0] == 'a'):\n", 396 | " return True\n", 397 | " else:\n", 398 | " return False" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 17, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "['apple', 'airport', 'ant']" 410 | ] 411 | }, 412 | "execution_count": 17, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "list(filter(starts_with_a,words_list)) #['apple','airport','ant']" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "### Ex2) To filter only the even numbers" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 18, 431 | "metadata": {}, 432 | "outputs": [], 433 | "source": [ 434 | "# Sequence\n", 435 | "nums = [0,1,2,3,4,5,6,7,8,9,10]" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 19, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "#function\n", 445 | "def check_even(num):\n", 446 | " if(num%2==0):\n", 447 | " return True\n", 448 | " else:\n", 449 | " return False\n", 450 | " \n", 451 | "# def check_even(num):\n", 452 | "# return num%2 == 0" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 20, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "[0, 2, 4, 6, 8, 10]" 464 | ] 465 | }, 466 | "execution_count": 20, 467 | "metadata": {}, 468 | "output_type": "execute_result" 469 | } 470 | ], 471 | "source": [ 472 | "list(filter(check_even,nums))" 473 | ] 474 | }, 475 | { 476 | "cell_type": "markdown", 477 | "metadata": {}, 478 | "source": [ 479 | "### Note : the difference between filter and map" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 21, 485 | "metadata": {}, 486 | "outputs": [ 487 | { 488 | "data": { 489 | "text/plain": [ 490 | "[True, False, True, False, True, False, True, False, True, False, True]" 491 | ] 492 | }, 493 | "execution_count": 21, 494 | "metadata": {}, 495 | "output_type": "execute_result" 496 | } 497 | ], 498 | "source": [ 499 | "# map\n", 500 | "list(map(check_even,nums))" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 22, 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "data": { 510 | "text/plain": [ 511 | "[0, 2, 4, 6, 8, 10]" 512 | ] 513 | }, 514 | "execution_count": 22, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "# filter\n", 521 | "list(filter(check_even,nums))" 522 | ] 523 | }, 524 | { 525 | "cell_type": "markdown", 526 | "metadata": {}, 527 | "source": [ 528 | "## Using map() and filter() with lambda functions" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": {}, 534 | "source": [ 535 | "### Ex1) Square Ex" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 23, 541 | "metadata": {}, 542 | "outputs": [ 543 | { 544 | "data": { 545 | "text/plain": [ 546 | "[1, 4, 9, 16, 25]" 547 | ] 548 | }, 549 | "execution_count": 23, 550 | "metadata": {}, 551 | "output_type": "execute_result" 552 | } 553 | ], 554 | "source": [ 555 | "# sequence\n", 556 | "my_nums = [1,2,3,4,5]\n", 557 | "\n", 558 | "# lambda with map function\n", 559 | "list(map(lambda num:num**2,my_nums))" 560 | ] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": {}, 565 | "source": [ 566 | "### Ex2)" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 24, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "data": { 576 | "text/plain": [ 577 | "" 578 | ] 579 | }, 580 | "execution_count": 24, 581 | "metadata": {}, 582 | "output_type": "execute_result" 583 | } 584 | ], 585 | "source": [ 586 | "nums = [0,1,2,3,4,5,6,7,8,9,10]\n", 587 | "\n", 588 | "# filter with map function\n", 589 | "filter(lambda num:num%2 == 0,nums)" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": 25, 595 | "metadata": {}, 596 | "outputs": [ 597 | { 598 | "name": "stdout", 599 | "output_type": "stream", 600 | "text": [ 601 | "0\n", 602 | "2\n", 603 | "4\n", 604 | "6\n", 605 | "8\n", 606 | "10\n" 607 | ] 608 | } 609 | ], 610 | "source": [ 611 | "for item in filter(lambda num:num%2 == 0,nums):\n", 612 | " print(item)" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": null, 618 | "metadata": {}, 619 | "outputs": [], 620 | "source": [] 621 | }, 622 | { 623 | "cell_type": "markdown", 624 | "metadata": {}, 625 | "source": [ 626 | "## Task10 : Go through\n", 627 | "* **reduce()**\n", 628 | "* **zip()**\n", 629 | "* **enumerate()**\n" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": null, 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [] 638 | } 639 | ], 640 | "metadata": { 641 | "kernelspec": { 642 | "display_name": "Python 3", 643 | "language": "python", 644 | "name": "python3" 645 | }, 646 | "language_info": { 647 | "codemirror_mode": { 648 | "name": "ipython", 649 | "version": 3 650 | }, 651 | "file_extension": ".py", 652 | "mimetype": "text/x-python", 653 | "name": "python", 654 | "nbconvert_exporter": "python", 655 | "pygments_lexer": "ipython3", 656 | "version": "3.7.3" 657 | } 658 | }, 659 | "nbformat": 4, 660 | "nbformat_minor": 2 661 | } 662 | -------------------------------------------------------------------------------- /14 - Exception Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exception Handling\n", 8 | "* Errors/Exceptions are bound to occur in any programming language\n", 9 | "* Whenever Error/Exception happens the program will abruptly terminate\n", 10 | "* We need to handle the Exceptions\n", 11 | "* In python we use **try**,**except**,**finally** blocks to handle the exception\n", 12 | "\n", 13 | "## In python there are two types of errors\n", 14 | "1) Syntax errors / parsing errors\n", 15 | "\n", 16 | "2) Exceptions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### Syntax Errors:\n", 24 | "* As the name suggest, these error occurs when there is error in syntax\n", 25 | "* cannot be handled" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "### Ex 1)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "metadata": { 39 | "scrolled": true 40 | }, 41 | "outputs": [ 42 | { 43 | "ename": "SyntaxError", 44 | "evalue": "invalid syntax (, line 1)", 45 | "output_type": "error", 46 | "traceback": [ 47 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m if(4>2)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "if(4>2)\n", 53 | " print('A')" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "ename": "SyntaxError", 63 | "evalue": "invalid syntax (, line 3)", 64 | "output_type": "error", 65 | "traceback": [ 66 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m if(4>2)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "# Cannot be handled using try except\n", 72 | "try:\n", 73 | " if(4>2)\n", 74 | " print('A')\n", 75 | "except:\n", 76 | " print(\"Exception\")" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### Ex2)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 3, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "ename": "IndentationError", 93 | "evalue": "unexpected indent (, line 2)", 94 | "output_type": "error", 95 | "traceback": [ 96 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m print(\"b\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "print(\"a\")\n", 102 | " print(\"b\")" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "### Exceptions\n", 110 | "* any error other than sytax errors are called as exception\n", 111 | "* Exceptions can be handled" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### Ex1) " 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 4, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "hello world\n" 131 | ] 132 | }, 133 | { 134 | "ename": "NameError", 135 | "evalue": "name 'name' is not defined", 136 | "output_type": "error", 137 | "traceback": [ 138 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 139 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 140 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello world\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Welcome to exception handling'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Thank you'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 141 | "\u001b[1;31mNameError\u001b[0m: name 'name' is not defined" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print(\"hello world\")\n", 147 | "print(name)\n", 148 | "print('Welcome to exception handling')\n", 149 | "print('Thank you')" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "## try:\n", 157 | "* **try** block will check if there are any exceptions in our code. \n", 158 | "* If there are exception, then it will pass the control to **except** block\n", 159 | "* If there is no exception then it will not do anything\n", 160 | "\n", 161 | "## except:\n", 162 | "* **except** block will handle the exception.\n", 163 | "* It prevents the abrupt termination of the program" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "\"\"\"\n", 173 | "syntax ---->\n", 174 | "\n", 175 | "try:\n", 176 | " statement1\n", 177 | " statement2\n", 178 | " .......\n", 179 | "except:\n", 180 | " statement1\n", 181 | " statement2\n", 182 | " .......\n", 183 | "\"\"\"" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 5, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "hello world\n", 196 | "Name variable is not defined\n", 197 | "Welcome to exception handling\n", 198 | "Thank you\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "print(\"hello world\")\n", 204 | "\n", 205 | "try:\n", 206 | " print(name)\n", 207 | "except:\n", 208 | " print(\"Name variable is not defined\")\n", 209 | " \n", 210 | "print('Welcome to exception handling')\n", 211 | "print('Thank you')" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "### Ex2)" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 6, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "#list\n", 228 | "list1 = [1,2,3,4,5]" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 7, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "ename": "IndexError", 238 | "evalue": "list index out of range", 239 | "output_type": "error", 240 | "traceback": [ 241 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 242 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 243 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#without\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0melem\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'element = '\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0melem\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 244 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "#without try except\n", 250 | "index = 6\n", 251 | "elem = list1[index]\n", 252 | "print('element = ',elem)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 8, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "name": "stdout", 262 | "output_type": "stream", 263 | "text": [ 264 | "Invalid index\n" 265 | ] 266 | } 267 | ], 268 | "source": [ 269 | "#with try except\n", 270 | "try:\n", 271 | " index = 6\n", 272 | " elem = list1[index]\n", 273 | " print('element = ',elem)\n", 274 | "except:\n", 275 | " print('Invalid index')" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "## finally:\n", 283 | "* No matter if the exception occurs or not this block will compulsorily get executed" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "\"\"\"\n", 293 | "syntax ---->\n", 294 | "\n", 295 | "try:\n", 296 | " statement1\n", 297 | " statement2\n", 298 | " .......\n", 299 | "except:\n", 300 | " statement1\n", 301 | " statement2\n", 302 | " .......\n", 303 | "finally:\n", 304 | " statement1\n", 305 | " statement2\n", 306 | " .......\n", 307 | "\"\"\"" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "### Ex3)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 9, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "Invalid index\n", 327 | "End of program\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "try:\n", 333 | " index = 6\n", 334 | " elem = list1[index]\n", 335 | " print('element = ',elem)\n", 336 | "except:\n", 337 | " print('Invalid index')\n", 338 | "finally:\n", 339 | " print(\"End of program\")\n" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "## else:\n", 347 | "* You can also use else block along with try except\n", 348 | "* else block will get execute only if there is no exception\n", 349 | "* it should be placed after the except block and before the finally block" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "\"\"\"\n", 359 | "syntax ---->\n", 360 | "\n", 361 | "try:\n", 362 | " statement1\n", 363 | " statement2\n", 364 | " .......\n", 365 | "except:\n", 366 | " statement1\n", 367 | " statement2\n", 368 | " .......\n", 369 | "else:\n", 370 | " statement1\n", 371 | " statement2\n", 372 | " .......\n", 373 | "finally:\n", 374 | " statement1\n", 375 | " statement2\n", 376 | " .......\n", 377 | "\"\"\"" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "### Ex4)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 10, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | "Invalid index\n", 397 | "End of program\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "try:\n", 403 | " index = 6\n", 404 | " elem = list1[index]\n", 405 | " print('element = ',elem)\n", 406 | "except:\n", 407 | " print('Invalid index')\n", 408 | "else:\n", 409 | " print('No exception')\n", 410 | "finally:\n", 411 | " print(\"End of program\")\n" 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "## Excepting multiple errors" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "### Ex 5.1 : Dividing two numbers without try except blocks" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 11, 431 | "metadata": { 432 | "scrolled": true 433 | }, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "start of the program\n", 440 | "Enter the numerator : 100\n", 441 | "Enter the denominator : 10\n", 442 | "Numerator = 100 , Denominator = 10\n", 443 | "result = 10.0\n", 444 | "End of program\n" 445 | ] 446 | } 447 | ], 448 | "source": [ 449 | "print(\"start of the program\")\n", 450 | "\n", 451 | "num1 = int(input(\"Enter the numerator : \"))\n", 452 | "num2 = int(input(\"Enter the denominator : \"))\n", 453 | "\n", 454 | "print('Numerator = {} , Denominator = {}'.format(num1,num2))\n", 455 | "\n", 456 | "result = num1/num2\n", 457 | "\n", 458 | "print(\"result = \",result)\n", 459 | "print(\"End of program\")" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": null, 465 | "metadata": {}, 466 | "outputs": [], 467 | "source": [] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "metadata": {}, 472 | "source": [ 473 | "### Ex 5.2 : Dividing two numbers with only one except block" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 12, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "start of the program\n", 486 | "Enter the numerator : python\n", 487 | "Something went wrong\n", 488 | "End of program\n" 489 | ] 490 | } 491 | ], 492 | "source": [ 493 | "print(\"start of the program\")\n", 494 | "\n", 495 | "try:\n", 496 | " num1 = int(input(\"Enter the numerator : \"))\n", 497 | " num2 = int(input(\"Enter the denominator : \"))\n", 498 | "\n", 499 | " print('Numerator = {} , Denominator = {}'.format(num1,num2))\n", 500 | "\n", 501 | " result = num1/num2\n", 502 | " print(\"result = \",result)\n", 503 | "except:\n", 504 | " print(\"Something went wrong\")\n", 505 | "finally:\n", 506 | " print(\"End of program\")\n" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": null, 512 | "metadata": {}, 513 | "outputs": [], 514 | "source": [] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "### Ex 5.3 : Dividing two numbers with multiple except blocks" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "\"\"\"\n", 530 | "syntax for excepting multiple exceptions\n", 531 | "\n", 532 | "try:\n", 533 | " statement1\n", 534 | " .......\n", 535 | "except Exception1:\n", 536 | " statement1\n", 537 | " .......\n", 538 | "except Exception2:\n", 539 | " statement1\n", 540 | " .......\n", 541 | "except:\n", 542 | " statement1\n", 543 | " .......\n", 544 | "\n", 545 | "\"\"\"" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 13, 551 | "metadata": {}, 552 | "outputs": [ 553 | { 554 | "name": "stdout", 555 | "output_type": "stream", 556 | "text": [ 557 | "start of the program\n", 558 | "Enter the numerator : 100\n", 559 | "Enter the denominator : 0\n", 560 | "Numerator = 100 , Denominator = 0\n", 561 | "Enter a non-zero denominator\n", 562 | "End of program\n" 563 | ] 564 | } 565 | ], 566 | "source": [ 567 | "print(\"start of the program\")\n", 568 | "\n", 569 | "try:\n", 570 | " num1 = int(input(\"Enter the numerator : \"))\n", 571 | " num2 = int(input(\"Enter the denominator : \"))\n", 572 | "\n", 573 | " print('Numerator = {} , Denominator = {}'.format(num1,num2))\n", 574 | "\n", 575 | " result = num1/num2\n", 576 | " print(\"result = \",result)\n", 577 | " \n", 578 | "except ZeroDivisionError:\n", 579 | " print(\"Enter a non-zero denominator\")\n", 580 | " \n", 581 | "except ValueError:\n", 582 | " print(\"Please enter the numbers only\")\n", 583 | " \n", 584 | "except:\n", 585 | " print(\"Something went wrong\")\n", 586 | " \n", 587 | "finally:\n", 588 | " print(\"End of program\")\n" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": null, 594 | "metadata": {}, 595 | "outputs": [], 596 | "source": [] 597 | } 598 | ], 599 | "metadata": { 600 | "kernelspec": { 601 | "display_name": "Python 3", 602 | "language": "python", 603 | "name": "python3" 604 | }, 605 | "language_info": { 606 | "codemirror_mode": { 607 | "name": "ipython", 608 | "version": 3 609 | }, 610 | "file_extension": ".py", 611 | "mimetype": "text/x-python", 612 | "name": "python", 613 | "nbconvert_exporter": "python", 614 | "pygments_lexer": "ipython3", 615 | "version": "3.8.3" 616 | } 617 | }, 618 | "nbformat": 4, 619 | "nbformat_minor": 2 620 | } 621 | -------------------------------------------------------------------------------- /15 - File Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# File Handling\n", 8 | "* Python supports file handling and allows users to handle files i.e., to read and write files\n", 9 | "* we use **open() function** to do the file handling in python" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### open() function" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "\"\"\"\n", 26 | "syntax --->\n", 27 | "open(filename,mode)\n", 28 | "\n", 29 | "filename - name of the file.\n", 30 | "mode - mode is used to specify what action you want to perform.\n", 31 | "\n", 32 | "mode can take the following values : \n", 33 | "\n", 34 | "\"r\" - Read - Default value. \n", 35 | "If the file is present, then it opens the file for reading.Else it will throw an error\n", 36 | "\n", 37 | "\"a\" - Append \n", 38 | "If the file is present, it opens the file for appending.Else creates the new file. \n", 39 | "\n", 40 | "\"w\" - Write \n", 41 | "If the file is present,it opens the file for writing.Else creates the new file.\n", 42 | "\n", 43 | "\"\"\"" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "### Note:\n", 51 | "Whenever you do the read/write operations follow these steps:\n", 52 | "* 1) open the file\n", 53 | "* 2) do read/write operations\n", 54 | "* 3) close the file" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "### 1) To open and Read the files" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 1, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "file = open('MyFile.txt','r') #same as open('MyFile.txt')" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "#### 1) read()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 2, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'Hello,I am a sentence 1.\\nI am a sentence 2.\\nI am a sentence 3.\\n'" 89 | ] 90 | }, 91 | "execution_count": 2, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "file.read()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 3, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "''" 109 | ] 110 | }, 111 | "execution_count": 3, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "#trying to read again\n", 118 | "file.read()" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 4, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "0" 130 | ] 131 | }, 132 | "execution_count": 4, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "#to take the cursor back to beginning of the text file, use seek()\n", 139 | "file.seek(0)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "'Hello,I am a sentence 1.\\nI am a sentence 2.\\nI am a sentence 3.\\n'" 151 | ] 152 | }, 153 | "execution_count": 5, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "file.read()" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "#### 2) readline()" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 6, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "'Hello,I am a sentence 1.\\n'" 178 | ] 179 | }, 180 | "execution_count": 6, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "file.seek(0)\n", 187 | "file.readline()" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 7, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "'I am a sentence 2.\\n'" 199 | ] 200 | }, 201 | "execution_count": 7, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "file.readline()" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 8, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "'I am a sentence 3.\\n'" 219 | ] 220 | }, 221 | "execution_count": 8, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "file.readline()" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "data": { 237 | "text/plain": [ 238 | "''" 239 | ] 240 | }, 241 | "execution_count": 9, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "file.readline()" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 10, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "0" 259 | ] 260 | }, 261 | "execution_count": 10, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "file.seek(0)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 11, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "name": "stdout", 277 | "output_type": "stream", 278 | "text": [ 279 | "Hello,I am a sentence 1.\n", 280 | "\n", 281 | "I am a sentence 2.\n", 282 | "\n", 283 | "I am a sentence 3.\n", 284 | "\n" 285 | ] 286 | } 287 | ], 288 | "source": [ 289 | "# you can use for loop to read all the lines\n", 290 | "for line in file:\n", 291 | " print(line)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 12, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "0" 303 | ] 304 | }, 305 | "execution_count": 12, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "file.seek(0)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 13, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "['Hello,I am a sentence 1.\\n', 'I am a sentence 2.\\n', 'I am a sentence 3.\\n']\n", 324 | "I am a sentence 3.\n", 325 | "\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "#To read perticular lines\n", 331 | "list1 = file.readlines()\n", 332 | "print(list1)\n", 333 | "#to get last line\n", 334 | "print(list1[-1])" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "### Note : Don't forget to close the files\n", 342 | "* to close the files we use **close() method**" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 14, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "file.close()" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 15, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "ename": "ValueError", 361 | "evalue": "I/O operation on closed file.", 362 | "output_type": "error", 363 | "traceback": [ 364 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 365 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 366 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#You cannot do read write operations on closed files\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 367 | "\u001b[1;31mValueError\u001b[0m: I/O operation on closed file." 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "#You cannot do read write operations on closed files\n", 373 | "file.read() #Error" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "### 2) To write to an existing file\n", 381 | "* we use **write() method** to write to a file\n", 382 | "* use mode = 'a' - append\n", 383 | "* use mode = 'w' - write" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "### Appending\n", 391 | "#### Ex1)" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 20, 397 | "metadata": {}, 398 | "outputs": [], 399 | "source": [ 400 | "# appending one line\n", 401 | "file = open('MyFile.txt','a')\n", 402 | "file.write('I am appending sentence 4')\n", 403 | "file.close()" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 21, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "Hello,I am a sentence 1.\n", 416 | "I am a sentence 2.\n", 417 | "I am a sentence 3.I am appending sentence 4\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "# reading the file after modifying\n", 423 | "file = open('MyFile.txt')\n", 424 | "print(file.read())\n", 425 | "file.close()" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "#### Ex2)" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 22, 438 | "metadata": {}, 439 | "outputs": [], 440 | "source": [ 441 | "file = open('MyFile.txt','a')\n", 442 | "file.write('\\nI am appending sentence 4')\n", 443 | "file.close()" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 23, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "name": "stdout", 453 | "output_type": "stream", 454 | "text": [ 455 | "Hello,I am a sentence 1.\n", 456 | "I am a sentence 2.\n", 457 | "I am a sentence 3.I am appending sentence 4\n", 458 | "I am appending sentence 4\n" 459 | ] 460 | } 461 | ], 462 | "source": [ 463 | "file = open('MyFile.txt')\n", 464 | "print(file.read())\n", 465 | "file.close()" 466 | ] 467 | }, 468 | { 469 | "cell_type": "markdown", 470 | "metadata": {}, 471 | "source": [ 472 | "### Writing to new file\n", 473 | "#### Ex1) To an existing file" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 24, 479 | "metadata": {}, 480 | "outputs": [], 481 | "source": [ 482 | "file = open('MyFile.txt','w')\n", 483 | "file.write('Hi I am a new sentence')\n", 484 | "file.close()" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 25, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | "Hi I am a new sentence\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "file = open('MyFile.txt')\n", 502 | "print(file.read())\n", 503 | "file.close()" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "#### Ex2) Writing to a new file" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 26, 516 | "metadata": {}, 517 | "outputs": [], 518 | "source": [ 519 | "file = open('MyFile11.txt','w')\n", 520 | "file.write('Hi I am a new sentence')\n", 521 | "file.close()" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": {}, 534 | "source": [ 535 | "### With statement\n", 536 | "\n", 537 | "* When we use with statement for handling files, all the cleanup will be automatically done" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": null, 543 | "metadata": {}, 544 | "outputs": [], 545 | "source": [ 546 | "\"\"\"\n", 547 | "syntax\n", 548 | "\n", 549 | "with open(filename,mode) as file:\n", 550 | " do read / write operations\n", 551 | "\"\"\"" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 27, 557 | "metadata": { 558 | "scrolled": true 559 | }, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "Hi I am a new sentence\n" 566 | ] 567 | } 568 | ], 569 | "source": [ 570 | "#Ex1) reading the files using with open\n", 571 | "with open('MyFile.txt') as file:\n", 572 | " data = file.read()\n", 573 | " print(data)\n" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": 28, 579 | "metadata": {}, 580 | "outputs": [ 581 | { 582 | "ename": "ValueError", 583 | "evalue": "I/O operation on closed file.", 584 | "output_type": "error", 585 | "traceback": [ 586 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 587 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 588 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 589 | "\u001b[1;31mValueError\u001b[0m: I/O operation on closed file." 590 | ] 591 | } 592 | ], 593 | "source": [ 594 | "file.read() #Error because the file is already closed" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 29, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "# to append\n", 604 | "with open('MyFile.txt','a') as file:\n", 605 | " file.write('\\nI am sentence 2')" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 30, 611 | "metadata": {}, 612 | "outputs": [], 613 | "source": [ 614 | "# to write\n", 615 | "with open('MyFile222.txt','w') as file:\n", 616 | " file.write('I am a new file')" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": null, 622 | "metadata": {}, 623 | "outputs": [], 624 | "source": [] 625 | } 626 | ], 627 | "metadata": { 628 | "kernelspec": { 629 | "display_name": "Python 3", 630 | "language": "python", 631 | "name": "python3" 632 | }, 633 | "language_info": { 634 | "codemirror_mode": { 635 | "name": "ipython", 636 | "version": 3 637 | }, 638 | "file_extension": ".py", 639 | "mimetype": "text/x-python", 640 | "name": "python", 641 | "nbconvert_exporter": "python", 642 | "pygments_lexer": "ipython3", 643 | "version": "3.8.3" 644 | } 645 | }, 646 | "nbformat": 4, 647 | "nbformat_minor": 2 648 | } 649 | -------------------------------------------------------------------------------- /16 - Classes and Objects.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Classes\n", 8 | "* Like every other Object Oriented Programming language Python also supports classes.\n", 9 | "* Class is like a **blueprint** or **template** for creating objects.\n", 10 | "* **class** keyword is used to create classes in python" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "\"\"\"\n", 20 | "syntax ---->\n", 21 | "\n", 22 | "class NameOfTheClass:\n", 23 | " .......\n", 24 | "\"\"\"" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "#Ex1) Consider the Dog example\n", 34 | "class Dog:\n", 35 | " pass" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Object\n", 43 | "* An object (instance) is an instantiation of a class\n", 44 | "* An object has two characteristics:\n", 45 | " * 1) attributes / properties / variables\n", 46 | " * Ex : breed,size,age,color,etc\n", 47 | " * 2) behavior / actions / methods\n", 48 | " * Ex : bark(),eat(),sleep() etc" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 2, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "#Creating the object\n", 58 | "dog_obj = Dog()" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "#### _ _init_ _() method\n", 66 | "\n", 67 | "* All classes have a function called _ _init_ _(), which is always executed when the class is being instantiated.\n", 68 | "* It is run as soon as an object of a class is instantiated.\n", 69 | "* use this function to assign values to object properties" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 3, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "class Dog:\n", 79 | " def __init__(self):\n", 80 | " print('Inside init method')" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 4, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "Inside init method\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "dog_obj = Dog()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "#### self parameter\n", 105 | "* Class methods must have an extra first parameter in method definition. We do not give a value for this parameter when we call the method, Python provides it\n", 106 | "* The self parameter is a reference to the current instance of the class \n", 107 | "* It can be used to access variables that belongs to the class.\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "class Dog:\n", 117 | " def __init__(self,breed,size,age):\n", 118 | " self.breed = breed\n", 119 | " self.size = size\n", 120 | " self.age = age" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "dog1 = Dog('Pug','small',1)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 7, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "1" 141 | ] 142 | }, 143 | "execution_count": 7, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "dog1.age" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 8, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "'Pug'" 161 | ] 162 | }, 163 | "execution_count": 8, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "dog1.breed" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 9, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "'small'" 181 | ] 182 | }, 183 | "execution_count": 9, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "dog1.size" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 10, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "dog2 = Dog('Bulldog','medium',2)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 11, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "2" 210 | ] 211 | }, 212 | "execution_count": 11, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "dog2.age" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 12, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "'Bulldog'" 230 | ] 231 | }, 232 | "execution_count": 12, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "dog2.breed" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 13, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/plain": [ 249 | "'medium'" 250 | ] 251 | }, 252 | "execution_count": 13, 253 | "metadata": {}, 254 | "output_type": "execute_result" 255 | } 256 | ], 257 | "source": [ 258 | "dog2.size" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "#### class variables and instance variables\n", 266 | "* class variables are the variables that are common to all the objects\n", 267 | "* instance variables are the variables that are specific to the perticular objects" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 14, 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "class Dog:\n", 277 | " #class variables\n", 278 | " species = 'mammal'\n", 279 | " city = 'Bengaluru' #Assumed for this example\n", 280 | " \n", 281 | " def __init__(self,breed,size,age):\n", 282 | " #instance variables\n", 283 | " self.breed = breed\n", 284 | " self.size = size\n", 285 | " self.age = age" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 15, 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [ 294 | "dog1 = Dog('pug','small',1)\n", 295 | "dog2 = Dog('bulldog','medium',2)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 16, 301 | "metadata": {}, 302 | "outputs": [ 303 | { 304 | "data": { 305 | "text/plain": [ 306 | "'mammal'" 307 | ] 308 | }, 309 | "execution_count": 16, 310 | "metadata": {}, 311 | "output_type": "execute_result" 312 | } 313 | ], 314 | "source": [ 315 | "dog1.species" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 17, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "'mammal'" 327 | ] 328 | }, 329 | "execution_count": 17, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "dog2.species" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 18, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "'Bengaluru'" 347 | ] 348 | }, 349 | "execution_count": 18, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "dog1.city" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 19, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "'Bengaluru'" 367 | ] 368 | }, 369 | "execution_count": 19, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "dog2.city" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 20, 381 | "metadata": {}, 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | "'pug'" 387 | ] 388 | }, 389 | "execution_count": 20, 390 | "metadata": {}, 391 | "output_type": "execute_result" 392 | } 393 | ], 394 | "source": [ 395 | "dog1.breed" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 21, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "data": { 405 | "text/plain": [ 406 | "'bulldog'" 407 | ] 408 | }, 409 | "execution_count": 21, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "dog2.breed" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "metadata": {}, 422 | "outputs": [], 423 | "source": [] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": null, 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "#### Note: you can access the class variables by using the classname itself ie.,\n", 437 | "* ClassName.class_variable" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": 22, 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "data": { 447 | "text/plain": [ 448 | "'mammal'" 449 | ] 450 | }, 451 | "execution_count": 22, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "Dog.species" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 23, 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "data": { 467 | "text/plain": [ 468 | "'Bengaluru'" 469 | ] 470 | }, 471 | "execution_count": 23, 472 | "metadata": {}, 473 | "output_type": "execute_result" 474 | } 475 | ], 476 | "source": [ 477 | "Dog.city" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "#### Methods" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 24, 490 | "metadata": {}, 491 | "outputs": [], 492 | "source": [ 493 | "class Dog:\n", 494 | " #class variables\n", 495 | " species = 'mammal'\n", 496 | " \n", 497 | " def __init__(self,breed,size,age):\n", 498 | " #instance variables\n", 499 | " self.breed = breed\n", 500 | " self.size = size\n", 501 | " self.age = age\n", 502 | " \n", 503 | " #without any parameters\n", 504 | " def bark(self):\n", 505 | " print('Boww Boww. I am '+self.breed)\n", 506 | " \n", 507 | " #with one parameter\n", 508 | " def eat(self,food):\n", 509 | " print('I will eat '+food)\n", 510 | " \n", 511 | " def sleep(self,hours = 1):\n", 512 | " print(\"HI i am {} and I sleep for {} hours\".format(self.breed,hours))" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 25, 518 | "metadata": {}, 519 | "outputs": [], 520 | "source": [ 521 | "dog1 = Dog('pug','small',1)" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 26, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "name": "stdout", 531 | "output_type": "stream", 532 | "text": [ 533 | "Boww Boww. I am pug\n" 534 | ] 535 | } 536 | ], 537 | "source": [ 538 | "dog1.bark()" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 27, 544 | "metadata": {}, 545 | "outputs": [ 546 | { 547 | "name": "stdout", 548 | "output_type": "stream", 549 | "text": [ 550 | "I will eat pedigree\n" 551 | ] 552 | } 553 | ], 554 | "source": [ 555 | "dog1.eat('pedigree')" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 28, 561 | "metadata": {}, 562 | "outputs": [ 563 | { 564 | "name": "stdout", 565 | "output_type": "stream", 566 | "text": [ 567 | "HI i am pug and I sleep for 4 hours\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "dog1.sleep(4)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": 29, 578 | "metadata": {}, 579 | "outputs": [], 580 | "source": [ 581 | "dog2 = Dog('bulldog','medium',2)" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 30, 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "name": "stdout", 591 | "output_type": "stream", 592 | "text": [ 593 | "Boww Boww. I am bulldog\n" 594 | ] 595 | } 596 | ], 597 | "source": [ 598 | "dog2.bark()" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 31, 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "name": "stdout", 608 | "output_type": "stream", 609 | "text": [ 610 | "I will eat chicken\n" 611 | ] 612 | } 613 | ], 614 | "source": [ 615 | "dog2.eat('chicken')" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 32, 621 | "metadata": {}, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "HI i am bulldog and I sleep for 10 hours\n" 628 | ] 629 | } 630 | ], 631 | "source": [ 632 | "dog2.sleep(10)" 633 | ] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "metadata": {}, 638 | "source": [ 639 | "### Circle" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": 33, 645 | "metadata": {}, 646 | "outputs": [], 647 | "source": [ 648 | "class CircleClass:\n", 649 | " #class variable\n", 650 | " pie = 3.14\n", 651 | " \n", 652 | " def __init__(self,radius):\n", 653 | " self.radius = radius\n", 654 | " \n", 655 | " def get_area(self):\n", 656 | " return self.pie * self.radius * self.radius\n", 657 | " \n", 658 | " def get_circumference(self):\n", 659 | " return 2 * self.pie * self.radius" 660 | ] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "execution_count": 34, 665 | "metadata": {}, 666 | "outputs": [], 667 | "source": [ 668 | "cir1 = CircleClass(5)\n", 669 | "cir2 = CircleClass(10)\n", 670 | "cir3 = CircleClass(15)" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 35, 676 | "metadata": {}, 677 | "outputs": [ 678 | { 679 | "data": { 680 | "text/plain": [ 681 | "78.5" 682 | ] 683 | }, 684 | "execution_count": 35, 685 | "metadata": {}, 686 | "output_type": "execute_result" 687 | } 688 | ], 689 | "source": [ 690 | "cir1.get_area()" 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": 36, 696 | "metadata": {}, 697 | "outputs": [ 698 | { 699 | "data": { 700 | "text/plain": [ 701 | "314.0" 702 | ] 703 | }, 704 | "execution_count": 36, 705 | "metadata": {}, 706 | "output_type": "execute_result" 707 | } 708 | ], 709 | "source": [ 710 | "cir2.get_area()" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 37, 716 | "metadata": {}, 717 | "outputs": [ 718 | { 719 | "data": { 720 | "text/plain": [ 721 | "706.5" 722 | ] 723 | }, 724 | "execution_count": 37, 725 | "metadata": {}, 726 | "output_type": "execute_result" 727 | } 728 | ], 729 | "source": [ 730 | "cir3.get_area()" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": 38, 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "data": { 740 | "text/plain": [ 741 | "31.400000000000002" 742 | ] 743 | }, 744 | "execution_count": 38, 745 | "metadata": {}, 746 | "output_type": "execute_result" 747 | } 748 | ], 749 | "source": [ 750 | "cir1.get_circumference()" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 39, 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "data": { 760 | "text/plain": [ 761 | "62.800000000000004" 762 | ] 763 | }, 764 | "execution_count": 39, 765 | "metadata": {}, 766 | "output_type": "execute_result" 767 | } 768 | ], 769 | "source": [ 770 | "cir2.get_circumference()" 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "execution_count": 40, 776 | "metadata": {}, 777 | "outputs": [ 778 | { 779 | "data": { 780 | "text/plain": [ 781 | "94.2" 782 | ] 783 | }, 784 | "execution_count": 40, 785 | "metadata": {}, 786 | "output_type": "execute_result" 787 | } 788 | ], 789 | "source": [ 790 | "cir3.get_circumference()" 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": null, 796 | "metadata": {}, 797 | "outputs": [], 798 | "source": [] 799 | } 800 | ], 801 | "metadata": { 802 | "kernelspec": { 803 | "display_name": "Python 3", 804 | "language": "python", 805 | "name": "python3" 806 | }, 807 | "language_info": { 808 | "codemirror_mode": { 809 | "name": "ipython", 810 | "version": 3 811 | }, 812 | "file_extension": ".py", 813 | "mimetype": "text/x-python", 814 | "name": "python", 815 | "nbconvert_exporter": "python", 816 | "pygments_lexer": "ipython3", 817 | "version": "3.8.3" 818 | } 819 | }, 820 | "nbformat": 4, 821 | "nbformat_minor": 2 822 | } 823 | -------------------------------------------------------------------------------- /OneHotEncodervsLabelEncoder/OneHotEncodervsLabelEncoder.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "\n", 11 | "df= pd.read_csv(\"carprices.csv\") " 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 13, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/html": [ 22 | "
\n", 23 | "\n", 36 | "\n", 37 | " \n", 38 | " \n", 39 | " \n", 40 | " \n", 41 | " \n", 42 | " \n", 43 | " \n", 44 | " \n", 45 | " \n", 46 | " \n", 47 | " \n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | "
Car_ModelMileageSell Price($)Age(yrs)
0BMW X569000180006
1BMW X535000340003
2BMW X557000261005
3BMW X522500400002
4BMW X546000315004
\n", 84 | "
" 85 | ], 86 | "text/plain": [ 87 | " Car_Model Mileage Sell Price($) Age(yrs)\n", 88 | "0 BMW X5 69000 18000 6\n", 89 | "1 BMW X5 35000 34000 3\n", 90 | "2 BMW X5 57000 26100 5\n", 91 | "3 BMW X5 22500 40000 2\n", 92 | "4 BMW X5 46000 31500 4" 93 | ] 94 | }, 95 | "execution_count": 13, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "df.head()" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "### OneHotEncoder vs LabelEncoding" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "### 1. OneHotEncoder" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 15, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/html": [ 126 | "
\n", 127 | "\n", 140 | "\n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | "
Audi A5BMW X5Mercedez Benz C class
0010
1010
2010
3010
4010
5100
6100
7100
8100
9001
10001
11001
12001
\n", 230 | "
" 231 | ], 232 | "text/plain": [ 233 | " Audi A5 BMW X5 Mercedez Benz C class\n", 234 | "0 0 1 0\n", 235 | "1 0 1 0\n", 236 | "2 0 1 0\n", 237 | "3 0 1 0\n", 238 | "4 0 1 0\n", 239 | "5 1 0 0\n", 240 | "6 1 0 0\n", 241 | "7 1 0 0\n", 242 | "8 1 0 0\n", 243 | "9 0 0 1\n", 244 | "10 0 0 1\n", 245 | "11 0 0 1\n", 246 | "12 0 0 1" 247 | ] 248 | }, 249 | "execution_count": 15, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "pd.get_dummies(df.Car_Model)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "### LabelEncoder from Sklearn" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 21, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "\n", 272 | "from sklearn.preprocessing import LabelEncoder\n", 273 | "le = LabelEncoder()\n" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 23, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "data": { 283 | "text/html": [ 284 | "
\n", 285 | "\n", 298 | "\n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | "
Car_ModelMileageSell Price($)Age(yrs)
0169000180006
1135000340003
2157000261005
3122500400002
4146000315004
5059000294005
6052000320005
7072000193006
8091000120008
9267000220006
10283000200007
11279000210007
12259000330005
\n", 402 | "
" 403 | ], 404 | "text/plain": [ 405 | " Car_Model Mileage Sell Price($) Age(yrs)\n", 406 | "0 1 69000 18000 6\n", 407 | "1 1 35000 34000 3\n", 408 | "2 1 57000 26100 5\n", 409 | "3 1 22500 40000 2\n", 410 | "4 1 46000 31500 4\n", 411 | "5 0 59000 29400 5\n", 412 | "6 0 52000 32000 5\n", 413 | "7 0 72000 19300 6\n", 414 | "8 0 91000 12000 8\n", 415 | "9 2 67000 22000 6\n", 416 | "10 2 83000 20000 7\n", 417 | "11 2 79000 21000 7\n", 418 | "12 2 59000 33000 5" 419 | ] 420 | }, 421 | "execution_count": 23, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "\n", 428 | "dfle = df\n", 429 | "dfle.Car_Model = le.fit_transform(dfle.Car_Model)\n", 430 | "dfle" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 1, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "ename": "NameError", 440 | "evalue": "name 'Ridge' is not defined", 441 | "output_type": "error", 442 | "traceback": [ 443 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 444 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 445 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRidge\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 446 | "\u001b[1;31mNameError\u001b[0m: name 'Ridge' is not defined" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "help()" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": null, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [] 460 | } 461 | ], 462 | "metadata": { 463 | "kernelspec": { 464 | "display_name": "Python 3", 465 | "language": "python", 466 | "name": "python3" 467 | }, 468 | "language_info": { 469 | "codemirror_mode": { 470 | "name": "ipython", 471 | "version": 3 472 | }, 473 | "file_extension": ".py", 474 | "mimetype": "text/x-python", 475 | "name": "python", 476 | "nbconvert_exporter": "python", 477 | "pygments_lexer": "ipython3", 478 | "version": "3.8.3" 479 | } 480 | }, 481 | "nbformat": 4, 482 | "nbformat_minor": 4 483 | } 484 | -------------------------------------------------------------------------------- /OneHotEncodervsLabelEncoder/carprices.csv: -------------------------------------------------------------------------------- 1 | Car_Model,Mileage,Sell Price($),Age(yrs) 2 | BMW X5,69000,18000,6 3 | BMW X5,35000,34000,3 4 | BMW X5,57000,26100,5 5 | BMW X5,22500,40000,2 6 | BMW X5,46000,31500,4 7 | Audi A5,59000,29400,5 8 | Audi A5,52000,32000,5 9 | Audi A5,72000,19300,6 10 | Audi A5,91000,12000,8 11 | Mercedez Benz C class,67000,22000,6 12 | Mercedez Benz C class,83000,20000,7 13 | Mercedez Benz C class,79000,21000,7 14 | Mercedez Benz C class,59000,33000,5 15 | -------------------------------------------------------------------------------- /Other Py FIles/Logging in Py.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "5e6ef173", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import logging as lg" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "cefc161f", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "lg.basicConfig(filename = 'test.log', level = lg.INFO, format = '%(asctime)s %(message)s')" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 4, 26 | "id": "9c54762e", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "lg.info(\"Info logging\")\n", 31 | "lg.debug(\"Debug Logging\")\n", 32 | "lg.warning('warning log')\n", 33 | "lg.error('error log')\n", 34 | "lg.exception('exception log')\n", 35 | "lg.critical('this is critical log')" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "id": "fbe06a59", 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.8.8" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 5 68 | } 69 | -------------------------------------------------------------------------------- /img/data_types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/data_types.png -------------------------------------------------------------------------------- /img/img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/img1.png -------------------------------------------------------------------------------- /img/img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/img2.png -------------------------------------------------------------------------------- /img/indexing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/indexing.png -------------------------------------------------------------------------------- /img/rev_indexing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/rev_indexing.png -------------------------------------------------------------------------------- /img/truthTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vageeshh/Python/2d8d7e66567aa4a1026ec63e99c6b30a14abfc77/img/truthTable.png -------------------------------------------------------------------------------- /marks.csv: -------------------------------------------------------------------------------- 1 | Name,Physics,Chemistry,Maths,Biology 2 | A,65,69,87,90 3 | B,44,76,57,80 4 | C,57,85,79,75 5 | D,54,66,71,70 6 | E,74,86,75,88 7 | F,64,67,77,81 --------------------------------------------------------------------------------