├── README.md ├── lecture_1.ipynb ├── lecture_2.ipynb ├── lecture_3.ipynb ├── lecture_4.ipynb ├── lecture_5.ipynb ├── lecture_6.ipynb ├── lecture_7.ipynb └── lecture_8.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Python_Fundamentals 2 | 3 | Lecture 01: Introduction to Python 4 | 5 | - Familiarity with Anaconda, jupyter notebook, and Github 6 | - Learn logical thinking by playing blockly games: Maze 7 | - Learn about Python data types, Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators 8 | 9 | 10 | Lecture 02: String Manipulation and Python Lists 11 | 12 | - Logical and membership operators in Python 13 | - Learn if-elseif-else condition by playing the Blockly games: Bird 14 | - Learn strig manipulation 15 | - Python List, slicing, changing, adding and removing list elements 16 | 17 | Lecture 03: Python data structure 18 | 19 | - Solution discussion of HW1: blockly games Maze and HW2 blockly games Bird and Turtle 20 | - Python Data Structure: List, tuple, dictionary, set 21 | 22 | Lecture 04: Conditional logic, loop and writing functions in Python 23 | 24 | - How to write if-else, while and for loop in Python 25 | - Writing functions in Python 26 | - LeetCode problems 27 | - https://leetcode.com/problems/defanging-an-ip-address/ 28 | - https://leetcode.com/problems/goal-parser-interpretation/ 29 | - https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/ 30 | 31 | Lecture 5: LeetCode problems 32 | 33 | - https://leetcode.com/problems/maximum-product-of-three-numbers/ 34 | - https://leetcode.com/problems/richest-customer-wealth/ 35 | - https://leetcode.com/problems/shuffle-the-array/ 36 | - https://leetcode.com/problems/valid-perfect-square/ 37 | 38 | Lecture 06: Application of Math functions, list, set and dictionary 39 | 40 | - Math functions 41 | - Application of list, set, and dictionary 42 | - Solving same problem in multiple ways 43 | - https://leetcode.com/problems/power-of-two/ 44 | - https://leetcode.com/problems/contains-duplicate/ 45 | - https://leetcode.com/problems/two-sum/submissions/ 46 | - https://leetcode.com/problems/roman-to-integer/ 47 | - https://leetcode.com/problems/unique-morse-code-words/ 48 | 49 | Lecture 07: Mathematical operations in Python: NumPy 50 | 51 | - Comparison between List and NumPy array 52 | - 1D and 2D NumPy array 53 | - Mathematical and Matrix operation in NumPy array 54 | - NumPy array slicing and filtering 55 | 56 | Lecture 08: Data Analysis in Python: Pandas 57 | 58 | - How to look at the data? 59 | - Good data or bad data? 60 | - Data Statistics 61 | -------------------------------------------------------------------------------- /lecture_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 01: Introduction to Python\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. Familiarity with Anaconda, jupyter notebook, and Github\n", 16 | "2. Learn logical thinking by playing blockly games: Maze\n", 17 | "3. Learn about Python data types, Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators\n", 18 | "\n", 19 | "#### References: \n", 20 | "1. Version 3.7 (preferred) or higher: https://www.python.org/downloads/%7B%7B6%7D%7D\n", 21 | "2. Download Anaconda installer: https://www.anaconda.com/products/individual\n", 22 | "3. How to Install Anaconda (Python) on Windows 10:\n", 23 | "https://www.youtube.com/watch?v=syijLJ3oQzU\n", 24 | "4. Easily Install Anaconda Python on MacOS:\n", 25 | "https://www.youtube.com/watch?v=V6ZAv7hBH6Y&ab_channel=JustUnderstandingDataJustUnderstandingData\n", 26 | "5. https://blockly.games/\n", 27 | "6. Python - Basic Operators: https://www.tutorialspoint.com/python/python_basic_operators.htm\n", 28 | "7. Data Science workshop: https://github.com/sadat1971/Conducting-Workshop-DATA-SCIENCE/tree/master/lecture_tutorials\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "- Q1: The sum of two even numbers will be always odd. True or **False**\n", 36 | "\n", 37 | "
The sum of two even numbers will be always even.\n", 38 | "
The sum of two odd numbers will be always even.\n", 39 | "
The sum of one even number and one odd number will be always odd\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 34, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "4" 51 | ] 52 | }, 53 | "execution_count": 34, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "# integer\n", 60 | "# পূর্ণসংখ্যা \n", 61 | "# কমেন্ট\n", 62 | "2 + 2 " 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 35, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "14" 74 | ] 75 | }, 76 | "execution_count": 35, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "4 + 10" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 36, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "6" 94 | ] 95 | }, 96 | "execution_count": 36, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "3 + 3" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 37, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "5" 114 | ] 115 | }, 116 | "execution_count": 37, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "2 + 3" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "- Q2: A positive value is always greater than a negative value. **True** or False\n" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 38, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "True" 141 | ] 142 | }, 143 | "execution_count": 38, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "1 > -1" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 39, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "False" 161 | ] 162 | }, 163 | "execution_count": 39, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "-9 > 1" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "- Q3: Can you divide a number by zero? Yes or **No**\n" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 40, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [ 185 | "# 0/0" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "- Q4. If x is less than 1 and y is greater than 1, then x/y is less than 1. True or False\n" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 41, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "True" 204 | ] 205 | }, 206 | "execution_count": 41, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "# 0.5 float \n", 213 | "x = 0.5\n", 214 | "y = 2\n", 215 | "\n", 216 | "x/y < 1" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "- Q5. The word 'Google' contains 5 unique characters. **True** or False\n" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 42, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "29" 235 | ] 236 | }, 237 | "execution_count": 42, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "# \"Google\", 'Google'\n", 244 | "\n", 245 | "#len(\"Google\")\n", 246 | "len(\"My phone number is 2082222 !&\")" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 43, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "True" 258 | ] 259 | }, 260 | "execution_count": 43, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "1 == 1" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 44, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/plain": [ 277 | "False" 278 | ] 279 | }, 280 | "execution_count": 44, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [ 286 | "2 == 3" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 45, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "data": { 296 | "text/plain": [ 297 | "False" 298 | ] 299 | }, 300 | "execution_count": 45, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "52 == 53" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 46, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "data": { 316 | "text/plain": [ 317 | "False" 318 | ] 319 | }, 320 | "execution_count": 46, 321 | "metadata": {}, 322 | "output_type": "execute_result" 323 | } 324 | ], 325 | "source": [ 326 | "\"G\" == \"g\"" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 47, 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "data": { 336 | "text/plain": [ 337 | "True" 338 | ] 339 | }, 340 | "execution_count": 47, 341 | "metadata": {}, 342 | "output_type": "execute_result" 343 | } 344 | ], 345 | "source": [ 346 | "\"G\" == \"G\"" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "- Q6. If x>1 and y>1, then x multiplied by y is greater than 1. **True** or False\n" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 48, 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "data": { 363 | "text/plain": [ 364 | "True" 365 | ] 366 | }, 367 | "execution_count": 48, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "x = 100\n", 374 | "y = 10\n", 375 | "\n", 376 | "x*y > 1" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "- Q7. The product of two odd numbers and one even number will always be an even number. **True** or False\n" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 49, 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "30" 395 | ] 396 | }, 397 | "execution_count": 49, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "3*5*2" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 50, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "data": { 413 | "text/plain": [ 414 | "28" 415 | ] 416 | }, 417 | "execution_count": 50, 418 | "metadata": {}, 419 | "output_type": "execute_result" 420 | } 421 | ], 422 | "source": [ 423 | "7*1*4" 424 | ] 425 | }, 426 | { 427 | "cell_type": "markdown", 428 | "metadata": {}, 429 | "source": [ 430 | "- Q8. Set A = {1, 3, 5}, Set B = {1, 2, 3}; Set A intersection Set B = {1, 2, 3, 5}. True or **False**\n" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 51, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "data": { 440 | "text/plain": [ 441 | "{1, 3}" 442 | ] 443 | }, 444 | "execution_count": 51, 445 | "metadata": {}, 446 | "output_type": "execute_result" 447 | } 448 | ], 449 | "source": [ 450 | "a = {1,3,5}\n", 451 | "b = {1,2,3}\n", 452 | "\n", 453 | "a.intersection(b)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "- Q9. A = [1,2,3,1,2,3,12,3], the number of unique integers present in A are 4. **True** or False\n" 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "metadata": {}, 466 | "source": [ 467 | "- Q10. 1 is not equal to 1.00. True or **False**" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 52, 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "data": { 477 | "text/plain": [ 478 | "True" 479 | ] 480 | }, 481 | "execution_count": 52, 482 | "metadata": {}, 483 | "output_type": "execute_result" 484 | } 485 | ], 486 | "source": [ 487 | "1 == 1.00" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 53, 493 | "metadata": {}, 494 | "outputs": [], 495 | "source": [ 496 | "a = True\n", 497 | "b = False" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "### Python Data Types\n", 505 | "
**str** : string, text\n", 506 | "
**int** : inetger numbers\n", 507 | "
**float** : real numbers, decimal values\n", 508 | "
**bool** : True, False" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 54, 514 | "metadata": {}, 515 | "outputs": [ 516 | { 517 | "name": "stdout", 518 | "output_type": "stream", 519 | "text": [ 520 | "name : Lionel Andres Messi\n", 521 | "birth_year : 1987\n", 522 | "height : 1.7\n", 523 | "no_1_player : True\n" 524 | ] 525 | } 526 | ], 527 | "source": [ 528 | "name = 'Lionel Andres Messi'\n", 529 | "print(\"name : \", name)\n", 530 | "#print('type of name is', type(name))\n", 531 | "\n", 532 | "birth_year = 1987\n", 533 | "print(\"birth_year : \", birth_year)\n", 534 | "#print('type of birth_year is', type(birth_year))\n", 535 | "\n", 536 | "height = 1.7\n", 537 | "print(\"height : \", height)\n", 538 | "#print('type of height is', type(height))\n", 539 | "\n", 540 | "no_1_player = True\n", 541 | "print(\"no_1_player : \", no_1_player)\n", 542 | "#print('type of no_1_player is', type(no_1_player))" 543 | ] 544 | }, 545 | { 546 | "cell_type": "markdown", 547 | "metadata": {}, 548 | "source": [ 549 | "### Mathematical operations" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 55, 555 | "metadata": {}, 556 | "outputs": [ 557 | { 558 | "name": "stdout", 559 | "output_type": "stream", 560 | "text": [ 561 | "a+b = 5\n", 562 | "a*b = 6\n", 563 | "a-b = -1\n", 564 | "a%b = 2\n", 565 | "a/b = 0.6666666666666666\n", 566 | "a//b = 0\n", 567 | "a**b = 8\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "a = 2 #integer\n", 573 | "b = 3 #integer \n", 574 | "\n", 575 | "# Addition\n", 576 | "print(\"a+b = \", a+b) \n", 577 | "\n", 578 | "# Multiplication\n", 579 | "print(\"a*b = \", a*b) \n", 580 | "\n", 581 | "# Subtraction\n", 582 | "print(\"a-b = \", a-b) \n", 583 | "\n", 584 | "# Modulus: Divides left hand operand by right hand operand and returns remainder\n", 585 | "print(\"a%b = \", a%b) \n", 586 | "\n", 587 | "# Division\n", 588 | "print(\"a/b = \", a/b)\n", 589 | "\n", 590 | "# Floor Division - The division of operands \n", 591 | "# where the result is the quotient in which \n", 592 | "# the digits after the decimal point are removed. \n", 593 | "# But if one of the operands is negative, the result is floored, \n", 594 | "# i.e., rounded away from zero (towards negative infinity)\n", 595 | "print(\"a//b = \", a//b) \n", 596 | "\n", 597 | "# Exponent: Performs exponential (power) calculation on operators\n", 598 | "print(\"a**b = \", a**b) " 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": {}, 604 | "source": [ 605 | "### Comparison Operators" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 56, 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "a == b : False\n", 618 | "a != b : True\n", 619 | "a >= b : False\n", 620 | "a <= b : True\n", 621 | "a > b : False\n", 622 | "a < b : True\n" 623 | ] 624 | } 625 | ], 626 | "source": [ 627 | "a = -1\n", 628 | "b = 1\n", 629 | "\n", 630 | "print(\"a == b : \", a == b)\n", 631 | "print(\"a != b : \", a != b)\n", 632 | "print(\"a >= b : \", a >= b)\n", 633 | "print(\"a <= b : \", a <= b)\n", 634 | "print(\"a > b : \", a > b)\n", 635 | "print(\"a < b : \", a < b)" 636 | ] 637 | }, 638 | { 639 | "cell_type": "code", 640 | "execution_count": null, 641 | "metadata": {}, 642 | "outputs": [], 643 | "source": [] 644 | } 645 | ], 646 | "metadata": { 647 | "kernelspec": { 648 | "display_name": "Python 3", 649 | "language": "python", 650 | "name": "python3" 651 | }, 652 | "language_info": { 653 | "codemirror_mode": { 654 | "name": "ipython", 655 | "version": 3 656 | }, 657 | "file_extension": ".py", 658 | "mimetype": "text/x-python", 659 | "name": "python", 660 | "nbconvert_exporter": "python", 661 | "pygments_lexer": "ipython3", 662 | "version": "3.7.6" 663 | } 664 | }, 665 | "nbformat": 4, 666 | "nbformat_minor": 4 667 | } 668 | -------------------------------------------------------------------------------- /lecture_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 02: String Manipulation and Python Lists\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. Logical and membership operators in Python\n", 16 | "2. Learn if-elseif-else condition by playing the Blockly games: Bird\n", 17 | "3. Learn strig manipulation\n", 18 | "4. Python List, slicing, changing, adding and removing list elements\n", 19 | "\n", 20 | "#### References: \n", 21 | "1. https://blockly.games/\n", 22 | "2. Python - Strings: https://www.tutorialspoint.com/python/python_strings.htm\n", 23 | "3. Data Science workshop: https://github.com/sadat1971/Conducting-Workshop-DATA-SCIENCE/tree/master/lecture_tutorials\n", 24 | "4. Python String: https://www.geeksforgeeks.org/python-strings/\n", 25 | "5. Python List: https://www.w3schools.com/python/python_lists.asp\n", 26 | "\n", 27 | "Best site to read articles on Data Science: (only 5 articles per month are free)\n", 28 | "\n", 29 | "6. medium.com (require paid subscription)\n", 30 | "7. towardsdatascience.com (require paid subscription)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 78, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Player name : Lionel Andres Messi\n", 43 | "\n", 44 | "Birth Year : 1987\n", 45 | "\n", 46 | "Height : 5.7\n", 47 | "\n", 48 | "Is he the greatest of all time? True\n", 49 | "\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "name = \"Lionel Andres Messi\"\n", 55 | "birth_year = 1987\n", 56 | "height = 5.7\n", 57 | "is_he_a_goat = True\n", 58 | "\n", 59 | "print(\"Player name : \", name)\n", 60 | "print(type(name))\n", 61 | "\n", 62 | "print(\"Birth Year : \", birth_year)\n", 63 | "print(type(birth_year))\n", 64 | "\n", 65 | "print(\"Height : \", height)\n", 66 | "print(type(height))\n", 67 | "\n", 68 | "print(\"Is he the greatest of all time? \", is_he_a_goat)\n", 69 | "print(type(is_he_a_goat))" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "### Logical operators" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 79, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "a == b and c == d : True\n", 89 | "a == b or c == d : True\n", 90 | "not c == d : False\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "a = 1\n", 96 | "b = 1\n", 97 | "c = 2\n", 98 | "d = 2\n", 99 | "\n", 100 | "print(\"a == b and c == d : \", a == b and c == d)\n", 101 | "print(\"a == b or c == d : \", a == b or c == d)\n", 102 | "print(\"not c == d : \", not c == d)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 80, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "True\n", 115 | "False\n", 116 | "False\n", 117 | "False\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "print(True and True)\n", 123 | "print(False and True)\n", 124 | "print(True and False)\n", 125 | "print(False and False)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 81, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "True\n", 138 | "True\n", 139 | "True\n", 140 | "False\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "print(True or True)\n", 146 | "print(False or True)\n", 147 | "print(True or False)\n", 148 | "print(False or False)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "### Membership operators" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 82, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "a in b : True\n", 168 | "a not in b : False\n" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "a = \"abul\"\n", 174 | "b = \"babul\"\n", 175 | "\n", 176 | "print(\"a in b : \", a in b)\n", 177 | "print(\"a not in b : \", a not in b)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 83, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [ 186 | "string = \"rOnaldo \"" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "- How to capitalize the first character of a string?" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 84, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "Ronaldo \n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "str_capitalize = string.capitalize()\n", 211 | "print(str_capitalize)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "- How to find the frequency of the substring in a string?" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 85, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "1\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "str_count = string.count(\"a\")\n", 236 | "print(str_count)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "- How to find if a string starts or ends with a specific substring?" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 86, 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "name": "stdout", 253 | "output_type": "stream", 254 | "text": [ 255 | "True\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "str_startswith = string.startswith(\"r\")\n", 261 | "print(str_startswith)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 87, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "False\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "str_endswith = string.endswith(\"o\")\n", 279 | "print(str_endswith)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "- How to find the index of a specific character in a string?" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 88, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "3\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "str_find = string.find(\"a\")\n", 304 | "print(str_find)" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 89, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "name": "stdout", 314 | "output_type": "stream", 315 | "text": [ 316 | "0\n" 317 | ] 318 | } 319 | ], 320 | "source": [ 321 | "str_index = string.index(\"r\")\n", 322 | "print(str_index)" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "- How to convert a string into lower case and upper case characters?" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 90, 335 | "metadata": {}, 336 | "outputs": [ 337 | { 338 | "name": "stdout", 339 | "output_type": "stream", 340 | "text": [ 341 | "ronaldo \n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "str_lower = string.lower()\n", 347 | "print(str_lower)" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 91, 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "name": "stdout", 357 | "output_type": "stream", 358 | "text": [ 359 | "RONALDO \n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "str_upper = string.upper()\n", 365 | "print(str_upper)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 92, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [ 374 | "string = \" Ronaldo\"" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "- How to remove spaces from a string?" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 93, 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "name": "stdout", 391 | "output_type": "stream", 392 | "text": [ 393 | "Ronaldo\n" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "str_lstrip = string.lstrip()\n", 399 | "print(str_lstrip)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 94, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [ 408 | "string = \"ronaldo \"" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 95, 414 | "metadata": {}, 415 | "outputs": [ 416 | { 417 | "name": "stdout", 418 | "output_type": "stream", 419 | "text": [ 420 | "ronaldo\n" 421 | ] 422 | } 423 | ], 424 | "source": [ 425 | "str_rstrip = string.rstrip()\n", 426 | "print(str_rstrip)" 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "metadata": {}, 432 | "source": [ 433 | "- How to replace a substring inside a string?" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 96, 439 | "metadata": {}, 440 | "outputs": [ 441 | { 442 | "name": "stdout", 443 | "output_type": "stream", 444 | "text": [ 445 | "ronbldo \n" 446 | ] 447 | } 448 | ], 449 | "source": [ 450 | "str_replace = string.replace(\"a\", \"b\")\n", 451 | "print(str_replace)" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 97, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [ 460 | "string = \"SakibAlHasan100\"" 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "metadata": {}, 466 | "source": [ 467 | "- How to find the presence of an alphanumeric character, alphabet or digit?" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 98, 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "name": "stdout", 477 | "output_type": "stream", 478 | "text": [ 479 | "True\n" 480 | ] 481 | } 482 | ], 483 | "source": [ 484 | "str_isalnum = string.isalnum()\n", 485 | "print(str_isalnum)" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 99, 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "name": "stdout", 495 | "output_type": "stream", 496 | "text": [ 497 | "False\n" 498 | ] 499 | } 500 | ], 501 | "source": [ 502 | "str_isalpha = string.isalpha()\n", 503 | "print(str_isalpha)" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 100, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "name": "stdout", 513 | "output_type": "stream", 514 | "text": [ 515 | "False\n" 516 | ] 517 | } 518 | ], 519 | "source": [ 520 | "str_isdigit = string.isdigit()\n", 521 | "print(str_isdigit)" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 101, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "name": "stdout", 531 | "output_type": "stream", 532 | "text": [ 533 | "False\n" 534 | ] 535 | } 536 | ], 537 | "source": [ 538 | "str_islower = string.islower()\n", 539 | "print(str_islower)" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 102, 545 | "metadata": {}, 546 | "outputs": [ 547 | { 548 | "name": "stdout", 549 | "output_type": "stream", 550 | "text": [ 551 | "False\n" 552 | ] 553 | } 554 | ], 555 | "source": [ 556 | "str_isspace = string.isspace()\n", 557 | "print(str_isspace)" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 103, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "name": "stdout", 567 | "output_type": "stream", 568 | "text": [ 569 | "False\n" 570 | ] 571 | } 572 | ], 573 | "source": [ 574 | "str_isupper = string.isupper()\n", 575 | "print(str_isupper)" 576 | ] 577 | }, 578 | { 579 | "cell_type": "markdown", 580 | "metadata": {}, 581 | "source": [ 582 | "### String slicing" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 104, 588 | "metadata": {}, 589 | "outputs": [ 590 | { 591 | "name": "stdout", 592 | "output_type": "stream", 593 | "text": [ 594 | "I\n", 595 | "h\n", 596 | "I live \n", 597 | "in Bangladesh\n", 598 | "I live \n", 599 | "in Banglades\n" 600 | ] 601 | } 602 | ], 603 | "source": [ 604 | "string = \"I live in Bangladesh\"\n", 605 | "\n", 606 | "print(string[0])\n", 607 | "print(string[-1])\n", 608 | "print(string[:7])\n", 609 | "print(string[7:])\n", 610 | "print(string[0:7])\n", 611 | "print(string[7:-1])" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": 105, 617 | "metadata": {}, 618 | "outputs": [ 619 | { 620 | "name": "stdout", 621 | "output_type": "stream", 622 | "text": [ 623 | "['abbu', 1.73, 'ammu', 1.68, 'vaia', 1.9, 'apu', 1.79]\n", 624 | "abbu\n", 625 | "1.73\n", 626 | "['ammu', 1.68]\n", 627 | "['vaia', 1.9]\n", 628 | "['abbu', 1.73, 'ammu', 1.68]\n", 629 | "['vaia', 1.9, 'apu', 1.79]\n", 630 | "apu\n", 631 | "1.79\n" 632 | ] 633 | } 634 | ], 635 | "source": [ 636 | "family = [\"abbu\", 1.73, \"ammu\", 1.68, \"vaia\", 1.9, \"apu\", 1.79]\n", 637 | "\n", 638 | "print(family)\n", 639 | "print(family[0])\n", 640 | "print(family[1])\n", 641 | "print(family[2:4])\n", 642 | "print(family[4:6])\n", 643 | "print(family[:4])\n", 644 | "print(family[4:])\n", 645 | "print(family[-2])\n", 646 | "print(family[-1])\n" 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": {}, 652 | "source": [ 653 | "#### List Manipulation:\n", 654 | "1. Change list elements\n", 655 | "2. Add list elements\n", 656 | "3. Remove list elements" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 106, 662 | "metadata": {}, 663 | "outputs": [ 664 | { 665 | "name": "stdout", 666 | "output_type": "stream", 667 | "text": [ 668 | "['abbu', 1.73, 'ammu', 1.68, 'vaia', 1.9, 'apu', 1.8]\n", 669 | "['baba', 1.74, 'ammu', 1.68, 'vaia', 1.9, 'apu', 1.8]\n" 670 | ] 671 | } 672 | ], 673 | "source": [ 674 | "# changing list elements\n", 675 | "family[7] = 1.8\n", 676 | "print(family)\n", 677 | "\n", 678 | "family[0:2] = [\"baba\", 1.74]\n", 679 | "print(family)" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 107, 685 | "metadata": {}, 686 | "outputs": [ 687 | { 688 | "name": "stdout", 689 | "output_type": "stream", 690 | "text": [ 691 | "['baba', 1.74, 'ammu', 1.68, 'vaia', 1.9, 'apu', 1.8, 'me', 1.6]\n", 692 | "['baba', 1.74, 'ammu', 1.68, 'vaia', 1.9, 'apu', 1.8, 'me']\n" 693 | ] 694 | } 695 | ], 696 | "source": [ 697 | "# adding list elements\n", 698 | "family = family + [\"me\", 1.6]\n", 699 | "print(family)\n", 700 | "\n", 701 | "# removing list elements\n", 702 | "del(family[-1])\n", 703 | "print(family)" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": null, 709 | "metadata": {}, 710 | "outputs": [], 711 | "source": [] 712 | } 713 | ], 714 | "metadata": { 715 | "kernelspec": { 716 | "display_name": "Python 3", 717 | "language": "python", 718 | "name": "python3" 719 | }, 720 | "language_info": { 721 | "codemirror_mode": { 722 | "name": "ipython", 723 | "version": 3 724 | }, 725 | "file_extension": ".py", 726 | "mimetype": "text/x-python", 727 | "name": "python", 728 | "nbconvert_exporter": "python", 729 | "pygments_lexer": "ipython3", 730 | "version": "3.7.6" 731 | } 732 | }, 733 | "nbformat": 4, 734 | "nbformat_minor": 4 735 | } 736 | -------------------------------------------------------------------------------- /lecture_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 03: Python data structure\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. Solution discussion of HW1 and HW2\n", 16 | "2. Python Data Structure: List, tuple, dictionary, set\n", 17 | "\n", 18 | "#### References: \n", 19 | "1. List vs Tuple: Difference Between List and Tuple: https://www.upgrad.com/blog/list-vs-tuple/" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "#### List Slicing" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "[['abul', 'babul', 'cabul', 'dabul'], [1111, 2222, 3333, 4444]]\n", 39 | "['abul', 'babul', 'cabul', 'dabul']\n", 40 | "[1111, 2222, 3333, 4444]\n", 41 | "abul\n", 42 | "1111\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "name = [\"abul\", \"babul\", \"cabul\", \"dabul\"]\n", 48 | "contact = [1111, 2222, 3333, 4444]\n", 49 | "\n", 50 | "phone_contacts = [name, contact]\n", 51 | "print(phone_contacts)\n", 52 | "\n", 53 | "print(phone_contacts[0])\n", 54 | "print(phone_contacts[1])\n", 55 | "print(phone_contacts[0][0])\n", 56 | "print(phone_contacts[1][0])" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "#### Checking length of a string" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 2, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "2\n", 76 | "4\n", 77 | "4\n", 78 | "5\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "print(len(phone_contacts))\n", 84 | "print(len(phone_contacts[0]))\n", 85 | "print(len(phone_contacts[1]))\n", 86 | "print(len(phone_contacts[0][1]))" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "#### Checking the presence of an element in a list" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 3, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "True" 105 | ] 106 | }, 107 | "execution_count": 3, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "\"abul\" not in phone_contacts" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "#### Insert a new element in the list" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 4, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "[2, 4, 0, 6, 8, 10]\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "even = [2, 4, 6, 8, 10]\n", 138 | "even.insert(2, 0)\n", 139 | "print(even)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "[2, 4, 0, 6, 8, 10, 12]\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "even.append(12)\n", 157 | "\n", 158 | "print(even)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "#### How to reverse a list?" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 6, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "[7, 8, 4, -9, 9, 3, -1, -1, 0]\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "nums = [0,-1,-1,3,9,-9,4,8,7]\n", 183 | "nums.reverse()\n", 184 | "print(nums)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "#### How to count the frequency of a specific element in a list?" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 7, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "2\n" 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "print(nums.count(-1))" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "#### How to remove an element from a list?" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 8, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "[7, 8, 4, -9, 3, -1, -1, 0]\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "nums.remove(9)\n", 233 | "print(nums)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "#### How to find the minimum value, maximum value and total sum of a list?" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 9, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "maximum value = 8\n", 253 | "minimum value = -9\n", 254 | "total sum = 11\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "print(\"maximum value = \", max(nums))\n", 260 | "print(\"minimum value = \", min(nums))\n", 261 | "print(\"total sum = \", sum(nums))" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "#### What will happen if you add two lists?" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 10, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "A + B = [9, 8, 7, 6, 99, 88, 77, 66]\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "A = [9, 8, 7, 6]\n", 286 | "B = [99, 88, 77, 66]\n", 287 | "print(\"A + B = \", A + B) #will contain all the element of A and B\n" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "#### How to sort a list?" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 11, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "name": "stdout", 304 | "output_type": "stream", 305 | "text": [ 306 | "[6, 7, 8, 9]\n", 307 | "[9, 8, 7, 6]\n" 308 | ] 309 | } 310 | ], 311 | "source": [ 312 | "print(sorted(A, reverse= False))\n", 313 | "print(sorted(A, reverse= True))" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "### Tuple\n", 321 | "\n", 322 | "1. tuple is used with parentheses\n", 323 | "2. tuple is immutable --> most important\n" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 12, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "name": "stdout", 333 | "output_type": "stream", 334 | "text": [ 335 | "\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "a = ('the', 'fault', 'in', 'our', 'stars')\n", 341 | "print(type(a))" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": {}, 347 | "source": [ 348 | "#### Tuple slicing" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 13, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "the\n", 361 | "stars\n", 362 | "('fault', 'in')\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "print(a[0])\n", 368 | "print(a[-1])\n", 369 | "print(a[1:3])" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "#### Immutability" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 14, 382 | "metadata": {}, 383 | "outputs": [], 384 | "source": [ 385 | "T = (8, 9, 'absolute', [1, 2])\n", 386 | "# T[0] = 2 #will throw an error" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": {}, 392 | "source": [ 393 | "**You cannot change any value inside a tuple and you cannot insert or delete as well**" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 15, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "name": "stdout", 403 | "output_type": "stream", 404 | "text": [ 405 | "3\n", 406 | "7\n", 407 | "1\n", 408 | "10\n", 409 | "1\n" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "#some functions that can be used with tuple\n", 415 | "Z = (2, 7, 1)\n", 416 | "print(len(Z))\n", 417 | "print(max(Z))\n", 418 | "print(min(Z))\n", 419 | "print(sum(Z))\n", 420 | "print(Z.count(2))" 421 | ] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": {}, 426 | "source": [ 427 | "**Similarities in List and Tuple**\n", 428 | "- The two data structures are both sequence data types that store collections of items.\n", 429 | "- Items of any data type can be stored in them.\n", 430 | "- Items can be accessed by their index.\n", 431 | "\n", 432 | "**Difference between List and Tuple**\n", 433 | "- List is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed." 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": {}, 439 | "source": [ 440 | "### Set\n", 441 | "1. It works with a curly brace--> {}\n", 442 | "2. One item can appear only once\n", 443 | "3. You cannot slice item from set, unlike lists or tuples" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 16, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "name": "stdout", 453 | "output_type": "stream", 454 | "text": [ 455 | "{12, 13, 14}\n", 456 | "\n", 457 | "3\n", 458 | "[12, 13, 14]\n", 459 | "{1, 2, 3, 4, 5, 7, 9}\n", 460 | "{1, 9, 7}\n", 461 | "{3, 5}\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "A = {12, 13, 14, 12}\n", 467 | "print(A) \n", 468 | "print(type(A))\n", 469 | "print(len(A))\n", 470 | "\n", 471 | "# convert to list\n", 472 | "A_converted = list(A)\n", 473 | "print(A_converted)\n", 474 | "\n", 475 | "A = {1, 3, 5, 7, 9}\n", 476 | "B = {1, 2, 4, 7, 9}\n", 477 | "\n", 478 | "print(A.union(B))\n", 479 | "print(A.intersection(B))\n", 480 | "print(A.difference(B))" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": {}, 486 | "source": [ 487 | "### Dictionary" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": 17, 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': 4444}\n", 500 | "1111\n", 501 | "2222\n", 502 | "3333\n", 503 | "4444\n", 504 | "dict_keys(['abul', 'babul', 'cabul', 'dabul'])\n", 505 | "dict_values([1111, 2222, 3333, 4444])\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "phone_contacts = {\"abul\":1111, \"babul\":2222, \"cabul\":3333, \"dabul\":4444}\n", 511 | "\n", 512 | "print(phone_contacts)\n", 513 | "print(phone_contacts[\"abul\"])\n", 514 | "print(phone_contacts[\"babul\"])\n", 515 | "print(phone_contacts[\"cabul\"])\n", 516 | "print(phone_contacts[\"dabul\"])\n", 517 | "\n", 518 | "print(phone_contacts.keys())\n", 519 | "print(phone_contacts.values())\n", 520 | "# print(phone_contacts.items())" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 18, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555]}\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "# adding a new phone number of the existing contact\n", 538 | "phone_contacts[\"dabul\"] = [4444,5555]\n", 539 | "print(phone_contacts)" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 19, 545 | "metadata": {}, 546 | "outputs": [ 547 | { 548 | "name": "stdout", 549 | "output_type": "stream", 550 | "text": [ 551 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666}\n" 552 | ] 553 | } 554 | ], 555 | "source": [ 556 | "# adding a new contact\n", 557 | "phone_contacts[\"fabul\"] = 6666\n", 558 | "print(phone_contacts)" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 20, 564 | "metadata": {}, 565 | "outputs": [ 566 | { 567 | "name": "stdout", 568 | "output_type": "stream", 569 | "text": [ 570 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666, 'gabul': 7777}\n" 571 | ] 572 | } 573 | ], 574 | "source": [ 575 | "# updating a dictionary\n", 576 | "new_dict = {\"gabul\":7777}\n", 577 | "phone_contacts.update(new_dict)\n", 578 | "print(phone_contacts)" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 21, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "name": "stdout", 588 | "output_type": "stream", 589 | "text": [ 590 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666}\n" 591 | ] 592 | } 593 | ], 594 | "source": [ 595 | "# removing a contact from the phonebook\n", 596 | "del phone_contacts[\"gabul\"]\n", 597 | "print(phone_contacts)" 598 | ] 599 | }, 600 | { 601 | "cell_type": "markdown", 602 | "metadata": {}, 603 | "source": [ 604 | "### Summary\n", 605 | "\n", 606 | "- List - indexed storage\n", 607 | "- Dictionary - Logical Association storage\n", 608 | "- Tuple - Memory optimized and Secure data storage\n", 609 | "- Set - unindexed data storage, widely useful for removing redundancy and mathematics ops" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": null, 615 | "metadata": {}, 616 | "outputs": [], 617 | "source": [] 618 | } 619 | ], 620 | "metadata": { 621 | "kernelspec": { 622 | "display_name": "Python 3", 623 | "language": "python", 624 | "name": "python3" 625 | }, 626 | "language_info": { 627 | "codemirror_mode": { 628 | "name": "ipython", 629 | "version": 3 630 | }, 631 | "file_extension": ".py", 632 | "mimetype": "text/x-python", 633 | "name": "python", 634 | "nbconvert_exporter": "python", 635 | "pygments_lexer": "ipython3", 636 | "version": "3.7.6" 637 | } 638 | }, 639 | "nbformat": 4, 640 | "nbformat_minor": 4 641 | } 642 | -------------------------------------------------------------------------------- /lecture_4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 04: Conditional logic, loop and writing functions in Python\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. How to write if-else, while and for loop in Python\n", 16 | "2. Writing functions in Python\n", 17 | "3. LeetCode problems\n", 18 | "\n", 19 | "\n", 20 | "#### References: \n", 21 | "1. https://leetcode.com/problems/defanging-an-ip-address/\n", 22 | "2. https://leetcode.com/problems/goal-parser-interpretation/\n", 23 | "3. https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/\n", 24 | "\n", 25 | "HW4:\n", 26 | "1. https://leetcode.com/problems/maximum-product-of-three-numbers/\n", 27 | "2. https://leetcode.com/problems/richest-customer-wealth/\n", 28 | "3. https://leetcode.com/problems/shuffle-the-array/\n", 29 | "4. https://leetcode.com/problems/valid-perfect-square/" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "#### Example: for loop" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 79, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "element = 1 square_value = 1 [1]\n", 49 | "element = 2 square_value = 4 [1, 4]\n", 50 | "element = 3 square_value = 9 [1, 4, 9]\n", 51 | "element = 4 square_value = 16 [1, 4, 9, 16]\n", 52 | "element = 5 square_value = 25 [1, 4, 9, 16, 25]\n", 53 | "element = 11 square_value = 121 [1, 4, 9, 16, 25, 121]\n", 54 | "element = 12 square_value = 144 [1, 4, 9, 16, 25, 121, 144]\n", 55 | "element = 13 square_value = 169 [1, 4, 9, 16, 25, 121, 144, 169]\n", 56 | "element = 14 square_value = 196 [1, 4, 9, 16, 25, 121, 144, 169, 196]\n", 57 | "element = 15 square_value = 225 [1, 4, 9, 16, 25, 121, 144, 169, 196, 225]\n", 58 | "[1, 4, 9, 16, 25, 121, 144, 169, 196, 225]\n", 59 | "\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "nums = [1,2,3,4,5,11,12,13,14,15] #input\n", 65 | "\n", 66 | "square_list = []\n", 67 | "\n", 68 | "for element in nums:\n", 69 | " square_value = element*element\n", 70 | " square_list.append(square_value)\n", 71 | " print(\"element = \", element, \"square_value = \", square_value, square_list)\n", 72 | "\n", 73 | "print(square_list)\n", 74 | " \n", 75 | "print(type(square_list))" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "#### Writing Functions" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 80, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "def multiply_numbers(a, b):\n", 92 | " \n", 93 | " total_sum = a*b\n", 94 | " \n", 95 | " return total_sum" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 81, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "2" 107 | ] 108 | }, 109 | "execution_count": 81, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "multiply_numbers(1,2)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 82, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "25000" 127 | ] 128 | }, 129 | "execution_count": 82, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "multiply_numbers(100,250)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 83, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "2.76" 147 | ] 148 | }, 149 | "execution_count": 83, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "multiply_numbers(1.2,2.3)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 84, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "def square_elements(input_list):\n", 165 | " \n", 166 | " square_list = []\n", 167 | "\n", 168 | " for element in input_list:\n", 169 | " square_value = element*element\n", 170 | " square_list.append(square_value)\n", 171 | " print(\"element = \", element, \"square_value = \", square_value, square_list)\n", 172 | " \n", 173 | " return square_list" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 85, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "element = 1 square_value = 1 [1]\n", 186 | "element = 2 square_value = 4 [1, 4]\n", 187 | "element = 3 square_value = 9 [1, 4, 9]\n", 188 | "element = 4 square_value = 16 [1, 4, 9, 16]\n", 189 | "element = 5 square_value = 25 [1, 4, 9, 16, 25]\n" 190 | ] 191 | }, 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "[1, 4, 9, 16, 25]" 196 | ] 197 | }, 198 | "execution_count": 85, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "square_elements([1,2,3,4,5])" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 86, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "element = -1 square_value = 1 [1]\n", 217 | "element = 2 square_value = 4 [1, 4]\n", 218 | "element = 4 square_value = 16 [1, 4, 16]\n", 219 | "element = 0 square_value = 0 [1, 4, 16, 0]\n", 220 | "element = 100 square_value = 10000 [1, 4, 16, 0, 10000]\n" 221 | ] 222 | }, 223 | { 224 | "data": { 225 | "text/plain": [ 226 | "[1, 4, 16, 0, 10000]" 227 | ] 228 | }, 229 | "execution_count": 86, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "square_elements([-1,2,4,0,100])" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "#### Even or odd" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 87, 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "0 even = True\n", 255 | "1 even = False\n", 256 | "2 even = True\n", 257 | "3 even = False\n", 258 | "4 even = True\n", 259 | "5 even = False\n", 260 | "6 even = True\n", 261 | "7 even = False\n", 262 | "8 even = True\n", 263 | "9 even = False\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "for i in range(0,10):\n", 269 | " if i%2 == 0:\n", 270 | " print(i, \"even = True\")\n", 271 | " else:\n", 272 | " print(i, \"even = False\")" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "#### Example of while condition" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 88, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "0\n", 292 | "Hello Geek\n", 293 | "1\n", 294 | "Hello Geek\n", 295 | "2\n", 296 | "Hello Geek\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "count = 0\n", 302 | "while count < 3: \n", 303 | " print(count)\n", 304 | " count = count + 1\n", 305 | " print(\"Hello Geek\")" 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "#### example: if-else" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 89, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "0 divisible by 2\n", 325 | "0 divisible by 3\n", 326 | "0 divisible by 4\n", 327 | "0 divisible by 2 and 4\n", 328 | "1 not divisible 2\n", 329 | "1 not divisible 3\n", 330 | "1 not divisible 4\n", 331 | "1 not divisible 2 and 4\n", 332 | "2 divisible by 2\n", 333 | "2 not divisible 3\n", 334 | "2 not divisible 4\n", 335 | "2 not divisible 2 and 4\n", 336 | "3 not divisible 2\n", 337 | "3 divisible by 3\n", 338 | "3 not divisible 4\n", 339 | "3 not divisible 2 and 4\n", 340 | "4 divisible by 2\n", 341 | "4 not divisible 3\n", 342 | "4 divisible by 4\n", 343 | "4 divisible by 2 and 4\n", 344 | "5 not divisible 2\n", 345 | "5 not divisible 3\n", 346 | "5 not divisible 4\n", 347 | "5 not divisible 2 and 4\n", 348 | "6 divisible by 2\n", 349 | "6 divisible by 3\n", 350 | "6 not divisible 4\n", 351 | "6 not divisible 2 and 4\n", 352 | "7 not divisible 2\n", 353 | "7 not divisible 3\n", 354 | "7 not divisible 4\n", 355 | "7 not divisible 2 and 4\n", 356 | "8 divisible by 2\n", 357 | "8 not divisible 3\n", 358 | "8 divisible by 4\n", 359 | "8 divisible by 2 and 4\n", 360 | "9 not divisible 2\n", 361 | "9 divisible by 3\n", 362 | "9 not divisible 4\n", 363 | "9 not divisible 2 and 4\n" 364 | ] 365 | } 366 | ], 367 | "source": [ 368 | "for i in range(0,10,1):\n", 369 | " \n", 370 | " # 0,2,4,6,8\n", 371 | " if i%2 == 0:\n", 372 | " print(i, \"divisible by 2\")\n", 373 | " else:\n", 374 | " print(i, \"not divisible 2\")\n", 375 | " \n", 376 | " # 3,6,9\n", 377 | " if i%3 == 0:\n", 378 | " print(i, \"divisible by 3\")\n", 379 | " else:\n", 380 | " print(i, \"not divisible 3\")\n", 381 | " \n", 382 | " # 4, 8\n", 383 | " if i%4 == 0:\n", 384 | " print(i, \"divisible by 4\")\n", 385 | " else:\n", 386 | " print(i, \"not divisible 4\")\n", 387 | " \n", 388 | " if i%2 == 0 and i%4 == 0:\n", 389 | " print(i, \"divisible by 2 and 4\")\n", 390 | " else:\n", 391 | " print(i, \"not divisible 2 and 4\")\n" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "#### Example of cumulative sum using for loop" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 90, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "name": "stdout", 408 | "output_type": "stream", 409 | "text": [ 410 | "i = 0 i+1 = 1 num = [1] sum = 1\n", 411 | "i = 1 i+1 = 2 num = [1, 2] sum = 3\n", 412 | "i = 2 i+1 = 3 num = [1, 2, 3] sum = 6\n", 413 | "i = 3 i+1 = 4 num = [1, 2, 3, 4] sum = 10\n", 414 | "i = 4 i+1 = 5 num = [1, 2, 3, 4, 5] sum = 15\n", 415 | "[1, 3, 6, 10, 15]\n" 416 | ] 417 | } 418 | ], 419 | "source": [ 420 | "nums = [1,2,3,4,5]\n", 421 | "sum_list = []\n", 422 | "\n", 423 | "for i in range(len(nums)):\n", 424 | " print(\"i = \", i, \"i+1 = \", i+1, \"num = \", nums[:i+1], \"sum = \", sum(nums[:i+1]))\n", 425 | " \n", 426 | " sum_list.append(sum(nums[:i+1]))\n", 427 | "\n", 428 | "print(sum_list)" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": {}, 434 | "source": [ 435 | "#### LeetCode Problem 01: Defanging an IP Address\n", 436 | "\n", 437 | "Given a valid (IPv4) IP address, return a defanged version of that IP address.\n", 438 | "\n", 439 | "A defanged IP address replaces every period \".\" with \"[.]\"\n", 440 | "\n", 441 | "**Example 1:**\n", 442 | "
Input: address = \"1.1.1.1\"\n", 443 | "
Output: \"1[.]1[.]1[.]1\"\n", 444 | "\n", 445 | "**Example 2:**\n", 446 | "
Input: address = \"255.100.50.0\"\n", 447 | "
Output: \"255[.]100[.]50[.]0\"" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 91, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "1[.]1[.]1[.]1\n", 460 | "255[.]100[.]50[.]0\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "def defangIPaddr(address):\n", 466 | " \"\"\"\n", 467 | " :type address: str\n", 468 | " :rtype: str\n", 469 | " \"\"\"\n", 470 | " return address.replace('.', '[.]')\n", 471 | "\n", 472 | "example_1 = defangIPaddr(\"1.1.1.1\")\n", 473 | "print(example_1)\n", 474 | "\n", 475 | "example_2 = defangIPaddr(\"255.100.50.0\")\n", 476 | "print(example_2)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "#### LeetCode Problem 02: Goal Parser Interpretation\n", 484 | "\n", 485 | "You own a Goal Parser that can interpret a string command. The command consists of an alphabet of \"G\", \"()\" and/or \"(al)\" in some order. The Goal Parser will interpret \"G\" as the string \"G\", \"()\" as the string \"o\", and \"(al)\" as the string \"al\". The interpreted strings are then concatenated in the original order.\n", 486 | "\n", 487 | "Given the string command, return the Goal Parser's interpretation of command.\n", 488 | "\n", 489 | "**Example 1:**\n", 490 | "
Input: command = \"G()(al)\"\n", 491 | "
Output: \"Goal\"\n", 492 | "Explanation: The Goal Parser interprets the command as follows:\n", 493 | "G -> G\n", 494 | "() -> o\n", 495 | "(al) -> al\n", 496 | "The final concatenated result is \"Goal\".\n", 497 | "\n", 498 | "**Example 2:**\n", 499 | "
Input: command = \"G()()()()(al)\"\n", 500 | "
Output: \"Gooooal\"\n", 501 | "\n", 502 | "**Example 3:**\n", 503 | "
Input: command = \"(al)G(al)()()G\"\n", 504 | "
Output: \"alGalooG\"" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 92, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "Goal\n", 517 | "Gooooal\n", 518 | "alGalooG\n" 519 | ] 520 | } 521 | ], 522 | "source": [ 523 | "def interpret(command):\n", 524 | " \"\"\"\n", 525 | " :type command: str\n", 526 | " :rtype: str\n", 527 | " \"\"\"\n", 528 | " command = command.replace('()', 'o')\n", 529 | " command = command.replace('(al)', 'al')\n", 530 | "\n", 531 | " return command\n", 532 | "\n", 533 | "example_1 = interpret(\"G()(al)\")\n", 534 | "print(example_1)\n", 535 | "\n", 536 | "example_2 = interpret(\"G()()()()(al)\")\n", 537 | "print(example_2)\n", 538 | "\n", 539 | "example_3 = interpret(\"(al)G(al)()()G\")\n", 540 | "print(example_3)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "#### LeetCode Problem 03: Check If Two String Arrays are Equivalent\n", 548 | "Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.\n", 549 | "\n", 550 | "A string is represented by an array if the array elements concatenated in order forms the string.\n", 551 | "\n", 552 | "**Example 1:**\n", 553 | "
Input: word1 = [\"ab\", \"c\"], word2 = [\"a\", \"bc\"]\n", 554 | "
Output: true\n", 555 | "
Explanation:\n", 556 | "
word1 represents string \"ab\" + \"c\" -> \"abc\"\n", 557 | "
word2 represents string \"a\" + \"bc\" -> \"abc\"\n", 558 | "
The strings are the same, so return true.\n", 559 | "\n", 560 | "**Example 2:**\n", 561 | "
Input: word1 = [\"a\", \"cb\"], word2 = [\"ab\", \"c\"]\n", 562 | "
Output: false\n", 563 | "\n", 564 | "**Example 3:**\n", 565 | "
Input: word1 = [\"abc\", \"d\", \"defg\"], word2 = [\"abcddefg\"]\n", 566 | "
Output: true" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 93, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "name": "stdout", 576 | "output_type": "stream", 577 | "text": [ 578 | "True\n", 579 | "False\n", 580 | "True\n" 581 | ] 582 | } 583 | ], 584 | "source": [ 585 | "def arrayStringsAreEqual(word1, word2):\n", 586 | " \n", 587 | "# print(\"\".join(word1))\n", 588 | "# print(\"\".join(word2))\n", 589 | "\n", 590 | " return \"\".join(word1) == \"\".join(word2)\n", 591 | "\n", 592 | "example_1 = arrayStringsAreEqual([\"ab\", \"c\"], [\"a\", \"bc\"])\n", 593 | "print(example_1)\n", 594 | "\n", 595 | "example_2 = arrayStringsAreEqual([\"a\", \"cb\"], [\"ab\", \"c\"])\n", 596 | "print(example_2)\n", 597 | "\n", 598 | "example_3 = arrayStringsAreEqual( [\"abc\", \"d\", \"defg\"], [\"abcddefg\"])\n", 599 | "print(example_3)" 600 | ] 601 | } 602 | ], 603 | "metadata": { 604 | "kernelspec": { 605 | "display_name": "Python 3", 606 | "language": "python", 607 | "name": "python3" 608 | }, 609 | "language_info": { 610 | "codemirror_mode": { 611 | "name": "ipython", 612 | "version": 3 613 | }, 614 | "file_extension": ".py", 615 | "mimetype": "text/x-python", 616 | "name": "python", 617 | "nbconvert_exporter": "python", 618 | "pygments_lexer": "ipython3", 619 | "version": "3.7.6" 620 | } 621 | }, 622 | "nbformat": 4, 623 | "nbformat_minor": 4 624 | } 625 | -------------------------------------------------------------------------------- /lecture_5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 5: LeetCode problems\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### References\n", 15 | "1. https://leetcode.com/problems/maximum-product-of-three-numbers/\n", 16 | "2. https://leetcode.com/problems/richest-customer-wealth/\n", 17 | "3. https://leetcode.com/problems/shuffle-the-array/\n", 18 | "4. https://leetcode.com/problems/valid-perfect-square/\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "**HW4 Problem 1: Maximum Product of Three Numbers**\n", 26 | "\n", 27 | "Given an integer array nums, find three numbers whose product is maximum and return the maximum product.\n", 28 | "\n", 29 | "**Example 1:**\n", 30 | "
Input: nums = [1,2,3]\n", 31 | "
Output: 6\n", 32 | "\n", 33 | "**Example 2:**\n", 34 | "
Input: nums = [1,2,3,4]\n", 35 | "
Output: 24\n", 36 | "\n", 37 | "**Example 3:**\n", 38 | "
Input: nums = [-1,-2,-3]\n", 39 | "
Output: -6" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 1, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "example = [-1,2,4,4]\n", 49 | "\n", 50 | "def maximumProduct(nums):\n", 51 | " \n", 52 | " nums = sorted(nums)\n", 53 | " print(\"sorted nums = \", nums)\n", 54 | " \n", 55 | " prod_1 = nums[0]*nums[1]*nums[-1]\n", 56 | " print(\"prod_1 = \", prod_1)\n", 57 | " prod_2 = nums[-1]*nums[-2]*nums[-3]\n", 58 | " print(\"prod_2 = \", prod_2)\n", 59 | " \n", 60 | " return max(prod_1, prod_2)\n", 61 | " " 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 2, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "sorted nums = [1, 2, 3]\n", 74 | "prod_1 = 6\n", 75 | "prod_2 = 6\n", 76 | "6\n", 77 | "----------------------------------------\n", 78 | "sorted nums = [1, 2, 3, 4]\n", 79 | "prod_1 = 8\n", 80 | "prod_2 = 24\n", 81 | "24\n", 82 | "----------------------------------------\n", 83 | "sorted nums = [-4, -2, -1, 1, 2, 6]\n", 84 | "prod_1 = 48\n", 85 | "prod_2 = 12\n", 86 | "48\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "p1_example_1 = maximumProduct([1,2,3])\n", 92 | "print(p1_example_1)\n", 93 | "print(\"----------------------------------------\")\n", 94 | "\n", 95 | "p1_example_2 = maximumProduct([1,2,3,4])\n", 96 | "print(p1_example_2)\n", 97 | "print(\"----------------------------------------\")\n", 98 | "\n", 99 | "p1_example_3 = maximumProduct([-1,-2,-4,1,2,6])\n", 100 | "print(p1_example_3)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "**HW4 Problem 2: 1672. Richest Customer Wealth**\n", 108 | "\n", 109 | "You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i-th customer has in the j-th bank. Return the wealth that the richest customer has.\n", 110 | "\n", 111 | "A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.\n", 112 | "\n", 113 | "**Example 1:**\n", 114 | "
Input: accounts = [[1,2,3],[3,2,1]]\n", 115 | "
Output: 6\n", 116 | "
Explanation:\n", 117 | "
1st customer has wealth = 1 + 2 + 3 = 6\n", 118 | "
2nd customer has wealth = 3 + 2 + 1 = 6\n", 119 | "
Both customers are considered the richest with a wealth of 6 each, so return 6.\n", 120 | "\n", 121 | "**Example 2:**\n", 122 | "
Input: accounts = [[1,5],[7,3],[3,5]]\n", 123 | "
Output: 10\n", 124 | "
Explanation: \n", 125 | "
1st customer has wealth = 6\n", 126 | "
2nd customer has wealth = 10 \n", 127 | "
3rd customer has wealth = 8\n", 128 | "
The 2nd customer is the richest with a wealth of 10.\n", 129 | "\n", 130 | "**Example 3:**\n", 131 | "
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]\n", 132 | "
Output: 17" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 3, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "def maximumWealth(accounts):\n", 142 | " \n", 143 | " wealth = []\n", 144 | " \n", 145 | " for element in range(len(accounts)):\n", 146 | " \n", 147 | " print(accounts[element])\n", 148 | " \n", 149 | " wealth.append(sum(accounts[element]))\n", 150 | " \n", 151 | " print(\"wealth = \", wealth)\n", 152 | " return max(wealth)\n", 153 | " " 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 | "[1, 2, 3]\n", 166 | "[3, 2, 1]\n", 167 | "wealth = [6, 6]\n", 168 | "6\n", 169 | "----------------------------------------\n", 170 | "[1, 5]\n", 171 | "[7, 3]\n", 172 | "[3, 5]\n", 173 | "wealth = [6, 10, 8]\n", 174 | "10\n", 175 | "----------------------------------------\n", 176 | "[2, 8, 7]\n", 177 | "[7, 1, 3]\n", 178 | "[1, 9, 5]\n", 179 | "wealth = [17, 11, 15]\n", 180 | "17\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "p2_example_1 = maximumWealth([[1,2,3],[3,2,1]])\n", 186 | "print(p2_example_1)\n", 187 | "print(\"----------------------------------------\")\n", 188 | "\n", 189 | "p2_example_2 = maximumWealth([[1,5],[7,3],[3,5]])\n", 190 | "print(p2_example_2)\n", 191 | "print(\"----------------------------------------\")\n", 192 | "\n", 193 | "p2_example_3 = maximumWealth([[2,8,7],[7,1,3],[1,9,5]])\n", 194 | "print(p2_example_3)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "**HW4 Problem 3: Shuffle the Array**\n", 202 | "\n", 203 | "Given the array nums consisting of 2n elements in the form [x_1,x_2,...,x_n,y_1,y_2,...,y_n].\n", 204 | "\n", 205 | "Return the array in the form [x_1,y_1,x_2,y_2,...,x_n,y_n].\n", 206 | "\n", 207 | "**Example 1:**\n", 208 | "
Input: nums = [2,5,1,3,4,7], n = 3\n", 209 | "
Output: [2,3,5,4,1,7] \n", 210 | "
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].\n", 211 | "\n", 212 | "**Example 2:**\n", 213 | "
Input: nums = [1,2,3,4,4,3,2,1], n = 4\n", 214 | "
Output: [1,4,2,3,3,2,4,1]\n", 215 | "\n", 216 | "**Example 3:**\n", 217 | "
Input: nums = [1,1,2,2], n = 2\n", 218 | "
Output: [1,2,1,2]" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 5, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "def shuffle(nums, n):\n", 228 | " \n", 229 | " first_half = nums[:n]\n", 230 | " last_half = nums[n:]\n", 231 | " \n", 232 | " shuffle = []\n", 233 | " \n", 234 | " for i in range(n):\n", 235 | " shuffle.append(first_half[i])\n", 236 | " \n", 237 | " print(\"after adding first half = \", shuffle)\n", 238 | " \n", 239 | " shuffle.append(last_half[i])\n", 240 | " \n", 241 | " print(\"after adding last half = \", shuffle)\n", 242 | " \n", 243 | " return shuffle\n", 244 | " " 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 6, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "after adding first half = [2]\n", 257 | "after adding last half = [2, 3]\n", 258 | "after adding first half = [2, 3, 5]\n", 259 | "after adding last half = [2, 3, 5, 4]\n", 260 | "after adding first half = [2, 3, 5, 4, 1]\n", 261 | "after adding last half = [2, 3, 5, 4, 1, 7]\n", 262 | "[2, 3, 5, 4, 1, 7]\n", 263 | "----------------------------------------\n", 264 | "after adding first half = [1]\n", 265 | "after adding last half = [1, 4]\n", 266 | "after adding first half = [1, 4, 2]\n", 267 | "after adding last half = [1, 4, 2, 3]\n", 268 | "after adding first half = [1, 4, 2, 3, 3]\n", 269 | "after adding last half = [1, 4, 2, 3, 3, 2]\n", 270 | "after adding first half = [1, 4, 2, 3, 3, 2, 4]\n", 271 | "after adding last half = [1, 4, 2, 3, 3, 2, 4, 1]\n", 272 | "[1, 4, 2, 3, 3, 2, 4, 1]\n", 273 | "----------------------------------------\n", 274 | "after adding first half = [1]\n", 275 | "after adding last half = [1, 2]\n", 276 | "after adding first half = [1, 2, 1]\n", 277 | "after adding last half = [1, 2, 1, 2]\n", 278 | "[1, 2, 1, 2]\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "p3_example_1 = shuffle([2,5,1,3,4,7], 3)\n", 284 | "print(p3_example_1)\n", 285 | "print(\"----------------------------------------\")\n", 286 | "\n", 287 | "p3_example_2 = shuffle([1,2,3,4,4,3,2,1], 4)\n", 288 | "print(p3_example_2)\n", 289 | "print(\"----------------------------------------\")\n", 290 | "\n", 291 | "p3_example_3 = shuffle([1,1,2,2], 2)\n", 292 | "print(p3_example_3)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "**HW4 Problem 4: Valid Perfect Square**\n", 300 | "\n", 301 | "Given a positive integer num, write a function which returns True if num is a perfect square else False.\n", 302 | "\n", 303 | "**Example 1:**\n", 304 | "
Input: num = 16\n", 305 | "
Output: true\n", 306 | "\n", 307 | "**Example 2:**\n", 308 | "
Input: num = 14\n", 309 | "
Output: false" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 7, 315 | "metadata": {}, 316 | "outputs": [], 317 | "source": [ 318 | "def isPerfectSquare(num):\n", 319 | " \n", 320 | " print(\"square root = \", num**0.5)\n", 321 | " \n", 322 | " print(\"square root (integer part only) = \", int(num**0.5))\n", 323 | " \n", 324 | " return int(num**0.5) == num**0.5\n" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 8, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "square root = 3.7416573867739413\n", 337 | "square root (integer part only) = 3\n", 338 | "False\n", 339 | "----------------------------------------\n", 340 | "square root = 3.7416573867739413\n", 341 | "square root (integer part only) = 3\n", 342 | "False\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "p4_example_1 = isPerfectSquare(14)\n", 348 | "print(p4_example_1)\n", 349 | "print(\"----------------------------------------\")\n", 350 | "\n", 351 | "p4_example_2 = isPerfectSquare(14)\n", 352 | "print(p4_example_2)" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "metadata": {}, 359 | "outputs": [], 360 | "source": [] 361 | } 362 | ], 363 | "metadata": { 364 | "kernelspec": { 365 | "display_name": "Python 3", 366 | "language": "python", 367 | "name": "python3" 368 | }, 369 | "language_info": { 370 | "codemirror_mode": { 371 | "name": "ipython", 372 | "version": 3 373 | }, 374 | "file_extension": ".py", 375 | "mimetype": "text/x-python", 376 | "name": "python", 377 | "nbconvert_exporter": "python", 378 | "pygments_lexer": "ipython3", 379 | "version": "3.7.6" 380 | } 381 | }, 382 | "nbformat": 4, 383 | "nbformat_minor": 4 384 | } 385 | -------------------------------------------------------------------------------- /lecture_6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 06: Application of Math functions, list, set and dictionary\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. Math functions\n", 16 | "2. Application of list, set, and dictionary\n", 17 | "3. Solving same problem in multiple ways\n", 18 | "\n", 19 | "\n", 20 | "#### References: \n", 21 | "1. https://leetcode.com/problems/power-of-two/\n", 22 | "2. https://leetcode.com/problems/contains-duplicate/\n", 23 | "3. https://leetcode.com/problems/two-sum/submissions/\n", 24 | "4. https://leetcode.com/problems/roman-to-integer/\n", 25 | "5. https://leetcode.com/problems/unique-morse-code-words/" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "**Problem 1: Power of Two**\n", 33 | "\n", 34 | "Given an integer n, return true if it is a power of two. Otherwise, return false.\n", 35 | "\n", 36 | "An integer n is a power of two, if there exists an integer x such that n == 2^x.\n", 37 | "\n", 38 | "**Example 1:**\n", 39 | "
Input: n = 1\n", 40 | "
Output: true\n", 41 | "
Explanation: 2^0 = 1\n", 42 | "\n", 43 | "**Example 2:**\n", 44 | "
Input: n = 16\n", 45 | "
Output: true\n", 46 | "
Explanation: 2^4 = 16\n", 47 | "\n", 48 | "**Example 3:**\n", 49 | "
Input: n = 3\n", 50 | "
Output: false\n", 51 | "\n", 52 | "**Example 4:**\n", 53 | "
Input: n = 4\n", 54 | "
Output: true\n", 55 | "\n", 56 | "**Example 5:**\n", 57 | "
Input: n = 5\n", 58 | "
Output: false" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "n == 2^x\n", 66 | "
=> log(n) = log(2^x)\n", 67 | "
=> log(n) = x*log(2)\n", 68 | "
=> x = log(n)/log(2)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 1, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "import math\n", 78 | "\n", 79 | "def isPowerOfTwo(n):\n", 80 | "\n", 81 | " if n <= 0:\n", 82 | " return False\n", 83 | "\n", 84 | " x = math.log10(n)/math.log10(2)\n", 85 | " return n == 2**int(x)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 2, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "True\n", 98 | "-----------------------------\n", 99 | "True\n", 100 | "-----------------------------\n", 101 | "False\n", 102 | "-----------------------------\n", 103 | "True\n", 104 | "-----------------------------\n", 105 | "False\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "p1_example_1 = isPowerOfTwo(1)\n", 111 | "print(p1_example_1)\n", 112 | "\n", 113 | "print(\"-----------------------------\")\n", 114 | "\n", 115 | "p1_example_2 = isPowerOfTwo(16)\n", 116 | "print(p1_example_2)\n", 117 | "\n", 118 | "print(\"-----------------------------\")\n", 119 | "\n", 120 | "p1_example_3 = isPowerOfTwo(3)\n", 121 | "print(p1_example_3)\n", 122 | "\n", 123 | "print(\"-----------------------------\")\n", 124 | "\n", 125 | "p1_example_4 = isPowerOfTwo(4)\n", 126 | "print(p1_example_4)\n", 127 | "\n", 128 | "print(\"-----------------------------\")\n", 129 | "\n", 130 | "p1_example_5 = isPowerOfTwo(5)\n", 131 | "print(p1_example_5)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "**Problem 2: Contains Duplicate**\n", 139 | "\n", 140 | "Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.\n", 141 | "\n", 142 | "**Example 1:**\n", 143 | "
Input: nums = [1,2,3,1]\n", 144 | "
Output: true\n", 145 | "\n", 146 | "**Example 2:**\n", 147 | "
Input: nums = [1,2,3,4]\n", 148 | "
Output: false\n", 149 | "\n", 150 | "**Example 3:**\n", 151 | "
Input: nums = [1,1,1,3,3,4,3,2,4,2]\n", 152 | "
Output: true" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 3, 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [ 161 | "def containsDuplicate(nums):\n", 162 | "\n", 163 | " # solution 1\n", 164 | " return len(nums) != len(set(nums))\n", 165 | "\n", 166 | " # solution 2\n", 167 | " # nums = sorted(nums)\n", 168 | " # # print(nums)\n", 169 | " # for index in range(len(nums)-1):\n", 170 | " # if nums[index] == nums[index+1]:\n", 171 | " # return True" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 4, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "True\n", 184 | "-----------------------------\n", 185 | "False\n", 186 | "-----------------------------\n", 187 | "True\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "p2_example_1 = containsDuplicate([1,2,3,1])\n", 193 | "print(p2_example_1)\n", 194 | "\n", 195 | "print(\"-----------------------------\")\n", 196 | "\n", 197 | "p2_example_2 = containsDuplicate([1,2,3,4])\n", 198 | "print(p2_example_2)\n", 199 | "\n", 200 | "print(\"-----------------------------\")\n", 201 | "\n", 202 | "p2_example_3 = containsDuplicate([1,1,1,3,3,4,3,2,4,2])\n", 203 | "print(p2_example_3)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 5, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "# nums = [1,2,3,4,5,-2000]\n", 213 | "\n", 214 | "# for i in nums:\n", 215 | "# if i < -1000 or i > 1000:\n", 216 | "# print(\"out of range\")\n", 217 | " " 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "**Problem 3: Two Sum**\n", 225 | "\n", 226 | "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\n", 227 | "\n", 228 | "You may assume that each input would have exactly one solution, and you may not use the same element twice.\n", 229 | "\n", 230 | "You can return the answer in any order.\n", 231 | "\n", 232 | "**Example 1:**\n", 233 | "
Input: nums = [2,7,11,15], target = 9\n", 234 | "
Output: [0,1]\n", 235 | "
Output: Because nums[0] + nums[1] == 9, we return [0, 1].\n", 236 | "\n", 237 | "**Example 2:**\n", 238 | "
Input: nums = [3,2,4], target = 6\n", 239 | "
Output: [1,2]\n", 240 | "\n", 241 | "**Example 3:**\n", 242 | "
Input: nums = [3,3], target = 6\n", 243 | "
Output: [0,1]" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 6, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [ 252 | "def twoSum(nums, target):\n", 253 | "\n", 254 | " val_index_dict = {}\n", 255 | "\n", 256 | " for index in range(0, len(nums)):\n", 257 | " goal = target - nums[index]\n", 258 | " print(\"x = \", goal, \"y = \", nums[index])\n", 259 | "\n", 260 | " if(goal in val_index_dict):\n", 261 | " print([val_index_dict[goal], index])\n", 262 | " return [val_index_dict[goal], index]\n", 263 | "\n", 264 | " val_index_dict[nums[index]] = index\n", 265 | " print(\"dictionary = \", val_index_dict)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 7, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "x = 7 y = 2\n", 278 | "dictionary = {2: 0}\n", 279 | "x = 2 y = 7\n", 280 | "[0, 1]\n", 281 | "[0, 1]\n", 282 | "-----------------------------\n", 283 | "x = 3 y = 3\n", 284 | "dictionary = {3: 0}\n", 285 | "x = 4 y = 2\n", 286 | "dictionary = {3: 0, 2: 1}\n", 287 | "x = 2 y = 4\n", 288 | "[1, 2]\n", 289 | "[1, 2]\n", 290 | "-----------------------------\n", 291 | "x = 3 y = 3\n", 292 | "dictionary = {3: 0}\n", 293 | "x = 3 y = 3\n", 294 | "[0, 1]\n", 295 | "[0, 1]\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "p3_example_1 = twoSum([2,7,11,15], 9)\n", 301 | "print(p3_example_1)\n", 302 | "\n", 303 | "print(\"-----------------------------\")\n", 304 | "\n", 305 | "p3_example_2 = twoSum([3,2,4], 6)\n", 306 | "print(p3_example_2)\n", 307 | "\n", 308 | "print(\"-----------------------------\")\n", 309 | "\n", 310 | "p3_example_3 = twoSum([3,3], 6)\n", 311 | "print(p3_example_3)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": {}, 317 | "source": [ 318 | "**Problem 4: Roman to Integer**\n", 319 | "\n", 320 | "Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.\n", 321 | "\n", 322 | "values = {\"I\": 1,\"V\": 5,\"X\": 10,\"L\": 50,\"C\": 100,\"D\": 500,\"M\": 1000}\n", 323 | "\n", 324 | "For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.\n", 325 | "\n", 326 | "Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:\n", 327 | "\n", 328 | "I can be placed before V (5) and X (10) to make 4 and 9. \n", 329 | "
X can be placed before L (50) and C (100) to make 40 and 90. \n", 330 | "
C can be placed before D (500) and M (1000) to make 400 and 900.\n", 331 | "
Given a roman numeral, convert it to an integer.\n", 332 | "\n", 333 | "**Example 1:**\n", 334 | "
Input: s = \"III\"\n", 335 | "
Output: 3\n", 336 | "\n", 337 | "**Example 2:**\n", 338 | "
Input: s = \"IV\"\n", 339 | "
Output: 4\n", 340 | "\n", 341 | "**Example 3:**\n", 342 | "
Input: s = \"IX\"\n", 343 | "
Output: 9\n", 344 | "\n", 345 | "**Example 4:**\n", 346 | "
Input: s = \"LVIII\"\n", 347 | "
Output: 58\n", 348 | "
Explanation: L = 50, V= 5, III = 3.\n", 349 | "\n", 350 | "**Example 5:**\n", 351 | "
Input: s = \"MCMXCIV\"\n", 352 | "
Output: 1994\n", 353 | "
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.\n", 354 | " \n", 355 | "**Constraints:**\n", 356 | "1. 1 <= s.length <= 15\n", 357 | "2. s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').\n", 358 | "3. It is guaranteed that s is a valid roman numeral in the range [1, 3999]." 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 8, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [ 367 | "values = {\"I\": 1,\"V\": 5,\"X\": 10,\"L\": 50,\"C\": 100,\"D\": 500,\"M\": 1000}\n", 368 | "\n", 369 | "def romanToInt(s):\n", 370 | "\n", 371 | " total = 0\n", 372 | " i = 0 #index\n", 373 | " \n", 374 | " while i < len(s):\n", 375 | " \n", 376 | " # If this is the subtractive case.\n", 377 | " if i + 1 < len(s) and values[s[i]] < values[s[i + 1]]:\n", 378 | "\n", 379 | " print(\"\\nsubtractive case\")\n", 380 | " print(\"current digit, \", s[i], \" = \", values[s[i]])\n", 381 | " print(\"next digit, \", s[i+1], \" = \", values[s[i+1]])\n", 382 | "\n", 383 | " total += values[s[i + 1]] - values[s[i]]\n", 384 | "\n", 385 | " print('total_by_subtraction = ', total)\n", 386 | " print(\"----------------------\")\n", 387 | " i += 2\n", 388 | " \n", 389 | " # Else this is NOT the subtractive case.\n", 390 | " else:\n", 391 | " print(\"current digit, \", s[i], \" = \", values[s[i]])\n", 392 | " total += values[s[i]]\n", 393 | " print('total_by_addition = ', total)\n", 394 | " print(\"----------------------\")\n", 395 | " i += 1\n", 396 | " \n", 397 | " print()\n", 398 | " return total " 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 12, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "name": "stdout", 408 | "output_type": "stream", 409 | "text": [ 410 | "current digit, I = 1\n", 411 | "total_by_addition = 1\n", 412 | "----------------------\n", 413 | "\n", 414 | "current digit, I = 1\n", 415 | "total_by_addition = 2\n", 416 | "----------------------\n", 417 | "\n", 418 | "current digit, I = 1\n", 419 | "total_by_addition = 3\n", 420 | "----------------------\n", 421 | "\n", 422 | "3\n", 423 | "-----------------------------------------------\n", 424 | "\n", 425 | "subtractive case\n", 426 | "current digit, I = 1\n", 427 | "next digit, V = 5\n", 428 | "total_by_subtraction = 4\n", 429 | "----------------------\n", 430 | "\n", 431 | "4\n", 432 | "-----------------------------------------------\n", 433 | "\n", 434 | "subtractive case\n", 435 | "current digit, I = 1\n", 436 | "next digit, X = 10\n", 437 | "total_by_subtraction = 9\n", 438 | "----------------------\n", 439 | "\n", 440 | "9\n", 441 | "-----------------------------------------------\n", 442 | "current digit, L = 50\n", 443 | "total_by_addition = 50\n", 444 | "----------------------\n", 445 | "\n", 446 | "current digit, V = 5\n", 447 | "total_by_addition = 55\n", 448 | "----------------------\n", 449 | "\n", 450 | "current digit, I = 1\n", 451 | "total_by_addition = 56\n", 452 | "----------------------\n", 453 | "\n", 454 | "current digit, I = 1\n", 455 | "total_by_addition = 57\n", 456 | "----------------------\n", 457 | "\n", 458 | "current digit, I = 1\n", 459 | "total_by_addition = 58\n", 460 | "----------------------\n", 461 | "\n", 462 | "58\n", 463 | "-----------------------------------------------\n", 464 | "current digit, M = 1000\n", 465 | "total_by_addition = 1000\n", 466 | "----------------------\n", 467 | "\n", 468 | "\n", 469 | "subtractive case\n", 470 | "current digit, C = 100\n", 471 | "next digit, M = 1000\n", 472 | "total_by_subtraction = 1900\n", 473 | "----------------------\n", 474 | "\n", 475 | "\n", 476 | "subtractive case\n", 477 | "current digit, X = 10\n", 478 | "next digit, C = 100\n", 479 | "total_by_subtraction = 1990\n", 480 | "----------------------\n", 481 | "\n", 482 | "\n", 483 | "subtractive case\n", 484 | "current digit, I = 1\n", 485 | "next digit, V = 5\n", 486 | "total_by_subtraction = 1994\n", 487 | "----------------------\n", 488 | "\n", 489 | "1994\n" 490 | ] 491 | } 492 | ], 493 | "source": [ 494 | "p4_example_1 = romanToInt(\"III\")\n", 495 | "print(p4_example_1)\n", 496 | "\n", 497 | "print(\"-----------------------------------------------\")\n", 498 | "\n", 499 | "p4_example_2 = romanToInt(\"IV\")\n", 500 | "print(p4_example_2)\n", 501 | "\n", 502 | "print(\"-----------------------------------------------\")\n", 503 | "\n", 504 | "p4_example_3 = romanToInt(\"IX\")\n", 505 | "print(p4_example_3)\n", 506 | "\n", 507 | "print(\"-----------------------------------------------\")\n", 508 | "\n", 509 | "p4_example_4 = romanToInt(\"LVIII\")\n", 510 | "print(p4_example_4)\n", 511 | "\n", 512 | "print(\"-----------------------------------------------\")\n", 513 | "\n", 514 | "p4_example_5 = romanToInt(\"MCMXCIV\")\n", 515 | "print(p4_example_5)" 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "**Problem 5: Unique Morse Code Words**\n", 523 | "\n", 524 | "International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: \"a\" maps to \".-\", \"b\" maps to \"-...\", \"c\" maps to \"-.-.\", and so on.\n", 525 | "\n", 526 | "For convenience, the full table for the 26 letters of the English alphabet is given below:\n", 527 | "\n", 528 | "[\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"]\n", 529 | "\n", 530 | "Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, \"cab\" can be written as \"-.-..--...\", (which is the concatenation \"-.-.\" + \".-\" + \"-...\"). We'll call such a concatenation, the transformation of a word.\n", 531 | "\n", 532 | "Return the number of different transformations among all words we have.\n", 533 | "\n", 534 | "**Example:**\n", 535 | "
Input: words = [\"gin\", \"zen\", \"gig\", \"msg\"]\n", 536 | "
Output: 2\n", 537 | "
Explanation: \n", 538 | "
The transformation of each word is:\n", 539 | "
\"gin\" -> \"--...-.\"\n", 540 | "
\"zen\" -> \"--...-.\"\n", 541 | "
\"gig\" -> \"--...--.\"\n", 542 | "
\"msg\" -> \"--...--.\"\n", 543 | "\n", 544 | "There are 2 different transformations, \"--...-.\" and \"--...--.\".\n" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 10, 550 | "metadata": {}, 551 | "outputs": [], 552 | "source": [ 553 | "def uniqueMorseRepresentations(words):\n", 554 | "\n", 555 | " morse = {'a':\".-\", 'b':\"-...\", 'c':\"-.-.\", 'd':\"-..\", 'e':\".\",\n", 556 | " 'f':\"..-.\", 'g':\"--.\", 'h':\"....\", 'i':\"..\", 'j':\".---\",\n", 557 | " 'k':\"-.-\", 'l':\".-..\", 'm':\"--\", 'n':\"-.\", 'o':\"---\",\n", 558 | " 'p':\".--.\", 'q':\"--.-\", 'r':\".-.\", 's':\"...\", 't':\"-\",\n", 559 | " 'u':\"..-\", 'v':\"...-\", 'w':\".--\", 'x':\"-..-\", 'y':\"-.--\",\n", 560 | " 'z':\"--..\"}\n", 561 | "\n", 562 | " morse_signal = \"\"\n", 563 | " morse_list = []\n", 564 | " \n", 565 | " for word in words:\n", 566 | " for char in word:\n", 567 | " morse_signal += morse[char]\n", 568 | "\n", 569 | " morse_list.append(morse_signal)\n", 570 | " morse_signal = \"\"\n", 571 | " print(morse_list)\n", 572 | "\n", 573 | " return len(set(morse_list))" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": 11, 579 | "metadata": {}, 580 | "outputs": [ 581 | { 582 | "name": "stdout", 583 | "output_type": "stream", 584 | "text": [ 585 | "['--...-.']\n", 586 | "['--...-.', '--...-.']\n", 587 | "['--...-.', '--...-.', '--...--.']\n", 588 | "['--...-.', '--...-.', '--...--.', '--...--.']\n", 589 | "2\n" 590 | ] 591 | } 592 | ], 593 | "source": [ 594 | "p5_example = uniqueMorseRepresentations([\"gin\", \"zen\", \"gig\", \"msg\"])\n", 595 | "print(p5_example)" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": null, 601 | "metadata": {}, 602 | "outputs": [], 603 | "source": [] 604 | } 605 | ], 606 | "metadata": { 607 | "kernelspec": { 608 | "display_name": "Python 3", 609 | "language": "python", 610 | "name": "python3" 611 | }, 612 | "language_info": { 613 | "codemirror_mode": { 614 | "name": "ipython", 615 | "version": 3 616 | }, 617 | "file_extension": ".py", 618 | "mimetype": "text/x-python", 619 | "name": "python", 620 | "nbconvert_exporter": "python", 621 | "pygments_lexer": "ipython3", 622 | "version": "3.7.6" 623 | } 624 | }, 625 | "nbformat": 4, 626 | "nbformat_minor": 4 627 | } 628 | -------------------------------------------------------------------------------- /lecture_7.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 07: Mathematical operations in Python: NumPy\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. Comparison between List and NumPy array\n", 16 | "2. 1D and 2D NumPy array\n", 17 | "3. Mathematical and Matrix operation in NumPy array\n", 18 | "4. NumPy array slicing and filtering\n", 19 | "\n", 20 | "#### References:\n", 21 | "1. https://numpy.org/doc/stable/reference/routines.math.html" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "#### Comparison of List and NumPy array" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 1, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "import numpy as np" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -2, 3, -4, 5, -6, 7, -8, 9]\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "list_1 = [1,2,3,4,5,6,7,8,9]\n", 55 | "list_2 = [1,-2,3,-4,5,-6,7,-8,9]\n", 56 | "\n", 57 | "list_add = list_1 + list_2\n", 58 | "print(list_add)\n", 59 | "\n", 60 | "# list_sub = list_1 - list_2\n", 61 | "# print(list_sub)\n", 62 | "\n", 63 | "# list_mul = list_1 * list_2\n", 64 | "# print(list_mul)\n", 65 | "\n", 66 | "# list_sub = list_1 / list_2\n", 67 | "# print(list_sub)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 84, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "\n", 80 | "\n", 81 | "int64\n", 82 | "sum of two array = [ 2 0 6 0 10 0 14 0 18]\n", 83 | "\n", 84 | "subtraction of two array = [ 0 4 0 8 0 12 0 16 0]\n", 85 | "multiplication of two array = [ 1 -4 9 -16 25 -36 49 -64 81]\n", 86 | "division of two array = [ 1. -1. 1. -1. 1. -1. 1. -1. 1.]\n", 87 | "remainder after dividing two arrays = [0 0 2 2 2]\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "arr_1 = np.array([1,2,3,4,5,6,7,8,9])\n", 93 | "arr_2 = np.array([1,-2,3,-4,5,-6,7,-8,9])\n", 94 | "\n", 95 | "print(type(arr_1))\n", 96 | "print(type(arr_2))\n", 97 | "print(arr_2.dtype)\n", 98 | "\n", 99 | "arr_add = arr_1 + arr_2\n", 100 | "print(\"addition of two array = \", arr_add)\n", 101 | "print(type(arr_add))\n", 102 | "\n", 103 | "# arr_add = np.add(arr_3, arr_4)\n", 104 | "# print(arr_add)\n", 105 | "\n", 106 | "arr_sub = arr_1 - arr_2\n", 107 | "print(\"subtraction of two array = \", arr_sub)\n", 108 | "\n", 109 | "# arr_sub = np.subtract(arr_3, arr_4)\n", 110 | "# print(arr_sub)\n", 111 | "\n", 112 | "arr_mul = arr_1 * arr_2\n", 113 | "print(\"multiplication of two array = \", arr_mul)\n", 114 | "\n", 115 | "# arr_mul = np.multiply(arr_3, arr_4)\n", 116 | "# print(arr_mul)\n", 117 | "\n", 118 | "arr_div = arr_1 / arr_2\n", 119 | "print(\"division of two array = \", arr_div)\n", 120 | "\n", 121 | "# arr_div = np.divide(arr_5, arr_6)\n", 122 | "# print(arr_div)\n", 123 | "\n", 124 | "arr_mod = np.mod(arr_5, arr_6)\n", 125 | "print(\"remainder after dividing two arrays = \", arr_mod)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 40, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "array_3 = \n", 138 | " [[1 2 3]\n", 139 | " [4 5 6]]\n", 140 | "\n", 141 | "(2, 3)\n", 142 | "array_4 = \n", 143 | " [[1 2 3]\n", 144 | " [4 5 6]]\n", 145 | "2d array sum = \n", 146 | " [[ 2 4 6]\n", 147 | " [ 8 10 12]]\n", 148 | "2d array multiplication = \n", 149 | " [[ 1 4 9]\n", 150 | " [16 25 36]]\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "arr_3 = np.array([[1,2,3], \n", 156 | " [4,5,6]])\n", 157 | "\n", 158 | "arr_4 = np.array([[1,2,3], \n", 159 | " [4,5,6]])\n", 160 | "\n", 161 | "print(\"array_3 = \\n\", arr_3)\n", 162 | "print(type(arr_3))\n", 163 | "print(arr_4.shape)\n", 164 | "print(\"array_4 = \\n\", arr_4)\n", 165 | "\n", 166 | "arr2d_add = arr_3 + arr_4\n", 167 | "print(\"2d array addition = \\n\", arr2d_add)\n", 168 | "\n", 169 | "arr2d_mul = arr_3 * arr_4\n", 170 | "print(\"2d array multiplication = \\n\", arr2d_mul)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 139, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "[ 2 4 8 16 32]\n", 183 | "[ 1 4 9 16 25]\n", 184 | "[1.05 2. 3.5 4. 5.75]\n", 185 | "[ 2. -2. 4. -4. 6.]\n", 186 | "[ 1. -2. 3. -4. 5.]\n", 187 | "[ 1. -2. 4. -4. 6.]\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "arr_5 = np.array([2, 2, 2, 2, 2])\n", 193 | "arr_6 = np.array([1, 2, 3, 4, 5])\n", 194 | "arr_7 = np.array([1.05, -2, 3.5, -4, 5.75])\n", 195 | "\n", 196 | "arr_pow = np.power(arr_5, arr_6)\n", 197 | "print(arr_pow)\n", 198 | "\n", 199 | "arr_square = np.square(arr_6)\n", 200 | "print(arr_square)\n", 201 | "\n", 202 | "arr_abs = np.abs(arr_7)\n", 203 | "print(arr_abs)\n", 204 | "\n", 205 | "arr_ceil = np.ceil(arr_7)\n", 206 | "print(arr_ceil)\n", 207 | "\n", 208 | "arr_floor = np.floor(arr_7)\n", 209 | "print(arr_floor)\n", 210 | "\n", 211 | "arr_round = np.round(arr_7)\n", 212 | "print(arr_round)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "#### array sum" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 88, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "25\n", 232 | "[3 4 5 6 7]\n", 233 | "[10 15]\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "arr_sum = np.sum([arr_5, arr_6])\n", 239 | "print(arr_sum)\n", 240 | "\n", 241 | "column_sum = np.sum([arr_5, arr_6], axis=0)\n", 242 | "print(column_sum)\n", 243 | "\n", 244 | "row_sum = np.sum([arr_5, arr_6], axis=1)\n", 245 | "print(row_sum)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "#### array product" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 89, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "name": "stdout", 262 | "output_type": "stream", 263 | "text": [ 264 | "3840\n", 265 | "[ 2 4 6 8 10]\n", 266 | "[ 32 120]\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "arr_prod = np.prod([arr_5, arr_6])\n", 272 | "print(arr_prod)\n", 273 | "\n", 274 | "column_prod = np.prod([arr_5, arr_6], axis=0)\n", 275 | "print(column_prod)\n", 276 | "\n", 277 | "row_prod = np.prod([arr_5, arr_6], axis=1)\n", 278 | "print(row_prod)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "#### Matrix operation" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 100, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "[[1 2 3]\n", 298 | " [4 5 6]\n", 299 | " [7 8 9]]\n", 300 | "[1 5 9]\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "arr_8 = np.array([[1,0,0], \n", 306 | " [0,1,0],\n", 307 | " [0,0,1]])\n", 308 | "\n", 309 | "arr_9 = np.array([[1,2,3], \n", 310 | " [4,5,6],\n", 311 | " [7,8,9]])\n", 312 | "\n", 313 | "arr_matmul = np.matmul(arr_8, arr_9)\n", 314 | "print(arr_matmul)\n", 315 | "\n", 316 | "arr_diag = np.diag(arr_9)\n", 317 | "print(arr_diag)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "#### Creating new matrix" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 109, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "[[1. 0. 0.]\n", 337 | " [0. 1. 0.]\n", 338 | " [0. 0. 1.]]\n", 339 | "[[1. 1. 1.]\n", 340 | " [1. 1. 1.]\n", 341 | " [1. 1. 1.]]\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "identity_matrix = np.eye(3)\n", 347 | "print(identity_matrix)\n", 348 | "\n", 349 | "identity_ones = np.ones((3,3))\n", 350 | "print(identity_ones)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": {}, 356 | "source": [ 357 | "#### Array indexing and slicing" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 59, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "[[1]\n", 370 | " [4]]\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "# print(arr_3[0])\n", 376 | "# print(arr_3[1])\n", 377 | "# print(arr_3[0,0])\n", 378 | "# print(arr_3[0,1])\n", 379 | "# print(arr_3[0,2])\n", 380 | "# print(arr_3[1,0])\n", 381 | "# print(arr_3[1,1])\n", 382 | "# print(arr_3[1,2])\n", 383 | "\n", 384 | "# print(arr_3[0,-1])\n", 385 | "# print(arr_3[0,-2])\n", 386 | "# print(arr_3[0,-3])\n", 387 | "\n", 388 | "print(arr_3[:,0:1])\n", 389 | "# print(arr_3[1:])" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "#### numpy array filtering" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 69, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "index of value 4 : (array([3, 5, 6]),)\n", 409 | "index of the even numbers in the array : (array([1, 3, 5, 6]),)\n", 410 | "index of the odd numbers in the array : (array([0, 2, 4]),)\n", 411 | "(array([3, 4, 5, 6]),)\n" 412 | ] 413 | } 414 | ], 415 | "source": [ 416 | "arr = np.array([1, 2, 3, 4, 5, 4, 4])\n", 417 | "\n", 418 | "find_four = np.where(arr == 4)\n", 419 | "find_even = np.where(arr%2 == 0)\n", 420 | "find_odd = np.where(arr%2 == 1)\n", 421 | "\n", 422 | "print(\"index of value 4 : \", find_four)\n", 423 | "print(\"index of the even numbers in the array : \", find_even)\n", 424 | "print(\"index of the odd numbers in the array : \", find_odd)\n" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 72, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "[False False False True True True True]\n", 437 | "[4 5 4 4]\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "find = arr[arr >= 4]\n", 443 | "\n", 444 | "print(arr >= 4)\n", 445 | "print(find)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": null, 451 | "metadata": {}, 452 | "outputs": [], 453 | "source": [] 454 | } 455 | ], 456 | "metadata": { 457 | "kernelspec": { 458 | "display_name": "Python 3", 459 | "language": "python", 460 | "name": "python3" 461 | }, 462 | "language_info": { 463 | "codemirror_mode": { 464 | "name": "ipython", 465 | "version": 3 466 | }, 467 | "file_extension": ".py", 468 | "mimetype": "text/x-python", 469 | "name": "python", 470 | "nbconvert_exporter": "python", 471 | "pygments_lexer": "ipython3", 472 | "version": "3.7.6" 473 | } 474 | }, 475 | "nbformat": 4, 476 | "nbformat_minor": 4 477 | } 478 | -------------------------------------------------------------------------------- /lecture_8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 08: Data Analysis in Python: Pandas\n", 8 | "\n", 9 | "Instructor:\n", 10 | "
**Md Shahidullah Kawsar**\n", 11 | "
Data Scientist\n", 12 | "
IDARE, Houston, TX, USA\n", 13 | "\n", 14 | "#### Objectives:\n", 15 | "1. How to look at the data?\n", 16 | "2. Good data or bad data?\n", 17 | "3. Data Statistics\n", 18 | "\n", 19 | "### What is customer churn?\n", 20 | "Source: DataCamp and Mark Peterson\n", 21 | "\n", 22 | "When an existing customer, user, player, subscriber or any kind of return client stops doing business or ends the relationship with a company is called customer churn.\n", 23 | "\n", 24 | "**Contractual churn:** When a customer is under contract for a service and decides to cancel their service. Example: Cable TV, SaaS products (Software as a Service e.g. Dropbox).\n", 25 | "\n", 26 | "**Voluntary churn:** When a user voluntarily cancels a service and includes prepaid cell phones, streaming subscriptions.\n", 27 | "\n", 28 | "**Non-contractual churn:** When a customer is not under contract for a service and includes customer loyality at a retail location or online browsing. \n", 29 | "\n", 30 | "**Involuntary churn:** When a churn occurs not at the request of the customer. For example: credit card expiration, utilities being shut off by the provider.\n", 31 | "\n", 32 | "Most likely, you as a customer have cancelled a service for a variety of reasons including lack of usage, poor service or better price. \n", 33 | "\n", 34 | "Dataset: cellular usage dataset that consists of records of actual cell phone that include specific features such as\n", 35 | "\n", 36 | "1. **Account_Length**: the number of days the customer has the subscription with the telecom company\n", 37 | "\n", 38 | "2. **Vmail_Message**: the total number of voicemails the customer has sent\n", 39 | "\n", 40 | "3. **Total_mins**: the total number of minutes the customer has talked over the phone\n", 41 | "\n", 42 | "4. **CustServ_Calls**: the number of customer service calls the customer made\n", 43 | "\n", 44 | "5. **Churn**: yes and no - indicating whether or not the customer has churned\n", 45 | "\n", 46 | "6. **Intl_Plan**: yes and no - indicating whether or not the customer has international plan or not\n", 47 | "\n", 48 | "7. **Vmail_Plan**: yes and no - indicating whether or not the customer has voicemail plan or not\n", 49 | "\n", 50 | "8. **Total_calls**: the total number of calls the customer has made\n", 51 | "\n", 52 | "9. **Total_charges**: the total amount of bill in $ the customer has paid" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 1, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "import pandas as pd" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 10, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/html": [ 72 | "
\n", 73 | "\n", 86 | "\n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | "
Account_LengthVmail_MessageCustServ_CallsChurnIntl_PlanVmail_PlanTotal_minsTotal_callsTotal_charges
0128251nonoyes717.2303320.26
1107261nonoyes625.2332313.64
213700nonono539.4333224.89
38402noyesno564.8255263.70
47503noyesno512.0359238.99
\n", 164 | "
" 165 | ], 166 | "text/plain": [ 167 | " Account_Length Vmail_Message CustServ_Calls Churn Intl_Plan Vmail_Plan \\\n", 168 | "0 128 25 1 no no yes \n", 169 | "1 107 26 1 no no yes \n", 170 | "2 137 0 0 no no no \n", 171 | "3 84 0 2 no yes no \n", 172 | "4 75 0 3 no yes no \n", 173 | "\n", 174 | " Total_mins Total_calls Total_charges \n", 175 | "0 717.2 303 320.26 \n", 176 | "1 625.2 332 313.64 \n", 177 | "2 539.4 333 224.89 \n", 178 | "3 564.8 255 263.70 \n", 179 | "4 512.0 359 238.99 " 180 | ] 181 | }, 182 | "metadata": {}, 183 | "output_type": "display_data" 184 | } 185 | ], 186 | "source": [ 187 | "# reading a csv file as dataframe\n", 188 | "df = pd.read_csv(\"telecom_data.csv\")\n", 189 | "\n", 190 | "# print(df)\n", 191 | "display(df.head())\n", 192 | "\n", 193 | "# display(df.tail(15))" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 11, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "\n", 206 | "RangeIndex: 3333 entries, 0 to 3332\n", 207 | "Data columns (total 9 columns):\n", 208 | " # Column Non-Null Count Dtype \n", 209 | "--- ------ -------------- ----- \n", 210 | " 0 Account_Length 3333 non-null int64 \n", 211 | " 1 Vmail_Message 3333 non-null int64 \n", 212 | " 2 CustServ_Calls 3333 non-null int64 \n", 213 | " 3 Churn 3333 non-null object \n", 214 | " 4 Intl_Plan 3333 non-null object \n", 215 | " 5 Vmail_Plan 3333 non-null object \n", 216 | " 6 Total_mins 3333 non-null float64\n", 217 | " 7 Total_calls 3333 non-null int64 \n", 218 | " 8 Total_charges 3333 non-null float64\n", 219 | "dtypes: float64(2), int64(4), object(3)\n", 220 | "memory usage: 234.5+ KB\n", 221 | "None\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "print(df.info())" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 12, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/html": [ 237 | "
\n", 238 | "\n", 251 | "\n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | "
Account_LengthVmail_MessageCustServ_CallsTotal_minsTotal_callsTotal_charges
count3333.0000003333.0000003333.0000003333.0000003333.0000003333.000000
mean101.0648068.0990101.562856591.864776305.137114260.321791
std39.82210613.6883651.31549189.95425134.44816453.810896
min1.0000000.0000000.000000284.300000191.00000068.370000
25%74.0000000.0000001.000000531.500000282.000000224.220000
50%101.0000000.0000001.000000593.600000305.000000260.560000
75%127.00000020.0000002.000000652.400000328.000000295.410000
max243.00000051.0000009.000000885.000000416.000000460.630000
\n", 338 | "
" 339 | ], 340 | "text/plain": [ 341 | " Account_Length Vmail_Message CustServ_Calls Total_mins \\\n", 342 | "count 3333.000000 3333.000000 3333.000000 3333.000000 \n", 343 | "mean 101.064806 8.099010 1.562856 591.864776 \n", 344 | "std 39.822106 13.688365 1.315491 89.954251 \n", 345 | "min 1.000000 0.000000 0.000000 284.300000 \n", 346 | "25% 74.000000 0.000000 1.000000 531.500000 \n", 347 | "50% 101.000000 0.000000 1.000000 593.600000 \n", 348 | "75% 127.000000 20.000000 2.000000 652.400000 \n", 349 | "max 243.000000 51.000000 9.000000 885.000000 \n", 350 | "\n", 351 | " Total_calls Total_charges \n", 352 | "count 3333.000000 3333.000000 \n", 353 | "mean 305.137114 260.321791 \n", 354 | "std 34.448164 53.810896 \n", 355 | "min 191.000000 68.370000 \n", 356 | "25% 282.000000 224.220000 \n", 357 | "50% 305.000000 260.560000 \n", 358 | "75% 328.000000 295.410000 \n", 359 | "max 416.000000 460.630000 " 360 | ] 361 | }, 362 | "metadata": {}, 363 | "output_type": "display_data" 364 | } 365 | ], 366 | "source": [ 367 | "display(df.describe())" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 13, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [ 376 | "import numpy as np" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 25, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "2.68\n", 389 | "1.0\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "arr = np.array([1,-1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,3,2,4,5,7,6,8,10,9])\n", 395 | "\n", 396 | "avg = np.mean(arr)\n", 397 | "print(avg)\n", 398 | "\n", 399 | "median = np.median(arr)\n", 400 | "print(median)" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 26, 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "data": { 410 | "text/plain": [ 411 | "[-1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" 412 | ] 413 | }, 414 | "execution_count": 26, 415 | "metadata": {}, 416 | "output_type": "execute_result" 417 | } 418 | ], 419 | "source": [ 420 | "sorted(arr)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 28, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "no 2850\n", 432 | "yes 483\n", 433 | "Name: Churn, dtype: int64" 434 | ] 435 | }, 436 | "execution_count": 28, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "df['Churn'].value_counts()" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 29, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "data": { 452 | "text/plain": [ 453 | "no 3010\n", 454 | "yes 323\n", 455 | "Name: Intl_Plan, dtype: int64" 456 | ] 457 | }, 458 | "metadata": {}, 459 | "output_type": "display_data" 460 | } 461 | ], 462 | "source": [ 463 | "display(df['Intl_Plan'].value_counts())" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 30, 469 | "metadata": {}, 470 | "outputs": [ 471 | { 472 | "data": { 473 | "text/plain": [ 474 | "no 2411\n", 475 | "yes 922\n", 476 | "Name: Vmail_Plan, dtype: int64" 477 | ] 478 | }, 479 | "execution_count": 30, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "df['Vmail_Plan'].value_counts()" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 31, 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "data": { 495 | "text/plain": [ 496 | "1 1181\n", 497 | "2 759\n", 498 | "0 697\n", 499 | "3 429\n", 500 | "4 166\n", 501 | "5 66\n", 502 | "6 22\n", 503 | "7 9\n", 504 | "9 2\n", 505 | "8 2\n", 506 | "Name: CustServ_Calls, dtype: int64" 507 | ] 508 | }, 509 | "execution_count": 31, 510 | "metadata": {}, 511 | "output_type": "execute_result" 512 | } 513 | ], 514 | "source": [ 515 | "df['CustServ_Calls'].value_counts()" 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "#### Course outcome:\n", 523 | "\n", 524 | "1. How to use with Anaconda, jupyter notebook, and Github\n", 525 | "2. imporved our logical thinking by playing blockly games: Maze, Bird, Turtle\n", 526 | "3. Learned about Python data types, Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators\n", 527 | "4. Learned if-elseif-else condition, while, for loop\n", 528 | "5. Learned strig manipulation\n", 529 | "6. Python List, list slicing, changing, adding and removing list elements\n", 530 | "7. Python Data Structure: List, tuple, dictionary, set\n", 531 | "8. How to write functions in Python \n", 532 | "9. Solved 10 LeetCode problems and learned how to solve same problem from different perspectives\n", 533 | "10. Learned how to use math functions and NumPy operations\n", 534 | "11. Comparison between List and NumPy array\n", 535 | "12. Mathematical and Matrix operation in NumPy array\n", 536 | "13. NumPy array slicing and filtering\n", 537 | "14. Reading a csv file using Pandas, finding missing values, data statistics and detail information\n", 538 | "\n", 539 | "#python #leetcode #datascience #dataanalysis #numpy #pandas" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": null, 545 | "metadata": {}, 546 | "outputs": [], 547 | "source": [] 548 | } 549 | ], 550 | "metadata": { 551 | "kernelspec": { 552 | "display_name": "Python 3", 553 | "language": "python", 554 | "name": "python3" 555 | }, 556 | "language_info": { 557 | "codemirror_mode": { 558 | "name": "ipython", 559 | "version": 3 560 | }, 561 | "file_extension": ".py", 562 | "mimetype": "text/x-python", 563 | "name": "python", 564 | "nbconvert_exporter": "python", 565 | "pygments_lexer": "ipython3", 566 | "version": "3.7.6" 567 | } 568 | }, 569 | "nbformat": 4, 570 | "nbformat_minor": 4 571 | } 572 | --------------------------------------------------------------------------------