├── AI-ML-DS.pdf ├── Github-Windows.pdf ├── Git-LinuxMac-Terminal.pdf ├── .ipynb_checkpoints ├── Untitled-checkpoint.ipynb ├── CourseContents-checkpoint └── 02-Python Crash Course Exercises-checkpoint.ipynb ├── Untitled.ipynb ├── CourseContents ├── 5 # Control Flow Statements.ipynb ├── 02-Python Crash Course Exercises.ipynb ├── 7 # Python Crash Course Exercises.ipynb ├── 2 # Print Statement, Precision and FieldWidth.ipynb ├── 1 # Variable, Operators and Built-in Functions.ipynb ├── 6 # Functions.ipynb ├── 4 # Strings and Dictionaries.ipynb └── 3 # Lists, Tuples and Sets.ipynb /AI-ML-DS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pksvv/25JulIntro/HEAD/AI-ML-DS.pdf -------------------------------------------------------------------------------- /Github-Windows.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pksvv/25JulIntro/HEAD/Github-Windows.pdf -------------------------------------------------------------------------------- /Git-LinuxMac-Terminal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pksvv/25JulIntro/HEAD/Git-LinuxMac-Terminal.pdf -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.6" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /CourseContents: -------------------------------------------------------------------------------- 1 | Checking github update 2 | 3 | 4 | Day 1 5 | 6 | Intro + Python Crash Course sharing + Career Discussion 7 | Google Drive + Github 8 | 9 | !! HANDS ON !! 10 | 11 | Day 2 12 | Data Wrangling - Numpy, Pandas, Matplotlib, Pandas Viz. 13 | Assigment 14 | During Week- You learn python and data wrangling 15 | 16 | Day 3 17 | complete left in Day2, Supervised Learning Linear Regression, Assigment 18 | 19 | Day 4 20 | Supervised Learning - Classification 21 | 22 | Day 5 23 | Ensemble Learning & Feature Engineering 24 | 25 | Day6 26 | Ensemble Learning & Feature Engineering 27 | 28 | Day 7 29 | Recommender Systems 30 | 31 | Day 8 32 | Natural language processing - Text Mining 33 | NLTK, spacy, advents in NLP using ML, DL, probabilistic methods 34 | 35 | Day 9 36 | Time Series Analysis 37 | 38 | Day 10 39 | Unsupervised Learning 40 | 41 | Day 11 42 | Project -------------------------------------------------------------------------------- /.ipynb_checkpoints/CourseContents-checkpoint: -------------------------------------------------------------------------------- 1 | Checking github update 2 | 3 | 4 | Day 1 5 | 6 | Intro + Python Crash Course sharing + Career Discussion 7 | Google Drive + Github 8 | 9 | !! HANDS ON !! 10 | 11 | Day 2 12 | Data Wrangling - Numpy, Pandas, Matplotlib, Pandas Viz. 13 | Assigment 14 | During Week- You learn python and data wrangling 15 | 16 | Day 3 17 | complete left in Day2, Supervised Learning Linear Regression, Assigment 18 | 19 | Day 4 20 | Supervised Learning - Classification 21 | 22 | Day 5 23 | Ensemble Learning & Feature Engineering 24 | 25 | Day6 26 | Ensemble Learning & Feature Engineering 27 | 28 | Day 7 29 | Recommender Systems 30 | 31 | Day 8 32 | Natural language processing - Text Mining 33 | NLTK, spacy, advents in NLP using ML, DL, probabilistic methods 34 | 35 | Day 9 36 | Time Series Analysis 37 | 38 | Day 10 39 | Unsupervised Learning 40 | 41 | Day 11 42 | Project -------------------------------------------------------------------------------- /5 # Control Flow Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Control Flow Statements" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## If" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "if some_condition:\n", 29 | " \n", 30 | " algorithm" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Hello\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "x = 12\n", 48 | "if x >10:\n", 49 | " print (\"Hello\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "## If-else" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "if some_condition:\n", 64 | " \n", 65 | " algorithm\n", 66 | " \n", 67 | "else:\n", 68 | " \n", 69 | " algorithm" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 3, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "hello\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "x = 12\n", 87 | "if x > 10:\n", 88 | " print (\"hello\")\n", 89 | "else:\n", 90 | " print (\"world\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "## if-elif" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "if some_condition:\n", 105 | " \n", 106 | " algorithm\n", 107 | "\n", 108 | "elif some_condition:\n", 109 | " \n", 110 | " algorithm\n", 111 | "\n", 112 | "else:\n", 113 | " \n", 114 | " algorithm" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 4, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "x y:\n", 134 | " print (\"x>y\")\n", 135 | "elif x < y:\n", 136 | " print (\"x y:\n", 166 | " print (\"x>y\")\n", 167 | "elif x < y:\n", 168 | " print (\"x=7:\n", 362 | " break" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "##Continue" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement. " 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 12, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "0\n", 389 | "1\n", 390 | "2\n", 391 | "3\n", 392 | "4\n", 393 | "The end.\n", 394 | "The end.\n", 395 | "The end.\n", 396 | "The end.\n", 397 | "The end.\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "for i in range(10):\n", 403 | " if i>4:\n", 404 | " print (\"The end.\")\n", 405 | " continue\n", 406 | " elif i<7:\n", 407 | " print (i)" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": {}, 413 | "source": [ 414 | "## List Comprehensions" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "metadata": {}, 420 | "source": [ 421 | "Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as," 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 14, 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "res = []\n", 439 | "for i in range(1,11):\n", 440 | " x = 27*i\n", 441 | " res.append(x)\n", 442 | "print (res)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem." 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 15, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 461 | ] 462 | }, 463 | "execution_count": 15, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "[27*x for x in range(1,11)]" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "That's it!. Only remember to enclose it in square brackets" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 16, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "data": { 493 | "text/plain": [ 494 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 495 | ] 496 | }, 497 | "execution_count": 16, 498 | "metadata": {}, 499 | "output_type": "execute_result" 500 | } 501 | ], 502 | "source": [ 503 | "[27*x for x in range(1,20) if x<=10]" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "Let me add one more loop to make you understand better, " 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 17, 516 | "metadata": {}, 517 | "outputs": [ 518 | { 519 | "data": { 520 | "text/plain": [ 521 | "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" 522 | ] 523 | }, 524 | "execution_count": 17, 525 | "metadata": {}, 526 | "output_type": "execute_result" 527 | } 528 | ], 529 | "source": [ 530 | "[27*z for i in range(50) if i==27 for z in range(1,11)]" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [] 539 | } 540 | ], 541 | "metadata": { 542 | "kernelspec": { 543 | "display_name": "Python 3", 544 | "language": "python", 545 | "name": "python3" 546 | }, 547 | "language_info": { 548 | "codemirror_mode": { 549 | "name": "ipython", 550 | "version": 3 551 | }, 552 | "file_extension": ".py", 553 | "mimetype": "text/x-python", 554 | "name": "python", 555 | "nbconvert_exporter": "python", 556 | "pygments_lexer": "ipython3", 557 | "version": "3.7.3" 558 | } 559 | }, 560 | "nbformat": 4, 561 | "nbformat_minor": 1 562 | } 563 | -------------------------------------------------------------------------------- /02-Python Crash Course Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "___\n", 9 | "# Python Crash Course Exercise" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Exercises\n", 17 | "\n", 18 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "** What is 7 to the power of 4?**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "2401" 37 | ] 38 | }, 39 | "execution_count": 1, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "7**4" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "** Split this string:**\n", 53 | "\n", 54 | " s = \"Hi there Sam!\"\n", 55 | " \n", 56 | "**into a list. **" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 2, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "['Hi', 'there', 'Sam!']" 68 | ] 69 | }, 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "s = \"Hi there Sam!\"\n", 77 | "s.split()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "['Hi', 'there', 'dad!']" 89 | ] 90 | }, 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "** Given the variables:**\n", 103 | "\n", 104 | " planet = \"Earth\"\n", 105 | " diameter = 12742\n", 106 | "\n", 107 | "** Use .format() to print the following string: **\n", 108 | "\n", 109 | " The diameter of Earth is 12742 kilometers." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 4, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "planet = \"Earth\"\n", 119 | "diameter = 12742" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 7, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "The diameter of Earth is 12742 kilometers.\n", 132 | "The diameter of Earth is 12742 kilometers.\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "print(f\"The diameter of {planet} is {diameter} kilometers.\")\n", 138 | "\n", 139 | "print(\"The diameter of \",planet,\" is \",diameter,\" kilometers.\")" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "** Given this nested list, use indexing to grab the word \"hello\" **" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 9, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 14, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "'hello'" 167 | ] 168 | }, 169 | "execution_count": 14, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "lst[3][1][2][0]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 15, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 23, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "'hello'" 203 | ] 204 | }, 205 | "execution_count": 23, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "d['k1'][3]['tricky'][3]['target'][3]" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "** What is the main difference between a tuple and a list? **" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 23, 224 | "metadata": { 225 | "collapsed": true, 226 | "jupyter": { 227 | "outputs_hidden": true 228 | } 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "# Tuple is immutable" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "** Create a function that grabs the email website domain from a string in the form: **\n", 240 | "\n", 241 | " user@domain.com\n", 242 | " \n", 243 | "**So for example, passing \"user@domain.com\" would return: domain.com**" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 29, 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "'domain.com'" 255 | ] 256 | }, 257 | "execution_count": 29, 258 | "metadata": {}, 259 | "output_type": "execute_result" 260 | } 261 | ], 262 | "source": [ 263 | "s = \"user@domain.com\"\n", 264 | "\n", 265 | "s.split('@')[1]" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 26, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "def domainGet(s):\n", 275 | " return s.split('@')[1]" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 27, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "'domain.com'" 287 | ] 288 | }, 289 | "execution_count": 27, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "domainGet('user@domain.com')" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 53, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "True" 314 | ] 315 | }, 316 | "execution_count": 53, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "s = 'Is there a Dog1 here?'\n", 323 | "\"dog1\" in s.lower().split()" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 41, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "def findDog(s):\n", 333 | " return 'dog' in s.lower().split()" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 42, 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "data": { 343 | "text/plain": [ 344 | "True" 345 | ] 346 | }, 347 | "execution_count": 42, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "findDog('Is there a dog here?')" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "** Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases. **" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 16, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "def countDog(s):\n", 370 | " count = 0\n", 371 | " for word in s.lower().split():\n", 372 | " if word==\"dog\":\n", 373 | " count = count+1\n", 374 | " return count\n", 375 | "\n", 376 | "countDog('This dog runs faster than the other dog dude dog!')" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 13, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "data": { 386 | "text/plain": [ 387 | "3" 388 | ] 389 | }, 390 | "execution_count": 13, 391 | "metadata": {}, 392 | "output_type": "execute_result" 393 | } 394 | ], 395 | "source": [ 396 | "countDog('This dog runs faster than the other dog dude dog!')" 397 | ] 398 | }, 399 | { 400 | "cell_type": "markdown", 401 | "metadata": {}, 402 | "source": [ 403 | "** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", 404 | "\n", 405 | " seq = ['soup','dog','salad','cat','great']\n", 406 | "\n", 407 | "**should be filtered down to:**\n", 408 | "\n", 409 | " ['soup','salad']" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 4, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [ 418 | "seq = ['soup','dog','salad','cat','great']" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 15, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "data": { 428 | "text/plain": [ 429 | "['soup', 'salad']" 430 | ] 431 | }, 432 | "execution_count": 15, 433 | "metadata": {}, 434 | "output_type": "execute_result" 435 | } 436 | ], 437 | "source": [ 438 | "s = \"soup\"\n", 439 | "\n", 440 | "list(filter(lambda s: s[0]=='s',seq))" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "### Final Problem\n", 448 | "**You are driving a little too fast, and a police officer stops you. Write a function\n", 449 | " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", 450 | " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", 451 | " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", 452 | " cases. **" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 17, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "def caught_speeding(speed, is_birthday):\n", 462 | " if is_birthday:\n", 463 | " speed -= 5 # speed = speed - 5\n", 464 | " \n", 465 | " if speed > 80:\n", 466 | " return \"Big Ticket\"\n", 467 | " elif speed > 60:\n", 468 | " return \"Small Ticket\"\n", 469 | " else:\n", 470 | " return \"No Ticket\"" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 18, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "'Small Ticket'" 482 | ] 483 | }, 484 | "execution_count": 18, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "caught_speeding(81,True)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 19, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "'Big Ticket'" 502 | ] 503 | }, 504 | "execution_count": 19, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "caught_speeding(81,False)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "# Great job!" 518 | ] 519 | } 520 | ], 521 | "metadata": { 522 | "kernelspec": { 523 | "display_name": "Python 3", 524 | "language": "python", 525 | "name": "python3" 526 | }, 527 | "language_info": { 528 | "codemirror_mode": { 529 | "name": "ipython", 530 | "version": 3 531 | }, 532 | "file_extension": ".py", 533 | "mimetype": "text/x-python", 534 | "name": "python", 535 | "nbconvert_exporter": "python", 536 | "pygments_lexer": "ipython3", 537 | "version": "3.7.6" 538 | } 539 | }, 540 | "nbformat": 4, 541 | "nbformat_minor": 4 542 | } 543 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/02-Python Crash Course Exercises-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "___\n", 9 | "# Python Crash Course Exercise" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Exercises\n", 17 | "\n", 18 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "** What is 7 to the power of 4?**" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "2401" 37 | ] 38 | }, 39 | "execution_count": 1, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "7**4" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "** Split this string:**\n", 53 | "\n", 54 | " s = \"Hi there Sam!\"\n", 55 | " \n", 56 | "**into a list. **" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 2, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "['Hi', 'there', 'Sam!']" 68 | ] 69 | }, 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "s = \"Hi there Sam!\"\n", 77 | "s.split()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "['Hi', 'there', 'dad!']" 89 | ] 90 | }, 91 | "execution_count": 3, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "** Given the variables:**\n", 103 | "\n", 104 | " planet = \"Earth\"\n", 105 | " diameter = 12742\n", 106 | "\n", 107 | "** Use .format() to print the following string: **\n", 108 | "\n", 109 | " The diameter of Earth is 12742 kilometers." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 4, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "planet = \"Earth\"\n", 119 | "diameter = 12742" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 7, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "The diameter of Earth is 12742 kilometers.\n", 132 | "The diameter of Earth is 12742 kilometers.\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "print(f\"The diameter of {planet} is {diameter} kilometers.\")\n", 138 | "\n", 139 | "print(\"The diameter of \",planet,\" is \",diameter,\" kilometers.\")" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "** Given this nested list, use indexing to grab the word \"hello\" **" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 9, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 14, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "'hello'" 167 | ] 168 | }, 169 | "execution_count": 14, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "lst[3][1][2][0]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 15, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 23, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "'hello'" 203 | ] 204 | }, 205 | "execution_count": 23, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "d['k1'][3]['tricky'][3]['target'][3]" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "** What is the main difference between a tuple and a list? **" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 23, 224 | "metadata": { 225 | "collapsed": true, 226 | "jupyter": { 227 | "outputs_hidden": true 228 | } 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "# Tuple is immutable" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "** Create a function that grabs the email website domain from a string in the form: **\n", 240 | "\n", 241 | " user@domain.com\n", 242 | " \n", 243 | "**So for example, passing \"user@domain.com\" would return: domain.com**" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 29, 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "'domain.com'" 255 | ] 256 | }, 257 | "execution_count": 29, 258 | "metadata": {}, 259 | "output_type": "execute_result" 260 | } 261 | ], 262 | "source": [ 263 | "s = \"user@domain.com\"\n", 264 | "\n", 265 | "s.split('@')[1]" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 26, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "def domainGet(s):\n", 275 | " return s.split('@')[1]" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 27, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "'domain.com'" 287 | ] 288 | }, 289 | "execution_count": 27, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "domainGet('user@domain.com')" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 53, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "True" 314 | ] 315 | }, 316 | "execution_count": 53, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "s = 'Is there a Dog1 here?'\n", 323 | "\"dog1\" in s.lower().split()" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 41, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "def findDog(s):\n", 333 | " return 'dog' in s.lower().split()" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 42, 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "data": { 343 | "text/plain": [ 344 | "True" 345 | ] 346 | }, 347 | "execution_count": 42, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "findDog('Is there a dog here?')" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "** Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases. **" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 16, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "def countDog(s):\n", 370 | " count = 0\n", 371 | " for word in s.lower().split():\n", 372 | " if word==\"dog\":\n", 373 | " count = count+1\n", 374 | " return count\n", 375 | "\n", 376 | "countDog('This dog runs faster than the other dog dude dog!')" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 13, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "data": { 386 | "text/plain": [ 387 | "3" 388 | ] 389 | }, 390 | "execution_count": 13, 391 | "metadata": {}, 392 | "output_type": "execute_result" 393 | } 394 | ], 395 | "source": [ 396 | "countDog('This dog runs faster than the other dog dude dog!')" 397 | ] 398 | }, 399 | { 400 | "cell_type": "markdown", 401 | "metadata": {}, 402 | "source": [ 403 | "** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", 404 | "\n", 405 | " seq = ['soup','dog','salad','cat','great']\n", 406 | "\n", 407 | "**should be filtered down to:**\n", 408 | "\n", 409 | " ['soup','salad']" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 4, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [ 418 | "seq = ['soup','dog','salad','cat','great']" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 15, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "data": { 428 | "text/plain": [ 429 | "['soup', 'salad']" 430 | ] 431 | }, 432 | "execution_count": 15, 433 | "metadata": {}, 434 | "output_type": "execute_result" 435 | } 436 | ], 437 | "source": [ 438 | "s = \"soup\"\n", 439 | "\n", 440 | "list(filter(lambda s: s[0]=='s',seq))" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "### Final Problem\n", 448 | "**You are driving a little too fast, and a police officer stops you. Write a function\n", 449 | " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", 450 | " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", 451 | " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", 452 | " cases. **" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 17, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "def caught_speeding(speed, is_birthday):\n", 462 | " if is_birthday:\n", 463 | " speed -= 5 # speed = speed - 5\n", 464 | " \n", 465 | " if speed > 80:\n", 466 | " return \"Big Ticket\"\n", 467 | " elif speed > 60:\n", 468 | " return \"Small Ticket\"\n", 469 | " else:\n", 470 | " return \"No Ticket\"" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 18, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "'Small Ticket'" 482 | ] 483 | }, 484 | "execution_count": 18, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "caught_speeding(81,True)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 19, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "'Big Ticket'" 502 | ] 503 | }, 504 | "execution_count": 19, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "caught_speeding(81,False)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "# Great job!" 518 | ] 519 | } 520 | ], 521 | "metadata": { 522 | "kernelspec": { 523 | "display_name": "Python 3", 524 | "language": "python", 525 | "name": "python3" 526 | }, 527 | "language_info": { 528 | "codemirror_mode": { 529 | "name": "ipython", 530 | "version": 3 531 | }, 532 | "file_extension": ".py", 533 | "mimetype": "text/x-python", 534 | "name": "python", 535 | "nbconvert_exporter": "python", 536 | "pygments_lexer": "ipython3", 537 | "version": "3.7.6" 538 | } 539 | }, 540 | "nbformat": 4, 541 | "nbformat_minor": 4 542 | } 543 | -------------------------------------------------------------------------------- /7 # Python Crash Course Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Crash Course Exercise" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Exercises\n", 15 | "\n", 16 | "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "**What is 7 to the power of 4?**" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "2401" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "7**4" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "** Split this string:**\n", 51 | "\n", 52 | " s = \"Hi there Sam!\"\n", 53 | " \n", 54 | "**into a list. **" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 11, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "['Hi,', 'there', 'Sam!']" 66 | ] 67 | }, 68 | "execution_count": 11, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "s = 'Hi, there Sam!'\n", 75 | "s[0:10]\n", 76 | "list(s)\n", 77 | "\n", 78 | "s.split()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "**Given the variables:**\n", 86 | "\n", 87 | " planet = \"Earth\"\n", 88 | " diameter = 12742\n", 89 | "\n", 90 | "**Use .format() to print the following string: **\n", 91 | "\n", 92 | " The diameter of Earth is 12742 kilometers." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 12, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "planet = \"Earth\"\n", 102 | "diameter = 12742" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 18, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "# 1 --- The diameter of Earth is 12742 kilometers.\n", 115 | "# 2 --- The diameter of Earth is 12742 kilometers.\n", 116 | "# 3 --- The diameter of Earth is 12742 kilometers.\n", 117 | "\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "print('# 1 --- The diameter of Earth is 12742 kilometers.') # First way\n", 123 | "\n", 124 | "print('# 2 --- The diameter of {} is {} kilometers.'.format(planet,diameter)) # Second way\n", 125 | "\n", 126 | "print('# 3 --- The diameter of {one} is {two} kilometers.'.format(two=diameter,one=planet)) # Third way\n", 127 | "\n", 128 | "print(f'') # - Fourth way for you to find out python 3.6" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "***Given this nested list, use indexing to grab the word \"hello\"***" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 20, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 32, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "'hello'" 156 | ] 157 | }, 158 | "execution_count": 32, 159 | "metadata": {}, 160 | "output_type": "execute_result" 161 | } 162 | ], 163 | "source": [ 164 | "lst[3][1][2][0]" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 2, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 8, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "'hello'" 192 | ] 193 | }, 194 | "execution_count": 8, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "d['k1'][3]['tricky'][3]['target'][3]" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "**What is the main difference between a tuple and a list?**" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 23, 213 | "metadata": { 214 | "collapsed": true 215 | }, 216 | "outputs": [], 217 | "source": [ 218 | "# Tuple is immutable" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "**Create a function that grabs the email website domain from a string in the form:**\n", 226 | "\n", 227 | " user@domain.com\n", 228 | " \n", 229 | "**So for example, passing \"user@domain.com\" would return: domain.com**" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 14, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "def domainGet(s):\n", 239 | " return s.split('@')[1]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 15, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "'domain.com'" 251 | ] 252 | }, 253 | "execution_count": 15, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "s = 'user@domain.com'\n", 260 | "s.split('@')[1]" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 16, 266 | "metadata": {}, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "'domain.com'" 272 | ] 273 | }, 274 | "execution_count": 16, 275 | "metadata": {}, 276 | "output_type": "execute_result" 277 | } 278 | ], 279 | "source": [ 280 | "domainGet('user@domain.com')" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "**Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization.**" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 42, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "s = 'dog Is there a Dog here dog? dog dog dog'\n", 297 | "\n", 298 | "def findDog(s):\n", 299 | " return len(s.lower().split('dog'))-1\n", 300 | "findDog(s)\n", 301 | "def findDog(s):\n", 302 | " return 'dog' in s.lower().split()" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 40, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "def findDog(s):\n", 312 | " return 'dog' in s.lower().split()" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 41, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "True" 324 | ] 325 | }, 326 | "execution_count": 41, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "findDog('Is there a dog here?')" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "**Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases.**" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 43, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "def countDog(s):\n", 349 | " count=0\n", 350 | " for w in s.lower().split():\n", 351 | " if w == 'dog':\n", 352 | " count +=1\n", 353 | " return count" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 44, 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "data": { 363 | "text/plain": [ 364 | "2" 365 | ] 366 | }, 367 | "execution_count": 44, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "countDog('This dog runs faster than the other dog dude!')" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "**Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", 381 | "\n", 382 | " seq = ['soup','dog','salad','cat','great']\n", 383 | "\n", 384 | "**should be filtered down to:**\n", 385 | "\n", 386 | " ['soup','salad']" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 45, 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [ 395 | "seq = ['soup','dog','salad','cat','great']" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 46, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "soup\n", 408 | "salad\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "for i in seq:\n", 414 | " if i[0]=='s':\n", 415 | " print(i)" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 48, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "['soup', 'salad']" 427 | ] 428 | }, 429 | "execution_count": 48, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "list(filter(lambda i: i[0]=='s',seq))" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "### Final Problem\n", 443 | "**You are driving a little too fast, and a police officer stops you. Write a function\n", 444 | " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", 445 | " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", 446 | " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", 447 | " cases.**" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 56, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "def caught_speeding(speed, is_birthday):\n", 457 | " if is_birthday:\n", 458 | " speed -= 5 # speed = speed - 5 s++ s-- --s ++s\n", 459 | "\n", 460 | " if speed > 80:\n", 461 | " return 'Big Ticket'\n", 462 | " elif speed > 60:\n", 463 | " return 'Small Ticket'\n", 464 | " else:\n", 465 | " return 'No Ticket'" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 53, 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "data": { 475 | "text/plain": [ 476 | "'Small Ticket'" 477 | ] 478 | }, 479 | "execution_count": 53, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "caught_speeding(81,True)" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 54, 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "data": { 495 | "text/plain": [ 496 | "'Big Ticket'" 497 | ] 498 | }, 499 | "execution_count": 54, 500 | "metadata": {}, 501 | "output_type": "execute_result" 502 | } 503 | ], 504 | "source": [ 505 | "caught_speeding(81,False)" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": {}, 511 | "source": [ 512 | "# Great job!" 513 | ] 514 | } 515 | ], 516 | "metadata": { 517 | "kernelspec": { 518 | "display_name": "Python 3", 519 | "language": "python", 520 | "name": "python3" 521 | }, 522 | "language_info": { 523 | "codemirror_mode": { 524 | "name": "ipython", 525 | "version": 3 526 | }, 527 | "file_extension": ".py", 528 | "mimetype": "text/x-python", 529 | "name": "python", 530 | "nbconvert_exporter": "python", 531 | "pygments_lexer": "ipython3", 532 | "version": "3.7.4" 533 | } 534 | }, 535 | "nbformat": 4, 536 | "nbformat_minor": 1 537 | } 538 | -------------------------------------------------------------------------------- /2 # Print Statement, Precision and FieldWidth.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Print Statement" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "The **print** statement can be used in the following different ways :\n", 22 | "\n", 23 | " - print (\"Hello World\")\n", 24 | " - print (\"Hello\", )\n", 25 | " - print (\"Hello\" + )\n", 26 | " - print (\"Hello %s\" % )" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "ename": "SyntaxError", 36 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"Hello World\")? (, line 1)", 37 | "output_type": "error", 38 | "traceback": [ 39 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print \"Hello World\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"Hello World\")?\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "print (\"Hello World\")" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "In Python, single, double and triple quotes are used to denote a string.\n", 52 | "Most use single quotes when declaring a single character. \n", 53 | "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "Hey\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "print ('Hey')" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 1, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "My name is Vipul Gaur\n", 83 | "\n", 84 | "I love Python.\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "print (\"\"\"My name is Vipul Gaur\n", 90 | "\n", 91 | "I love Python.\"\"\")" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "metadata": { 105 | "scrolled": true 106 | }, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "Hello World\n", 113 | "Hello World !\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "string1 = 'World'\n", 119 | "print ('Hello', string1)\n", 120 | "\n", 121 | "string2 = '!'\n", 122 | "print ('Hello', string1, string2)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 3, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "HelloWorld!\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print ('Hello' + string1 + string2)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "**%s** is used to refer to a variable which contains a string." 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 4, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "Hello World\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "print (\"Hello %s\" % string1)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "Similarly, when using other data types\n", 178 | "\n", 179 | " - %s -> string\n", 180 | " - %d -> Integer\n", 181 | " - %f -> Float\n", 182 | " - %o -> Octal\n", 183 | " - %x -> Hexadecimal\n", 184 | " - %e -> exponential\n", 185 | " \n", 186 | "This can be used for conversions inside the print statement itself." 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 5, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "Actual Number = 18\n", 199 | "Float of the number = 18.000000\n", 200 | "Octal equivalent of the number = 22\n", 201 | "Hexadecimal equivalent of the number = 12\n", 202 | "Exponential equivalent of the number = 1.800000e+01\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print (\"Actual Number = %d\" %18)\n", 208 | "print (\"Float of the number = %f\" %18)\n", 209 | "print (\"Octal equivalent of the number = %o\" %18)\n", 210 | "print (\"Hexadecimal equivalent of the number = %x\" %18)\n", 211 | "print (\"Exponential equivalent of the number = %e\" %18)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "When referring to multiple variables parenthesis is used." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 6, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "Hello World !\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "print (\"Hello %s %s\" %(string1,string2))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "#### Other Examples" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "The following are other different ways the print statement can be put to use." 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 7, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "I want %d to be printed here\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "print (\"I want %%d to be printed %s\" %'here')" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 8, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "_A_A_A_A_A_A_A_A_A_A\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "print ('_A'*10)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 9, 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "Jan\n", 296 | "Feb\n", 297 | "Mar\n", 298 | "Apr\n", 299 | "May\n", 300 | "Jun\n", 301 | "Jul\n", 302 | "Aug\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "print (\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 10, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "I want \\n to be printed.\n" 320 | ] 321 | } 322 | ], 323 | "source": [ 324 | "print (\"I want \\\\n to be printed.\")" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 11, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "\n", 337 | "Routine:\n", 338 | "\t- Eat\n", 339 | "\t- Sleep\n", 340 | "\t- Repeat\n", 341 | "\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "print (\"\"\"\n", 347 | "Routine:\n", 348 | "\\t- Eat\n", 349 | "\\t- Sleep\\n\\t- Repeat\n", 350 | "\"\"\")" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": {}, 356 | "source": [ 357 | "#### PrecisionWidth and FieldWidth" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", 365 | "\n", 366 | "The default Precision Width is set to 6." 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 12, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "'3.121312'" 378 | ] 379 | }, 380 | "execution_count": 12, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "\"%f\" % 3.121312312312" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": {}, 392 | "source": [ 393 | "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 13, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "'3.12131'" 405 | ] 406 | }, 407 | "execution_count": 13, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "\"%.5f\" % 3.121312312312" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 14, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "' 3.12131'" 432 | ] 433 | }, 434 | "execution_count": 14, 435 | "metadata": {}, 436 | "output_type": "execute_result" 437 | } 438 | ], 439 | "source": [ 440 | "\"%9.5f\" % 3.121312312312" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "Zero padding is done by adding a 0 at the start of fieldwidth." 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 15, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "'00000000000003.12131'" 459 | ] 460 | }, 461 | "execution_count": 15, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "\"%020.5f\" % 3.121312312312" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": {}, 473 | "source": [ 474 | "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 16, 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "name": "stdout", 484 | "output_type": "stream", 485 | "text": [ 486 | " 3.121312\n", 487 | "-3.121312\n" 488 | ] 489 | } 490 | ], 491 | "source": [ 492 | "print (\"% 9f\" % 3.121312312312)\n", 493 | "print (\"% 9f\" % -3.121312312312)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 17, 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "+3.121312\n", 513 | "-3.121312\n" 514 | ] 515 | } 516 | ], 517 | "source": [ 518 | "print (\"%+9f\" % 3.121312312312)\n", 519 | "print (\"% 9f\" % -3.121312312312)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actually field width. But left alignment can be done by specifying a negative symbol in the field width." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 18, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "data": { 536 | "text/plain": [ 537 | "'3.121 '" 538 | ] 539 | }, 540 | "execution_count": 18, 541 | "metadata": {}, 542 | "output_type": "execute_result" 543 | } 544 | ], 545 | "source": [ 546 | "\"%-9.3f\" % 3.121312312312" 547 | ] 548 | } 549 | ], 550 | "metadata": { 551 | "kernelspec": { 552 | "display_name": "Python 3", 553 | "language": "python", 554 | "name": "python3" 555 | }, 556 | "language_info": { 557 | "codemirror_mode": { 558 | "name": "ipython", 559 | "version": 3 560 | }, 561 | "file_extension": ".py", 562 | "mimetype": "text/x-python", 563 | "name": "python", 564 | "nbconvert_exporter": "python", 565 | "pygments_lexer": "ipython3", 566 | "version": "3.7.4" 567 | } 568 | }, 569 | "nbformat": 4, 570 | "nbformat_minor": 1 571 | } 572 | -------------------------------------------------------------------------------- /1 # Variable, Operators and Built-in Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Easter egg\n", 15 | " - an unexpected or undocumented feature in a piece of computer software or on a DVD, included as a joke or a bonus.\n", 16 | "\n", 17 | "#### The Zen Of Python\n", 18 | "\n", 19 | "\n", 20 | "Let's start with some fun facts about python" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "import this\n", 30 | "\n", 31 | "# try import antigravity" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "# Variables" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows," 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "str" 57 | ] 58 | }, 59 | "execution_count": 5, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "x = 2\n", 66 | "y = 5\n", 67 | "xy = 'Hey'\n", 68 | "\n", 69 | "type(xy)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "7 Hey\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "print (x+y, xy)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Multiple variables can be assigned with the same value." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "x = y = 1" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 6, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "1 1\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "print (x,y)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "# Operators" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "##Arithmetic Operators" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "| Symbol | Task Performed |\n", 141 | "|----|---|\n", 142 | "| + | Addition |\n", 143 | "| - | Subtraction |\n", 144 | "| / | division |\n", 145 | "| % | mod |\n", 146 | "| * | multiplication |\n", 147 | "| // | floor division |\n", 148 | "| ** | to the power of |" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "3" 160 | ] 161 | }, 162 | "execution_count": 7, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "1+2" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 8, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "1" 180 | ] 181 | }, 182 | "execution_count": 8, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "2-1" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "2" 200 | ] 201 | }, 202 | "execution_count": 9, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "1*2" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 10, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "0.5" 220 | ] 221 | }, 222 | "execution_count": 10, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "1/2" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained." 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 11, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "0.5" 247 | ] 248 | }, 249 | "execution_count": 11, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "1/2.0" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 12, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "5" 267 | ] 268 | }, 269 | "execution_count": 12, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "15%10" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "Floor division is nothing but converting the result so obtained to the nearest integer." 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 13, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "1.0" 294 | ] 295 | }, 296 | "execution_count": 13, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "2.8//2.0" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "##Relational Operators" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "| Symbol | Task Performed |\n", 317 | "|----|---|\n", 318 | "| == | True, if it is equal |\n", 319 | "| != | True, if not equal to |\n", 320 | "| < | less than |\n", 321 | "| > | greater than |\n", 322 | "| <= | less than or equal to |\n", 323 | "| >= | greater than or equal to |" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 14, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "z = 1" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 15, 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "True" 344 | ] 345 | }, 346 | "execution_count": 15, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "z == 1" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 16, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "False" 364 | ] 365 | }, 366 | "execution_count": 16, 367 | "metadata": {}, 368 | "output_type": "execute_result" 369 | } 370 | ], 371 | "source": [ 372 | "z > 1" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "##Bitwise Operators" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "| Symbol | Task Performed |\n", 387 | "|----|---|\n", 388 | "| & | Logical And |\n", 389 | "| l | Logical OR |\n", 390 | "| ^ | XOR |\n", 391 | "| ~ | Negate |\n", 392 | "| >> | Right shift |\n", 393 | "| << | Left shift |" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 17, 399 | "metadata": {}, 400 | "outputs": [], 401 | "source": [ 402 | "a = 2 #10\n", 403 | "b = 3 #11" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 19, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "2\n", 416 | "0b10\n" 417 | ] 418 | } 419 | ], 420 | "source": [ 421 | "print (a & b)\n", 422 | "print (bin(a&b))" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 20, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "2" 434 | ] 435 | }, 436 | "execution_count": 20, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "5 >> 1" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "0000 0101 -> 5 \n", 450 | "\n", 451 | "Shifting the digits by 1 to the right and zero padding\n", 452 | "\n", 453 | "0000 0010 -> 2" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 21, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": [ 464 | "10" 465 | ] 466 | }, 467 | "execution_count": 21, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "5 << 1" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": {}, 479 | "source": [ 480 | "0000 0101 -> 5 \n", 481 | "\n", 482 | "Shifting the digits by 1 to the left and zero padding\n", 483 | "\n", 484 | "0000 1010 -> 10" 485 | ] 486 | }, 487 | { 488 | "cell_type": "markdown", 489 | "metadata": {}, 490 | "source": [ 491 | "# Built-in Functions" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": {}, 497 | "source": [ 498 | "Python comes loaded with pre-built functions" 499 | ] 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "metadata": {}, 504 | "source": [ 505 | "## Conversion from one system to another" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": {}, 511 | "source": [ 512 | "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**." 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 22, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "data": { 522 | "text/plain": [ 523 | "'0xaa'" 524 | ] 525 | }, 526 | "execution_count": 22, 527 | "metadata": {}, 528 | "output_type": "execute_result" 529 | } 530 | ], 531 | "source": [ 532 | "hex(170)" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 23, 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "170" 544 | ] 545 | }, 546 | "execution_count": 23, 547 | "metadata": {}, 548 | "output_type": "execute_result" 549 | } 550 | ], 551 | "source": [ 552 | "0xAA" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 24, 558 | "metadata": {}, 559 | "outputs": [ 560 | { 561 | "data": { 562 | "text/plain": [ 563 | "'0o10'" 564 | ] 565 | }, 566 | "execution_count": 24, 567 | "metadata": {}, 568 | "output_type": "execute_result" 569 | } 570 | ], 571 | "source": [ 572 | "oct(8)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 26, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "name": "stdout", 589 | "output_type": "stream", 590 | "text": [ 591 | "8\n", 592 | "170\n", 593 | "10\n" 594 | ] 595 | } 596 | ], 597 | "source": [ 598 | "print (int('010',8))\n", 599 | "print (int('0xaa',16))\n", 600 | "print (int('1010',2))" 601 | ] 602 | }, 603 | { 604 | "cell_type": "markdown", 605 | "metadata": {}, 606 | "source": [ 607 | "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 28, 613 | "metadata": {}, 614 | "outputs": [ 615 | { 616 | "name": "stdout", 617 | "output_type": "stream", 618 | "text": [ 619 | "7\n", 620 | "7\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "print (int(7.7))\n", 626 | "print (int('7'))" 627 | ] 628 | }, 629 | { 630 | "cell_type": "markdown", 631 | "metadata": {}, 632 | "source": [ 633 | "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round." 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": 29, 639 | "metadata": {}, 640 | "outputs": [ 641 | { 642 | "data": { 643 | "text/plain": [ 644 | "'b'" 645 | ] 646 | }, 647 | "execution_count": 29, 648 | "metadata": {}, 649 | "output_type": "execute_result" 650 | } 651 | ], 652 | "source": [ 653 | "chr(98)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 30, 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": [ 664 | "98" 665 | ] 666 | }, 667 | "execution_count": 30, 668 | "metadata": {}, 669 | "output_type": "execute_result" 670 | } 671 | ], 672 | "source": [ 673 | "ord('b')" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "## Simplifying Arithmetic Operations" 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 32, 693 | "metadata": { 694 | "scrolled": false 695 | }, 696 | "outputs": [ 697 | { 698 | "name": "stdout", 699 | "output_type": "stream", 700 | "text": [ 701 | "6\n", 702 | "4.56\n" 703 | ] 704 | } 705 | ], 706 | "source": [ 707 | "print (round(5.6231))\n", 708 | "print (round(4.55892, 2))" 709 | ] 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "metadata": {}, 714 | "source": [ 715 | "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." 716 | ] 717 | }, 718 | { 719 | "cell_type": "code", 720 | "execution_count": 33, 721 | "metadata": {}, 722 | "outputs": [ 723 | { 724 | "name": "stdout", 725 | "output_type": "stream", 726 | "text": [ 727 | "5.385164807134504\n" 728 | ] 729 | } 730 | ], 731 | "source": [ 732 | "c = complex('5+2j')\n", 733 | "print (abs(c))" 734 | ] 735 | }, 736 | { 737 | "cell_type": "markdown", 738 | "metadata": {}, 739 | "source": [ 740 | "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " 741 | ] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": 34, 746 | "metadata": {}, 747 | "outputs": [ 748 | { 749 | "data": { 750 | "text/plain": [ 751 | "(4, 1)" 752 | ] 753 | }, 754 | "execution_count": 34, 755 | "metadata": {}, 756 | "output_type": "execute_result" 757 | } 758 | ], 759 | "source": [ 760 | "divmod(9,2)" 761 | ] 762 | }, 763 | { 764 | "cell_type": "markdown", 765 | "metadata": {}, 766 | "source": [ 767 | "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 35, 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "True\n", 780 | "False\n", 781 | "True\n" 782 | ] 783 | } 784 | ], 785 | "source": [ 786 | "print (isinstance(1, int))\n", 787 | "print (isinstance(1.0,int))\n", 788 | "print (isinstance(1.0,(int,float)))" 789 | ] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "**cmp(x,y)**\n", 796 | "\n", 797 | "|x ? y|Output|\n", 798 | "|---|---|\n", 799 | "| x < y | -1 |\n", 800 | "| x == y | 0 |\n", 801 | "| x > y | 1 |" 802 | ] 803 | }, 804 | { 805 | "cell_type": "markdown", 806 | "metadata": {}, 807 | "source": [ 808 | "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 37, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "name": "stdout", 818 | "output_type": "stream", 819 | "text": [ 820 | "27\n", 821 | "2\n" 822 | ] 823 | } 824 | ], 825 | "source": [ 826 | "print (pow(3,3))\n", 827 | "print (pow(3,3,5))" 828 | ] 829 | }, 830 | { 831 | "cell_type": "markdown", 832 | "metadata": {}, 833 | "source": [ 834 | "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 42, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "name": "stdout", 844 | "output_type": "stream", 845 | "text": [ 846 | "range(0, 3)\n", 847 | "range(2, 9)\n", 848 | "range(2, 27, 8)\n", 849 | "\n" 850 | ] 851 | } 852 | ], 853 | "source": [ 854 | "print (range(3))\n", 855 | "print (range(2,9))\n", 856 | "print (range(2,27,8))\n", 857 | "\n", 858 | "print(type(range(3)))" 859 | ] 860 | }, 861 | { 862 | "cell_type": "markdown", 863 | "metadata": {}, 864 | "source": [ 865 | "##Accepting User Inputs" 866 | ] 867 | }, 868 | { 869 | "cell_type": "markdown", 870 | "metadata": {}, 871 | "source": [ 872 | "**raw_input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 44, 878 | "metadata": {}, 879 | "outputs": [ 880 | { 881 | "name": "stdout", 882 | "output_type": "stream", 883 | "text": [ 884 | "Type something here and it will be stored in variable abc \t567890\n" 885 | ] 886 | } 887 | ], 888 | "source": [ 889 | "abc = input(\"Type something here and it will be stored in variable abc \\t\")" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": 45, 895 | "metadata": {}, 896 | "outputs": [ 897 | { 898 | "data": { 899 | "text/plain": [ 900 | "str" 901 | ] 902 | }, 903 | "execution_count": 45, 904 | "metadata": {}, 905 | "output_type": "execute_result" 906 | } 907 | ], 908 | "source": [ 909 | "type(abc)" 910 | ] 911 | }, 912 | { 913 | "cell_type": "markdown", 914 | "metadata": {}, 915 | "source": [ 916 | "**input( )**, this is used only for accepting only integer inputs." 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 46, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "Only integer can be stored in variable abc \t1234\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "abc1 = input(\"Only integer can be stored in variable abc \\t\")" 934 | ] 935 | }, 936 | { 937 | "cell_type": "code", 938 | "execution_count": 47, 939 | "metadata": {}, 940 | "outputs": [ 941 | { 942 | "data": { 943 | "text/plain": [ 944 | "str" 945 | ] 946 | }, 947 | "execution_count": 47, 948 | "metadata": {}, 949 | "output_type": "execute_result" 950 | } 951 | ], 952 | "source": [ 953 | "type(abc1)" 954 | ] 955 | }, 956 | { 957 | "cell_type": "markdown", 958 | "metadata": {}, 959 | "source": [ 960 | "Note that **type( )** returns the format or the type of a variable or a number" 961 | ] 962 | }, 963 | { 964 | "cell_type": "code", 965 | "execution_count": null, 966 | "metadata": {}, 967 | "outputs": [], 968 | "source": [] 969 | } 970 | ], 971 | "metadata": { 972 | "kernelspec": { 973 | "display_name": "Python 3", 974 | "language": "python", 975 | "name": "python3" 976 | }, 977 | "language_info": { 978 | "codemirror_mode": { 979 | "name": "ipython", 980 | "version": 3 981 | }, 982 | "file_extension": ".py", 983 | "mimetype": "text/x-python", 984 | "name": "python", 985 | "nbconvert_exporter": "python", 986 | "pygments_lexer": "ipython3", 987 | "version": "3.7.3" 988 | } 989 | }, 990 | "nbformat": 4, 991 | "nbformat_minor": 1 992 | } 993 | -------------------------------------------------------------------------------- /6 # Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Functions" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Most of the times, In a algorithm the statements keep repeating and it will be a tedious job to execute the same statements again and again and will consume a lot of memory and is not efficient. Enter Functions." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "This is the basic syntax of a function" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "def funcname(arg1, arg2,... argN):\n", 36 | " \n", 37 | " ''' Document String'''\n", 38 | "\n", 39 | " statements\n", 40 | "\n", 41 | "\n", 42 | " return " 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Read the above syntax as, A function by name \"funcname\" is defined, which accepts arguements \"arg1,arg2,....argN\". The function is documented and it is '''Document String'''. The function after executing the statements returns a \"value\"." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Hey Vipul!\n", 62 | "Vipul, How do you do?\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "print (\"Hey Vipul!\")\n", 68 | "print (\"Vipul, How do you do?\")" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Instead of writing the above two statements every single time it can be replaced by defining a function which would do the job in just one line. \n", 76 | "\n", 77 | "Defining a function firstfunc()." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "def firstfunc():\n", 87 | " print (\"Hey Vipul!\")\n", 88 | " print (\"Vipul, How do you do?\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Hey Vipul!\n", 101 | "Vipul, How do you do?\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "firstfunc()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "def firstfunc(username):\n", 123 | " print (\"Hey\", username + '!')\n", 124 | " print (username + ',' ,\"How do you do?\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 9, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Please enter your name : Vipul\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "name1 = input('Please enter your name : ')" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "The name \"Vipul\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 11, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "Hey Vipul!\n", 161 | "Vipul, How do you do?\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "firstfunc(name1)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself." 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 12, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "def firstfunc(username):\n", 183 | " print (\"Hey\", username + '!')\n", 184 | " print (username + ',' ,\"How do you do?\")\n", 185 | "def secondfunc():\n", 186 | " name = input(\"Please enter your name : \")\n", 187 | " firstfunc(name)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 13, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "Please enter your name : V Gaur\n", 200 | "Hey V Gaur!\n", 201 | "V Gaur, How do you do?\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "secondfunc()" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "## Return Statement" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "When the function results in some value and that value has to be stored in a variable or needs to be sent back or returned for further operation to the main algorithm, return statement is used." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 14, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "def times(x,y):\n", 230 | " z = x*y\n", 231 | " return z" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 15, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "20\n" 251 | ] 252 | } 253 | ], 254 | "source": [ 255 | "c = times(4,5)\n", 256 | "print (c)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "The z value is stored in variable c and can be used for further operations." 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "Instead of declaring another variable the entire statement itself can be used in the return statement as shown." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 16, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "def times(x,y):\n", 280 | " '''This multiplies the two input arguments'''\n", 281 | " return x*y" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 17, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "20\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "c = times(4,5)\n", 299 | "print (c)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 18, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "Help on function times in module __main__:\n", 319 | "\n", 320 | "times(x, y)\n", 321 | " This multiplies the two input arguments\n", 322 | "\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "help(times)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "Multiple variable can also be returned, But keep in mind the order." 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 19, 340 | "metadata": {}, 341 | "outputs": [], 342 | "source": [ 343 | "eglist = [10,50,30,12,6,8,100]" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 20, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "def egfunc(eglist):\n", 353 | " highest = max(eglist)\n", 354 | " lowest = min(eglist)\n", 355 | " first = eglist[0]\n", 356 | " last = eglist[-1]\n", 357 | " return highest,lowest,first,last" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "If the function is just called without any variable for it to be assigned to, the result is returned inside a tuple. But if the variables are mentioned then the result is assigned to the variable in a particular order which is declared in the return statement." 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 21, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "(100, 6, 10, 100)" 376 | ] 377 | }, 378 | "execution_count": 21, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "egfunc(eglist)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 23, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | " a = 100 \n", 397 | " b = 6 \n", 398 | " c = 10 \n", 399 | " d = 100\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "a,b,c,d = egfunc(eglist)\n", 405 | "print (' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "## Implicit arguments" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 24, 425 | "metadata": {}, 426 | "outputs": [], 427 | "source": [ 428 | "def implicitadd(x,y=3):\n", 429 | " return x+y" 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 25, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "7" 455 | ] 456 | }, 457 | "execution_count": 25, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "implicitadd(4)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "metadata": {}, 469 | "source": [ 470 | "But if the second argument is specified then this value overrides the implicit value assigned to the argument " 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 26, 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "8" 482 | ] 483 | }, 484 | "execution_count": 26, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "implicitadd(4,4)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "metadata": {}, 496 | "source": [ 497 | "## Any number of arguments" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "If the number of arguments that is to be accepted by a function is not known then a asterisk symbol is used before the argument." 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 29, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [ 513 | "def add_n(*args):\n", 514 | " res = 0\n", 515 | " reslist = []\n", 516 | " for i in args:\n", 517 | " reslist.append(i)\n", 518 | " print (reslist)\n", 519 | " return sum(reslist)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "The above function accepts any number of arguments, defines a list and appends all the arguments into that list and return the sum of all the arguments." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 30, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "name": "stdout", 536 | "output_type": "stream", 537 | "text": [ 538 | "[1, 2, 3, 4, 5]\n" 539 | ] 540 | }, 541 | { 542 | "data": { 543 | "text/plain": [ 544 | "15" 545 | ] 546 | }, 547 | "execution_count": 30, 548 | "metadata": {}, 549 | "output_type": "execute_result" 550 | } 551 | ], 552 | "source": [ 553 | "add_n(1,2,3,4,5)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 31, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "[1, 2, 3]\n" 566 | ] 567 | }, 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "6" 572 | ] 573 | }, 574 | "execution_count": 31, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "add_n(1,2,3)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "## Global and Local Variables" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "Whatever variable is declared inside a function is local variable and outside the function in global variable." 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 32, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "eg1 = [1,2,3,4,5]" 604 | ] 605 | }, 606 | { 607 | "cell_type": "markdown", 608 | "metadata": {}, 609 | "source": [ 610 | "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable." 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 35, 616 | "metadata": {}, 617 | "outputs": [], 618 | "source": [ 619 | "def egfunc1():\n", 620 | " def thirdfunc(arg1):\n", 621 | " eg2 = arg1[:]\n", 622 | " eg2.append(6)\n", 623 | " print (\"This is happening inside the function :\", eg2)\n", 624 | " print (\"This is happening before the function is called : \", eg1)\n", 625 | " thirdfunc(eg1)\n", 626 | " print (\"This is happening outside the function :\", eg1)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 36, 632 | "metadata": {}, 633 | "outputs": [ 634 | { 635 | "name": "stdout", 636 | "output_type": "stream", 637 | "text": [ 638 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 639 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 640 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "egfunc1()" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": {}, 651 | "source": [ 652 | "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 37, 658 | "metadata": {}, 659 | "outputs": [], 660 | "source": [ 661 | "eg3 = [1,2,3,4,5]" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 38, 667 | "metadata": {}, 668 | "outputs": [ 669 | { 670 | "ename": "SyntaxError", 671 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)? (, line 6)", 672 | "output_type": "error", 673 | "traceback": [ 674 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m print \"This is happening inside the function :\", eg2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"This is happening inside the function :\", eg2)?\n" 675 | ] 676 | } 677 | ], 678 | "source": [ 679 | "def egfunc1():\n", 680 | " def thirdfunc(arg1):\n", 681 | " global eg2\n", 682 | " eg2 = arg1[:]\n", 683 | " eg2.append(6)\n", 684 | " print \"This is happening inside the function :\", eg2 \n", 685 | " print \"This is happening before the function is called : \", eg1\n", 686 | " thirdfunc(eg1)\n", 687 | " print \"This is happening outside the function :\", eg1 \n", 688 | " print \"Accessing a variable declared inside the function from outside :\" , eg2" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 39, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "This is happening before the function is called : [1, 2, 3, 4, 5]\n", 701 | "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", 702 | "This is happening outside the function : [1, 2, 3, 4, 5]\n" 703 | ] 704 | } 705 | ], 706 | "source": [ 707 | "egfunc1()" 708 | ] 709 | }, 710 | { 711 | "cell_type": "markdown", 712 | "metadata": {}, 713 | "source": [ 714 | "## Lambda Functions" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression." 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 1, 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [ 730 | "z = lambda x: x * x\n", 731 | "\n", 732 | "\n", 733 | "def squared(x):\n", 734 | " return x * x" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": 3, 740 | "metadata": {}, 741 | "outputs": [ 742 | { 743 | "name": "stdout", 744 | "output_type": "stream", 745 | "text": [ 746 | "64\n", 747 | "64\n" 748 | ] 749 | } 750 | ], 751 | "source": [ 752 | "print(z(8))\n", 753 | "\n", 754 | "print(squared(8))" 755 | ] 756 | }, 757 | { 758 | "cell_type": "markdown", 759 | "metadata": {}, 760 | "source": [ 761 | "###map" 762 | ] 763 | }, 764 | { 765 | "cell_type": "markdown", 766 | "metadata": {}, 767 | "source": [ 768 | "**map( )** function basically executes the function that is defined to each of the list's element separately." 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 42, 774 | "metadata": {}, 775 | "outputs": [], 776 | "source": [ 777 | "list1 = [1,2,3,4,5,6,7,8,9]" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": 44, 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "name": "stdout", 787 | "output_type": "stream", 788 | "text": [ 789 | "\n" 790 | ] 791 | } 792 | ], 793 | "source": [ 794 | "eg = map(lambda x:x+2, list1)\n", 795 | "print (eg)" 796 | ] 797 | }, 798 | { 799 | "cell_type": "markdown", 800 | "metadata": {}, 801 | "source": [ 802 | "You can also add two lists." 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 45, 808 | "metadata": {}, 809 | "outputs": [], 810 | "source": [ 811 | "list2 = [9,8,7,6,5,4,3,2,1]" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": 47, 817 | "metadata": {}, 818 | "outputs": [ 819 | { 820 | "name": "stdout", 821 | "output_type": "stream", 822 | "text": [ 823 | "\n" 824 | ] 825 | } 826 | ], 827 | "source": [ 828 | "eg2 = map(lambda x,y:x+y, list1,list2)\n", 829 | "print (eg2)" 830 | ] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": {}, 835 | "source": [ 836 | "Not only lambda function but also other built in functions can also be used." 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": 48, 842 | "metadata": {}, 843 | "outputs": [ 844 | { 845 | "name": "stdout", 846 | "output_type": "stream", 847 | "text": [ 848 | "\n" 849 | ] 850 | } 851 | ], 852 | "source": [ 853 | "eg3 = map(str,eg2)\n", 854 | "print (eg3)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "### filter" 862 | ] 863 | }, 864 | { 865 | "cell_type": "markdown", 866 | "metadata": {}, 867 | "source": [ 868 | "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 49, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "list1 = [1,2,3,4,5,6,7,8,9]" 878 | ] 879 | }, 880 | { 881 | "cell_type": "markdown", 882 | "metadata": {}, 883 | "source": [ 884 | "To get the elements which are less than 5," 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": 50, 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "data": { 894 | "text/plain": [ 895 | "" 896 | ] 897 | }, 898 | "execution_count": 50, 899 | "metadata": {}, 900 | "output_type": "execute_result" 901 | } 902 | ], 903 | "source": [ 904 | "filter(lambda x:x<5,list1)" 905 | ] 906 | }, 907 | { 908 | "cell_type": "markdown", 909 | "metadata": {}, 910 | "source": [ 911 | "Notice what happens when **map()** is used." 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 51, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "data": { 921 | "text/plain": [ 922 | "" 923 | ] 924 | }, 925 | "execution_count": 51, 926 | "metadata": {}, 927 | "output_type": "execute_result" 928 | } 929 | ], 930 | "source": [ 931 | "map(lambda x:x<5, list1)" 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": {}, 937 | "source": [ 938 | "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 52, 944 | "metadata": {}, 945 | "outputs": [ 946 | { 947 | "data": { 948 | "text/plain": [ 949 | "" 950 | ] 951 | }, 952 | "execution_count": 52, 953 | "metadata": {}, 954 | "output_type": "execute_result" 955 | } 956 | ], 957 | "source": [ 958 | "filter(lambda x:x%4==0,list1)" 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "execution_count": null, 964 | "metadata": {}, 965 | "outputs": [], 966 | "source": [] 967 | } 968 | ], 969 | "metadata": { 970 | "kernelspec": { 971 | "display_name": "Python 3", 972 | "language": "python", 973 | "name": "python3" 974 | }, 975 | "language_info": { 976 | "codemirror_mode": { 977 | "name": "ipython", 978 | "version": 3 979 | }, 980 | "file_extension": ".py", 981 | "mimetype": "text/x-python", 982 | "name": "python", 983 | "nbconvert_exporter": "python", 984 | "pygments_lexer": "ipython3", 985 | "version": "3.7.3" 986 | } 987 | }, 988 | "nbformat": 4, 989 | "nbformat_minor": 1 990 | } 991 | -------------------------------------------------------------------------------- /4 # Strings and Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Strings" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "String0 = 'Taj Mahal is beautiful'\n", 31 | "String1 = \"Taj Mahal is beautiful\"\n", 32 | "String2 = '''Taj Mahal\n", 33 | "is\n", 34 | "beautiful'''" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "Taj Mahal is beautiful \n", 47 | "Taj Mahal is beautiful \n", 48 | "Taj Mahal\n", 49 | "is\n", 50 | "beautiful \n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print (String0 , type(String0))\n", 56 | "print (String1 , type(String1))\n", 57 | "print (String2 , type(String2))" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "String Indexing and Slicing are similar to Lists which was explained in detail earlier." 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 4, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "M\n", 77 | "Mahal is beautiful\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "print (String0[4])\n", 83 | "print (String0[4:])" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Built-in Functions" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "7\n", 110 | "-1\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "print (String0.find('al'))\n", 116 | "print (String0.find('am'))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "The index value returned is the index of the first element in the input data." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 10, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "a\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print (String0[7])" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "One can also input **find( )** function between which index values it has to search." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 11, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "2\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "print (String0.find('j',1,3))" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "**capitalize( )** is used to capitalize the first element in the string." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 12, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Observe the first letter in this sentence.\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "String3 = 'observe the first letter in this sentence.'\n", 189 | "print (String3.capitalize())" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "**center( )** is used to center align the string by specifying the field width." 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 13, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "' Taj Mahal is beautiful '" 208 | ] 209 | }, 210 | "execution_count": 13, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "String0.center(70)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "One can also fill the left out spaces with any other character." 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 14, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "'------------------------Taj Mahal is beautiful------------------------'" 235 | ] 236 | }, 237 | "execution_count": 14, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "String0.center(70,'-')" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "**zfill( )** is used for zero padding by specifying the field width." 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 15, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "'00000000Taj Mahal is beautiful'" 262 | ] 263 | }, 264 | "execution_count": 15, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "String0.zfill(30)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 20, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "h\te\tl\tl\to\n", 290 | "h e l l o\n", 291 | "h e l l o\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "s = 'h\\te\\tl\\tl\\to'\n", 297 | "print (s)\n", 298 | "print (s.expandtabs(2))\n", 299 | "print (s.expandtabs())" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": { 305 | "collapsed": true 306 | }, 307 | "source": [ 308 | "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 21, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "name": "stdout", 318 | "output_type": "stream", 319 | "text": [ 320 | "0\n", 321 | "4\n" 322 | ] 323 | }, 324 | { 325 | "ename": "ValueError", 326 | "evalue": "substring not found", 327 | "output_type": "error", 328 | "traceback": [ 329 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 330 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 331 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Taj'\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[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Mahal'\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[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Mahal'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 332 | "\u001b[1;31mValueError\u001b[0m: substring not found" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "print (String0.index('Taj'))\n", 338 | "print (String0.index('Mahal',0))\n", 339 | "print (String0.index('Mahal',10,20))" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 22, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "False\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "print (String0.endswith('y'))" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "The start and stop index values can also be specified." 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 24, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "True\n", 383 | "True\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "print (String0.endswith('l',0))\n", 389 | "print (String0.endswith('M',0,5))" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 25, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "4\n", 409 | "2\n" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "print (String0.count('a',0))\n", 415 | "print (String0.count('a',5,10))" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": {}, 421 | "source": [ 422 | "**join( )** function is used add a char in between the elements of the input string." 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 26, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "'*a_a-'" 434 | ] 435 | }, 436 | "execution_count": 26, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "'a'.join('*_-')" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "'*_-' is the input string and char 'a' is added in between each element" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "metadata": {}, 455 | "source": [ 456 | "**join( )** function can also be used to convert a list into a string." 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 28, 462 | "metadata": {}, 463 | "outputs": [ 464 | { 465 | "name": "stdout", 466 | "output_type": "stream", 467 | "text": [ 468 | "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", 469 | "Taj Mahal is beautiful\n" 470 | ] 471 | } 472 | ], 473 | "source": [ 474 | "a = list(String0)\n", 475 | "print (a)\n", 476 | "b = ''.join(a)\n", 477 | "print (b)" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 29, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | " /i/s/ /b/e/a/u/t/i/f/u/l\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "c = '/'.join(a)[18:]\n", 502 | "print (c)" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "metadata": {}, 508 | "source": [ 509 | "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 30, 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "name": "stdout", 519 | "output_type": "stream", 520 | "text": [ 521 | "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "d = c.split('/')\n", 527 | "print (d)" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 31, 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "name": "stdout", 544 | "output_type": "stream", 545 | "text": [ 546 | "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", 547 | "4\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "e = c.split('/',3)\n", 553 | "print (e)\n", 554 | "print (len(e))" 555 | ] 556 | }, 557 | { 558 | "cell_type": "markdown", 559 | "metadata": {}, 560 | "source": [ 561 | "**lower( )** converts any capital letter to small letter." 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 32, 567 | "metadata": {}, 568 | "outputs": [ 569 | { 570 | "name": "stdout", 571 | "output_type": "stream", 572 | "text": [ 573 | "Taj Mahal is beautiful\n", 574 | "taj mahal is beautiful\n" 575 | ] 576 | } 577 | ], 578 | "source": [ 579 | "print (String0)\n", 580 | "print (String0.lower())" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "**upper( )** converts any small letter to capital letter." 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 33, 593 | "metadata": {}, 594 | "outputs": [ 595 | { 596 | "data": { 597 | "text/plain": [ 598 | "'TAJ MAHAL IS BEAUTIFUL'" 599 | ] 600 | }, 601 | "execution_count": 33, 602 | "metadata": {}, 603 | "output_type": "execute_result" 604 | } 605 | ], 606 | "source": [ 607 | "String0.upper()" 608 | ] 609 | }, 610 | { 611 | "cell_type": "markdown", 612 | "metadata": {}, 613 | "source": [ 614 | "**replace( )** function replaces the element with another element." 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 35, 620 | "metadata": {}, 621 | "outputs": [ 622 | { 623 | "data": { 624 | "text/plain": [ 625 | "'Chandigarh is beautiful'" 626 | ] 627 | }, 628 | "execution_count": 35, 629 | "metadata": {}, 630 | "output_type": "execute_result" 631 | } 632 | ], 633 | "source": [ 634 | "String0.replace('Taj Mahal','Chandigarh')" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": {}, 640 | "source": [ 641 | "**strip( )** function is used to delete elements from the right end and the left end which is not required." 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": 36, 647 | "metadata": {}, 648 | "outputs": [], 649 | "source": [ 650 | "f = ' hello '" 651 | ] 652 | }, 653 | { 654 | "cell_type": "markdown", 655 | "metadata": {}, 656 | "source": [ 657 | "If no char is specified then it will delete all the spaces that is present in the right and left hand side of the data." 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 37, 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "'hello'" 669 | ] 670 | }, 671 | "execution_count": 37, 672 | "metadata": {}, 673 | "output_type": "execute_result" 674 | } 675 | ], 676 | "source": [ 677 | "f.strip()" 678 | ] 679 | }, 680 | { 681 | "cell_type": "markdown", 682 | "metadata": {}, 683 | "source": [ 684 | "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 38, 690 | "metadata": {}, 691 | "outputs": [], 692 | "source": [ 693 | "f = ' ***----hello---******* '" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": 40, 699 | "metadata": {}, 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/plain": [ 704 | "' ***----hello---******* '" 705 | ] 706 | }, 707 | "execution_count": 40, 708 | "metadata": {}, 709 | "output_type": "execute_result" 710 | } 711 | ], 712 | "source": [ 713 | "f.strip('*')" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "metadata": {}, 719 | "source": [ 720 | "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present." 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": 42, 726 | "metadata": {}, 727 | "outputs": [ 728 | { 729 | "name": "stdout", 730 | "output_type": "stream", 731 | "text": [ 732 | "----hello---\n", 733 | "hello\n" 734 | ] 735 | } 736 | ], 737 | "source": [ 738 | "print (f.strip(' *'))\n", 739 | "print (f.strip(' *-'))" 740 | ] 741 | }, 742 | { 743 | "cell_type": "markdown", 744 | "metadata": {}, 745 | "source": [ 746 | "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right." 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 43, 752 | "metadata": {}, 753 | "outputs": [ 754 | { 755 | "name": "stdout", 756 | "output_type": "stream", 757 | "text": [ 758 | "----hello---******* \n", 759 | " ***----hello---\n" 760 | ] 761 | } 762 | ], 763 | "source": [ 764 | "print (f.lstrip(' *'))\n", 765 | "print (f.rstrip(' *'))" 766 | ] 767 | }, 768 | { 769 | "cell_type": "markdown", 770 | "metadata": {}, 771 | "source": [ 772 | "## Dictionaries" 773 | ] 774 | }, 775 | { 776 | "cell_type": "markdown", 777 | "metadata": {}, 778 | "source": [ 779 | "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." 780 | ] 781 | }, 782 | { 783 | "cell_type": "markdown", 784 | "metadata": {}, 785 | "source": [ 786 | "To define a dictionary, equate a variable to { } or dict()" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": 44, 792 | "metadata": {}, 793 | "outputs": [ 794 | { 795 | "name": "stdout", 796 | "output_type": "stream", 797 | "text": [ 798 | " \n" 799 | ] 800 | } 801 | ], 802 | "source": [ 803 | "d0 = {}\n", 804 | "d1 = dict()\n", 805 | "print (type(d0), type(d1))" 806 | ] 807 | }, 808 | { 809 | "cell_type": "markdown", 810 | "metadata": {}, 811 | "source": [ 812 | "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": 45, 818 | "metadata": {}, 819 | "outputs": [ 820 | { 821 | "name": "stdout", 822 | "output_type": "stream", 823 | "text": [ 824 | "{'One': 1, 'OneTwo': 12}\n" 825 | ] 826 | } 827 | ], 828 | "source": [ 829 | "d0['One'] = 1\n", 830 | "d0['OneTwo'] = 12 \n", 831 | "print (d0)" 832 | ] 833 | }, 834 | { 835 | "cell_type": "markdown", 836 | "metadata": {}, 837 | "source": [ 838 | "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" 839 | ] 840 | }, 841 | { 842 | "cell_type": "code", 843 | "execution_count": 46, 844 | "metadata": {}, 845 | "outputs": [ 846 | { 847 | "name": "stdout", 848 | "output_type": "stream", 849 | "text": [ 850 | "1\n" 851 | ] 852 | } 853 | ], 854 | "source": [ 855 | "print (d0['One'])" 856 | ] 857 | }, 858 | { 859 | "cell_type": "markdown", 860 | "metadata": {}, 861 | "source": [ 862 | "Two lists which are related can be merged to form a dictionary." 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 47, 868 | "metadata": {}, 869 | "outputs": [], 870 | "source": [ 871 | "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", 872 | "numbers = [1, 2, 3, 4, 5]" 873 | ] 874 | }, 875 | { 876 | "cell_type": "markdown", 877 | "metadata": {}, 878 | "source": [ 879 | "**zip( )** function is used to combine two lists" 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "execution_count": 48, 885 | "metadata": {}, 886 | "outputs": [ 887 | { 888 | "name": "stdout", 889 | "output_type": "stream", 890 | "text": [ 891 | "\n" 892 | ] 893 | } 894 | ], 895 | "source": [ 896 | "d2 = zip(names,numbers)\n", 897 | "print (d2)" 898 | ] 899 | }, 900 | { 901 | "cell_type": "markdown", 902 | "metadata": {}, 903 | "source": [ 904 | "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n", 905 | "\n", 906 | "Further, To convert the above into a dictionary. **dict( )** function is used." 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": 49, 912 | "metadata": {}, 913 | "outputs": [ 914 | { 915 | "name": "stdout", 916 | "output_type": "stream", 917 | "text": [ 918 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 919 | ] 920 | } 921 | ], 922 | "source": [ 923 | "a1 = dict(d2)\n", 924 | "print (a1)" 925 | ] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "### Built-in Functions" 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": {}, 937 | "source": [ 938 | "**clear( )** function is used to erase the entire database that was created." 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 50, 944 | "metadata": {}, 945 | "outputs": [ 946 | { 947 | "name": "stdout", 948 | "output_type": "stream", 949 | "text": [ 950 | "{}\n" 951 | ] 952 | } 953 | ], 954 | "source": [ 955 | "a1.clear()\n", 956 | "print (a1)" 957 | ] 958 | }, 959 | { 960 | "cell_type": "markdown", 961 | "metadata": {}, 962 | "source": [ 963 | "Dictionary can also be built using loops." 964 | ] 965 | }, 966 | { 967 | "cell_type": "code", 968 | "execution_count": 52, 969 | "metadata": {}, 970 | "outputs": [ 971 | { 972 | "name": "stdout", 973 | "output_type": "stream", 974 | "text": [ 975 | "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n" 976 | ] 977 | } 978 | ], 979 | "source": [ 980 | "for i in range(len(names)):\n", 981 | " a1[names[i]] = numbers[i]\n", 982 | "print (a1)" 983 | ] 984 | }, 985 | { 986 | "cell_type": "markdown", 987 | "metadata": {}, 988 | "source": [ 989 | "**values( )** function returns a list with all the assigned values in the dictionary." 990 | ] 991 | }, 992 | { 993 | "cell_type": "code", 994 | "execution_count": 53, 995 | "metadata": {}, 996 | "outputs": [ 997 | { 998 | "data": { 999 | "text/plain": [ 1000 | "dict_values([1, 2, 3, 4, 5])" 1001 | ] 1002 | }, 1003 | "execution_count": 53, 1004 | "metadata": {}, 1005 | "output_type": "execute_result" 1006 | } 1007 | ], 1008 | "source": [ 1009 | "a1.values()" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "markdown", 1014 | "metadata": {}, 1015 | "source": [ 1016 | "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": 54, 1022 | "metadata": {}, 1023 | "outputs": [ 1024 | { 1025 | "data": { 1026 | "text/plain": [ 1027 | "dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])" 1028 | ] 1029 | }, 1030 | "execution_count": 54, 1031 | "metadata": {}, 1032 | "output_type": "execute_result" 1033 | } 1034 | ], 1035 | "source": [ 1036 | "a1.keys()" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": {}, 1042 | "source": [ 1043 | "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "execution_count": 55, 1049 | "metadata": {}, 1050 | "outputs": [ 1051 | { 1052 | "data": { 1053 | "text/plain": [ 1054 | "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)])" 1055 | ] 1056 | }, 1057 | "execution_count": 55, 1058 | "metadata": {}, 1059 | "output_type": "execute_result" 1060 | } 1061 | ], 1062 | "source": [ 1063 | "a1.items()" 1064 | ] 1065 | }, 1066 | { 1067 | "cell_type": "markdown", 1068 | "metadata": {}, 1069 | "source": [ 1070 | "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": 56, 1076 | "metadata": {}, 1077 | "outputs": [ 1078 | { 1079 | "name": "stdout", 1080 | "output_type": "stream", 1081 | "text": [ 1082 | "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}\n", 1083 | "4\n" 1084 | ] 1085 | } 1086 | ], 1087 | "source": [ 1088 | "a2 = a1.pop('Four')\n", 1089 | "print (a1)\n", 1090 | "print (a2)" 1091 | ] 1092 | }, 1093 | { 1094 | "cell_type": "code", 1095 | "execution_count": null, 1096 | "metadata": {}, 1097 | "outputs": [], 1098 | "source": [] 1099 | } 1100 | ], 1101 | "metadata": { 1102 | "kernelspec": { 1103 | "display_name": "Python 3", 1104 | "language": "python", 1105 | "name": "python3" 1106 | }, 1107 | "language_info": { 1108 | "codemirror_mode": { 1109 | "name": "ipython", 1110 | "version": 3 1111 | }, 1112 | "file_extension": ".py", 1113 | "mimetype": "text/x-python", 1114 | "name": "python", 1115 | "nbconvert_exporter": "python", 1116 | "pygments_lexer": "ipython3", 1117 | "version": "3.7.3" 1118 | } 1119 | }, 1120 | "nbformat": 4, 1121 | "nbformat_minor": 1 1122 | } 1123 | -------------------------------------------------------------------------------- /3 # Lists, Tuples and Sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "All the Jupyter Notebooks in this lecture series are available at https://github.com/pksvv/MachineLearningSL" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Data Structures" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "In simple terms, It is the the collection or group of data in a particular structure." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Lists" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling it's index value.\n", 36 | "\n", 37 | "Lists are declared by just equating a variable to '[ ]' or list." 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "a = []" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "print (type(a))" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "One can directly assign the sequence of data to a list x as shown." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "x = ['apple', 'orange']" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "### Indexing" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "In python, Indexing starts from 0. Thus now the list x, which has two elements will have apple at 0 index and orange at 1 index." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "'apple'" 105 | ] 106 | }, 107 | "execution_count": 4, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "x[0]" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Indexing can also be done in reverse order. That is the last element can be accessed first. Here, indexing starts from -1. Thus index value -1 will be orange and index -2 will be apple." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 5, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "x[0] > apple\n", 133 | "x[1] > orange\n", 134 | "x[-1] > orange\n", 135 | "x[-2] > apple\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print('x[0] > ', x[0])\n", 141 | "print('x[1] > ', x[1])\n", 142 | "print('x[-1] > ',x[-1])\n", 143 | "print('x[-2] > ', x[-2])" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "As you might have already guessed, x[0] = x[-2], x[1] = x[-1]. This concept can be extended towards lists with more many elements." 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 6, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "y = ['carrot','potato']" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "Here we have declared two lists x and y each containing its own data. Now, these two lists can again be put into another list say z which will have it's data as two lists. This list inside a list is called as nested lists and is how an array would be declared which we will see later." 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 7, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "[['apple', 'orange'], ['carrot', 'potato']]\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "z = [x,y]\n", 184 | "print (z)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Indexing in nested lists can be quite confusing if you do not understand how indexing works in python. So let us break it down and then arrive at a conclusion.\n", 192 | "\n", 193 | "Let us access the data 'apple' in the above nested list.\n", 194 | "First, at index 0 there is a list ['apple','orange'] and at index 1 there is another list ['carrot','potato']. Hence z[0] should give us the first list which contains 'apple'." 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "['apple', 'orange']\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "z1 = z[0]\n", 212 | "print (z1)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "Now observe that z1 is not at all a nested list thus to access 'apple', z1 should be indexed at 0." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 9, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "data": { 229 | "text/plain": [ 230 | "'apple'" 231 | ] 232 | }, 233 | "execution_count": 9, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "z1[0]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "Instead of doing the above, In python, you can access 'apple' by just writing the index values each time side by side." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 10, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "'potato'" 258 | ] 259 | }, 260 | "execution_count": 10, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "z[1][1]" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "If there was a list inside a list inside a list then you can access the innermost value by executing z[ ][ ][ ]." 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "### Slicing" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "Indexing was only limited to accessing a single element, Slicing on the other hand is accessing a sequence of data inside the list. In other words \"slicing\" the list.\n", 288 | "\n", 289 | "Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist [ a : b ] where a,b are the index values from the parent list. If a or b is not defined then the index value is considered to be the first value for a if a is not defined and the last value for b when b is not defined." 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 11, 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [ 298 | "num = [0,1,2,3,4,5,6,7,8,9]" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 12, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "[0, 1, 2, 3]\n", 311 | "[4, 5, 6, 7, 8, 9]\n" 312 | ] 313 | } 314 | ], 315 | "source": [ 316 | "print (num[0:4])\n", 317 | "print (num[4:])" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "You can also slice a parent list with a fixed length or step length." 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 13, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "[0, 3, 6]" 336 | ] 337 | }, 338 | "execution_count": 13, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "num[:9:3]" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "### Built in List Functions" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "To find the length of the list or the number of elements in a list, **len( )** is used." 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 14, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "10" 370 | ] 371 | }, 372 | "execution_count": 14, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "len(num)" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "If the list consists of all integer elements then **min( )** and **max( )** gives the minimum and maximum value in the list." 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 15, 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "data": { 395 | "text/plain": [ 396 | "0" 397 | ] 398 | }, 399 | "execution_count": 15, 400 | "metadata": {}, 401 | "output_type": "execute_result" 402 | } 403 | ], 404 | "source": [ 405 | "min(num)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 16, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "data": { 415 | "text/plain": [ 416 | "9" 417 | ] 418 | }, 419 | "execution_count": 16, 420 | "metadata": {}, 421 | "output_type": "execute_result" 422 | } 423 | ], 424 | "source": [ 425 | "max(num)" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "Lists can be concatenated by adding, '+' them. The resultant list will contain all the elements of the lists that were added. The resultant list will not be a nested list." 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 17, 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "[1, 2, 3, 5, 4, 7]" 444 | ] 445 | }, 446 | "execution_count": 17, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "[1,2,3] + [5,4,7]" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "There might arise a requirement where you might need to check if a particular element is there in a predefined list. Consider the below list." 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 18, 465 | "metadata": {}, 466 | "outputs": [], 467 | "source": [ 468 | "names = ['Earth','Air','Fire','Water']" 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "metadata": {}, 474 | "source": [ 475 | "To check if 'Fire' and 'Modi' is present in the list names. A conventional approach would be to use a for loop and iterate over the list and use the if condition. But in python you can use 'a in b' concept which would return 'True' if a is present in b and 'False' if not." 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 19, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "data": { 485 | "text/plain": [ 486 | "True" 487 | ] 488 | }, 489 | "execution_count": 19, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "'Fire' in names" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 20, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "data": { 505 | "text/plain": [ 506 | "False" 507 | ] 508 | }, 509 | "execution_count": 20, 510 | "metadata": {}, 511 | "output_type": "execute_result" 512 | } 513 | ], 514 | "source": [ 515 | "'Modi' in names" 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "In a list with elements as string, **max( )** and **min( )** is applicable. **max( )** would return a string element whose ASCII value is the highest and the lowest when **min( )** is used. Note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth." 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 21, 528 | "metadata": {}, 529 | "outputs": [], 530 | "source": [ 531 | "mlist = ['bzaa','ds','nc','az','z','klm']" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 22, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "name": "stdout", 541 | "output_type": "stream", 542 | "text": [ 543 | "z\n", 544 | "az\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "print (max(mlist))\n", 550 | "print (min(mlist))" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "Here the first index of each element is considered and thus z has the highest ASCII value thus it is returned and minimum ASCII is a. But what if numbers are declared as strings?" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 23, 563 | "metadata": {}, 564 | "outputs": [], 565 | "source": [ 566 | "nlist = ['1','94','93','1000']" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 24, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "name": "stdout", 576 | "output_type": "stream", 577 | "text": [ 578 | "94\n", 579 | "1\n" 580 | ] 581 | } 582 | ], 583 | "source": [ 584 | "print (max(nlist))\n", 585 | "print (min(nlist))" 586 | ] 587 | }, 588 | { 589 | "cell_type": "markdown", 590 | "metadata": {}, 591 | "source": [ 592 | "Even if the numbers are declared in a string the first index of each element is considered and the maximum and minimum values are returned accordingly." 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "But if you want to find the **max( )** string element based on the length of the string then another parameter 'key=len' is declared inside the **max( )** and **min( )** function." 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 25, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "name": "stdout", 609 | "output_type": "stream", 610 | "text": [ 611 | "Earth\n", 612 | "Air\n" 613 | ] 614 | } 615 | ], 616 | "source": [ 617 | "print (max(names, key=len))\n", 618 | "print (min(names, key=len))" 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "But even 'Water' has length 5. **max()** or **min()** function returns the first element when there are two or more elements with the same length.\n", 626 | "\n", 627 | "Any other built in function can be used or lambda function (will be discussed later) in place of len.\n", 628 | "\n", 629 | "A string can be converted into a list by using the **list()** function." 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 26, 635 | "metadata": {}, 636 | "outputs": [ 637 | { 638 | "data": { 639 | "text/plain": [ 640 | "['h', 'e', 'l', 'l', 'o']" 641 | ] 642 | }, 643 | "execution_count": 26, 644 | "metadata": {}, 645 | "output_type": "execute_result" 646 | } 647 | ], 648 | "source": [ 649 | "list('hello')" 650 | ] 651 | }, 652 | { 653 | "cell_type": "markdown", 654 | "metadata": {}, 655 | "source": [ 656 | "**append( )** is used to add a element at the end of the list." 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 27, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [ 665 | "lst = [1,1,4,8,7]" 666 | ] 667 | }, 668 | { 669 | "cell_type": "code", 670 | "execution_count": 28, 671 | "metadata": {}, 672 | "outputs": [ 673 | { 674 | "name": "stdout", 675 | "output_type": "stream", 676 | "text": [ 677 | "[1, 1, 4, 8, 7, 1]\n" 678 | ] 679 | } 680 | ], 681 | "source": [ 682 | "lst.append(1)\n", 683 | "print (lst)" 684 | ] 685 | }, 686 | { 687 | "cell_type": "markdown", 688 | "metadata": {}, 689 | "source": [ 690 | "**count( )** is used to count the number of a particular element that is present in the list. " 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": 29, 696 | "metadata": {}, 697 | "outputs": [ 698 | { 699 | "data": { 700 | "text/plain": [ 701 | "3" 702 | ] 703 | }, 704 | "execution_count": 29, 705 | "metadata": {}, 706 | "output_type": "execute_result" 707 | } 708 | ], 709 | "source": [ 710 | "lst.count(1)" 711 | ] 712 | }, 713 | { 714 | "cell_type": "markdown", 715 | "metadata": {}, 716 | "source": [ 717 | "**append( )** function can also be used to add a entire list at the end. Observe that the resultant list becomes a nested list." 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 30, 723 | "metadata": {}, 724 | "outputs": [], 725 | "source": [ 726 | "lst1 = [5,4,2,8]" 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "execution_count": 31, 732 | "metadata": {}, 733 | "outputs": [ 734 | { 735 | "name": "stdout", 736 | "output_type": "stream", 737 | "text": [ 738 | "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n" 739 | ] 740 | } 741 | ], 742 | "source": [ 743 | "lst.append(lst1)\n", 744 | "print (lst)" 745 | ] 746 | }, 747 | { 748 | "cell_type": "markdown", 749 | "metadata": {}, 750 | "source": [ 751 | "But if nested list is not what is desired then **extend( )** function can be used." 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 32, 757 | "metadata": {}, 758 | "outputs": [ 759 | { 760 | "name": "stdout", 761 | "output_type": "stream", 762 | "text": [ 763 | "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" 764 | ] 765 | } 766 | ], 767 | "source": [ 768 | "lst.extend(lst1)\n", 769 | "print (lst)" 770 | ] 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": {}, 775 | "source": [ 776 | "**index( )** is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned." 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 33, 782 | "metadata": {}, 783 | "outputs": [ 784 | { 785 | "data": { 786 | "text/plain": [ 787 | "0" 788 | ] 789 | }, 790 | "execution_count": 33, 791 | "metadata": {}, 792 | "output_type": "execute_result" 793 | } 794 | ], 795 | "source": [ 796 | "lst.index(1)" 797 | ] 798 | }, 799 | { 800 | "cell_type": "markdown", 801 | "metadata": {}, 802 | "source": [ 803 | "**insert(x,y)** is used to insert a element y at a specified index value x. **append( )** function made it only possible to insert at the end. " 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "execution_count": 34, 809 | "metadata": {}, 810 | "outputs": [ 811 | { 812 | "name": "stdout", 813 | "output_type": "stream", 814 | "text": [ 815 | "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" 816 | ] 817 | } 818 | ], 819 | "source": [ 820 | "lst.insert(5, 'name')\n", 821 | "print (lst)" 822 | ] 823 | }, 824 | { 825 | "cell_type": "markdown", 826 | "metadata": {}, 827 | "source": [ 828 | "**insert(x,y)** inserts but does not replace element. If you want to replace the element with another element you simply assign the value to that particular index." 829 | ] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": 35, 834 | "metadata": {}, 835 | "outputs": [ 836 | { 837 | "name": "stdout", 838 | "output_type": "stream", 839 | "text": [ 840 | "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" 841 | ] 842 | } 843 | ], 844 | "source": [ 845 | "lst[5] = 'Python'\n", 846 | "print (lst)" 847 | ] 848 | }, 849 | { 850 | "cell_type": "markdown", 851 | "metadata": {}, 852 | "source": [ 853 | "**pop( )** function return the last element in the list. This is similar to the operation of a stack. Hence it wouldn't be wrong to tell that lists can be used as a stack." 854 | ] 855 | }, 856 | { 857 | "cell_type": "code", 858 | "execution_count": 36, 859 | "metadata": {}, 860 | "outputs": [ 861 | { 862 | "data": { 863 | "text/plain": [ 864 | "8" 865 | ] 866 | }, 867 | "execution_count": 36, 868 | "metadata": {}, 869 | "output_type": "execute_result" 870 | } 871 | ], 872 | "source": [ 873 | "lst.pop()" 874 | ] 875 | }, 876 | { 877 | "cell_type": "markdown", 878 | "metadata": {}, 879 | "source": [ 880 | "Index value can be specified to pop a ceratin element corresponding to that index value." 881 | ] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "execution_count": 37, 886 | "metadata": {}, 887 | "outputs": [ 888 | { 889 | "data": { 890 | "text/plain": [ 891 | "1" 892 | ] 893 | }, 894 | "execution_count": 37, 895 | "metadata": {}, 896 | "output_type": "execute_result" 897 | } 898 | ], 899 | "source": [ 900 | "lst.pop(0)" 901 | ] 902 | }, 903 | { 904 | "cell_type": "markdown", 905 | "metadata": {}, 906 | "source": [ 907 | "**pop( )** is used to remove element based on it's index value which can be assigned to a variable. One can also remove element by specifying the element itself using the **remove( )** function." 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": 38, 913 | "metadata": {}, 914 | "outputs": [ 915 | { 916 | "name": "stdout", 917 | "output_type": "stream", 918 | "text": [ 919 | "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" 920 | ] 921 | } 922 | ], 923 | "source": [ 924 | "lst.remove('Python')\n", 925 | "print (lst)" 926 | ] 927 | }, 928 | { 929 | "cell_type": "markdown", 930 | "metadata": {}, 931 | "source": [ 932 | "Alternative to **remove** function but with using index value is **del**" 933 | ] 934 | }, 935 | { 936 | "cell_type": "code", 937 | "execution_count": 39, 938 | "metadata": {}, 939 | "outputs": [ 940 | { 941 | "name": "stdout", 942 | "output_type": "stream", 943 | "text": [ 944 | "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" 945 | ] 946 | } 947 | ], 948 | "source": [ 949 | "del lst[1]\n", 950 | "print (lst)" 951 | ] 952 | }, 953 | { 954 | "cell_type": "markdown", 955 | "metadata": {}, 956 | "source": [ 957 | "The entire elements present in the list can be reversed by using the **reverse()** function." 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": 40, 963 | "metadata": {}, 964 | "outputs": [ 965 | { 966 | "name": "stdout", 967 | "output_type": "stream", 968 | "text": [ 969 | "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]\n" 970 | ] 971 | } 972 | ], 973 | "source": [ 974 | "lst.reverse()\n", 975 | "print (lst)" 976 | ] 977 | }, 978 | { 979 | "cell_type": "markdown", 980 | "metadata": {}, 981 | "source": [ 982 | "Note that the nested list [5,4,2,8] is treated as a single element of the parent list lst. Thus the elements inside the nested list is not reversed.\n", 983 | "\n", 984 | "Python offers built in operation **sort( )** to arrange the elements in ascending order." 985 | ] 986 | }, 987 | { 988 | "cell_type": "code", 989 | "execution_count": 41, 990 | "metadata": {}, 991 | "outputs": [ 992 | { 993 | "name": "stdout", 994 | "output_type": "stream", 995 | "text": [ 996 | "[-1, 0, 0.1, 2, 3, 5, 6, 24, 45, 90, 221]\n" 997 | ] 998 | } 999 | ], 1000 | "source": [ 1001 | "lst=[5,3,2,0,0.1,-1,6,90,24,45,221]\n", 1002 | "\n", 1003 | "lst.sort()\n", 1004 | "print (lst)" 1005 | ] 1006 | }, 1007 | { 1008 | "cell_type": "markdown", 1009 | "metadata": {}, 1010 | "source": [ 1011 | "For descending order, By default the reverse condition will be False for reverse. Hence changing it to True would arrange the elements in descending order." 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": 42, 1017 | "metadata": {}, 1018 | "outputs": [ 1019 | { 1020 | "name": "stdout", 1021 | "output_type": "stream", 1022 | "text": [ 1023 | "[221, 90, 45, 24, 6, 5, 3, 2, 0.1, 0, -1]\n" 1024 | ] 1025 | } 1026 | ], 1027 | "source": [ 1028 | "lst.sort(reverse=True)\n", 1029 | "print (lst)" 1030 | ] 1031 | }, 1032 | { 1033 | "cell_type": "markdown", 1034 | "metadata": {}, 1035 | "source": [ 1036 | "Similarly for lists containing string elements, **sort( )** would sort the elements based on it's ASCII value in ascending and by specifying reverse=True in descending." 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "code", 1041 | "execution_count": 43, 1042 | "metadata": {}, 1043 | "outputs": [ 1044 | { 1045 | "name": "stdout", 1046 | "output_type": "stream", 1047 | "text": [ 1048 | "['Air', 'Earth', 'Fire', 'Water']\n", 1049 | "['Water', 'Fire', 'Earth', 'Air']\n" 1050 | ] 1051 | } 1052 | ], 1053 | "source": [ 1054 | "names.sort()\n", 1055 | "print (names)\n", 1056 | "names.sort(reverse=True)\n", 1057 | "print (names)" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "markdown", 1062 | "metadata": {}, 1063 | "source": [ 1064 | "To sort based on length key=len should be specified as shown." 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "code", 1069 | "execution_count": 44, 1070 | "metadata": {}, 1071 | "outputs": [ 1072 | { 1073 | "name": "stdout", 1074 | "output_type": "stream", 1075 | "text": [ 1076 | "['Air', 'Fire', 'Water', 'Earth']\n", 1077 | "['Water', 'Earth', 'Fire', 'Air']\n" 1078 | ] 1079 | } 1080 | ], 1081 | "source": [ 1082 | "names.sort(key=len)\n", 1083 | "print (names)\n", 1084 | "names.sort(key=len,reverse=True)\n", 1085 | "print (names)" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "markdown", 1090 | "metadata": {}, 1091 | "source": [ 1092 | "### Copying a list" 1093 | ] 1094 | }, 1095 | { 1096 | "cell_type": "markdown", 1097 | "metadata": {}, 1098 | "source": [ 1099 | "Most of the new python programmers commit this mistake. Consider the following," 1100 | ] 1101 | }, 1102 | { 1103 | "cell_type": "code", 1104 | "execution_count": 45, 1105 | "metadata": {}, 1106 | "outputs": [], 1107 | "source": [ 1108 | "lista= [2,1,4,3]" 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "execution_count": 46, 1114 | "metadata": {}, 1115 | "outputs": [ 1116 | { 1117 | "name": "stdout", 1118 | "output_type": "stream", 1119 | "text": [ 1120 | "[2, 1, 4, 3]\n" 1121 | ] 1122 | } 1123 | ], 1124 | "source": [ 1125 | "listb = lista\n", 1126 | "print (listb)" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "markdown", 1131 | "metadata": {}, 1132 | "source": [ 1133 | "Here, We have declared a list, lista = [2,1,4,3]. This list is copied to listb by assigning it's value and it get's copied as seen. Now we perform some random operations on lista." 1134 | ] 1135 | }, 1136 | { 1137 | "cell_type": "code", 1138 | "execution_count": 47, 1139 | "metadata": {}, 1140 | "outputs": [ 1141 | { 1142 | "name": "stdout", 1143 | "output_type": "stream", 1144 | "text": [ 1145 | "[2, 1, 4]\n", 1146 | "[2, 1, 4, 9]\n" 1147 | ] 1148 | } 1149 | ], 1150 | "source": [ 1151 | "lista.pop()\n", 1152 | "print (lista)\n", 1153 | "lista.append(9)\n", 1154 | "print (lista)" 1155 | ] 1156 | }, 1157 | { 1158 | "cell_type": "code", 1159 | "execution_count": 48, 1160 | "metadata": {}, 1161 | "outputs": [ 1162 | { 1163 | "name": "stdout", 1164 | "output_type": "stream", 1165 | "text": [ 1166 | "[2, 1, 4, 9]\n" 1167 | ] 1168 | } 1169 | ], 1170 | "source": [ 1171 | "print (listb)" 1172 | ] 1173 | }, 1174 | { 1175 | "cell_type": "markdown", 1176 | "metadata": {}, 1177 | "source": [ 1178 | "listb has also changed though no operation has been performed on it. This is because you have assigned the same memory space of lista to listb. So how do fix this?\n", 1179 | "\n", 1180 | "If you recall, in slicing we had seen that parentlist[a:b] returns a list from parent list with start index a and end index b and if a and b is not mentioned then by default it considers the first and last element. We use the same concept here. By doing so, we are assigning the data of lista to listb as a variable." 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "code", 1185 | "execution_count": 49, 1186 | "metadata": {}, 1187 | "outputs": [], 1188 | "source": [ 1189 | "lista = [2,1,4,3]" 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "code", 1194 | "execution_count": 50, 1195 | "metadata": {}, 1196 | "outputs": [ 1197 | { 1198 | "name": "stdout", 1199 | "output_type": "stream", 1200 | "text": [ 1201 | "[2, 1, 4, 3]\n" 1202 | ] 1203 | } 1204 | ], 1205 | "source": [ 1206 | "listb = lista[:]\n", 1207 | "print (listb)" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": 51, 1213 | "metadata": {}, 1214 | "outputs": [ 1215 | { 1216 | "name": "stdout", 1217 | "output_type": "stream", 1218 | "text": [ 1219 | "[2, 1, 4]\n", 1220 | "[2, 1, 4, 9]\n" 1221 | ] 1222 | } 1223 | ], 1224 | "source": [ 1225 | "lista.pop()\n", 1226 | "print (lista)\n", 1227 | "lista.append(9)\n", 1228 | "print (lista)" 1229 | ] 1230 | }, 1231 | { 1232 | "cell_type": "code", 1233 | "execution_count": 52, 1234 | "metadata": {}, 1235 | "outputs": [ 1236 | { 1237 | "name": "stdout", 1238 | "output_type": "stream", 1239 | "text": [ 1240 | "[2, 1, 4, 3]\n" 1241 | ] 1242 | } 1243 | ], 1244 | "source": [ 1245 | "print (listb)" 1246 | ] 1247 | }, 1248 | { 1249 | "cell_type": "markdown", 1250 | "metadata": {}, 1251 | "source": [ 1252 | "##Tuples" 1253 | ] 1254 | }, 1255 | { 1256 | "cell_type": "markdown", 1257 | "metadata": {}, 1258 | "source": [ 1259 | "Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. Think of tuples as something which has to be True for a particular something and cannot be True for no other values. \n", 1260 | "\n", 1261 | "For better understanding, Recall **divmod()** function.\n", 1262 | "\n", 1263 | " If x and y are integers, the return value is\n", 1264 | " (x / y, x % y)\n", 1265 | "\n", 1266 | " If x or y is a float, the result is\n", 1267 | " (q, x % y), where q is the whole part of the quotient." 1268 | ] 1269 | }, 1270 | { 1271 | "cell_type": "code", 1272 | "execution_count": 53, 1273 | "metadata": {}, 1274 | "outputs": [ 1275 | { 1276 | "name": "stdout", 1277 | "output_type": "stream", 1278 | "text": [ 1279 | "(3, 1)\n", 1280 | "\n" 1281 | ] 1282 | } 1283 | ], 1284 | "source": [ 1285 | "xyz = divmod(10,3)\n", 1286 | "print (xyz)\n", 1287 | "print (type(xyz))" 1288 | ] 1289 | }, 1290 | { 1291 | "cell_type": "markdown", 1292 | "metadata": {}, 1293 | "source": [ 1294 | "Here the quotient has to be 3 and the remainder has to be 1. These values cannot be changed whatsoever when 10 is divided by 3. Hence divmod returns these values in a tuple." 1295 | ] 1296 | }, 1297 | { 1298 | "cell_type": "markdown", 1299 | "metadata": {}, 1300 | "source": [ 1301 | "To define a tuple, A variable is assigned to paranthesis ( ) or tuple( )." 1302 | ] 1303 | }, 1304 | { 1305 | "cell_type": "code", 1306 | "execution_count": 54, 1307 | "metadata": {}, 1308 | "outputs": [], 1309 | "source": [ 1310 | "tup = ()\n", 1311 | "tup2 = tuple()" 1312 | ] 1313 | }, 1314 | { 1315 | "cell_type": "markdown", 1316 | "metadata": {}, 1317 | "source": [ 1318 | "If you want to directly declare a tuple it can be done by using a comma at the end of the data." 1319 | ] 1320 | }, 1321 | { 1322 | "cell_type": "code", 1323 | "execution_count": 55, 1324 | "metadata": {}, 1325 | "outputs": [ 1326 | { 1327 | "data": { 1328 | "text/plain": [ 1329 | "(27,)" 1330 | ] 1331 | }, 1332 | "execution_count": 55, 1333 | "metadata": {}, 1334 | "output_type": "execute_result" 1335 | } 1336 | ], 1337 | "source": [ 1338 | "27," 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "markdown", 1343 | "metadata": {}, 1344 | "source": [ 1345 | "27 when multiplied by 2 yields 54, But when multiplied with a tuple the data is repeated twice." 1346 | ] 1347 | }, 1348 | { 1349 | "cell_type": "code", 1350 | "execution_count": 56, 1351 | "metadata": {}, 1352 | "outputs": [ 1353 | { 1354 | "data": { 1355 | "text/plain": [ 1356 | "(27, 27)" 1357 | ] 1358 | }, 1359 | "execution_count": 56, 1360 | "metadata": {}, 1361 | "output_type": "execute_result" 1362 | } 1363 | ], 1364 | "source": [ 1365 | "2*(27,)" 1366 | ] 1367 | }, 1368 | { 1369 | "cell_type": "markdown", 1370 | "metadata": {}, 1371 | "source": [ 1372 | "Values can be assigned while declaring a tuple. It takes a list as input and converts it into a tuple or it takes a string and converts it into a tuple." 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "code", 1377 | "execution_count": 57, 1378 | "metadata": { 1379 | "scrolled": true 1380 | }, 1381 | "outputs": [ 1382 | { 1383 | "name": "stdout", 1384 | "output_type": "stream", 1385 | "text": [ 1386 | "(1, 2, 3)\n", 1387 | "('H', 'e', 'l', 'l', 'o')\n" 1388 | ] 1389 | } 1390 | ], 1391 | "source": [ 1392 | "tup3 = tuple([1,2,3])\n", 1393 | "print (tup3)\n", 1394 | "tup4 = tuple('Hello')\n", 1395 | "print (tup4)" 1396 | ] 1397 | }, 1398 | { 1399 | "cell_type": "markdown", 1400 | "metadata": {}, 1401 | "source": [ 1402 | "It follows the same indexing and slicing as Lists." 1403 | ] 1404 | }, 1405 | { 1406 | "cell_type": "code", 1407 | "execution_count": 58, 1408 | "metadata": {}, 1409 | "outputs": [ 1410 | { 1411 | "name": "stdout", 1412 | "output_type": "stream", 1413 | "text": [ 1414 | "2\n", 1415 | "('H', 'e', 'l')\n" 1416 | ] 1417 | } 1418 | ], 1419 | "source": [ 1420 | "print (tup3[1])\n", 1421 | "tup5 = tup4[:3]\n", 1422 | "print (tup5)" 1423 | ] 1424 | }, 1425 | { 1426 | "cell_type": "markdown", 1427 | "metadata": {}, 1428 | "source": [ 1429 | "### Mapping one tuple to another" 1430 | ] 1431 | }, 1432 | { 1433 | "cell_type": "code", 1434 | "execution_count": 59, 1435 | "metadata": {}, 1436 | "outputs": [], 1437 | "source": [ 1438 | "(a,b,c)= ('alpha','beta','gamma')" 1439 | ] 1440 | }, 1441 | { 1442 | "cell_type": "code", 1443 | "execution_count": 60, 1444 | "metadata": {}, 1445 | "outputs": [ 1446 | { 1447 | "name": "stdout", 1448 | "output_type": "stream", 1449 | "text": [ 1450 | "alpha beta gamma\n" 1451 | ] 1452 | } 1453 | ], 1454 | "source": [ 1455 | "print (a,b,c)" 1456 | ] 1457 | }, 1458 | { 1459 | "cell_type": "code", 1460 | "execution_count": 61, 1461 | "metadata": {}, 1462 | "outputs": [ 1463 | { 1464 | "name": "stdout", 1465 | "output_type": "stream", 1466 | "text": [ 1467 | "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n" 1468 | ] 1469 | } 1470 | ], 1471 | "source": [ 1472 | "d = tuple('RajathKumarMP')\n", 1473 | "print (d)" 1474 | ] 1475 | }, 1476 | { 1477 | "cell_type": "markdown", 1478 | "metadata": {}, 1479 | "source": [ 1480 | "### Built In Tuple functions" 1481 | ] 1482 | }, 1483 | { 1484 | "cell_type": "markdown", 1485 | "metadata": {}, 1486 | "source": [ 1487 | "**count()** function counts the number of specified element that is present in the tuple." 1488 | ] 1489 | }, 1490 | { 1491 | "cell_type": "code", 1492 | "execution_count": 62, 1493 | "metadata": {}, 1494 | "outputs": [ 1495 | { 1496 | "data": { 1497 | "text/plain": [ 1498 | "3" 1499 | ] 1500 | }, 1501 | "execution_count": 62, 1502 | "metadata": {}, 1503 | "output_type": "execute_result" 1504 | } 1505 | ], 1506 | "source": [ 1507 | "d.count('a')" 1508 | ] 1509 | }, 1510 | { 1511 | "cell_type": "markdown", 1512 | "metadata": {}, 1513 | "source": [ 1514 | "**index()** function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned" 1515 | ] 1516 | }, 1517 | { 1518 | "cell_type": "code", 1519 | "execution_count": 63, 1520 | "metadata": {}, 1521 | "outputs": [ 1522 | { 1523 | "data": { 1524 | "text/plain": [ 1525 | "1" 1526 | ] 1527 | }, 1528 | "execution_count": 63, 1529 | "metadata": {}, 1530 | "output_type": "execute_result" 1531 | } 1532 | ], 1533 | "source": [ 1534 | "d.index('a')" 1535 | ] 1536 | }, 1537 | { 1538 | "cell_type": "markdown", 1539 | "metadata": {}, 1540 | "source": [ 1541 | "##Sets" 1542 | ] 1543 | }, 1544 | { 1545 | "cell_type": "markdown", 1546 | "metadata": {}, 1547 | "source": [ 1548 | "Sets are mainly used to eliminate repeated numbers in a sequence/list. It is also used to perform some standard set operations.\n", 1549 | "\n", 1550 | "Sets are declared as set() which will initialize a empty set. Also set([sequence]) can be executed to declare a set with elements" 1551 | ] 1552 | }, 1553 | { 1554 | "cell_type": "code", 1555 | "execution_count": 64, 1556 | "metadata": {}, 1557 | "outputs": [ 1558 | { 1559 | "name": "stdout", 1560 | "output_type": "stream", 1561 | "text": [ 1562 | "\n" 1563 | ] 1564 | } 1565 | ], 1566 | "source": [ 1567 | "set1 = set()\n", 1568 | "print (type(set1))" 1569 | ] 1570 | }, 1571 | { 1572 | "cell_type": "code", 1573 | "execution_count": 65, 1574 | "metadata": {}, 1575 | "outputs": [ 1576 | { 1577 | "name": "stdout", 1578 | "output_type": "stream", 1579 | "text": [ 1580 | "{1, 2, 3, 4}\n" 1581 | ] 1582 | } 1583 | ], 1584 | "source": [ 1585 | "set0 = set([1,2,2,3,3,4])\n", 1586 | "print (set0)" 1587 | ] 1588 | }, 1589 | { 1590 | "cell_type": "markdown", 1591 | "metadata": {}, 1592 | "source": [ 1593 | "elements 2,3 which are repeated twice are seen only once. Thus in a set each element is distinct." 1594 | ] 1595 | }, 1596 | { 1597 | "cell_type": "markdown", 1598 | "metadata": {}, 1599 | "source": [ 1600 | "###Built-in Functions" 1601 | ] 1602 | }, 1603 | { 1604 | "cell_type": "code", 1605 | "execution_count": 66, 1606 | "metadata": {}, 1607 | "outputs": [], 1608 | "source": [ 1609 | "set1 = set([1,2,3])" 1610 | ] 1611 | }, 1612 | { 1613 | "cell_type": "code", 1614 | "execution_count": 67, 1615 | "metadata": {}, 1616 | "outputs": [], 1617 | "source": [ 1618 | "set2 = set([2,3,4,5])" 1619 | ] 1620 | }, 1621 | { 1622 | "cell_type": "markdown", 1623 | "metadata": {}, 1624 | "source": [ 1625 | "**union( )** function returns a set which contains all the elements of both the sets without repition." 1626 | ] 1627 | }, 1628 | { 1629 | "cell_type": "code", 1630 | "execution_count": 68, 1631 | "metadata": {}, 1632 | "outputs": [ 1633 | { 1634 | "data": { 1635 | "text/plain": [ 1636 | "{1, 2, 3, 4, 5}" 1637 | ] 1638 | }, 1639 | "execution_count": 68, 1640 | "metadata": {}, 1641 | "output_type": "execute_result" 1642 | } 1643 | ], 1644 | "source": [ 1645 | "set1.union(set2)" 1646 | ] 1647 | }, 1648 | { 1649 | "cell_type": "markdown", 1650 | "metadata": {}, 1651 | "source": [ 1652 | "**add( )** will add a particular element into the set. Note that the index of the newly added element is arbitrary and can be placed anywhere not neccessarily in the end." 1653 | ] 1654 | }, 1655 | { 1656 | "cell_type": "code", 1657 | "execution_count": 69, 1658 | "metadata": {}, 1659 | "outputs": [ 1660 | { 1661 | "data": { 1662 | "text/plain": [ 1663 | "{0, 1, 2, 3}" 1664 | ] 1665 | }, 1666 | "execution_count": 69, 1667 | "metadata": {}, 1668 | "output_type": "execute_result" 1669 | } 1670 | ], 1671 | "source": [ 1672 | "set1.add(0)\n", 1673 | "set1" 1674 | ] 1675 | }, 1676 | { 1677 | "cell_type": "markdown", 1678 | "metadata": {}, 1679 | "source": [ 1680 | "**intersection( )** function outputs a set which contains all the elements that are in both sets." 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "code", 1685 | "execution_count": 70, 1686 | "metadata": {}, 1687 | "outputs": [ 1688 | { 1689 | "data": { 1690 | "text/plain": [ 1691 | "{2, 3}" 1692 | ] 1693 | }, 1694 | "execution_count": 70, 1695 | "metadata": {}, 1696 | "output_type": "execute_result" 1697 | } 1698 | ], 1699 | "source": [ 1700 | "set1.intersection(set2)" 1701 | ] 1702 | }, 1703 | { 1704 | "cell_type": "markdown", 1705 | "metadata": {}, 1706 | "source": [ 1707 | "**difference( )** function ouptuts a set which contains elements that are in set1 and not in set2." 1708 | ] 1709 | }, 1710 | { 1711 | "cell_type": "code", 1712 | "execution_count": 71, 1713 | "metadata": {}, 1714 | "outputs": [ 1715 | { 1716 | "data": { 1717 | "text/plain": [ 1718 | "{0, 1}" 1719 | ] 1720 | }, 1721 | "execution_count": 71, 1722 | "metadata": {}, 1723 | "output_type": "execute_result" 1724 | } 1725 | ], 1726 | "source": [ 1727 | "set1.difference(set2)" 1728 | ] 1729 | }, 1730 | { 1731 | "cell_type": "markdown", 1732 | "metadata": {}, 1733 | "source": [ 1734 | "**symmetric_difference( )** function ouputs a function which contains elements that are in one of the sets." 1735 | ] 1736 | }, 1737 | { 1738 | "cell_type": "code", 1739 | "execution_count": 72, 1740 | "metadata": {}, 1741 | "outputs": [ 1742 | { 1743 | "data": { 1744 | "text/plain": [ 1745 | "{0, 1, 4, 5}" 1746 | ] 1747 | }, 1748 | "execution_count": 72, 1749 | "metadata": {}, 1750 | "output_type": "execute_result" 1751 | } 1752 | ], 1753 | "source": [ 1754 | "set2.symmetric_difference(set1)" 1755 | ] 1756 | }, 1757 | { 1758 | "cell_type": "markdown", 1759 | "metadata": {}, 1760 | "source": [ 1761 | "**issubset( ), isdisjoint( ), issuperset( )** is used to check if the set1/set2 is a subset, disjoint or superset of set2/set1 respectively." 1762 | ] 1763 | }, 1764 | { 1765 | "cell_type": "code", 1766 | "execution_count": 73, 1767 | "metadata": {}, 1768 | "outputs": [ 1769 | { 1770 | "data": { 1771 | "text/plain": [ 1772 | "False" 1773 | ] 1774 | }, 1775 | "execution_count": 73, 1776 | "metadata": {}, 1777 | "output_type": "execute_result" 1778 | } 1779 | ], 1780 | "source": [ 1781 | "set1.issubset(set2)" 1782 | ] 1783 | }, 1784 | { 1785 | "cell_type": "code", 1786 | "execution_count": 74, 1787 | "metadata": {}, 1788 | "outputs": [ 1789 | { 1790 | "data": { 1791 | "text/plain": [ 1792 | "False" 1793 | ] 1794 | }, 1795 | "execution_count": 74, 1796 | "metadata": {}, 1797 | "output_type": "execute_result" 1798 | } 1799 | ], 1800 | "source": [ 1801 | "set2.isdisjoint(set1)" 1802 | ] 1803 | }, 1804 | { 1805 | "cell_type": "code", 1806 | "execution_count": 75, 1807 | "metadata": {}, 1808 | "outputs": [ 1809 | { 1810 | "data": { 1811 | "text/plain": [ 1812 | "False" 1813 | ] 1814 | }, 1815 | "execution_count": 75, 1816 | "metadata": {}, 1817 | "output_type": "execute_result" 1818 | } 1819 | ], 1820 | "source": [ 1821 | "set2.issuperset(set1)" 1822 | ] 1823 | }, 1824 | { 1825 | "cell_type": "markdown", 1826 | "metadata": {}, 1827 | "source": [ 1828 | "**pop( )** is used to remove an arbitrary element in the set" 1829 | ] 1830 | }, 1831 | { 1832 | "cell_type": "code", 1833 | "execution_count": 76, 1834 | "metadata": {}, 1835 | "outputs": [ 1836 | { 1837 | "name": "stdout", 1838 | "output_type": "stream", 1839 | "text": [ 1840 | "{1, 2, 3}\n" 1841 | ] 1842 | } 1843 | ], 1844 | "source": [ 1845 | "set1.pop()\n", 1846 | "print (set1)" 1847 | ] 1848 | }, 1849 | { 1850 | "cell_type": "markdown", 1851 | "metadata": {}, 1852 | "source": [ 1853 | "**remove( )** function deletes the specified element from the set." 1854 | ] 1855 | }, 1856 | { 1857 | "cell_type": "code", 1858 | "execution_count": 77, 1859 | "metadata": {}, 1860 | "outputs": [ 1861 | { 1862 | "data": { 1863 | "text/plain": [ 1864 | "{1, 3}" 1865 | ] 1866 | }, 1867 | "execution_count": 77, 1868 | "metadata": {}, 1869 | "output_type": "execute_result" 1870 | } 1871 | ], 1872 | "source": [ 1873 | "set1.remove(2)\n", 1874 | "set1" 1875 | ] 1876 | }, 1877 | { 1878 | "cell_type": "markdown", 1879 | "metadata": {}, 1880 | "source": [ 1881 | "**clear( )** is used to clear all the elements and make that set an empty set." 1882 | ] 1883 | }, 1884 | { 1885 | "cell_type": "code", 1886 | "execution_count": 78, 1887 | "metadata": {}, 1888 | "outputs": [ 1889 | { 1890 | "data": { 1891 | "text/plain": [ 1892 | "set()" 1893 | ] 1894 | }, 1895 | "execution_count": 78, 1896 | "metadata": {}, 1897 | "output_type": "execute_result" 1898 | } 1899 | ], 1900 | "source": [ 1901 | "set1.clear()\n", 1902 | "set1" 1903 | ] 1904 | }, 1905 | { 1906 | "cell_type": "code", 1907 | "execution_count": null, 1908 | "metadata": {}, 1909 | "outputs": [], 1910 | "source": [] 1911 | } 1912 | ], 1913 | "metadata": { 1914 | "kernelspec": { 1915 | "display_name": "Python 3", 1916 | "language": "python", 1917 | "name": "python3" 1918 | }, 1919 | "language_info": { 1920 | "codemirror_mode": { 1921 | "name": "ipython", 1922 | "version": 3 1923 | }, 1924 | "file_extension": ".py", 1925 | "mimetype": "text/x-python", 1926 | "name": "python", 1927 | "nbconvert_exporter": "python", 1928 | "pygments_lexer": "ipython3", 1929 | "version": "3.7.3" 1930 | } 1931 | }, 1932 | "nbformat": 4, 1933 | "nbformat_minor": 1 1934 | } 1935 | --------------------------------------------------------------------------------