├── 04 - Basic Syntax - Python Programming Tutorial.ipynb ├── 05 - Different Data Types - Python Programming Tutorial.ipynb ├── 06 - Basic Operators - Python Programming Tutorial.ipynb ├── 07 - Decision Making - Python Programming Tutorial.ipynb ├── 08 - Loops - Python Programming Tutorial.ipynb ├── 09 - Strings - Python Programming Tutorial.ipynb ├── 10 - Lists - Python Programming Tutorial.ipynb ├── 11 - Tuples - Python Programming Tutorial.ipynb ├── 12 - Dictionary - Python Programming Tutorial.ipynb ├── 13 - Functions - Python Programming Tutorial.ipynb ├── 14 - Modules - Python Programming Tutorial.ipynb ├── 15 - Classes - Python Programming Tutorial.ipynb ├── 16 - File Handling - Python Programming Tutorial.ipynb ├── 17 - Exceptions - Python Programming Tutorial.ipynb ├── 18 - Multithreading - Python Programming Tutorial.ipynb └── README.md /04 - Basic Syntax - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Hello World\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "# print\n", 18 | "print(\"Hello World\")" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "10\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "# variable\n", 36 | "a = 10\n", 37 | "print(a)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 7, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "6\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "# multiline statements\n", 55 | "total = 1 + \\\n", 56 | " 2 + \\\n", 57 | " 3\n", 58 | "print(total)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 12, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "hacker\n", 71 | "realm\n", 72 | "This is a\n", 73 | "big paragraph\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "# quotes\n", 79 | "var = 'hacker'\n", 80 | "print(var)\n", 81 | "var = \"realm\"\n", 82 | "print(var)\n", 83 | "var = \"\"\"This is a\n", 84 | "big paragraph\"\"\"\n", 85 | "print(var)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 20, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "# comment\n", 95 | "# This is single line comment\n", 96 | "# print(\"hello\")\n", 97 | "\n", 98 | "# \"\"\"\n", 99 | "# type your content\n", 100 | "# \"\"\"\n", 101 | "# Ctrl + /" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 25, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "Enter your name: ash\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "# input\n", 119 | "# a = input()\n", 120 | "a = input(\"Enter your name: \")" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 26, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "ash\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "print(a)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 29, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "1 2 3\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# multiple statements in a single line\n", 155 | "a = 1;b = 2;c = 3;print(a,b,c)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 30, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "# how to swap two integers without the third varible\n", 165 | "a,b = b,a" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 31, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "2 1\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "print(a,b)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [] 205 | } 206 | ], 207 | "metadata": { 208 | "kernelspec": { 209 | "display_name": "Python 3", 210 | "language": "python", 211 | "name": "python3" 212 | }, 213 | "language_info": { 214 | "codemirror_mode": { 215 | "name": "ipython", 216 | "version": 3 217 | }, 218 | "file_extension": ".py", 219 | "mimetype": "text/x-python", 220 | "name": "python", 221 | "nbconvert_exporter": "python", 222 | "pygments_lexer": "ipython3", 223 | "version": "3.6.5" 224 | } 225 | }, 226 | "nbformat": 4, 227 | "nbformat_minor": 2 228 | } 229 | -------------------------------------------------------------------------------- /05 - Different Data Types - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1 2.0 ash\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "# basic data types\n", 18 | "a = 1 # integer\n", 19 | "b = 2.0 # float\n", 20 | "c = \"ash\" # string\n", 21 | "print(a, b, c)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "0 0 0\n", 34 | "1 2.0 ash\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# multiple assignment\n", 40 | "a = b = c = 0\n", 41 | "print(a, b, c)\n", 42 | "\n", 43 | "\n", 44 | "a,b,c = 1, 2.0, \"ash\"\n", 45 | "print(a,b,c)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Hackers Realm\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "# String\n", 63 | "s = \"Hackers Realm\"\n", 64 | "print(s)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "H\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "print(s[0]) # first character" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 8, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "Hackers\n", 94 | "Hackers\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "print(s[0:7]) # it will print the word in index range\n", 100 | "print(s[:7]) " 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 9, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "Realm\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "print(s[8:]) # prints from 8th index to the end of the string" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 11, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "Hackers RealmHackers RealmHackers Realm\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print(s * 3) # it multiplies the string 3 times" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 12, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "Hackers Realm Channel\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "print(s + \" Channel\") # concatenate" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 17, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "ename": "TypeError", 161 | "evalue": "'str' object does not support item assignment", 162 | "output_type": "error", 163 | "traceback": [ 164 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 165 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 166 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'a'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 167 | "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "s[0] = 'a' # immutable" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 13, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "[1, 3.0, 'goku']\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "# list\n", 211 | "l = [1, 3.0, \"goku\"]\n", 212 | "print(l)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 16, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "1\n" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "print(l[0]) #first element" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 18, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "[2, 3.0, 'goku']\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "l[0] = 2 # mutable\n", 247 | "print(l)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 19, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "[2, 3.0, 'goku', 1, 2, 3, 4, 5]\n" 260 | ] 261 | } 262 | ], 263 | "source": [ 264 | "t = [1,2,3,4,5]\n", 265 | "newlist = l + t # concatenate\n", 266 | "print(newlist)" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 20, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "name": "stdout", 283 | "output_type": "stream", 284 | "text": [ 285 | "(1, 5.0, True, 'str')\n" 286 | ] 287 | } 288 | ], 289 | "source": [ 290 | "# tuples\n", 291 | "# immmutable\n", 292 | "tup = (1, 5.0, True, \"str\")\n", 293 | "print(tup)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 21, 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "name": "stdout", 310 | "output_type": "stream", 311 | "text": [ 312 | "{1: 'one', 'ten': 10}\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "# dictionary - hashmap\n", 318 | "# key: value\n", 319 | "\n", 320 | "d = {1:\"one\", \"ten\":10}\n", 321 | "print(d)" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 22, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "one\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "print(d[1]) # print value for 1" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 23, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "10\n" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "print(d['ten'])" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 24, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "dict_keys([1, 'ten'])\n" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "print(d.keys()) # print key values" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 25, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "dict_values(['one', 10])\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "print(d.values()) # print the values" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": null, 395 | "metadata": {}, 396 | "outputs": [], 397 | "source": [] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 27, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "# data type conversion\n", 414 | "a = 2.0\n", 415 | "print(type(a))" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 28, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "name": "stdout", 425 | "output_type": "stream", 426 | "text": [ 427 | "\n" 428 | ] 429 | } 430 | ], 431 | "source": [ 432 | "a = int(a) # float to int\n", 433 | "print(type(a))" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 30, 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "name": "stdout", 443 | "output_type": "stream", 444 | "text": [ 445 | "\n", 446 | "2\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "a = str(a) # int to string\n", 452 | "print(type(a))\n", 453 | "print(a)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 31, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "name": "stdout", 470 | "output_type": "stream", 471 | "text": [ 472 | "{1, 2, 3}\n" 473 | ] 474 | } 475 | ], 476 | "source": [ 477 | "# set - unique values\n", 478 | "s = {1,1,2,2,3}\n", 479 | "print(s)" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": null, 485 | "metadata": {}, 486 | "outputs": [], 487 | "source": [] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 34, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "97\n" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "# ascii char\n", 504 | "print(ord(\"a\")) # char to int" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 35, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "C\n" 517 | ] 518 | } 519 | ], 520 | "source": [ 521 | "print(chr(67)) # int to char" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "name": "stdout", 531 | "output_type": "stream", 532 | "text": [ 533 | "\n", 534 | "Welcome to Python 3.6's help utility!\n", 535 | "\n", 536 | "If this is your first time using Python, you should definitely check out\n", 537 | "the tutorial on the Internet at https://docs.python.org/3.6/tutorial/.\n", 538 | "\n", 539 | "Enter the name of any module, keyword, or topic to get help on writing\n", 540 | "Python programs and using Python modules. To quit this help utility and\n", 541 | "return to the interpreter, just type \"quit\".\n", 542 | "\n", 543 | "To get a list of available modules, keywords, symbols, or topics, type\n", 544 | "\"modules\", \"keywords\", \"symbols\", or \"topics\". Each module also comes\n", 545 | "with a one-line summary of what it does; to list the modules whose name\n", 546 | "or summary contain a given string such as \"spam\", type \"modules spam\".\n", 547 | "\n", 548 | "help> list\n", 549 | "Help on class list in module builtins:\n", 550 | "\n", 551 | "class list(object)\n", 552 | " | list() -> new empty list\n", 553 | " | list(iterable) -> new list initialized from iterable's items\n", 554 | " | \n", 555 | " | Methods defined here:\n", 556 | " | \n", 557 | " | __add__(self, value, /)\n", 558 | " | Return self+value.\n", 559 | " | \n", 560 | " | __contains__(self, key, /)\n", 561 | " | Return key in self.\n", 562 | " | \n", 563 | " | __delitem__(self, key, /)\n", 564 | " | Delete self[key].\n", 565 | " | \n", 566 | " | __eq__(self, value, /)\n", 567 | " | Return self==value.\n", 568 | " | \n", 569 | " | __ge__(self, value, /)\n", 570 | " | Return self>=value.\n", 571 | " | \n", 572 | " | __getattribute__(self, name, /)\n", 573 | " | Return getattr(self, name).\n", 574 | " | \n", 575 | " | __getitem__(...)\n", 576 | " | x.__getitem__(y) <==> x[y]\n", 577 | " | \n", 578 | " | __gt__(self, value, /)\n", 579 | " | Return self>value.\n", 580 | " | \n", 581 | " | __iadd__(self, value, /)\n", 582 | " | Implement self+=value.\n", 583 | " | \n", 584 | " | __imul__(self, value, /)\n", 585 | " | Implement self*=value.\n", 586 | " | \n", 587 | " | __init__(self, /, *args, **kwargs)\n", 588 | " | Initialize self. See help(type(self)) for accurate signature.\n", 589 | " | \n", 590 | " | __iter__(self, /)\n", 591 | " | Implement iter(self).\n", 592 | " | \n", 593 | " | __le__(self, value, /)\n", 594 | " | Return self<=value.\n", 595 | " | \n", 596 | " | __len__(self, /)\n", 597 | " | Return len(self).\n", 598 | " | \n", 599 | " | __lt__(self, value, /)\n", 600 | " | Return self None -- append object to end\n", 628 | " | \n", 629 | " | clear(...)\n", 630 | " | L.clear() -> None -- remove all items from L\n", 631 | " | \n", 632 | " | copy(...)\n", 633 | " | L.copy() -> list -- a shallow copy of L\n", 634 | " | \n", 635 | " | count(...)\n", 636 | " | L.count(value) -> integer -- return number of occurrences of value\n", 637 | " | \n", 638 | " | extend(...)\n", 639 | " | L.extend(iterable) -> None -- extend list by appending elements from the iterable\n", 640 | " | \n", 641 | " | index(...)\n", 642 | " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n", 643 | " | Raises ValueError if the value is not present.\n", 644 | " | \n", 645 | " | insert(...)\n", 646 | " | L.insert(index, object) -- insert object before index\n", 647 | " | \n", 648 | " | pop(...)\n", 649 | " | L.pop([index]) -> item -- remove and return item at index (default last).\n", 650 | " | Raises IndexError if list is empty or index is out of range.\n", 651 | " | \n", 652 | " | remove(...)\n", 653 | " | L.remove(value) -> None -- remove first occurrence of value.\n", 654 | " | Raises ValueError if the value is not present.\n", 655 | " | \n", 656 | " | reverse(...)\n", 657 | " | L.reverse() -- reverse *IN PLACE*\n", 658 | " | \n", 659 | " | sort(...)\n", 660 | " | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*\n", 661 | " | \n", 662 | " | ----------------------------------------------------------------------\n", 663 | " | Data and other attributes defined here:\n", 664 | " | \n", 665 | " | __hash__ = None\n", 666 | "\n" 667 | ] 668 | } 669 | ], 670 | "source": [ 671 | "help()" 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": null, 677 | "metadata": {}, 678 | "outputs": [], 679 | "source": [] 680 | } 681 | ], 682 | "metadata": { 683 | "kernelspec": { 684 | "display_name": "Python 3", 685 | "language": "python", 686 | "name": "python3" 687 | }, 688 | "language_info": { 689 | "codemirror_mode": { 690 | "name": "ipython", 691 | "version": 3 692 | }, 693 | "file_extension": ".py", 694 | "mimetype": "text/x-python", 695 | "name": "python", 696 | "nbconvert_exporter": "python", 697 | "pygments_lexer": "ipython3", 698 | "version": "3.6.5" 699 | } 700 | }, 701 | "nbformat": 4, 702 | "nbformat_minor": 2 703 | } 704 | -------------------------------------------------------------------------------- /06 - Basic Operators - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# arithmetic operators" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "5" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "2 + 3 # addition" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "5" 41 | ] 42 | }, 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "10 - 5 # subtraction" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "6" 61 | ] 62 | }, 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "2 * 3 # multiplication" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 5, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "1.6666666666666667" 81 | ] 82 | }, 83 | "execution_count": 5, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "5 / 3 # division" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "1" 101 | ] 102 | }, 103 | "execution_count": 6, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "5 // 3 # floor division" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 7, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "2" 121 | ] 122 | }, 123 | "execution_count": 7, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "6 % 4 # modulus" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 8, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "16" 141 | ] 142 | }, 143 | "execution_count": 8, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "2 ** 4 # exponentiation" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# comparison operators\n", 166 | "# return boolean values" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 10, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "True" 178 | ] 179 | }, 180 | "execution_count": 10, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "2 == 2 # equals" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 13, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "data": { 196 | "text/plain": [ 197 | "False" 198 | ] 199 | }, 200 | "execution_count": 13, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "2 != 2 # not equals" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 15, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "data": { 216 | "text/plain": [ 217 | "True" 218 | ] 219 | }, 220 | "execution_count": 15, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "5 < 10 # less than" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 16, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "False" 238 | ] 239 | }, 240 | "execution_count": 16, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "5 > 10 # greater than" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 17, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "True" 258 | ] 259 | }, 260 | "execution_count": 17, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "5 <= 5 # less than or equal to" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 18, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/plain": [ 277 | "True" 278 | ] 279 | }, 280 | "execution_count": 18, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [ 286 | "10 >= 9 # greater than or equal to" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 19, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [ 302 | "# assignment operators" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 20, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "1" 314 | ] 315 | }, 316 | "execution_count": 20, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "a = 1\n", 323 | "a" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 21, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "data": { 333 | "text/plain": [ 334 | "3" 335 | ] 336 | }, 337 | "execution_count": 21, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "a += 2 # a = a + 2\n", 344 | "a" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 22, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "2" 356 | ] 357 | }, 358 | "execution_count": 22, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "a -= 1\n", 365 | "a" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 25, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "data": { 375 | "text/plain": [ 376 | "6" 377 | ] 378 | }, 379 | "execution_count": 25, 380 | "metadata": {}, 381 | "output_type": "execute_result" 382 | } 383 | ], 384 | "source": [ 385 | "a *= 3\n", 386 | "a" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 26, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "3.0" 398 | ] 399 | }, 400 | "execution_count": 26, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "a /= 2\n", 407 | "a" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 29, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/plain": [ 418 | "1.0" 419 | ] 420 | }, 421 | "execution_count": 29, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "a //= 2\n", 428 | "a" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 30, 434 | "metadata": {}, 435 | "outputs": [ 436 | { 437 | "data": { 438 | "text/plain": [ 439 | "2" 440 | ] 441 | }, 442 | "execution_count": 30, 443 | "metadata": {}, 444 | "output_type": "execute_result" 445 | } 446 | ], 447 | "source": [ 448 | "a = 5\n", 449 | "a %= 3\n", 450 | "a" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 31, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "data": { 460 | "text/plain": [ 461 | "32" 462 | ] 463 | }, 464 | "execution_count": 31, 465 | "metadata": {}, 466 | "output_type": "execute_result" 467 | } 468 | ], 469 | "source": [ 470 | "a **= 5\n", 471 | "a" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": {}, 478 | "outputs": [], 479 | "source": [] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 32, 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [ 487 | "# bitwise operators" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 34, 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "5: 0b101\n", 500 | "10: 0b1010\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "a = 5\n", 506 | "b = 10\n", 507 | "print(\"5: \", bin(a))\n", 508 | "print(\"10: \", bin(b))" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 35, 514 | "metadata": {}, 515 | "outputs": [ 516 | { 517 | "name": "stdout", 518 | "output_type": "stream", 519 | "text": [ 520 | "0b0\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "c = a & b # bitwise AND\n", 526 | "print(bin(c))" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 36, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "0b1111\n" 539 | ] 540 | } 541 | ], 542 | "source": [ 543 | "c = a | b # bitwise OR\n", 544 | "print(bin(c))" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 37, 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "name": "stdout", 554 | "output_type": "stream", 555 | "text": [ 556 | "0b1111\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "c = a ^ b # bitwise EXOR\n", 562 | "print(bin(c))" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": 38, 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "name": "stdout", 572 | "output_type": "stream", 573 | "text": [ 574 | "-0b110\n" 575 | ] 576 | } 577 | ], 578 | "source": [ 579 | "c = ~a # bitwise complement\n", 580 | "print(bin(c))" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 40, 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "name": "stdout", 590 | "output_type": "stream", 591 | "text": [ 592 | "0b1010\n" 593 | ] 594 | } 595 | ], 596 | "source": [ 597 | "c = a << 1 # bitwise left shift\n", 598 | "print(bin(c))" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 42, 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "name": "stdout", 608 | "output_type": "stream", 609 | "text": [ 610 | "0b10\n" 611 | ] 612 | } 613 | ], 614 | "source": [ 615 | "c = b >> 2 # bitwise right shift\n", 616 | "print(bin(c))" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": null, 622 | "metadata": {}, 623 | "outputs": [], 624 | "source": [] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": 43, 629 | "metadata": {}, 630 | "outputs": [], 631 | "source": [ 632 | "# logical operators" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 44, 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "data": { 642 | "text/plain": [ 643 | "True" 644 | ] 645 | }, 646 | "execution_count": 44, 647 | "metadata": {}, 648 | "output_type": "execute_result" 649 | } 650 | ], 651 | "source": [ 652 | "True and True # logical AND" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 45, 658 | "metadata": {}, 659 | "outputs": [ 660 | { 661 | "data": { 662 | "text/plain": [ 663 | "False" 664 | ] 665 | }, 666 | "execution_count": 45, 667 | "metadata": {}, 668 | "output_type": "execute_result" 669 | } 670 | ], 671 | "source": [ 672 | "True and False " 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 46, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "data": { 682 | "text/plain": [ 683 | "True" 684 | ] 685 | }, 686 | "execution_count": 46, 687 | "metadata": {}, 688 | "output_type": "execute_result" 689 | } 690 | ], 691 | "source": [ 692 | "True or False # logical OR" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 47, 698 | "metadata": {}, 699 | "outputs": [ 700 | { 701 | "data": { 702 | "text/plain": [ 703 | "True" 704 | ] 705 | }, 706 | "execution_count": 47, 707 | "metadata": {}, 708 | "output_type": "execute_result" 709 | } 710 | ], 711 | "source": [ 712 | "not False # logical NOT" 713 | ] 714 | }, 715 | { 716 | "cell_type": "code", 717 | "execution_count": null, 718 | "metadata": {}, 719 | "outputs": [], 720 | "source": [] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "execution_count": 48, 725 | "metadata": {}, 726 | "outputs": [], 727 | "source": [ 728 | "# membership operators" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": 49, 734 | "metadata": {}, 735 | "outputs": [ 736 | { 737 | "data": { 738 | "text/plain": [ 739 | "True" 740 | ] 741 | }, 742 | "execution_count": 49, 743 | "metadata": {}, 744 | "output_type": "execute_result" 745 | } 746 | ], 747 | "source": [ 748 | "2 in [1,2,3] # presents in" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 50, 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "data": { 758 | "text/plain": [ 759 | "False" 760 | ] 761 | }, 762 | "execution_count": 50, 763 | "metadata": {}, 764 | "output_type": "execute_result" 765 | } 766 | ], 767 | "source": [ 768 | "5 in [1,2,3]" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 51, 774 | "metadata": {}, 775 | "outputs": [ 776 | { 777 | "data": { 778 | "text/plain": [ 779 | "True" 780 | ] 781 | }, 782 | "execution_count": 51, 783 | "metadata": {}, 784 | "output_type": "execute_result" 785 | } 786 | ], 787 | "source": [ 788 | "'a' in 'abc'" 789 | ] 790 | }, 791 | { 792 | "cell_type": "code", 793 | "execution_count": 52, 794 | "metadata": {}, 795 | "outputs": [ 796 | { 797 | "data": { 798 | "text/plain": [ 799 | "True" 800 | ] 801 | }, 802 | "execution_count": 52, 803 | "metadata": {}, 804 | "output_type": "execute_result" 805 | } 806 | ], 807 | "source": [ 808 | "5 not in [1,2,3] # not present in" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 53, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "data": { 818 | "text/plain": [ 819 | "False" 820 | ] 821 | }, 822 | "execution_count": 53, 823 | "metadata": {}, 824 | "output_type": "execute_result" 825 | } 826 | ], 827 | "source": [ 828 | "2 not in [1,2,3]" 829 | ] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": null, 834 | "metadata": {}, 835 | "outputs": [], 836 | "source": [] 837 | }, 838 | { 839 | "cell_type": "code", 840 | "execution_count": 54, 841 | "metadata": {}, 842 | "outputs": [], 843 | "source": [ 844 | "# identity operator" 845 | ] 846 | }, 847 | { 848 | "cell_type": "code", 849 | "execution_count": 55, 850 | "metadata": {}, 851 | "outputs": [ 852 | { 853 | "data": { 854 | "text/plain": [ 855 | "True" 856 | ] 857 | }, 858 | "execution_count": 55, 859 | "metadata": {}, 860 | "output_type": "execute_result" 861 | } 862 | ], 863 | "source": [ 864 | "'word' is 'word' # equality" 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": 56, 870 | "metadata": {}, 871 | "outputs": [ 872 | { 873 | "data": { 874 | "text/plain": [ 875 | "False" 876 | ] 877 | }, 878 | "execution_count": 56, 879 | "metadata": {}, 880 | "output_type": "execute_result" 881 | } 882 | ], 883 | "source": [ 884 | "'wor' is 'word'" 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": 57, 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "data": { 894 | "text/plain": [ 895 | "False" 896 | ] 897 | }, 898 | "execution_count": 57, 899 | "metadata": {}, 900 | "output_type": "execute_result" 901 | } 902 | ], 903 | "source": [ 904 | "'word' is not 'word' # not equality" 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "execution_count": 58, 910 | "metadata": {}, 911 | "outputs": [ 912 | { 913 | "data": { 914 | "text/plain": [ 915 | "True" 916 | ] 917 | }, 918 | "execution_count": 58, 919 | "metadata": {}, 920 | "output_type": "execute_result" 921 | } 922 | ], 923 | "source": [ 924 | "'word' is not 'wor'" 925 | ] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "execution_count": null, 930 | "metadata": {}, 931 | "outputs": [], 932 | "source": [] 933 | } 934 | ], 935 | "metadata": { 936 | "kernelspec": { 937 | "display_name": "Python 3", 938 | "language": "python", 939 | "name": "python3" 940 | }, 941 | "language_info": { 942 | "codemirror_mode": { 943 | "name": "ipython", 944 | "version": 3 945 | }, 946 | "file_extension": ".py", 947 | "mimetype": "text/x-python", 948 | "name": "python", 949 | "nbconvert_exporter": "python", 950 | "pygments_lexer": "ipython3", 951 | "version": "3.6.5" 952 | } 953 | }, 954 | "nbformat": 4, 955 | "nbformat_minor": 2 956 | } 957 | -------------------------------------------------------------------------------- /07 - Decision Making - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "It is positive\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "num = 5\n", 18 | "# check it is positive\n", 19 | "# if condition\n", 20 | "if num >= 0:\n", 21 | " print(\"It is positive\")" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "It is negative\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "num = -5\n", 39 | "# check it is positive or negative\n", 40 | "# if-else condition\n", 41 | "if num >= 0:\n", 42 | " print(\"It is positive\")\n", 43 | "else:\n", 44 | " print(\"It is negative\")" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "It is zero\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "num = 0\n", 62 | "# check it is positive or negative or zero\n", 63 | "# if-elif-else condition\n", 64 | "if num > 0:\n", 65 | " print(\"It is positive\")\n", 66 | "elif num < 0:\n", 67 | " print(\"It is negative\")\n", 68 | "else:\n", 69 | " print(\"It is zero\")" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 7, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "greater than 100\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "# nested if conditions\n", 87 | "num = 150\n", 88 | "if num > 0:\n", 89 | " if num > 100:\n", 90 | " print(\"greater than 100\")\n", 91 | " else:\n", 92 | " print(\"less than 100\")\n", 93 | "else:\n", 94 | " print(\"less than zero\")" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 9, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "'odd'" 106 | ] 107 | }, 108 | "execution_count": 9, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "# one line if condtion\n", 115 | "num = 5\n", 116 | "string = \"even\" if num%2==0 else \"odd\"\n", 117 | "string" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 10, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "even\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "num = 10\n", 135 | "print(\"even\" if num%2==0 else \"odd\")" 136 | ] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.6.10" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 4 160 | } 161 | -------------------------------------------------------------------------------- /08 - Loops - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1\n", 13 | "2\n", 14 | "3\n", 15 | "4\n", 16 | "5\n", 17 | "6\n", 18 | "7\n", 19 | "8\n", 20 | "9\n", 21 | "10\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "# while loop\n", 27 | "# print from 1 to 10\n", 28 | "num = 1\n", 29 | "while num <= 10:\n", 30 | " print(num)\n", 31 | " num += 1" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "0\n", 44 | "1\n", 45 | "2\n", 46 | "3\n", 47 | "4\n", 48 | "5\n", 49 | "6\n", 50 | "7\n", 51 | "8\n", 52 | "9\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "# for loop\n", 58 | "for num in range(10):\n", 59 | " print(num)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "1\n", 72 | "2\n", 73 | "3\n", 74 | "4\n", 75 | "5\n", 76 | "6\n", 77 | "7\n", 78 | "8\n", 79 | "9\n", 80 | "10\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "# for loop\n", 86 | "for num in range(1,11,1):\n", 87 | " print(num)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 4, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "1\n", 100 | "3\n", 101 | "5\n", 102 | "7\n", 103 | "9\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "# print odd numbers\n", 109 | "for num in range(1,11,2):\n", 110 | " print(num)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 5, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "1\n", 123 | "2\n", 124 | "3\n", 125 | "4\n", 126 | "5\n", 127 | "6\n", 128 | "7\n", 129 | "8\n", 130 | "9\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# nested loops\n", 136 | "mat = [[1,2,3],[4,5,6],[7,8,9]]\n", 137 | "for i in range(3):\n", 138 | " for j in range(3):\n", 139 | " print(mat[i][j])" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "1 2 3 \n", 152 | "4 5 6 \n", 153 | "7 8 9 \n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "for i in range(3):\n", 159 | " for j in range(3):\n", 160 | " print(mat[i][j], end = \" \")\n", 161 | " print()" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 7, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "0\n", 174 | "1\n", 175 | "2\n", 176 | "3\n", 177 | "4\n", 178 | "5\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "# break\n", 184 | "for i in range(10):\n", 185 | " print(i)\n", 186 | " if i == 5:\n", 187 | " break" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 8, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "0\n", 200 | "2\n", 201 | "4\n", 202 | "6\n", 203 | "8\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "# continue\n", 209 | "for i in range(10):\n", 210 | " if i%2==1:\n", 211 | " continue\n", 212 | " print(i)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "# pass\n", 222 | "for i in range(10):\n", 223 | " pass" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 12, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "def fun():\n", 233 | " pass" 234 | ] 235 | } 236 | ], 237 | "metadata": { 238 | "kernelspec": { 239 | "display_name": "Python 3", 240 | "language": "python", 241 | "name": "python3" 242 | }, 243 | "language_info": { 244 | "codemirror_mode": { 245 | "name": "ipython", 246 | "version": 3 247 | }, 248 | "file_extension": ".py", 249 | "mimetype": "text/x-python", 250 | "name": "python", 251 | "nbconvert_exporter": "python", 252 | "pygments_lexer": "ipython3", 253 | "version": "3.6.10" 254 | } 255 | }, 256 | "nbformat": 4, 257 | "nbformat_minor": 4 258 | } 259 | -------------------------------------------------------------------------------- /09 - Strings - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "string = 'hello'" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "'hello'" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "string" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'hello world'" 41 | ] 42 | }, 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "string = string + \" world\"\n", 50 | "string" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "ename": "TypeError", 60 | "evalue": "'str' object does not support item assignment", 61 | "output_type": "error", 62 | "traceback": [ 63 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 64 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 65 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstring\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"H\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 66 | "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "# immutable\n", 72 | "string[0] = \"H\"" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "'Hello world'" 84 | ] 85 | }, 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "string = \"H\" + string[1:]\n", 93 | "string" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# string manipulation" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "gamer is the best\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "var = \"gamer\"\n", 120 | "print(\"%s is the best\" % (var))" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 9, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "'Hello'" 132 | ] 133 | }, 134 | "execution_count": 9, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "string[:5]" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 10, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "'world'" 152 | ] 153 | }, 154 | "execution_count": 10, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "string[6:]" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 11, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/plain": [ 171 | "'lo '" 172 | ] 173 | }, 174 | "execution_count": 11, 175 | "metadata": {}, 176 | "output_type": "execute_result" 177 | } 178 | ], 179 | "source": [ 180 | "string[3:6]" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 15, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "'dlrow olleH'" 192 | ] 193 | }, 194 | "execution_count": 15, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "# reverse\n", 201 | "string[::-1]" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 16, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "# string functions" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 19, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "'Hello world'" 229 | ] 230 | }, 231 | "execution_count": 19, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "string" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 17, 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "11" 249 | ] 250 | }, 251 | "execution_count": 17, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "# length\n", 258 | "len(string)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 18, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "3" 270 | ] 271 | }, 272 | "execution_count": 18, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "# count\n", 279 | "string.count('l')" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 20, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "data": { 289 | "text/plain": [ 290 | "6" 291 | ] 292 | }, 293 | "execution_count": 20, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "# find\n", 300 | "string.find('world')" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 21, 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "4" 312 | ] 313 | }, 314 | "execution_count": 21, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "# index\n", 321 | "string.index('o')" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 22, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "7" 333 | ] 334 | }, 335 | "execution_count": 22, 336 | "metadata": {}, 337 | "output_type": "execute_result" 338 | } 339 | ], 340 | "source": [ 341 | "# use r to find from reverse\n", 342 | "string.rindex('o')" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 26, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "data": { 352 | "text/plain": [ 353 | "'hello world'" 354 | ] 355 | }, 356 | "execution_count": 26, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "# lowercase\n", 363 | "string.lower()" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 27, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "'HELLO WORLD'" 375 | ] 376 | }, 377 | "execution_count": 27, 378 | "metadata": {}, 379 | "output_type": "execute_result" 380 | } 381 | ], 382 | "source": [ 383 | "# uppercase\n", 384 | "string.upper()" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 29, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "data": { 394 | "text/plain": [ 395 | "'Hello users'" 396 | ] 397 | }, 398 | "execution_count": 29, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "# replace\n", 405 | "string.replace('world', 'users')" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 30, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "data": { 415 | "text/plain": [ 416 | "['Hello', 'world']" 417 | ] 418 | }, 419 | "execution_count": 30, 420 | "metadata": {}, 421 | "output_type": "execute_result" 422 | } 423 | ], 424 | "source": [ 425 | "# split\n", 426 | "string.split(\" \")" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 31, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "'hELLO WORLD'" 438 | ] 439 | }, 440 | "execution_count": 31, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "# swapcase\n", 447 | "string.swapcase()" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 32, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "# string formatting" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 33, 462 | "metadata": {}, 463 | "outputs": [ 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "' Hello world '" 468 | ] 469 | }, 470 | "execution_count": 33, 471 | "metadata": {}, 472 | "output_type": "execute_result" 473 | } 474 | ], 475 | "source": [ 476 | "string.center(30, \" \")" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 34, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "data": { 486 | "text/plain": [ 487 | "'Hello world '" 488 | ] 489 | }, 490 | "execution_count": 34, 491 | "metadata": {}, 492 | "output_type": "execute_result" 493 | } 494 | ], 495 | "source": [ 496 | "string.ljust(30, \" \")" 497 | ] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": 37, 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "data": { 506 | "text/plain": [ 507 | "' Hello world'" 508 | ] 509 | }, 510 | "execution_count": 37, 511 | "metadata": {}, 512 | "output_type": "execute_result" 513 | } 514 | ], 515 | "source": [ 516 | "string.rjust(30, \" \")" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 38, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "data": { 526 | "text/plain": [ 527 | "'%%%%%%%%%Hello world%%%%%%%%%%'" 528 | ] 529 | }, 530 | "execution_count": 38, 531 | "metadata": {}, 532 | "output_type": "execute_result" 533 | } 534 | ], 535 | "source": [ 536 | "string.center(30, \"%\")" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": null, 542 | "metadata": {}, 543 | "outputs": [], 544 | "source": [] 545 | } 546 | ], 547 | "metadata": { 548 | "kernelspec": { 549 | "display_name": "Python 3", 550 | "language": "python", 551 | "name": "python3" 552 | }, 553 | "language_info": { 554 | "codemirror_mode": { 555 | "name": "ipython", 556 | "version": 3 557 | }, 558 | "file_extension": ".py", 559 | "mimetype": "text/x-python", 560 | "name": "python", 561 | "nbconvert_exporter": "python", 562 | "pygments_lexer": "ipython3", 563 | "version": "3.6.10" 564 | } 565 | }, 566 | "nbformat": 4, 567 | "nbformat_minor": 4 568 | } 569 | -------------------------------------------------------------------------------- /10 - Lists - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "l = [1, 2.0, \"hacker\", True]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "[1, 2.0, 'hacker', True]" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "l" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# how the list grows dynamically\n", 39 | "2^n\n", 40 | "2,4,8,16,32,64,..." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "# access the list\n", 50 | "# index" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "1" 62 | ] 63 | }, 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "l[0]" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "'hacker'" 82 | ] 83 | }, 84 | "execution_count": 5, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "l[2]" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 6, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "True" 102 | ] 103 | }, 104 | "execution_count": 6, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# use negative index to access from reverse\n", 111 | "l[-1]" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "2.0" 123 | ] 124 | }, 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "l[-3]" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 8, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "[1, 2.0, 'hacker', True]" 143 | ] 144 | }, 145 | "execution_count": 8, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "l" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "[2.0, 'hacker', True]" 163 | ] 164 | }, 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "# slicing\n", 172 | "l[1:]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 10, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "[2.0, 'hacker']" 184 | ] 185 | }, 186 | "execution_count": 10, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "l[1:3]" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 13, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "[True, 'hacker', 2.0, 1]" 204 | ] 205 | }, 206 | "execution_count": 13, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "# reverse\n", 213 | "l[::-1]" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 14, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "[1, 2.0, 'hacker', 'aswin']" 225 | ] 226 | }, 227 | "execution_count": 14, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "# update\n", 234 | "l[3] = 'aswin'\n", 235 | "l" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 15, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "[1, 2.0, 'hacker']" 247 | ] 248 | }, 249 | "execution_count": 15, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "# delete\n", 256 | "del l[3]\n", 257 | "l" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 16, 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "# basic operations" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 17, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "data": { 283 | "text/plain": [ 284 | "3" 285 | ] 286 | }, 287 | "execution_count": 17, 288 | "metadata": {}, 289 | "output_type": "execute_result" 290 | } 291 | ], 292 | "source": [ 293 | "# length\n", 294 | "len(l) - O(1)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 18, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "data": { 304 | "text/plain": [ 305 | "[1, 2.0, 'hacker', 4, 5, True]" 306 | ] 307 | }, 308 | "execution_count": 18, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "# concatenate\n", 315 | "l2 = [4, 5, True]\n", 316 | "l = l + l2\n", 317 | "l" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 19, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" 329 | ] 330 | }, 331 | "execution_count": 19, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "# repeat certain values\n", 338 | "l2 = [0] * 10\n", 339 | "l2" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 20, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "1\n", 352 | "2.0\n", 353 | "hacker\n", 354 | "4\n", 355 | "5\n", 356 | "True\n" 357 | ] 358 | } 359 | ], 360 | "source": [ 361 | "# iterate values from list\n", 362 | "for i in l:\n", 363 | " print(i)" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 21, 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "name": "stdout", 373 | "output_type": "stream", 374 | "text": [ 375 | "1 0\n", 376 | "2.0 0\n", 377 | "hacker 0\n", 378 | "4 0\n", 379 | "5 0\n", 380 | "True 0\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "# iterate two or more list\n", 386 | "for i,j in zip(l,l2):\n", 387 | " print(i,j)" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 22, 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [ 396 | "# list functions" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 24, 402 | "metadata": {}, 403 | "outputs": [], 404 | "source": [ 405 | "l = [1,2,3,4,5,6]" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 25, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "data": { 415 | "text/plain": [ 416 | "1" 417 | ] 418 | }, 419 | "execution_count": 25, 420 | "metadata": {}, 421 | "output_type": "execute_result" 422 | } 423 | ], 424 | "source": [ 425 | "# minimum \n", 426 | "min(l)" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 26, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "6" 438 | ] 439 | }, 440 | "execution_count": 26, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "# maximum\n", 447 | "max(l)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 27, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "['h', 'a', 'c', 'k', 'e', 'r', 's', 'r', 'e', 'a', 'l', 'm']" 459 | ] 460 | }, 461 | "execution_count": 27, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "# list conversion\n", 468 | "temp = list(\"hackersrealm\")\n", 469 | "temp" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 28, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "data": { 479 | "text/plain": [ 480 | "[1, 2, 3, 4, 5, 6, 10]" 481 | ] 482 | }, 483 | "execution_count": 28, 484 | "metadata": {}, 485 | "output_type": "execute_result" 486 | } 487 | ], 488 | "source": [ 489 | "# add a new element at the end\n", 490 | "l.append(10)\n", 491 | "l" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 29, 497 | "metadata": {}, 498 | "outputs": [ 499 | { 500 | "name": "stdout", 501 | "output_type": "stream", 502 | "text": [ 503 | "10 [1, 2, 3, 4, 5, 6]\n" 504 | ] 505 | } 506 | ], 507 | "source": [ 508 | "# remove element from the end\n", 509 | "t = l.pop()\n", 510 | "print(t,l)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 30, 516 | "metadata": {}, 517 | "outputs": [ 518 | { 519 | "data": { 520 | "text/plain": [ 521 | "[1, 2, 3, 4, 5, 6, 'h', 'a', 'c', 'k', 'e', 'r', 's', 'r', 'e', 'a', 'l', 'm']" 522 | ] 523 | }, 524 | "execution_count": 30, 525 | "metadata": {}, 526 | "output_type": "execute_result" 527 | } 528 | ], 529 | "source": [ 530 | "# extend list\n", 531 | "l.extend(temp)\n", 532 | "l" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 31, 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "2" 544 | ] 545 | }, 546 | "execution_count": 31, 547 | "metadata": {}, 548 | "output_type": "execute_result" 549 | } 550 | ], 551 | "source": [ 552 | "# count\n", 553 | "l.count('a')" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 32, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "data": { 563 | "text/plain": [ 564 | "5" 565 | ] 566 | }, 567 | "execution_count": 32, 568 | "metadata": {}, 569 | "output_type": "execute_result" 570 | } 571 | ], 572 | "source": [ 573 | "# find index of element\n", 574 | "l.index(6)" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 33, 580 | "metadata": { 581 | "collapsed": true 582 | }, 583 | "outputs": [ 584 | { 585 | "data": { 586 | "text/plain": [ 587 | "[1,\n", 588 | " 2,\n", 589 | " 3,\n", 590 | " 4,\n", 591 | " 5,\n", 592 | " 7,\n", 593 | " 6,\n", 594 | " 'h',\n", 595 | " 'a',\n", 596 | " 'c',\n", 597 | " 'k',\n", 598 | " 'e',\n", 599 | " 'r',\n", 600 | " 's',\n", 601 | " 'r',\n", 602 | " 'e',\n", 603 | " 'a',\n", 604 | " 'l',\n", 605 | " 'm']" 606 | ] 607 | }, 608 | "execution_count": 33, 609 | "metadata": {}, 610 | "output_type": "execute_result" 611 | } 612 | ], 613 | "source": [ 614 | "# insert new element at certain index\n", 615 | "l.insert(5, 7)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 34, 621 | "metadata": {}, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "[1, 2, 3, 4, 5, 7, 6, 'h', 'a', 'c', 'k', 'e', 'r', 's', 'r', 'e', 'a', 'l', 'm']\n" 628 | ] 629 | } 630 | ], 631 | "source": [ 632 | "print(l)" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 35, 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "data": { 642 | "text/plain": [ 643 | "[1, 2, 3, 4, 5, 7, 6, 'h', 'a', 'c', 'k', 'e', 's', 'r', 'e', 'a', 'l', 'm']" 644 | ] 645 | }, 646 | "execution_count": 35, 647 | "metadata": {}, 648 | "output_type": "execute_result" 649 | } 650 | ], 651 | "source": [ 652 | "# remove element\n", 653 | "l.remove('r')\n", 654 | "l" 655 | ] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": 36, 660 | "metadata": {}, 661 | "outputs": [ 662 | { 663 | "data": { 664 | "text/plain": [ 665 | "['m', 'l', 'a', 'e', 'r', 's', 'e', 'k', 'c', 'a', 'h', 6, 7, 5, 4, 3, 2, 1]" 666 | ] 667 | }, 668 | "execution_count": 36, 669 | "metadata": {}, 670 | "output_type": "execute_result" 671 | } 672 | ], 673 | "source": [ 674 | "# reversing the list\n", 675 | "l.reverse()\n", 676 | "l" 677 | ] 678 | }, 679 | { 680 | "cell_type": "code", 681 | "execution_count": 37, 682 | "metadata": {}, 683 | "outputs": [ 684 | { 685 | "ename": "TypeError", 686 | "evalue": "'<' not supported between instances of 'int' and 'str'", 687 | "output_type": "error", 688 | "traceback": [ 689 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 690 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 691 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# sort\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[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 692 | "\u001b[1;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'" 693 | ] 694 | } 695 | ], 696 | "source": [ 697 | "# sort\n", 698 | "l.sort()\n", 699 | "l" 700 | ] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "execution_count": 40, 705 | "metadata": {}, 706 | "outputs": [], 707 | "source": [ 708 | "# sorting reverse\n", 709 | "temp.sort(reverse=True)" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 41, 715 | "metadata": {}, 716 | "outputs": [ 717 | { 718 | "data": { 719 | "text/plain": [ 720 | "['s', 'r', 'r', 'm', 'l', 'k', 'h', 'e', 'e', 'c', 'a', 'a']" 721 | ] 722 | }, 723 | "execution_count": 41, 724 | "metadata": {}, 725 | "output_type": "execute_result" 726 | } 727 | ], 728 | "source": [ 729 | "temp" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 42, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "name": "stdout", 739 | "output_type": "stream", 740 | "text": [ 741 | "['a', 'a', 'c', 'e', 'e', 'h', 'k', 'l', 'm', 'r', 'r', 's']\n", 742 | "['s', 'r', 'r', 'm', 'l', 'k', 'h', 'e', 'e', 'c', 'a', 'a']\n" 743 | ] 744 | } 745 | ], 746 | "source": [ 747 | "t = sorted(temp)\n", 748 | "print(t)\n", 749 | "print(temp)" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": null, 755 | "metadata": {}, 756 | "outputs": [], 757 | "source": [] 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": 43, 762 | "metadata": {}, 763 | "outputs": [], 764 | "source": [ 765 | "# list comprehension" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": 44, 771 | "metadata": {}, 772 | "outputs": [ 773 | { 774 | "data": { 775 | "text/plain": [ 776 | "[0, 0, 0, 0, 0]" 777 | ] 778 | }, 779 | "execution_count": 44, 780 | "metadata": {}, 781 | "output_type": "execute_result" 782 | } 783 | ], 784 | "source": [ 785 | "temp = [0 for i in range(5)]\n", 786 | "temp" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": 45, 792 | "metadata": {}, 793 | "outputs": [ 794 | { 795 | "data": { 796 | "text/plain": [ 797 | "[0, 1, 2, 3, 4]" 798 | ] 799 | }, 800 | "execution_count": 45, 801 | "metadata": {}, 802 | "output_type": "execute_result" 803 | } 804 | ], 805 | "source": [ 806 | "temp = [i for i in range(5)]\n", 807 | "temp" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": 46, 813 | "metadata": {}, 814 | "outputs": [ 815 | { 816 | "data": { 817 | "text/plain": [ 818 | "[[], [], []]" 819 | ] 820 | }, 821 | "execution_count": 46, 822 | "metadata": {}, 823 | "output_type": "execute_result" 824 | } 825 | ], 826 | "source": [ 827 | "# create a matrix\n", 828 | "mat = [[] for i in range(3)]\n", 829 | "mat" 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": 47, 835 | "metadata": {}, 836 | "outputs": [ 837 | { 838 | "data": { 839 | "text/plain": [ 840 | "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]" 841 | ] 842 | }, 843 | "execution_count": 47, 844 | "metadata": {}, 845 | "output_type": "execute_result" 846 | } 847 | ], 848 | "source": [ 849 | "mat = [[0 for j in range(3)] for i in range(3)]\n", 850 | "mat" 851 | ] 852 | }, 853 | { 854 | "cell_type": "code", 855 | "execution_count": 48, 856 | "metadata": {}, 857 | "outputs": [ 858 | { 859 | "name": "stdout", 860 | "output_type": "stream", 861 | "text": [ 862 | "0 0 0\n", 863 | "0 0 0\n", 864 | "0 0 0\n" 865 | ] 866 | } 867 | ], 868 | "source": [ 869 | "for row in mat:\n", 870 | " print(*row)" 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "execution_count": null, 876 | "metadata": {}, 877 | "outputs": [], 878 | "source": [] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": null, 883 | "metadata": {}, 884 | "outputs": [], 885 | "source": [] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": null, 890 | "metadata": {}, 891 | "outputs": [], 892 | "source": [] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": null, 897 | "metadata": {}, 898 | "outputs": [], 899 | "source": [] 900 | } 901 | ], 902 | "metadata": { 903 | "kernelspec": { 904 | "display_name": "Python 3", 905 | "language": "python", 906 | "name": "python3" 907 | }, 908 | "language_info": { 909 | "codemirror_mode": { 910 | "name": "ipython", 911 | "version": 3 912 | }, 913 | "file_extension": ".py", 914 | "mimetype": "text/x-python", 915 | "name": "python", 916 | "nbconvert_exporter": "python", 917 | "pygments_lexer": "ipython3", 918 | "version": "3.6.10" 919 | } 920 | }, 921 | "nbformat": 4, 922 | "nbformat_minor": 4 923 | } 924 | -------------------------------------------------------------------------------- /11 - Tuples - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "(1, 2.0, 'hacker', True)" 12 | ] 13 | }, 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "t = (1, 2.0, \"hacker\", True)\n", 21 | "t" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "(1, 2, 3)" 33 | ] 34 | }, 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "t = 1,2,3\n", 42 | "t" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "ename": "TypeError", 52 | "evalue": "'tuple' object does not support item assignment", 53 | "output_type": "error", 54 | "traceback": [ 55 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 56 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 57 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# immutable\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[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 58 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "# immutable\n", 64 | "t[0] = 4\n", 65 | "t" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 6, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# access the tuples\n", 75 | "# index" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 9, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "(1, 2, 3)" 87 | ] 88 | }, 89 | "execution_count": 9, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "t" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 7, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "3" 107 | ] 108 | }, 109 | "execution_count": 7, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "t[2]" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 8, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "3" 127 | ] 128 | }, 129 | "execution_count": 8, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "# access from reverse\n", 136 | "t[-1]" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 10, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "(2, 3)" 148 | ] 149 | }, 150 | "execution_count": 10, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "# slicing\n", 157 | "t[1:3]" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 11, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "(3, 2, 1)" 169 | ] 170 | }, 171 | "execution_count": 11, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "# reverse\n", 178 | "t[::-1]" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 12, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "ename": "TypeError", 188 | "evalue": "'tuple' object doesn't support item deletion", 189 | "output_type": "error", 190 | "traceback": [ 191 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 192 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 193 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# delete element\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[1;32mdel\u001b[0m \u001b[0mt\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\u001b[0;32m 3\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 194 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object doesn't support item deletion" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "# delete element\n", 200 | "del t[0]\n", 201 | "t" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 13, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "# delete entire tuple\n", 211 | "del t" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 14, 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "ename": "NameError", 221 | "evalue": "name 't' is not defined", 222 | "output_type": "error", 223 | "traceback": [ 224 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 225 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 226 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 227 | "\u001b[1;31mNameError\u001b[0m: name 't' is not defined" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "t" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 15, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "# basic operation in tuple" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 16, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "t = (1,2,3,4)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 17, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "4" 269 | ] 270 | }, 271 | "execution_count": 17, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "# length\n", 278 | "len(t)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 18, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "t2 = 5,6,7" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 19, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "(1, 2, 3, 4, 5, 6, 7)" 299 | ] 300 | }, 301 | "execution_count": 19, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "# concatenate\n", 308 | "t = t + t2\n", 309 | "t" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 21, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "(0, 0, 0, 0, 0)" 321 | ] 322 | }, 323 | "execution_count": 21, 324 | "metadata": {}, 325 | "output_type": "execute_result" 326 | } 327 | ], 328 | "source": [ 329 | "# repeated numbers\n", 330 | "temp = (0,) * 5\n", 331 | "temp" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 22, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [ 340 | "l = [1,2,3,4,5]" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 23, 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "text/plain": [ 351 | "(1, 2, 3, 4, 5)" 352 | ] 353 | }, 354 | "execution_count": 23, 355 | "metadata": {}, 356 | "output_type": "execute_result" 357 | } 358 | ], 359 | "source": [ 360 | "# convert list to tuple\n", 361 | "t = tuple(l)\n", 362 | "t" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 24, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "data": { 372 | "text/plain": [ 373 | "[1, 2, 3, 4, 5]" 374 | ] 375 | }, 376 | "execution_count": 24, 377 | "metadata": {}, 378 | "output_type": "execute_result" 379 | } 380 | ], 381 | "source": [ 382 | "l" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": null, 388 | "metadata": {}, 389 | "outputs": [], 390 | "source": [] 391 | } 392 | ], 393 | "metadata": { 394 | "kernelspec": { 395 | "display_name": "Python 3", 396 | "language": "python", 397 | "name": "python3" 398 | }, 399 | "language_info": { 400 | "codemirror_mode": { 401 | "name": "ipython", 402 | "version": 3 403 | }, 404 | "file_extension": ".py", 405 | "mimetype": "text/x-python", 406 | "name": "python", 407 | "nbconvert_exporter": "python", 408 | "pygments_lexer": "ipython3", 409 | "version": "3.6.10" 410 | } 411 | }, 412 | "nbformat": 4, 413 | "nbformat_minor": 4 414 | } 415 | -------------------------------------------------------------------------------- /12 - Dictionary - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# key:value" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "{'name': 'Ash', 'age': 22}" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "d = {\"name\":'Ash', \"age\":22}\n", 30 | "d" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# access the dictionary\n", 40 | "# key" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "'Ash'" 52 | ] 53 | }, 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "d['name']" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "22" 72 | ] 73 | }, 74 | "execution_count": 5, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "d['age']" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "ename": "KeyError", 90 | "evalue": "'gender'", 91 | "output_type": "error", 92 | "traceback": [ 93 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 94 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 95 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'gender'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 96 | "\u001b[1;31mKeyError\u001b[0m: 'gender'" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "d['gender']" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 8, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "{'name': 'Ash', 'age': 22}" 113 | ] 114 | }, 115 | "execution_count": 8, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "# change values\n", 122 | "d['name'] = 'Ash'\n", 123 | "d" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 9, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "{'name': 'Ash', 'age': 22, 'gender': 'male'}" 135 | ] 136 | }, 137 | "execution_count": 9, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "# add new key\n", 144 | "d['gender'] = 'male'\n", 145 | "d" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 10, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "{'name': 'Ash', 'age': 22}" 157 | ] 158 | }, 159 | "execution_count": 10, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "# delete key\n", 166 | "del d['gender']\n", 167 | "d" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 11, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "# key should be unique\n", 177 | "# values can be same" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 12, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "# functions" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 16, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "{'name': 'Ash', 'age': 22, 'gender': 'male'}" 205 | ] 206 | }, 207 | "execution_count": 16, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "d['gender'] = 'male'\n", 214 | "d" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 17, 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "data": { 224 | "text/plain": [ 225 | "'male'" 226 | ] 227 | }, 228 | "execution_count": 17, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "# get\n", 235 | "#d['gender']\n", 236 | "d.get('gender', 'female')" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 18, 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "'gaming'" 248 | ] 249 | }, 250 | "execution_count": 18, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "# setdefault\n", 257 | "d.setdefault('hobby', 'gaming')" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 19, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "{'name': 'Ash', 'age': 22, 'gender': 'male', 'hobby': 'gaming'}" 269 | ] 270 | }, 271 | "execution_count": 19, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "d" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 20, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "4" 289 | ] 290 | }, 291 | "execution_count": 20, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "# length\n", 298 | "len(d)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 21, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/plain": [ 309 | "dict_keys(['name', 'age', 'gender', 'hobby'])" 310 | ] 311 | }, 312 | "execution_count": 21, 313 | "metadata": {}, 314 | "output_type": "execute_result" 315 | } 316 | ], 317 | "source": [ 318 | "# get key values alone from dict\n", 319 | "d.keys()" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 22, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "data": { 329 | "text/plain": [ 330 | "dict_values(['Ash', 22, 'male', 'gaming'])" 331 | ] 332 | }, 333 | "execution_count": 22, 334 | "metadata": {}, 335 | "output_type": "execute_result" 336 | } 337 | ], 338 | "source": [ 339 | "# get values from dict\n", 340 | "d.values()" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 23, 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "name": "stdout", 350 | "output_type": "stream", 351 | "text": [ 352 | "name Ash\n", 353 | "age 22\n", 354 | "gender male\n", 355 | "hobby gaming\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "# iterate key,value pairs from dict\n", 361 | "for key,value in d.items():\n", 362 | " print(key, value)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 24, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "name Ash\n", 375 | "age 22\n", 376 | "gender male\n", 377 | "hobby gaming\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "for key in d:\n", 383 | " print(key, d[key])" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 25, 389 | "metadata": {}, 390 | "outputs": [], 391 | "source": [ 392 | "# update key values from another dict\n", 393 | "temp = {'origname':\"Aswin\", \"language\":'english'}" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 26, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "{'name': 'Ash',\n", 405 | " 'age': 22,\n", 406 | " 'gender': 'male',\n", 407 | " 'hobby': 'gaming',\n", 408 | " 'origname': 'Aswin',\n", 409 | " 'language': 'english'}" 410 | ] 411 | }, 412 | "execution_count": 26, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "d.update(temp)\n", 419 | "d" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 28, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "data": { 429 | "text/plain": [ 430 | "True" 431 | ] 432 | }, 433 | "execution_count": 28, 434 | "metadata": {}, 435 | "output_type": "execute_result" 436 | } 437 | ], 438 | "source": [ 439 | "# key is present or not\n", 440 | "'name' in d" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 29, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "data": { 450 | "text/plain": [ 451 | "False" 452 | ] 453 | }, 454 | "execution_count": 29, 455 | "metadata": {}, 456 | "output_type": "execute_result" 457 | } 458 | ], 459 | "source": [ 460 | "'country' in d" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [] 469 | } 470 | ], 471 | "metadata": { 472 | "kernelspec": { 473 | "display_name": "Python 3", 474 | "language": "python", 475 | "name": "python3" 476 | }, 477 | "language_info": { 478 | "codemirror_mode": { 479 | "name": "ipython", 480 | "version": 3 481 | }, 482 | "file_extension": ".py", 483 | "mimetype": "text/x-python", 484 | "name": "python", 485 | "nbconvert_exporter": "python", 486 | "pygments_lexer": "ipython3", 487 | "version": "3.6.10" 488 | } 489 | }, 490 | "nbformat": 4, 491 | "nbformat_minor": 4 492 | } 493 | -------------------------------------------------------------------------------- /13 - Functions - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# code reuse\n", 10 | "# to perform certain task" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "def welcome():\n", 20 | " print(\"Welcome to Hackers Realm!!!\")" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Welcome to Hackers Realm!!!\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "welcome()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 4, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# arguments\n", 47 | "def add(a, b):\n", 48 | " return a+b" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 6, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "8" 60 | ] 61 | }, 62 | "execution_count": 6, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "add(3, 5)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 7, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# default arguments\n", 78 | "def add(a, b=10):\n", 79 | " return a+b" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 10, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "15" 91 | ] 92 | }, 93 | "execution_count": 10, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "add(5)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 13, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "# variable length arguments\n", 109 | "def add(*num):\n", 110 | " s = 0\n", 111 | " print(num)\n", 112 | " for i in num:\n", 113 | " s+=i\n", 114 | " return s" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 14, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "(1, 2, 3, 4, 10, 15)\n" 127 | ] 128 | }, 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "35" 133 | ] 134 | }, 135 | "execution_count": 14, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "add(1,2,3,4,10,15)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 15, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# pass by value\n", 151 | "# won't affect original variable\n", 152 | "def temp(a):\n", 153 | " a=5\n", 154 | " print(a)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 16, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "5\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "a = 10\n", 172 | "temp(a)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 17, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "10\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "print(a)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 18, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "# pass by reference\n", 199 | "# it affects the original varible also\n", 200 | "def temp(l):\n", 201 | " l.append(5)\n", 202 | " print(l)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 19, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "[1, 2, 3, 5]\n" 215 | ] 216 | } 217 | ], 218 | "source": [ 219 | "l1 = [1,2,3]\n", 220 | "temp(l1)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 20, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "[1, 2, 3, 5]\n" 233 | ] 234 | } 235 | ], 236 | "source": [ 237 | "print(l1)" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 21, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [ 253 | "# anonymous functions\n", 254 | "# lambda\n", 255 | "add = lambda a,b: a+b" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 22, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "15" 267 | ] 268 | }, 269 | "execution_count": 22, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "add(5,10)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 23, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "[1, 4, 9, 25]\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "print(list(map(lambda x: x**2, l1)))" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "# scope of varibles" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 31, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [ 310 | "counter = 0\n", 311 | "def inc():\n", 312 | " temp = 0 # local\n", 313 | " global counter # global\n", 314 | " counter += 1\n", 315 | " temp += 1\n", 316 | " print(temp, counter)" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 33, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "name": "stdout", 326 | "output_type": "stream", 327 | "text": [ 328 | "1 2\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "inc()" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 1, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "# return \n", 343 | "def swap(a,b):\n", 344 | " a,b = b,a\n", 345 | " return a,b" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 2, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "10 5\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "a = 5\n", 363 | "b = 10\n", 364 | "a, b = swap(a,b)\n", 365 | "print(a,b)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [] 381 | } 382 | ], 383 | "metadata": { 384 | "kernelspec": { 385 | "display_name": "Python 3", 386 | "language": "python", 387 | "name": "python3" 388 | }, 389 | "language_info": { 390 | "codemirror_mode": { 391 | "name": "ipython", 392 | "version": 3 393 | }, 394 | "file_extension": ".py", 395 | "mimetype": "text/x-python", 396 | "name": "python", 397 | "nbconvert_exporter": "python", 398 | "pygments_lexer": "ipython3", 399 | "version": "3.6.10" 400 | } 401 | }, 402 | "nbformat": 4, 403 | "nbformat_minor": 4 404 | } 405 | -------------------------------------------------------------------------------- /14 - Modules - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import math" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "8.0" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "math.sqrt(64)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# to save memory\n", 39 | "from math import sqrt" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 4, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "2.0" 51 | ] 52 | }, 53 | "execution_count": 4, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "sqrt(4)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "from math import *" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 6, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "8.0" 80 | ] 81 | }, 82 | "execution_count": 6, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "pow(2,3)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "['__doc__',\n", 100 | " '__loader__',\n", 101 | " '__name__',\n", 102 | " '__package__',\n", 103 | " '__spec__',\n", 104 | " 'acos',\n", 105 | " 'acosh',\n", 106 | " 'asin',\n", 107 | " 'asinh',\n", 108 | " 'atan',\n", 109 | " 'atan2',\n", 110 | " 'atanh',\n", 111 | " 'ceil',\n", 112 | " 'copysign',\n", 113 | " 'cos',\n", 114 | " 'cosh',\n", 115 | " 'degrees',\n", 116 | " 'e',\n", 117 | " 'erf',\n", 118 | " 'erfc',\n", 119 | " 'exp',\n", 120 | " 'expm1',\n", 121 | " 'fabs',\n", 122 | " 'factorial',\n", 123 | " 'floor',\n", 124 | " 'fmod',\n", 125 | " 'frexp',\n", 126 | " 'fsum',\n", 127 | " 'gamma',\n", 128 | " 'gcd',\n", 129 | " 'hypot',\n", 130 | " 'inf',\n", 131 | " 'isclose',\n", 132 | " 'isfinite',\n", 133 | " 'isinf',\n", 134 | " 'isnan',\n", 135 | " 'ldexp',\n", 136 | " 'lgamma',\n", 137 | " 'log',\n", 138 | " 'log10',\n", 139 | " 'log1p',\n", 140 | " 'log2',\n", 141 | " 'modf',\n", 142 | " 'nan',\n", 143 | " 'pi',\n", 144 | " 'pow',\n", 145 | " 'radians',\n", 146 | " 'sin',\n", 147 | " 'sinh',\n", 148 | " 'sqrt',\n", 149 | " 'tan',\n", 150 | " 'tanh',\n", 151 | " 'tau',\n", 152 | " 'trunc']" 153 | ] 154 | }, 155 | "execution_count": 7, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "# to list all functions in module\n", 162 | "dir(math)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 8, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Help on built-in module math:\n", 175 | "\n", 176 | "NAME\n", 177 | " math\n", 178 | "\n", 179 | "DESCRIPTION\n", 180 | " This module is always available. It provides access to the\n", 181 | " mathematical functions defined by the C standard.\n", 182 | "\n", 183 | "FUNCTIONS\n", 184 | " acos(...)\n", 185 | " acos(x)\n", 186 | " \n", 187 | " Return the arc cosine (measured in radians) of x.\n", 188 | " \n", 189 | " acosh(...)\n", 190 | " acosh(x)\n", 191 | " \n", 192 | " Return the inverse hyperbolic cosine of x.\n", 193 | " \n", 194 | " asin(...)\n", 195 | " asin(x)\n", 196 | " \n", 197 | " Return the arc sine (measured in radians) of x.\n", 198 | " \n", 199 | " asinh(...)\n", 200 | " asinh(x)\n", 201 | " \n", 202 | " Return the inverse hyperbolic sine of x.\n", 203 | " \n", 204 | " atan(...)\n", 205 | " atan(x)\n", 206 | " \n", 207 | " Return the arc tangent (measured in radians) of x.\n", 208 | " \n", 209 | " atan2(...)\n", 210 | " atan2(y, x)\n", 211 | " \n", 212 | " Return the arc tangent (measured in radians) of y/x.\n", 213 | " Unlike atan(y/x), the signs of both x and y are considered.\n", 214 | " \n", 215 | " atanh(...)\n", 216 | " atanh(x)\n", 217 | " \n", 218 | " Return the inverse hyperbolic tangent of x.\n", 219 | " \n", 220 | " ceil(...)\n", 221 | " ceil(x)\n", 222 | " \n", 223 | " Return the ceiling of x as an Integral.\n", 224 | " This is the smallest integer >= x.\n", 225 | " \n", 226 | " copysign(...)\n", 227 | " copysign(x, y)\n", 228 | " \n", 229 | " Return a float with the magnitude (absolute value) of x but the sign \n", 230 | " of y. On platforms that support signed zeros, copysign(1.0, -0.0) \n", 231 | " returns -1.0.\n", 232 | " \n", 233 | " cos(...)\n", 234 | " cos(x)\n", 235 | " \n", 236 | " Return the cosine of x (measured in radians).\n", 237 | " \n", 238 | " cosh(...)\n", 239 | " cosh(x)\n", 240 | " \n", 241 | " Return the hyperbolic cosine of x.\n", 242 | " \n", 243 | " degrees(...)\n", 244 | " degrees(x)\n", 245 | " \n", 246 | " Convert angle x from radians to degrees.\n", 247 | " \n", 248 | " erf(...)\n", 249 | " erf(x)\n", 250 | " \n", 251 | " Error function at x.\n", 252 | " \n", 253 | " erfc(...)\n", 254 | " erfc(x)\n", 255 | " \n", 256 | " Complementary error function at x.\n", 257 | " \n", 258 | " exp(...)\n", 259 | " exp(x)\n", 260 | " \n", 261 | " Return e raised to the power of x.\n", 262 | " \n", 263 | " expm1(...)\n", 264 | " expm1(x)\n", 265 | " \n", 266 | " Return exp(x)-1.\n", 267 | " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", 268 | " \n", 269 | " fabs(...)\n", 270 | " fabs(x)\n", 271 | " \n", 272 | " Return the absolute value of the float x.\n", 273 | " \n", 274 | " factorial(...)\n", 275 | " factorial(x) -> Integral\n", 276 | " \n", 277 | " Find x!. Raise a ValueError if x is negative or non-integral.\n", 278 | " \n", 279 | " floor(...)\n", 280 | " floor(x)\n", 281 | " \n", 282 | " Return the floor of x as an Integral.\n", 283 | " This is the largest integer <= x.\n", 284 | " \n", 285 | " fmod(...)\n", 286 | " fmod(x, y)\n", 287 | " \n", 288 | " Return fmod(x, y), according to platform C. x % y may differ.\n", 289 | " \n", 290 | " frexp(...)\n", 291 | " frexp(x)\n", 292 | " \n", 293 | " Return the mantissa and exponent of x, as pair (m, e).\n", 294 | " m is a float and e is an int, such that x = m * 2.**e.\n", 295 | " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", 296 | " \n", 297 | " fsum(...)\n", 298 | " fsum(iterable)\n", 299 | " \n", 300 | " Return an accurate floating point sum of values in the iterable.\n", 301 | " Assumes IEEE-754 floating point arithmetic.\n", 302 | " \n", 303 | " gamma(...)\n", 304 | " gamma(x)\n", 305 | " \n", 306 | " Gamma function at x.\n", 307 | " \n", 308 | " gcd(...)\n", 309 | " gcd(x, y) -> int\n", 310 | " greatest common divisor of x and y\n", 311 | " \n", 312 | " hypot(...)\n", 313 | " hypot(x, y)\n", 314 | " \n", 315 | " Return the Euclidean distance, sqrt(x*x + y*y).\n", 316 | " \n", 317 | " isclose(...)\n", 318 | " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool\n", 319 | " \n", 320 | " Determine whether two floating point numbers are close in value.\n", 321 | " \n", 322 | " rel_tol\n", 323 | " maximum difference for being considered \"close\", relative to the\n", 324 | " magnitude of the input values\n", 325 | " abs_tol\n", 326 | " maximum difference for being considered \"close\", regardless of the\n", 327 | " magnitude of the input values\n", 328 | " \n", 329 | " Return True if a is close in value to b, and False otherwise.\n", 330 | " \n", 331 | " For the values to be considered close, the difference between them\n", 332 | " must be smaller than at least one of the tolerances.\n", 333 | " \n", 334 | " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", 335 | " is, NaN is not close to anything, even itself. inf and -inf are\n", 336 | " only close to themselves.\n", 337 | " \n", 338 | " isfinite(...)\n", 339 | " isfinite(x) -> bool\n", 340 | " \n", 341 | " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", 342 | " \n", 343 | " isinf(...)\n", 344 | " isinf(x) -> bool\n", 345 | " \n", 346 | " Return True if x is a positive or negative infinity, and False otherwise.\n", 347 | " \n", 348 | " isnan(...)\n", 349 | " isnan(x) -> bool\n", 350 | " \n", 351 | " Return True if x is a NaN (not a number), and False otherwise.\n", 352 | " \n", 353 | " ldexp(...)\n", 354 | " ldexp(x, i)\n", 355 | " \n", 356 | " Return x * (2**i).\n", 357 | " \n", 358 | " lgamma(...)\n", 359 | " lgamma(x)\n", 360 | " \n", 361 | " Natural logarithm of absolute value of Gamma function at x.\n", 362 | " \n", 363 | " log(...)\n", 364 | " log(x[, base])\n", 365 | " \n", 366 | " Return the logarithm of x to the given base.\n", 367 | " If the base not specified, returns the natural logarithm (base e) of x.\n", 368 | " \n", 369 | " log10(...)\n", 370 | " log10(x)\n", 371 | " \n", 372 | " Return the base 10 logarithm of x.\n", 373 | " \n", 374 | " log1p(...)\n", 375 | " log1p(x)\n", 376 | " \n", 377 | " Return the natural logarithm of 1+x (base e).\n", 378 | " The result is computed in a way which is accurate for x near zero.\n", 379 | " \n", 380 | " log2(...)\n", 381 | " log2(x)\n", 382 | " \n", 383 | " Return the base 2 logarithm of x.\n", 384 | " \n", 385 | " modf(...)\n", 386 | " modf(x)\n", 387 | " \n", 388 | " Return the fractional and integer parts of x. Both results carry the sign\n", 389 | " of x and are floats.\n", 390 | " \n", 391 | " pow(...)\n", 392 | " pow(x, y)\n", 393 | " \n", 394 | " Return x**y (x to the power of y).\n", 395 | " \n", 396 | " radians(...)\n", 397 | " radians(x)\n", 398 | " \n", 399 | " Convert angle x from degrees to radians.\n", 400 | " \n", 401 | " sin(...)\n", 402 | " sin(x)\n", 403 | " \n", 404 | " Return the sine of x (measured in radians).\n", 405 | " \n", 406 | " sinh(...)\n", 407 | " sinh(x)\n", 408 | " \n", 409 | " Return the hyperbolic sine of x.\n", 410 | " \n", 411 | " sqrt(...)\n", 412 | " sqrt(x)\n", 413 | " \n", 414 | " Return the square root of x.\n", 415 | " \n", 416 | " tan(...)\n", 417 | " tan(x)\n", 418 | " \n", 419 | " Return the tangent of x (measured in radians).\n", 420 | " \n", 421 | " tanh(...)\n", 422 | " tanh(x)\n", 423 | " \n", 424 | " Return the hyperbolic tangent of x.\n", 425 | " \n", 426 | " trunc(...)\n", 427 | " trunc(x:Real) -> Integral\n", 428 | " \n", 429 | " Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.\n", 430 | "\n", 431 | "DATA\n", 432 | " e = 2.718281828459045\n", 433 | " inf = inf\n", 434 | " nan = nan\n", 435 | " pi = 3.141592653589793\n", 436 | " tau = 6.283185307179586\n", 437 | "\n", 438 | "FILE\n", 439 | " (built-in)\n", 440 | "\n", 441 | "\n" 442 | ] 443 | } 444 | ], 445 | "source": [ 446 | "# need help on particular module\n", 447 | "help(math)" 448 | ] 449 | } 450 | ], 451 | "metadata": { 452 | "kernelspec": { 453 | "display_name": "Python 3", 454 | "language": "python", 455 | "name": "python3" 456 | }, 457 | "language_info": { 458 | "codemirror_mode": { 459 | "name": "ipython", 460 | "version": 3 461 | }, 462 | "file_extension": ".py", 463 | "mimetype": "text/x-python", 464 | "name": "python", 465 | "nbconvert_exporter": "python", 466 | "pygments_lexer": "ipython3", 467 | "version": "3.6.10" 468 | } 469 | }, 470 | "nbformat": 4, 471 | "nbformat_minor": 4 472 | } 473 | -------------------------------------------------------------------------------- /15 - Classes - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# class\n", 10 | "class Student:\n", 11 | " count = 1" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 5, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "s = Student()" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 6, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "1" 32 | ] 33 | }, 34 | "execution_count": 6, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "s.count" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 20, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "class Student:\n", 50 | " count = 0\n", 51 | " \n", 52 | " # constructor\n", 53 | " def __init__(self, name):\n", 54 | " self.name = name\n", 55 | " Student.count += 1\n", 56 | " \n", 57 | " def displayname(self):\n", 58 | " print(\"The name is\",self.name)\n", 59 | " \n", 60 | " # destructor\n", 61 | " def __del__(self):\n", 62 | " print(\"The object is destroyed\")" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 21, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "s1 = Student(\"Ash\")" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 12, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "The name is Ash\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "s1.displayname()" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 13, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "1" 100 | ] 101 | }, 102 | "execution_count": 13, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "s1.count" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 14, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "1" 120 | ] 121 | }, 122 | "execution_count": 14, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "Student.count" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 15, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "s2 = Student(\"Naruto\")" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 16, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "The name is Naruto\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "s2.displayname()" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 17, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "2" 166 | ] 167 | }, 168 | "execution_count": 17, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "s2.count" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 18, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "# delete the object\n", 184 | "del s2" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 23, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "# s2.displayname()" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 22, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "The object is destroyed\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "del s1" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 24, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "# inheritance" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 25, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "class A:\n", 236 | " tempdata = 10\n", 237 | " def fun(self):\n", 238 | " print(\"This is base class\")" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 26, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "class B(A):\n", 248 | " temp = 15" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 27, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "obj = B()" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 28, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "15" 269 | ] 270 | }, 271 | "execution_count": 28, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "obj.temp" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 29, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "10" 289 | ] 290 | }, 291 | "execution_count": 29, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "obj.tempdata" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 30, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "This is base class\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "obj.fun()" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [ 330 | "# overriding" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 31, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "class A:\n", 340 | " tempdata = 10\n", 341 | " def fun(self):\n", 342 | " print(\"This is base class\")\n", 343 | "\n", 344 | "class B(A):\n", 345 | " temp = 15\n", 346 | " def fun(self):\n", 347 | " print(\"This is derived class\")" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 32, 353 | "metadata": {}, 354 | "outputs": [], 355 | "source": [ 356 | "obj = B()" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 33, 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "name": "stdout", 366 | "output_type": "stream", 367 | "text": [ 368 | "This is derived class\n" 369 | ] 370 | } 371 | ], 372 | "source": [ 373 | "obj.fun()" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 34, 379 | "metadata": {}, 380 | "outputs": [], 381 | "source": [ 382 | "obj = A()" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 35, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "This is base class\n" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "obj.fun()" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 36, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "# data hiding\n", 416 | "# public by default" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 44, 422 | "metadata": {}, 423 | "outputs": [], 424 | "source": [ 425 | "class A:\n", 426 | " # private variable\n", 427 | " __data = 5\n", 428 | " temp = 10\n", 429 | " \n", 430 | " def fun(self):\n", 431 | " print(self.__data)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 45, 437 | "metadata": {}, 438 | "outputs": [], 439 | "source": [ 440 | "obj = A()" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 46, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "data": { 450 | "text/plain": [ 451 | "10" 452 | ] 453 | }, 454 | "execution_count": 46, 455 | "metadata": {}, 456 | "output_type": "execute_result" 457 | } 458 | ], 459 | "source": [ 460 | "obj.temp" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 43, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "ename": "AttributeError", 470 | "evalue": "'A' object has no attribute '__data'", 471 | "output_type": "error", 472 | "traceback": [ 473 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 474 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 475 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 476 | "\u001b[1;31mAttributeError\u001b[0m: 'A' object has no attribute '__data'" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "obj.__data" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 47, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "name": "stdout", 491 | "output_type": "stream", 492 | "text": [ 493 | "5\n" 494 | ] 495 | } 496 | ], 497 | "source": [ 498 | "obj.fun()" 499 | ] 500 | } 501 | ], 502 | "metadata": { 503 | "kernelspec": { 504 | "display_name": "Python 3", 505 | "language": "python", 506 | "name": "python3" 507 | }, 508 | "language_info": { 509 | "codemirror_mode": { 510 | "name": "ipython", 511 | "version": 3 512 | }, 513 | "file_extension": ".py", 514 | "mimetype": "text/x-python", 515 | "name": "python", 516 | "nbconvert_exporter": "python", 517 | "pygments_lexer": "ipython3", 518 | "version": "3.6.10" 519 | } 520 | }, 521 | "nbformat": 4, 522 | "nbformat_minor": 4 523 | } 524 | -------------------------------------------------------------------------------- /16 - File Handling - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# open the file\n", 10 | "file = open(\"input.txt\", 'r')" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "# read the file\n", 20 | "str = file.read()" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Welcome\n", 33 | "to\n", 34 | "python\n", 35 | "tutorial\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "print(str)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "# close the file\n", 50 | "file.close()" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "Welcome\n", 63 | "to\n", 64 | "python\n", 65 | "tutorial\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "# alternate operation - with keyword\n", 71 | "with open(\"input.txt\", 'r') as file:\n", 72 | " print(file.read())" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# access modes\n", 82 | "# r - read \n", 83 | "# w - write\n", 84 | "# a - append\n", 85 | "# r+,w+,a+ - both modes\n", 86 | "# rb,wb,ab - access in binary format" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "file = open(\"input.txt\", 'r')" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 7, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "'Welco'" 107 | ] 108 | }, 109 | "execution_count": 7, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "# read certain characters\n", 116 | "file.read(5)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 8, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "5" 128 | ] 129 | }, 130 | "execution_count": 8, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "# position of file pointer\n", 137 | "file.tell()" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 15, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "10" 149 | ] 150 | }, 151 | "execution_count": 15, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "# go to certain point of file\n", 158 | "file.seek(10, 0)\n", 159 | "file.tell()" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 16, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "file.close()" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 17, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "# write operation\n", 178 | "with open(\"input.txt\", 'w') as file:\n", 179 | " file.write(\"You are awesome\")" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 19, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "You are awesome\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "with open(\"input.txt\", 'r') as file:\n", 197 | " print(file.readline())" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 20, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "# os module\n", 214 | "import os" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 21, 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "data": { 224 | "text/plain": [ 225 | "['.ipynb_checkpoints',\n", 226 | " '16 - File Handling - Python Programming Tutorial.ipynb',\n", 227 | " 'input.txt']" 228 | ] 229 | }, 230 | "execution_count": 21, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [ 236 | "# list the files\n", 237 | "os.listdir()" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 22, 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "'C:\\\\Users\\\\Aswin\\\\notebooks\\\\python tutorial\\\\test'" 249 | ] 250 | }, 251 | "execution_count": 22, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "# to get path of current dir\n", 258 | "os.getcwd()" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 23, 264 | "metadata": {}, 265 | "outputs": [], 266 | "source": [ 267 | "# rename the file\n", 268 | "os.rename(\"input.txt\", \"test.txt\")" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 24, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "['.ipynb_checkpoints',\n", 280 | " '16 - File Handling - Python Programming Tutorial.ipynb',\n", 281 | " 'test.txt']" 282 | ] 283 | }, 284 | "execution_count": 24, 285 | "metadata": {}, 286 | "output_type": "execute_result" 287 | } 288 | ], 289 | "source": [ 290 | "os.listdir()" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 25, 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "# remove the file\n", 300 | "os.remove(\"test.txt\")" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 26, 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "['.ipynb_checkpoints',\n", 312 | " '16 - File Handling - Python Programming Tutorial.ipynb']" 313 | ] 314 | }, 315 | "execution_count": 26, 316 | "metadata": {}, 317 | "output_type": "execute_result" 318 | } 319 | ], 320 | "source": [ 321 | "os.listdir()" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 27, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "['.ipynb_checkpoints',\n", 333 | " '16 - File Handling - Python Programming Tutorial.ipynb',\n", 334 | " 'demo']" 335 | ] 336 | }, 337 | "execution_count": 27, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "# create a new directory\n", 344 | "os.mkdir(\"demo\")\n", 345 | "os.listdir()" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 28, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "data": { 355 | "text/plain": [ 356 | "['.ipynb_checkpoints',\n", 357 | " '16 - File Handling - Python Programming Tutorial.ipynb']" 358 | ] 359 | }, 360 | "execution_count": 28, 361 | "metadata": {}, 362 | "output_type": "execute_result" 363 | } 364 | ], 365 | "source": [ 366 | "# remove directory\n", 367 | "os.rmdir(\"demo\")\n", 368 | "os.listdir()" 369 | ] 370 | } 371 | ], 372 | "metadata": { 373 | "kernelspec": { 374 | "display_name": "Python 3", 375 | "language": "python", 376 | "name": "python3" 377 | }, 378 | "language_info": { 379 | "codemirror_mode": { 380 | "name": "ipython", 381 | "version": 3 382 | }, 383 | "file_extension": ".py", 384 | "mimetype": "text/x-python", 385 | "name": "python", 386 | "nbconvert_exporter": "python", 387 | "pygments_lexer": "ipython3", 388 | "version": "3.6.10" 389 | } 390 | }, 391 | "nbformat": 4, 392 | "nbformat_minor": 4 393 | } 394 | -------------------------------------------------------------------------------- /17 - Exceptions - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# assert\n", 10 | "output = 10\n", 11 | "assert output < 15, \"The condition fails\"" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "ename": "AssertionError", 21 | "evalue": "The condition fails", 22 | "output_type": "error", 23 | "traceback": [ 24 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 25 | "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", 26 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0moutput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m20\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0moutput\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"The condition fails\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 27 | "\u001b[1;31mAssertionError\u001b[0m: The condition fails" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "output = 20\n", 33 | "assert output < 15, \"The condition fails\"" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# try - except" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "Your code has error\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "# error\n", 60 | "l = [1,2,3]\n", 61 | "try:\n", 62 | " l[5] = 10\n", 63 | "except:\n", 64 | " print(\"Your code has error\")" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# no error\n", 74 | "l = [1,2,3]\n", 75 | "try:\n", 76 | " l[0] = 10\n", 77 | "except:\n", 78 | " print(\"Your code has error\")" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 8, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Your code has error list assignment index out of range\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "# use appropriate error\n", 96 | "l = [1,2,3]\n", 97 | "try:\n", 98 | " l[5] = 10\n", 99 | "except IndexError as e:\n", 100 | " print(\"Your code has error\", e)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 9, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "ename": "IndexError", 110 | "evalue": "list index out of range", 111 | "output_type": "error", 112 | "traceback": [ 113 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 114 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 115 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml\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[0m", 116 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "l[5]" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 11, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "This is final code\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "# finally\n", 139 | "l = [1,2,3]\n", 140 | "try:\n", 141 | " l[2] = 10\n", 142 | "except IndexError as e:\n", 143 | " print(\"Your code has error\", e)\n", 144 | "finally:\n", 145 | " print(\"This is final code\")" 146 | ] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 3 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython3", 165 | "version": "3.6.10" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 4 170 | } 171 | -------------------------------------------------------------------------------- /18 - Multithreading - Python Programming Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import threading\n", 10 | "import time" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 8, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "def counter(thread_name, n, delay):\n", 20 | " i = 1\n", 21 | " while i <= n:\n", 22 | " time.sleep(delay)\n", 23 | " print(thread_name, i)\n", 24 | " i += 1\n", 25 | " print(thread_name, \"Completed\")" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 9, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "thread1 = threading.Thread(target=counter, args=('Thread 1', 10, 2))\n", 35 | "thread2 = threading.Thread(target=counter, args=('Thread 2', 5, 1))" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 10, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Main Program Completed\n", 48 | "Thread 2 1\n", 49 | "Thread 1 1\n", 50 | "Thread 2 2\n", 51 | "Thread 2 3\n", 52 | "Thread 1 2\n", 53 | "Thread 2 4\n", 54 | "Thread 2 5\n", 55 | "Thread 2 Completed\n", 56 | "Thread 1 3\n", 57 | "Thread 1 4\n", 58 | "Thread 1 5\n", 59 | "Thread 1 6\n", 60 | "Thread 1 7\n", 61 | "Thread 1 8\n", 62 | "Thread 1 9\n", 63 | "Thread 1 10\n", 64 | "Thread 1 Completed\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "thread1.start()\n", 70 | "thread2.start()\n", 71 | "print(\"Main Program Completed\")" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "## Execute Threads in Order" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 14, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "def counter(thread_name, n, delay):\n", 88 | " i = 1\n", 89 | " thread_lock.acquire()\n", 90 | " print('Acquiring Lock')\n", 91 | " while i <= n:\n", 92 | " time.sleep(delay)\n", 93 | " print(thread_name, i)\n", 94 | " i += 1\n", 95 | " print(thread_name, \"Completed\")\n", 96 | " thread_lock.release()\n", 97 | " print('Releasing Lock')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 15, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "thread1 = threading.Thread(target=counter, args=('Thread 1', 10, 0.5))\n", 107 | "thread2 = threading.Thread(target=counter, args=('Thread 2', 5, 0.5))" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 16, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "# access thread locks\n", 117 | "thread_lock = threading.Lock()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 17, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "Acquiring Lock\n", 130 | "Thread 1 1\n", 131 | "Thread 1 2\n", 132 | "Thread 1 3\n", 133 | "Thread 1 4\n", 134 | "Thread 1 5\n", 135 | "Thread 1 6\n", 136 | "Thread 1 7\n", 137 | "Thread 1 8\n", 138 | "Thread 1 9\n", 139 | "Thread 1 10\n", 140 | "Thread 1 Completed\n", 141 | "Releasing LockAcquiring Lock\n", 142 | "\n", 143 | "Thread 2 1\n", 144 | "Thread 2 2\n", 145 | "Thread 2 3\n", 146 | "Thread 2 4\n", 147 | "Thread 2 5\n", 148 | "Thread 2 Completed\n", 149 | "Releasing Lock\n", 150 | "Main Program Completed\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "# start the threads\n", 156 | "thread1.start()\n", 157 | "thread2.start()\n", 158 | "\n", 159 | "# wait until all threads finish\n", 160 | "thread1.join()\n", 161 | "thread2.join()\n", 162 | "\n", 163 | "print(\"Main Program Completed\")" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.8.3" 191 | } 192 | }, 193 | "nbformat": 4, 194 | "nbformat_minor": 4 195 | } 196 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Tutorials 2 | This repository contains all the python tutorial code files with all the basic concepts explained with simple examples. 3 | 4 | # Python programming tutorial videos playlist:- 5 | http://bit.ly/python3playlist 6 | --------------------------------------------------------------------------------