├── README.md ├── 7 Math operations in NumPy.ipynb ├── 4 Data structure.ipynb ├── 1. Python basics.ipynb ├── 5 Loop and writing functions.ipynb ├── 6 LeetCode Problems.ipynb ├── 3. Data Types and String Manipulation.ipynb └── 8 Data Analysis in Pandas.ipynb /README.md: -------------------------------------------------------------------------------- 1 | I taught Python to 200+ students through an online platform. These are the course materials I built for my students (Mostly graduate-level students from the Non-CS background). 2 | 3 | Video Lectures on Youtube: https://lnkd.in/g2nFUxZe 4 | 5 | Medium blogs for pandas: https://kawsar34.medium.com/list/learn-pandas-from-leetcode-a06903853aed 6 | 7 | # Fundamentals of Programming with Python 8 | 9 | Lecture 01: Introduction to Python 10 | - Familiarity with Google Colab, Anaconda, Jupyter Notebook, and GitHub 11 | - Learn logical thinking by playing blockly games: Maze 12 | - Learn about Python data types and basic Mathematical Operations 13 | 14 | Lecture 02: Conditional Programming 15 | - Learn if-else if-else and while loop by playing blockly games: Maze, Bird 16 | 17 | Lecture 03: Python Data Types and String Manipulation 18 | - Python data types: String, Integer, Float, Boolean 19 | - Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators 20 | - String manipulation 21 | 22 | Lecture 04: Python data structure 23 | - Python Data Structure: List, tuple, dictionary, set 24 | 25 | Lecture 05: Conditional logic, loop, and writing functions in Python 26 | - How to write if-else, while and for loop in Python 27 | - Writing functions in Python 28 | - LeetCode problems 29 | 30 | Lecture 6: LeetCode Problems 31 | - Math functions 32 | - Application of list, set, and dictionary 33 | - Solving the same problem in multiple ways 34 | - LeetCode problems: HW 2 solution 35 | 36 | Lecture 07: Mathematical operations in Python: NumPy 37 | - Comparison between List and NumPy array 38 | - 1D and 2D NumPy array 39 | - Mathematical and Matrix operation in NumPy array 40 | - NumPy array slicing and filtering 41 | 42 | Lecture 08: Data Analysis in Python: Pandas 43 | - How to look at the data? 44 | - Good data or bad data? 45 | - How to read a csv file 46 | - How to check data types and is there any missing values? 47 | - Data Statistics 48 | -------------------------------------------------------------------------------- /7 Math operations in NumPy.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. Mathematical functions in NumPy: https://numpy.org/doc/stable/reference/routines.math.html\n", 22 | "2. Multiplying a matrix by a matrix: https://www.youtube.com/watch?v=OMA2Mwo0aZg&ab_channel=KhanAcademy" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "#### Comparison of List and NumPy array" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "import numpy as np" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -2, 3, -4, 5, -6, 7, -8, 9]\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "list_1 = [1,2,3,4,5,6,7,8,9]\n", 56 | "list_2 = [1,-2,3,-4,5,-6,7,-8,9]\n", 57 | "\n", 58 | "list_add = list_1 + list_2\n", 59 | "print(list_add)\n", 60 | "\n", 61 | "# list_sub = list_1 - list_2\n", 62 | "# print(list_sub)\n", 63 | "\n", 64 | "# list_mul = list_1 * list_2\n", 65 | "# print(list_mul)\n", 66 | "\n", 67 | "# list_sub = list_1 / list_2\n", 68 | "# print(list_sub)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "\n", 81 | "\n", 82 | "int64\n", 83 | "addition of two array = [ 2 0 6 0 10 0 14 0 18]\n", 84 | "\n", 85 | "subtraction of two array = [ 0 4 0 8 0 12 0 16 0]\n", 86 | "multiplication of two array = [ 1 -4 9 -16 25 -36 49 -64 81]\n", 87 | "division of two array = [ 1. -1. 1. -1. 1. -1. 1. -1. 1.]\n", 88 | "remainder after dividing two arrays = [0 0 0 0 0 0 0 0 0]\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "arr_1 = np.array([1,2,3,4,5,6,7,8,9])\n", 94 | "arr_2 = np.array([1,-2,3,-4,5,-6,7,-8,9])\n", 95 | "\n", 96 | "print(type(arr_1))\n", 97 | "print(type(arr_2))\n", 98 | "print(arr_2.dtype)\n", 99 | "\n", 100 | "arr_add = arr_1 + arr_2\n", 101 | "print(\"addition of two array = \", arr_add)\n", 102 | "print(type(arr_add))\n", 103 | "\n", 104 | "# arr_add = np.add(arr_3, arr_4)\n", 105 | "# print(arr_add)\n", 106 | "\n", 107 | "arr_sub = arr_1 - arr_2\n", 108 | "print(\"subtraction of two array = \", arr_sub)\n", 109 | "\n", 110 | "# arr_sub = np.subtract(arr_3, arr_4)\n", 111 | "# print(arr_sub)\n", 112 | "\n", 113 | "arr_mul = arr_1 * arr_2\n", 114 | "print(\"multiplication of two array = \", arr_mul)\n", 115 | "\n", 116 | "# arr_mul = np.multiply(arr_3, arr_4)\n", 117 | "# print(arr_mul)\n", 118 | "\n", 119 | "arr_div = arr_1 / arr_2\n", 120 | "print(\"division of two array = \", arr_div)\n", 121 | "\n", 122 | "# arr_div = np.divide(arr_5, arr_6)\n", 123 | "# print(arr_div)\n", 124 | "\n", 125 | "arr_mod = np.mod(arr_1, arr_2)\n", 126 | "print(\"remainder after dividing two arrays = \", arr_mod)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "array_3 = \n", 139 | " [[1 2 3]\n", 140 | " [4 5 6]]\n", 141 | "\n", 142 | "(2, 3)\n", 143 | "array_4 = \n", 144 | " [[1 2 3]\n", 145 | " [4 5 6]]\n", 146 | "2d array addition = \n", 147 | " [[ 2 4 6]\n", 148 | " [ 8 10 12]]\n", 149 | "2d array multiplication = \n", 150 | " [[ 1 4 9]\n", 151 | " [16 25 36]]\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "arr_3 = np.array([[1,2,3], \n", 157 | " [4,5,6]])\n", 158 | "\n", 159 | "arr_4 = np.array([[1,2,3], \n", 160 | " [4,5,6]])\n", 161 | "\n", 162 | "print(\"array_3 = \\n\", arr_3)\n", 163 | "print(type(arr_3))\n", 164 | "print(arr_4.shape)\n", 165 | "print(\"array_4 = \\n\", arr_4)\n", 166 | "\n", 167 | "arr2d_add = arr_3 + arr_4\n", 168 | "print(\"2d array addition = \\n\", arr2d_add)\n", 169 | "\n", 170 | "arr2d_mul = arr_3 * arr_4\n", 171 | "print(\"2d array multiplication = \\n\", arr2d_mul)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 5, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "[ 2 4 8 16 32]\n", 184 | "[ 1 4 9 16 25]\n", 185 | "[1.05 2. 3.5 4. 5.75]\n", 186 | "[ 2. -2. 4. -4. 6.]\n", 187 | "[ 1. -2. 3. -4. 5.]\n", 188 | "[ 1. -2. 4. -4. 6.]\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "arr_5 = np.array([2, 2, 2, 2, 2])\n", 194 | "arr_6 = np.array([1, 2, 3, 4, 5])\n", 195 | "arr_7 = np.array([1.05, -2, 3.5, -4, 5.75])\n", 196 | "\n", 197 | "arr_pow = np.power(arr_5, arr_6)\n", 198 | "print(arr_pow)\n", 199 | "\n", 200 | "arr_square = np.square(arr_6)\n", 201 | "print(arr_square)\n", 202 | "\n", 203 | "arr_abs = np.abs(arr_7)\n", 204 | "print(arr_abs)\n", 205 | "\n", 206 | "arr_ceil = np.ceil(arr_7)\n", 207 | "print(arr_ceil)\n", 208 | "\n", 209 | "arr_floor = np.floor(arr_7)\n", 210 | "print(arr_floor)\n", 211 | "\n", 212 | "arr_round = np.round(arr_7)\n", 213 | "print(arr_round)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "#### array sum" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 6, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "25\n", 233 | "[3 4 5 6 7]\n", 234 | "[10 15]\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "arr_sum = np.sum([arr_5, arr_6])\n", 240 | "print(arr_sum)\n", 241 | "\n", 242 | "column_sum = np.sum([arr_5, arr_6], axis=0)\n", 243 | "print(column_sum)\n", 244 | "\n", 245 | "row_sum = np.sum([arr_5, arr_6], axis=1)\n", 246 | "print(row_sum)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "#### array product" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 7, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "3840\n", 266 | "[ 2 4 6 8 10]\n", 267 | "[ 32 120]\n" 268 | ] 269 | } 270 | ], 271 | "source": [ 272 | "arr_prod = np.prod([arr_5, arr_6])\n", 273 | "print(arr_prod)\n", 274 | "\n", 275 | "column_prod = np.prod([arr_5, arr_6], axis=0)\n", 276 | "print(column_prod)\n", 277 | "\n", 278 | "row_prod = np.prod([arr_5, arr_6], axis=1)\n", 279 | "print(row_prod)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "#### Matrix operation" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 8, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "[[1 2 3]\n", 299 | " [4 5 6]\n", 300 | " [7 8 9]]\n", 301 | "[1 5 9]\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "arr_8 = np.array([[1,0,0], \n", 307 | " [0,1,0],\n", 308 | " [0,0,1]])\n", 309 | "\n", 310 | "arr_9 = np.array([[1,2,3], \n", 311 | " [4,5,6],\n", 312 | " [7,8,9]])\n", 313 | "\n", 314 | "arr_matmul = np.matmul(arr_8, arr_9)\n", 315 | "print(arr_matmul)\n", 316 | "\n", 317 | "arr_diag = np.diag(arr_9)\n", 318 | "print(arr_diag)" 319 | ] 320 | }, 321 | { 322 | "cell_type": "markdown", 323 | "metadata": {}, 324 | "source": [ 325 | "#### Creating new matrix" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 9, 331 | "metadata": {}, 332 | "outputs": [ 333 | { 334 | "name": "stdout", 335 | "output_type": "stream", 336 | "text": [ 337 | "[[1. 0. 0.]\n", 338 | " [0. 1. 0.]\n", 339 | " [0. 0. 1.]]\n", 340 | "[[1. 1. 1.]\n", 341 | " [1. 1. 1.]\n", 342 | " [1. 1. 1.]]\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "identity_matrix = np.eye(3)\n", 348 | "print(identity_matrix)\n", 349 | "\n", 350 | "identity_ones = np.ones((3,3))\n", 351 | "print(identity_ones)" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "#### Array indexing and slicing" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 10, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "name": "stdout", 368 | "output_type": "stream", 369 | "text": [ 370 | "[[1]\n", 371 | " [4]]\n" 372 | ] 373 | } 374 | ], 375 | "source": [ 376 | "# print(arr_3[0])\n", 377 | "# print(arr_3[1])\n", 378 | "# print(arr_3[0,0])\n", 379 | "# print(arr_3[0,1])\n", 380 | "# print(arr_3[0,2])\n", 381 | "# print(arr_3[1,0])\n", 382 | "# print(arr_3[1,1])\n", 383 | "# print(arr_3[1,2])\n", 384 | "\n", 385 | "# print(arr_3[0,-1])\n", 386 | "# print(arr_3[0,-2])\n", 387 | "# print(arr_3[0,-3])\n", 388 | "\n", 389 | "print(arr_3[:,0:1])\n", 390 | "# print(arr_3[1:])" 391 | ] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "#### numpy array filtering" 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 11, 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "name": "stdout", 407 | "output_type": "stream", 408 | "text": [ 409 | "index of value 4 : (array([3, 5, 6]),)\n", 410 | "index of the even numbers in the array : (array([1, 3, 5, 6]),)\n", 411 | "index of the odd numbers in the array : (array([0, 2, 4]),)\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": 12, 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.8.5" 473 | } 474 | }, 475 | "nbformat": 4, 476 | "nbformat_minor": 4 477 | } 478 | -------------------------------------------------------------------------------- /4 Data structure.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 04: 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 | "- Python Data Structure: List, tuple, dictionary, set\n", 16 | "\n", 17 | "#### References: \n", 18 | "1. List vs Tuple: Difference Between List and Tuple: https://www.upgrad.com/blog/list-vs-tuple/" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "#### List Slicing" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "[['abul', 'babul', 'cabul', 'dabul'], [1111, 2222, 3333, 4444]]\n", 38 | "['abul', 'babul', 'cabul', 'dabul']\n", 39 | "[1111, 2222, 3333, 4444]\n", 40 | "abul\n", 41 | "1111\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "name = [\"abul\", \"babul\", \"cabul\", \"dabul\"]\n", 47 | "contact = [1111, 2222, 3333, 4444]\n", 48 | "\n", 49 | "phone_contacts = [name, contact]\n", 50 | "print(phone_contacts)\n", 51 | "\n", 52 | "print(phone_contacts[0])\n", 53 | "print(phone_contacts[1])\n", 54 | "print(phone_contacts[0][0])\n", 55 | "print(phone_contacts[1][0])" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "#### Checking length of a string" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "2\n", 75 | "4\n", 76 | "4\n", 77 | "5\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "print(len(phone_contacts))\n", 83 | "print(len(phone_contacts[0]))\n", 84 | "print(len(phone_contacts[1]))\n", 85 | "print(len(phone_contacts[0][1]))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "#### Checking the presence of an element in a list" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "True" 104 | ] 105 | }, 106 | "execution_count": 3, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "\"abul\" not in phone_contacts" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "#### Insert a new element in the list" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 4, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "[2, 4, 0, 6, 8, 10]\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "even = [2, 4, 6, 8, 10]\n", 137 | "even.insert(2, 0)\n", 138 | "print(even)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 5, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "[2, 4, 0, 6, 8, 10, 12]\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "even.append(12)\n", 156 | "\n", 157 | "print(even)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "#### How to reverse a list?" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 6, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "[7, 8, 4, -9, 9, 3, -1, -1, 0]\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "nums = [0,-1,-1,3,9,-9,4,8,7]\n", 182 | "nums.reverse()\n", 183 | "print(nums)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "#### How to count the frequency of a specific element in a list?" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 7, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "2\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print(nums.count(-1))" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "#### How to remove an element from a list?" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 8, 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "[7, 8, 4, -9, 3, -1, -1, 0]\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "nums.remove(9)\n", 232 | "print(nums)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "#### How to find the minimum value, maximum value and total sum of a list?" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 9, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "maximum value = 8\n", 252 | "minimum value = -9\n", 253 | "total sum = 11\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "print(\"maximum value = \", max(nums))\n", 259 | "print(\"minimum value = \", min(nums))\n", 260 | "print(\"total sum = \", sum(nums))" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "#### What will happen if you add two lists?" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 10, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "name": "stdout", 277 | "output_type": "stream", 278 | "text": [ 279 | "A + B = [9, 8, 7, 6, 99, 88, 77, 66]\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "A = [9, 8, 7, 6]\n", 285 | "B = [99, 88, 77, 66]\n", 286 | "print(\"A + B = \", A + B) #will contain all the element of A and B\n" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "#### How to sort a list?" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 11, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "[6, 7, 8, 9]\n", 306 | "[9, 8, 7, 6]\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "print(sorted(A, reverse= False))\n", 312 | "print(sorted(A, reverse= True))" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "### Tuple\n", 320 | "\n", 321 | "1. tuple is used with parentheses\n", 322 | "2. tuple is immutable --> most important\n" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 12, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "a = ('the', 'fault', 'in', 'our', 'stars')\n", 340 | "print(type(a))" 341 | ] 342 | }, 343 | { 344 | "cell_type": "markdown", 345 | "metadata": {}, 346 | "source": [ 347 | "#### Tuple slicing" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 13, 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "name": "stdout", 357 | "output_type": "stream", 358 | "text": [ 359 | "the\n", 360 | "stars\n", 361 | "('fault', 'in')\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "print(a[0])\n", 367 | "print(a[-1])\n", 368 | "print(a[1:3])" 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "#### Immutability" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 14, 381 | "metadata": {}, 382 | "outputs": [], 383 | "source": [ 384 | "T = (8, 9, 'absolute', [1, 2])\n", 385 | "# T[0] = 2 #will throw an error" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "**You cannot change any value inside a tuple and you cannot insert or delete as well**" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 15, 398 | "metadata": {}, 399 | "outputs": [ 400 | { 401 | "name": "stdout", 402 | "output_type": "stream", 403 | "text": [ 404 | "3\n", 405 | "7\n", 406 | "1\n", 407 | "10\n", 408 | "1\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "#some functions that can be used with tuple\n", 414 | "Z = (2, 7, 1)\n", 415 | "print(len(Z))\n", 416 | "print(max(Z))\n", 417 | "print(min(Z))\n", 418 | "print(sum(Z))\n", 419 | "print(Z.count(2))" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "**Similarities in List and Tuple**\n", 427 | "- The two data structures are both sequence data types that store collections of items.\n", 428 | "- Items of any data type can be stored in them.\n", 429 | "- Items can be accessed by their index.\n", 430 | "\n", 431 | "**Difference between List and Tuple**\n", 432 | "- List is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed." 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "### Set\n", 440 | "1. It works with a curly brace--> {}\n", 441 | "2. One item can appear only once\n", 442 | "3. You cannot slice item from set, unlike lists or tuples" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 16, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "name": "stdout", 452 | "output_type": "stream", 453 | "text": [ 454 | "{12, 13, 14}\n", 455 | "\n", 456 | "3\n", 457 | "[12, 13, 14]\n", 458 | "{1, 2, 3, 4, 5, 7, 9}\n", 459 | "{1, 9, 7}\n", 460 | "{3, 5}\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "A = {12, 13, 14, 12}\n", 466 | "print(A) \n", 467 | "print(type(A))\n", 468 | "print(len(A))\n", 469 | "\n", 470 | "# convert to list\n", 471 | "A_converted = list(A)\n", 472 | "print(A_converted)\n", 473 | "\n", 474 | "A = {1, 3, 5, 7, 9}\n", 475 | "B = {1, 2, 4, 7, 9}\n", 476 | "\n", 477 | "print(A.union(B))\n", 478 | "print(A.intersection(B))\n", 479 | "print(A.difference(B))" 480 | ] 481 | }, 482 | { 483 | "cell_type": "markdown", 484 | "metadata": {}, 485 | "source": [ 486 | "### Dictionary" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 17, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': 4444}\n", 499 | "1111\n", 500 | "2222\n", 501 | "3333\n", 502 | "4444\n", 503 | "dict_keys(['abul', 'babul', 'cabul', 'dabul'])\n", 504 | "dict_values([1111, 2222, 3333, 4444])\n" 505 | ] 506 | } 507 | ], 508 | "source": [ 509 | "phone_contacts = {\"abul\":1111, \"babul\":2222, \"cabul\":3333, \"dabul\":4444}\n", 510 | "\n", 511 | "print(phone_contacts)\n", 512 | "print(phone_contacts[\"abul\"])\n", 513 | "print(phone_contacts[\"babul\"])\n", 514 | "print(phone_contacts[\"cabul\"])\n", 515 | "print(phone_contacts[\"dabul\"])\n", 516 | "\n", 517 | "print(phone_contacts.keys())\n", 518 | "print(phone_contacts.values())\n", 519 | "# print(phone_contacts.items())" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 18, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "name": "stdout", 529 | "output_type": "stream", 530 | "text": [ 531 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555]}\n" 532 | ] 533 | } 534 | ], 535 | "source": [ 536 | "# adding a new phone number of the existing contact\n", 537 | "phone_contacts[\"dabul\"] = [4444,5555]\n", 538 | "print(phone_contacts)" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 19, 544 | "metadata": {}, 545 | "outputs": [ 546 | { 547 | "name": "stdout", 548 | "output_type": "stream", 549 | "text": [ 550 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666}\n" 551 | ] 552 | } 553 | ], 554 | "source": [ 555 | "# adding a new contact\n", 556 | "phone_contacts[\"fabul\"] = 6666\n", 557 | "print(phone_contacts)" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 20, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "name": "stdout", 567 | "output_type": "stream", 568 | "text": [ 569 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666, 'gabul': 7777}\n" 570 | ] 571 | } 572 | ], 573 | "source": [ 574 | "# updating a dictionary\n", 575 | "new_dict = {\"gabul\":7777}\n", 576 | "phone_contacts.update(new_dict)\n", 577 | "print(phone_contacts)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 21, 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "name": "stdout", 587 | "output_type": "stream", 588 | "text": [ 589 | "{'abul': 1111, 'babul': 2222, 'cabul': 3333, 'dabul': [4444, 5555], 'fabul': 6666}\n" 590 | ] 591 | } 592 | ], 593 | "source": [ 594 | "# removing a contact from the phonebook\n", 595 | "del phone_contacts[\"gabul\"]\n", 596 | "print(phone_contacts)" 597 | ] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": {}, 602 | "source": [ 603 | "### Summary\n", 604 | "\n", 605 | "- List - indexed storage\n", 606 | "- Dictionary - Logical Association storage\n", 607 | "- Tuple - Memory optimized and Secure data storage\n", 608 | "- Set - unindexed data storage, widely useful for removing redundancy and mathematics ops" 609 | ] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": null, 614 | "metadata": {}, 615 | "outputs": [], 616 | "source": [] 617 | } 618 | ], 619 | "metadata": { 620 | "kernelspec": { 621 | "display_name": "Python 3", 622 | "language": "python", 623 | "name": "python3" 624 | }, 625 | "language_info": { 626 | "codemirror_mode": { 627 | "name": "ipython", 628 | "version": 3 629 | }, 630 | "file_extension": ".py", 631 | "mimetype": "text/x-python", 632 | "name": "python", 633 | "nbconvert_exporter": "python", 634 | "pygments_lexer": "ipython3", 635 | "version": "3.8.5" 636 | } 637 | }, 638 | "nbformat": 4, 639 | "nbformat_minor": 4 640 | } 641 | -------------------------------------------------------------------------------- /1. Python basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "2MPjGERTdfjx" 17 | }, 18 | "source": [ 19 | "# Lecture 01: Introduction to Python\n", 20 | "\n", 21 | "Instructor:\n", 22 | "
**Md Shahidullah Kawsar**\n", 23 | "
Data Scientist\n", 24 | "
IDARE, Houston, TX, USA\n", 25 | "\n", 26 | "#### Objectives:\n", 27 | "1. Familiarity with Google Colab, Anaconda, jupyter notebook, and Github\n", 28 | "2. Learn logical thinking by playing blockly games: Maze\n", 29 | "3. Learn about Python data types, Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators\n", 30 | "\n", 31 | "#### References: \n", 32 | "1. Google Colab: https://colab.research.google.com/\n", 33 | "2. Download Anaconda installer: https://www.anaconda.com/products/individual\n", 34 | "3. How to Install Anaconda (Python) on Windows 10:\n", 35 | "https://www.youtube.com/watch?v=syijLJ3oQzU\n", 36 | "4. Easily Install Anaconda Python on MacOS:\n", 37 | "https://www.youtube.com/watch?v=V6ZAv7hBH6Y&ab_channel=JustUnderstandingDataJustUnderstandingData\n", 38 | "5. https://blockly.games/\n", 39 | "6. Python - Basic Operators: https://www.tutorialspoint.com/python/python_basic_operators.htm\n", 40 | "7. Python Fundamentals: https://github.com/SKawsar/Python_Fundamentals\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": { 46 | "id": "7zSU6kgydfjy" 47 | }, 48 | "source": [ 49 | "- Q1: The sum of two even numbers will be always odd. True or **False**\n", 50 | "\n", 51 | "
The sum of two even numbers will be always even.\n", 52 | "
The sum of two odd numbers will be always even.\n", 53 | "
The sum of one even number and one odd number will be always odd." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 42, 59 | "metadata": { 60 | "id": "wRT1IioRdfj0", 61 | "outputId": "3a255b08-5780-4cc7-9400-2c72ab56e8be", 62 | "colab": { 63 | "base_uri": "https://localhost:8080/" 64 | } 65 | }, 66 | "outputs": [ 67 | { 68 | "output_type": "execute_result", 69 | "data": { 70 | "text/plain": [ 71 | "4" 72 | ] 73 | }, 74 | "metadata": {}, 75 | "execution_count": 42 76 | } 77 | ], 78 | "source": [ 79 | "# integer\n", 80 | "# পূর্ণসংখ্যা \n", 81 | "# কমেন্ট\n", 82 | "2 + 2 " 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "source": [ 88 | "4 + 10" 89 | ], 90 | "metadata": { 91 | "id": "52z-19y8L2re", 92 | "outputId": "88dfc27d-cee5-4184-e16f-a26c3f8d2305", 93 | "colab": { 94 | "base_uri": "https://localhost:8080/" 95 | } 96 | }, 97 | "execution_count": 43, 98 | "outputs": [ 99 | { 100 | "output_type": "execute_result", 101 | "data": { 102 | "text/plain": [ 103 | "14" 104 | ] 105 | }, 106 | "metadata": {}, 107 | "execution_count": 43 108 | } 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "source": [ 114 | "3 + 3 " 115 | ], 116 | "metadata": { 117 | "id": "fx1z5iKPL23o", 118 | "outputId": "bcc197f7-29d2-4908-9032-36a61c9aecdc", 119 | "colab": { 120 | "base_uri": "https://localhost:8080/" 121 | } 122 | }, 123 | "execution_count": 44, 124 | "outputs": [ 125 | { 126 | "output_type": "execute_result", 127 | "data": { 128 | "text/plain": [ 129 | "6" 130 | ] 131 | }, 132 | "metadata": {}, 133 | "execution_count": 44 134 | } 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "source": [ 140 | "2+3" 141 | ], 142 | "metadata": { 143 | "id": "k6xfblRJL9fB", 144 | "outputId": "964c71d1-f923-4591-c9ad-713f1745bc9e", 145 | "colab": { 146 | "base_uri": "https://localhost:8080/" 147 | } 148 | }, 149 | "execution_count": 45, 150 | "outputs": [ 151 | { 152 | "output_type": "execute_result", 153 | "data": { 154 | "text/plain": [ 155 | "5" 156 | ] 157 | }, 158 | "metadata": {}, 159 | "execution_count": 45 160 | } 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": { 166 | "id": "ZX6ecNGrdfj0" 167 | }, 168 | "source": [ 169 | "- Q2: A positive value is always greater than a negative value. **True** or False\n" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 46, 175 | "metadata": { 176 | "id": "WAcga3ZQdfj1", 177 | "outputId": "3525aba7-7db7-4b46-e130-fccdb51fb45a", 178 | "colab": { 179 | "base_uri": "https://localhost:8080/" 180 | } 181 | }, 182 | "outputs": [ 183 | { 184 | "output_type": "execute_result", 185 | "data": { 186 | "text/plain": [ 187 | "True" 188 | ] 189 | }, 190 | "metadata": {}, 191 | "execution_count": 46 192 | } 193 | ], 194 | "source": [ 195 | "1>-1" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "source": [ 201 | "-9>1" 202 | ], 203 | "metadata": { 204 | "id": "YRFWdWI4MAFt", 205 | "outputId": "4fd50b2e-c2ae-453e-d643-a7f9aa8cb7aa", 206 | "colab": { 207 | "base_uri": "https://localhost:8080/" 208 | } 209 | }, 210 | "execution_count": 47, 211 | "outputs": [ 212 | { 213 | "output_type": "execute_result", 214 | "data": { 215 | "text/plain": [ 216 | "False" 217 | ] 218 | }, 219 | "metadata": {}, 220 | "execution_count": 47 221 | } 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": { 227 | "id": "bShjVn2Odfj1" 228 | }, 229 | "source": [ 230 | "- Q3: Can you divide a number by zero? Yes or **No**\n" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 48, 236 | "metadata": { 237 | "id": "mB5LHyFNdfj1" 238 | }, 239 | "outputs": [], 240 | "source": [ 241 | "# 0/0" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": { 247 | "id": "4t-kajZQdfj2" 248 | }, 249 | "source": [ 250 | "- Q4. If x is less than 1 and y is greater than 1, then x/y is less than 1. **True** or False\n" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 49, 256 | "metadata": { 257 | "id": "ijfK_0ludfj2", 258 | "outputId": "3d501be0-1ca0-4811-98d3-f66b075dd5e2", 259 | "colab": { 260 | "base_uri": "https://localhost:8080/" 261 | } 262 | }, 263 | "outputs": [ 264 | { 265 | "output_type": "execute_result", 266 | "data": { 267 | "text/plain": [ 268 | "True" 269 | ] 270 | }, 271 | "metadata": {}, 272 | "execution_count": 49 273 | } 274 | ], 275 | "source": [ 276 | "# 0.5 float \n", 277 | "x = 0.5\n", 278 | "y = 2\n", 279 | "\n", 280 | "x/y < 1" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": { 286 | "id": "HjZB6LbBdfj2" 287 | }, 288 | "source": [ 289 | "- Q5. The word 'Google' contains 5 unique characters. **True** or False\n" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 50, 295 | "metadata": { 296 | "id": "lJC6rD6rdfj3", 297 | "outputId": "e20efb6a-35ee-4d00-d98c-68a94a591e94", 298 | "colab": { 299 | "base_uri": "https://localhost:8080/" 300 | } 301 | }, 302 | "outputs": [ 303 | { 304 | "output_type": "stream", 305 | "name": "stdout", 306 | "text": [ 307 | "6\n", 308 | "29\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "# \"Google\", 'Google'\n", 314 | "\n", 315 | "print(len(\"Google\"))\n", 316 | "print(len(\"My phone number is 2082222 !&\"))" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "source": [ 322 | "\"G\" == \"g\"" 323 | ], 324 | "metadata": { 325 | "id": "QFWv0AJ9MeOc", 326 | "outputId": "a0017b6d-8856-44fb-c115-9c7482761fcd", 327 | "colab": { 328 | "base_uri": "https://localhost:8080/" 329 | } 330 | }, 331 | "execution_count": 51, 332 | "outputs": [ 333 | { 334 | "output_type": "execute_result", 335 | "data": { 336 | "text/plain": [ 337 | "False" 338 | ] 339 | }, 340 | "metadata": {}, 341 | "execution_count": 51 342 | } 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "source": [ 348 | "52 == 53" 349 | ], 350 | "metadata": { 351 | "id": "CHsPRCcZMeXV", 352 | "outputId": "211d584d-c2fa-4f0d-be51-0460352dbe83", 353 | "colab": { 354 | "base_uri": "https://localhost:8080/" 355 | } 356 | }, 357 | "execution_count": 52, 358 | "outputs": [ 359 | { 360 | "output_type": "execute_result", 361 | "data": { 362 | "text/plain": [ 363 | "False" 364 | ] 365 | }, 366 | "metadata": {}, 367 | "execution_count": 52 368 | } 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": { 374 | "id": "W2cNF-0bdfj5" 375 | }, 376 | "source": [ 377 | "- Q6. If x>1 and y>1, then x multiplied by y is greater than 1. **True** or False\n" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 53, 383 | "metadata": { 384 | "id": "1c2ezzredfj5", 385 | "outputId": "897bf952-d6fa-4a5f-9f87-96c20475903c", 386 | "colab": { 387 | "base_uri": "https://localhost:8080/" 388 | } 389 | }, 390 | "outputs": [ 391 | { 392 | "output_type": "execute_result", 393 | "data": { 394 | "text/plain": [ 395 | "True" 396 | ] 397 | }, 398 | "metadata": {}, 399 | "execution_count": 53 400 | } 401 | ], 402 | "source": [ 403 | "x = 100\n", 404 | "y = 10\n", 405 | "\n", 406 | "x*y > 1" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": { 412 | "id": "4TFwXI3kdfj5" 413 | }, 414 | "source": [ 415 | "- Q7. The product of two odd numbers and one even number will always be an even number. **True** or False\n" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 54, 421 | "metadata": { 422 | "id": "CXmJqlArdfj6", 423 | "outputId": "65e5e1ad-19dd-4c39-eae7-d274a9726f07", 424 | "colab": { 425 | "base_uri": "https://localhost:8080/" 426 | } 427 | }, 428 | "outputs": [ 429 | { 430 | "output_type": "stream", 431 | "name": "stdout", 432 | "text": [ 433 | "6\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "a = 1\n", 439 | "b = 3\n", 440 | "c = 2\n", 441 | "\n", 442 | "d = a*b*c\n", 443 | "print(d)" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": { 449 | "id": "KKx0BxtOdfj6" 450 | }, 451 | "source": [ 452 | "- Q8. Set A = {1, 3, 5}, Set B = {1, 2, 3}; Set A intersection Set B = {1, 2, 3, 5}. True or **False**\n" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 55, 458 | "metadata": { 459 | "id": "NgxJkm6ldfj6", 460 | "outputId": "b82aab1f-437d-414f-f02a-22458f108aa6", 461 | "colab": { 462 | "base_uri": "https://localhost:8080/" 463 | } 464 | }, 465 | "outputs": [ 466 | { 467 | "output_type": "execute_result", 468 | "data": { 469 | "text/plain": [ 470 | "{1, 3}" 471 | ] 472 | }, 473 | "metadata": {}, 474 | "execution_count": 55 475 | } 476 | ], 477 | "source": [ 478 | "a = {1,3,5}\n", 479 | "b = {1,2,3}\n", 480 | "\n", 481 | "a.intersection(b)" 482 | ] 483 | }, 484 | { 485 | "cell_type": "markdown", 486 | "metadata": { 487 | "id": "qUNf7NDMdfj6" 488 | }, 489 | "source": [ 490 | "- Q9. A = [1,2,3,1,2,3,12,3], the number of unique integers present in A are 4. **True** or False\n" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "source": [ 496 | "A = [1,2,3,1,2,3,12,3]\n", 497 | "\n", 498 | "print(set(A))\n", 499 | "print(len(set(A)))" 500 | ], 501 | "metadata": { 502 | "id": "fejrOES-eeF2", 503 | "outputId": "88a50648-6eaa-4884-8fd6-7aea76ca4cc7", 504 | "colab": { 505 | "base_uri": "https://localhost:8080/" 506 | } 507 | }, 508 | "execution_count": 56, 509 | "outputs": [ 510 | { 511 | "output_type": "stream", 512 | "name": "stdout", 513 | "text": [ 514 | "{1, 2, 3, 12}\n", 515 | "4\n" 516 | ] 517 | } 518 | ] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "metadata": { 523 | "id": "WqaKHN_Ydfj6" 524 | }, 525 | "source": [ 526 | "- Q10. 1 is not equal to 1.00. True or **False**" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 57, 532 | "metadata": { 533 | "id": "lXIHT3b2dfj6", 534 | "outputId": "500a5f2b-2fc4-4f64-d913-ae8db6b51d1c", 535 | "colab": { 536 | "base_uri": "https://localhost:8080/" 537 | } 538 | }, 539 | "outputs": [ 540 | { 541 | "output_type": "execute_result", 542 | "data": { 543 | "text/plain": [ 544 | "False" 545 | ] 546 | }, 547 | "metadata": {}, 548 | "execution_count": 57 549 | } 550 | ], 551 | "source": [ 552 | "1 != 1.00" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": { 558 | "id": "55e4JXpydfj7" 559 | }, 560 | "source": [ 561 | "### Python Data Types\n", 562 | "
**str** : string, text\n", 563 | "
**int** : inetger numbers\n", 564 | "
**float** : real numbers, decimal values\n", 565 | "
**bool** : True, False" 566 | ] 567 | } 568 | ], 569 | "metadata": { 570 | "kernelspec": { 571 | "display_name": "Python 3", 572 | "language": "python", 573 | "name": "python3" 574 | }, 575 | "language_info": { 576 | "codemirror_mode": { 577 | "name": "ipython", 578 | "version": 3 579 | }, 580 | "file_extension": ".py", 581 | "mimetype": "text/x-python", 582 | "name": "python", 583 | "nbconvert_exporter": "python", 584 | "pygments_lexer": "ipython3", 585 | "version": "3.7.6" 586 | }, 587 | "colab": { 588 | "name": "Lecture_1.ipynb", 589 | "provenance": [], 590 | "include_colab_link": true 591 | } 592 | }, 593 | "nbformat": 4, 594 | "nbformat_minor": 0 595 | } 596 | -------------------------------------------------------------------------------- /5 Loop and writing functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 05: 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 | "HW2:\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.8.5" 620 | } 621 | }, 622 | "nbformat": 4, 623 | "nbformat_minor": 4 624 | } 625 | -------------------------------------------------------------------------------- /6 LeetCode Problems.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lecture 6: 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 | "#### 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 | "4. HW 2 solution\n", 19 | "\n", 20 | "#### References\n", 21 | "HW 2: 1. https://leetcode.com/problems/maximum-product-of-three-numbers/\n", 22 | "
HW 2: 2. https://leetcode.com/problems/richest-customer-wealth/\n", 23 | "
HW 2: 3. https://leetcode.com/problems/shuffle-the-array/\n", 24 | "
HW 2: 4. https://leetcode.com/problems/valid-perfect-square/\n", 25 | "
5. https://leetcode.com/problems/power-of-two/\n", 26 | "
6. https://leetcode.com/problems/contains-duplicate/\n", 27 | "
7. https://leetcode.com/problems/two-sum/submissions/\n", 28 | "
Self Study: 8. https://leetcode.com/problems/roman-to-integer/\n", 29 | "
Self Study: 9. https://leetcode.com/problems/unique-morse-code-words/\n", 30 | "\n", 31 | "References:\n", 32 | "
[1] চটপট গণিত- লগ কী : https://www.youtube.com/watch?v=YHqbj5c2OIo&ab_channel=ChamokHasan" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "**HW2 Problem 1: Maximum Product of Three Numbers**\n", 40 | "\n", 41 | "Given an integer array nums, find three numbers whose product is maximum and return the maximum product.\n", 42 | "\n", 43 | "**Example 1:**\n", 44 | "
Input: nums = [1,2,3]\n", 45 | "
Output: 6\n", 46 | "\n", 47 | "**Example 2:**\n", 48 | "
Input: nums = [1,2,3,4]\n", 49 | "
Output: 24\n", 50 | "\n", 51 | "**Example 3:**\n", 52 | "
Input: nums = [-1,-2,-3]\n", 53 | "
Output: -6" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 9, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "example = [-1,2,4,4]\n", 63 | "\n", 64 | "def maximumProduct(nums):\n", 65 | " \n", 66 | " nums = sorted(nums)\n", 67 | " print(\"sorted nums = \", nums)\n", 68 | " \n", 69 | " prod_1 = nums[0]*nums[1]*nums[-1]\n", 70 | " print(\"prod_1 = \", prod_1)\n", 71 | " prod_2 = nums[-1]*nums[-2]*nums[-3]\n", 72 | " print(\"prod_2 = \", prod_2)\n", 73 | " \n", 74 | " return max(prod_1, prod_2)\n", 75 | " " 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 10, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "sorted nums = [1, 2, 3]\n", 88 | "prod_1 = 6\n", 89 | "prod_2 = 6\n", 90 | "6\n", 91 | "----------------------------------------\n", 92 | "sorted nums = [1, 2, 3, 4]\n", 93 | "prod_1 = 8\n", 94 | "prod_2 = 24\n", 95 | "24\n", 96 | "----------------------------------------\n", 97 | "sorted nums = [-4, -2, -1, 1, 2, 6]\n", 98 | "prod_1 = 48\n", 99 | "prod_2 = 12\n", 100 | "48\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "p1_example_1 = maximumProduct([1,2,3])\n", 106 | "print(p1_example_1)\n", 107 | "print(\"----------------------------------------\")\n", 108 | "\n", 109 | "p1_example_2 = maximumProduct([1,2,3,4])\n", 110 | "print(p1_example_2)\n", 111 | "print(\"----------------------------------------\")\n", 112 | "\n", 113 | "p1_example_3 = maximumProduct([-1,-2,-4,1,2,6])\n", 114 | "print(p1_example_3)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "**HW2 Problem 2: 1672. Richest Customer Wealth**\n", 122 | "\n", 123 | "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", 124 | "\n", 125 | "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", 126 | "\n", 127 | "**Example 1:**\n", 128 | "
Input: accounts = [[1,2,3],[3,2,1]]\n", 129 | "
Output: 6\n", 130 | "
Explanation:\n", 131 | "
1st customer has wealth = 1 + 2 + 3 = 6\n", 132 | "
2nd customer has wealth = 3 + 2 + 1 = 6\n", 133 | "
Both customers are considered the richest with a wealth of 6 each, so return 6.\n", 134 | "\n", 135 | "**Example 2:**\n", 136 | "
Input: accounts = [[1,5],[7,3],[3,5]]\n", 137 | "
Output: 10\n", 138 | "
Explanation: \n", 139 | "
1st customer has wealth = 6\n", 140 | "
2nd customer has wealth = 10 \n", 141 | "
3rd customer has wealth = 8\n", 142 | "
The 2nd customer is the richest with a wealth of 10.\n", 143 | "\n", 144 | "**Example 3:**\n", 145 | "
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]\n", 146 | "
Output: 17" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 11, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "def maximumWealth(accounts):\n", 156 | " \n", 157 | " wealth = []\n", 158 | " \n", 159 | " for element in range(len(accounts)):\n", 160 | " \n", 161 | " print(accounts[element])\n", 162 | " \n", 163 | " wealth.append(sum(accounts[element]))\n", 164 | " \n", 165 | " print(\"wealth = \", wealth)\n", 166 | " return max(wealth)\n", 167 | " " 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 12, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "[1, 2, 3]\n", 180 | "[3, 2, 1]\n", 181 | "wealth = [6, 6]\n", 182 | "6\n", 183 | "----------------------------------------\n", 184 | "[1, 5]\n", 185 | "[7, 3]\n", 186 | "[3, 5]\n", 187 | "wealth = [6, 10, 8]\n", 188 | "10\n", 189 | "----------------------------------------\n", 190 | "[2, 8, 7]\n", 191 | "[7, 1, 3]\n", 192 | "[1, 9, 5]\n", 193 | "wealth = [17, 11, 15]\n", 194 | "17\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "p2_example_1 = maximumWealth([[1,2,3],[3,2,1]])\n", 200 | "print(p2_example_1)\n", 201 | "print(\"----------------------------------------\")\n", 202 | "\n", 203 | "p2_example_2 = maximumWealth([[1,5],[7,3],[3,5]])\n", 204 | "print(p2_example_2)\n", 205 | "print(\"----------------------------------------\")\n", 206 | "\n", 207 | "p2_example_3 = maximumWealth([[2,8,7],[7,1,3],[1,9,5]])\n", 208 | "print(p2_example_3)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "**HW2 Problem 3: Shuffle the Array**\n", 216 | "\n", 217 | "Given the array nums consisting of 2n elements in the form [x_1,x_2,...,x_n,y_1,y_2,...,y_n].\n", 218 | "\n", 219 | "Return the array in the form [x_1,y_1,x_2,y_2,...,x_n,y_n].\n", 220 | "\n", 221 | "**Example 1:**\n", 222 | "
Input: nums = [2,5,1,3,4,7], n = 3\n", 223 | "
Output: [2,3,5,4,1,7] \n", 224 | "
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", 225 | "\n", 226 | "**Example 2:**\n", 227 | "
Input: nums = [1,2,3,4,4,3,2,1], n = 4\n", 228 | "
Output: [1,4,2,3,3,2,4,1]\n", 229 | "\n", 230 | "**Example 3:**\n", 231 | "
Input: nums = [1,1,2,2], n = 2\n", 232 | "
Output: [1,2,1,2]" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 13, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "def shuffle(nums, n):\n", 242 | " \n", 243 | " first_half = nums[:n]\n", 244 | " last_half = nums[n:]\n", 245 | " \n", 246 | " shuffle = []\n", 247 | " \n", 248 | " for i in range(n):\n", 249 | " shuffle.append(first_half[i])\n", 250 | " \n", 251 | " print(\"after adding first half = \", shuffle)\n", 252 | " \n", 253 | " shuffle.append(last_half[i])\n", 254 | " \n", 255 | " print(\"after adding last half = \", shuffle)\n", 256 | " \n", 257 | " return shuffle\n", 258 | " " 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 14, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "after adding first half = [2]\n", 271 | "after adding last half = [2, 3]\n", 272 | "after adding first half = [2, 3, 5]\n", 273 | "after adding last half = [2, 3, 5, 4]\n", 274 | "after adding first half = [2, 3, 5, 4, 1]\n", 275 | "after adding last half = [2, 3, 5, 4, 1, 7]\n", 276 | "[2, 3, 5, 4, 1, 7]\n", 277 | "----------------------------------------\n", 278 | "after adding first half = [1]\n", 279 | "after adding last half = [1, 4]\n", 280 | "after adding first half = [1, 4, 2]\n", 281 | "after adding last half = [1, 4, 2, 3]\n", 282 | "after adding first half = [1, 4, 2, 3, 3]\n", 283 | "after adding last half = [1, 4, 2, 3, 3, 2]\n", 284 | "after adding first half = [1, 4, 2, 3, 3, 2, 4]\n", 285 | "after adding last half = [1, 4, 2, 3, 3, 2, 4, 1]\n", 286 | "[1, 4, 2, 3, 3, 2, 4, 1]\n", 287 | "----------------------------------------\n", 288 | "after adding first half = [1]\n", 289 | "after adding last half = [1, 2]\n", 290 | "after adding first half = [1, 2, 1]\n", 291 | "after adding last half = [1, 2, 1, 2]\n", 292 | "[1, 2, 1, 2]\n" 293 | ] 294 | } 295 | ], 296 | "source": [ 297 | "p3_example_1 = shuffle([2,5,1,3,4,7], 3)\n", 298 | "print(p3_example_1)\n", 299 | "print(\"----------------------------------------\")\n", 300 | "\n", 301 | "p3_example_2 = shuffle([1,2,3,4,4,3,2,1], 4)\n", 302 | "print(p3_example_2)\n", 303 | "print(\"----------------------------------------\")\n", 304 | "\n", 305 | "p3_example_3 = shuffle([1,1,2,2], 2)\n", 306 | "print(p3_example_3)" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "**HW2 Problem 4: Valid Perfect Square**\n", 314 | "\n", 315 | "Given a positive integer num, write a function which returns True if num is a perfect square else False.\n", 316 | "\n", 317 | "**Example 1:**\n", 318 | "
Input: num = 16\n", 319 | "
Output: true\n", 320 | "\n", 321 | "**Example 2:**\n", 322 | "
Input: num = 14\n", 323 | "
Output: false" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 15, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "def isPerfectSquare(num):\n", 333 | " \n", 334 | " print(\"square root = \", num**0.5)\n", 335 | " \n", 336 | " print(\"square root (integer part only) = \", int(num**0.5))\n", 337 | " \n", 338 | " return int(num**0.5) == num**0.5\n" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 16, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "square root = 3.7416573867739413\n", 351 | "square root (integer part only) = 3\n", 352 | "False\n", 353 | "----------------------------------------\n", 354 | "square root = 4.0\n", 355 | "square root (integer part only) = 4\n", 356 | "True\n" 357 | ] 358 | } 359 | ], 360 | "source": [ 361 | "p4_example_1 = isPerfectSquare(14)\n", 362 | "print(p4_example_1)\n", 363 | "print(\"----------------------------------------\")\n", 364 | "\n", 365 | "p4_example_2 = isPerfectSquare(16)\n", 366 | "print(p4_example_2)" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": {}, 372 | "source": [ 373 | "**Problem 1: Power of Two**\n", 374 | "\n", 375 | "Given an integer n, return true if it is a power of two. Otherwise, return false.\n", 376 | "\n", 377 | "An integer n is a power of two, if there exists an integer x such that n == 2^x.\n", 378 | "\n", 379 | "**Example 1:**\n", 380 | "
Input: n = 1\n", 381 | "
Output: true\n", 382 | "
Explanation: 2^0 = 1\n", 383 | "\n", 384 | "**Example 2:**\n", 385 | "
Input: n = 16\n", 386 | "
Output: true\n", 387 | "
Explanation: 2^4 = 16\n", 388 | "\n", 389 | "**Example 3:**\n", 390 | "
Input: n = 3\n", 391 | "
Output: false\n", 392 | "\n", 393 | "**Example 4:**\n", 394 | "
Input: n = 4\n", 395 | "
Output: true\n", 396 | "\n", 397 | "**Example 5:**\n", 398 | "
Input: n = 5\n", 399 | "
Output: false" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "n == 2^x\n", 407 | "
=> log(n) = log(2^x)\n", 408 | "
=> log(n) = x*log(2)\n", 409 | "
=> x = log(n)/log(2)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 17, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [ 418 | "import math\n", 419 | "\n", 420 | "def isPowerOfTwo(n):\n", 421 | "\n", 422 | " if n <= 0:\n", 423 | " return False\n", 424 | "\n", 425 | " x = math.log10(n)/math.log10(2)\n", 426 | " return n == 2**int(x)" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 18, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "name": "stdout", 436 | "output_type": "stream", 437 | "text": [ 438 | "True\n", 439 | "-----------------------------\n", 440 | "True\n", 441 | "-----------------------------\n", 442 | "False\n", 443 | "-----------------------------\n", 444 | "True\n", 445 | "-----------------------------\n", 446 | "False\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "p1_example_1 = isPowerOfTwo(1)\n", 452 | "print(p1_example_1)\n", 453 | "\n", 454 | "print(\"-----------------------------\")\n", 455 | "\n", 456 | "p1_example_2 = isPowerOfTwo(16)\n", 457 | "print(p1_example_2)\n", 458 | "\n", 459 | "print(\"-----------------------------\")\n", 460 | "\n", 461 | "p1_example_3 = isPowerOfTwo(3)\n", 462 | "print(p1_example_3)\n", 463 | "\n", 464 | "print(\"-----------------------------\")\n", 465 | "\n", 466 | "p1_example_4 = isPowerOfTwo(4)\n", 467 | "print(p1_example_4)\n", 468 | "\n", 469 | "print(\"-----------------------------\")\n", 470 | "\n", 471 | "p1_example_5 = isPowerOfTwo(5)\n", 472 | "print(p1_example_5)" 473 | ] 474 | }, 475 | { 476 | "cell_type": "markdown", 477 | "metadata": {}, 478 | "source": [ 479 | "**Problem 2: Contains Duplicate**\n", 480 | "\n", 481 | "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", 482 | "\n", 483 | "**Example 1:**\n", 484 | "
Input: nums = [1,2,3,1]\n", 485 | "
Output: true\n", 486 | "\n", 487 | "**Example 2:**\n", 488 | "
Input: nums = [1,2,3,4]\n", 489 | "
Output: false\n", 490 | "\n", 491 | "**Example 3:**\n", 492 | "
Input: nums = [1,1,1,3,3,4,3,2,4,2]\n", 493 | "
Output: true" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 19, 499 | "metadata": {}, 500 | "outputs": [], 501 | "source": [ 502 | "def containsDuplicate(nums):\n", 503 | "\n", 504 | " # solution 1\n", 505 | " return len(nums) != len(set(nums))" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 20, 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "name": "stdout", 515 | "output_type": "stream", 516 | "text": [ 517 | "True\n", 518 | "-----------------------------\n", 519 | "False\n", 520 | "-----------------------------\n", 521 | "True\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "p2_example_1 = containsDuplicate([1,2,3,1])\n", 527 | "print(p2_example_1)\n", 528 | "\n", 529 | "print(\"-----------------------------\")\n", 530 | "\n", 531 | "p2_example_2 = containsDuplicate([1,2,3,4])\n", 532 | "print(p2_example_2)\n", 533 | "\n", 534 | "print(\"-----------------------------\")\n", 535 | "\n", 536 | "p2_example_3 = containsDuplicate([1,1,1,3,3,4,3,2,4,2])\n", 537 | "print(p2_example_3)" 538 | ] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": {}, 543 | "source": [ 544 | "**Problem 3: Two Sum**\n", 545 | "\n", 546 | "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\n", 547 | "\n", 548 | "You may assume that each input would have exactly one solution, and you may not use the same element twice.\n", 549 | "\n", 550 | "You can return the answer in any order.\n", 551 | "\n", 552 | "**Example 1:**\n", 553 | "
Input: nums = [2,7,11,15], target = 9\n", 554 | "
Output: [0,1]\n", 555 | "
Output: Because nums[0] + nums[1] == 9, we return [0, 1].\n", 556 | "\n", 557 | "**Example 2:**\n", 558 | "
Input: nums = [3,2,4], target = 6\n", 559 | "
Output: [1,2]\n", 560 | "\n", 561 | "**Example 3:**\n", 562 | "
Input: nums = [3,3], target = 6\n", 563 | "
Output: [0,1]" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 21, 569 | "metadata": {}, 570 | "outputs": [], 571 | "source": [ 572 | "def twoSum(nums, target):\n", 573 | "\n", 574 | " val_index_dict = {}\n", 575 | "\n", 576 | " for index in range(0, len(nums)):\n", 577 | " goal = target - nums[index]\n", 578 | " print(\"item = \", nums[index], \"goal = \", goal)\n", 579 | "\n", 580 | " if(goal in val_index_dict):\n", 581 | " return [val_index_dict[goal], index]\n", 582 | "\n", 583 | " val_index_dict[nums[index]] = index\n", 584 | " print(\"dictionary = \", val_index_dict)" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 22, 590 | "metadata": {}, 591 | "outputs": [ 592 | { 593 | "name": "stdout", 594 | "output_type": "stream", 595 | "text": [ 596 | "item = 2 goal = 7\n", 597 | "dictionary = {2: 0}\n", 598 | "item = 7 goal = 2\n", 599 | "[0, 1]\n", 600 | "-----------------------------\n", 601 | "item = 3 goal = 3\n", 602 | "dictionary = {3: 0}\n", 603 | "item = 2 goal = 4\n", 604 | "dictionary = {3: 0, 2: 1}\n", 605 | "item = 4 goal = 2\n", 606 | "[1, 2]\n", 607 | "-----------------------------\n", 608 | "item = 3 goal = 3\n", 609 | "dictionary = {3: 0}\n", 610 | "item = 3 goal = 3\n", 611 | "[0, 1]\n" 612 | ] 613 | } 614 | ], 615 | "source": [ 616 | "p3_example_1 = twoSum([2,7,11,15], 9)\n", 617 | "print(p3_example_1)\n", 618 | "\n", 619 | "print(\"-----------------------------\")\n", 620 | "\n", 621 | "p3_example_2 = twoSum([3,2,4], 6)\n", 622 | "print(p3_example_2)\n", 623 | "\n", 624 | "print(\"-----------------------------\")\n", 625 | "\n", 626 | "p3_example_3 = twoSum([3,3], 6)\n", 627 | "print(p3_example_3)" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": null, 633 | "metadata": {}, 634 | "outputs": [], 635 | "source": [] 636 | } 637 | ], 638 | "metadata": { 639 | "kernelspec": { 640 | "display_name": "Python 3", 641 | "language": "python", 642 | "name": "python3" 643 | }, 644 | "language_info": { 645 | "codemirror_mode": { 646 | "name": "ipython", 647 | "version": 3 648 | }, 649 | "file_extension": ".py", 650 | "mimetype": "text/x-python", 651 | "name": "python", 652 | "nbconvert_exporter": "python", 653 | "pygments_lexer": "ipython3", 654 | "version": "3.8.5" 655 | } 656 | }, 657 | "nbformat": 4, 658 | "nbformat_minor": 4 659 | } 660 | -------------------------------------------------------------------------------- /3. Data Types and String Manipulation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Lecture_3.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "language_info": { 16 | "name": "python" 17 | } 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "source": [ 33 | "# Lecture 03: Python Data Types and String Manipulation\n", 34 | "Instructor:\n", 35 | "Md Shahidullah Kawsar,\n", 36 | "Data Scientist, IDARE, Houston, TX, USA\n", 37 | "\n", 38 | "#### Objectives:\n", 39 | "1. Learn about Python data types, Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators\n", 40 | "2. Learn string manipulation\n", 41 | "\n", 42 | "#### References:\n", 43 | "1. Python - Basic Operators: https://www.tutorialspoint.com/python/python_basic_operators.html\n" 44 | ], 45 | "metadata": { 46 | "id": "RPo9iWFO2nEn" 47 | } 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "source": [ 52 | "**str** : string, text\n", 53 | "
**int** : inetger numbers\n", 54 | "
**float** : real numbers, decimal values\n", 55 | "
**bool** : True, False" 56 | ], 57 | "metadata": { 58 | "id": "pbtfPN7o3NxN" 59 | } 60 | }, 61 | { 62 | "cell_type": "code", 63 | "source": [ 64 | "name = \"Lionel Andres Messi\"\n", 65 | "birth_year = 1987\n", 66 | "height = 5.6\n", 67 | "is_he_a_goat = True\n", 68 | "\n", 69 | "print(\"Player name : \", name)\n", 70 | "print(type(name))\n", 71 | "\n", 72 | "print(\"Birth Year : \", birth_year)\n", 73 | "print(type(birth_year))\n", 74 | "\n", 75 | "print(\"Height : \", height)\n", 76 | "print(type(height))\n", 77 | "\n", 78 | "print(\"Is he the greatest of all time? \", is_he_a_goat)\n", 79 | "print(type(is_he_a_goat))" 80 | ], 81 | "metadata": { 82 | "id": "tYkP-3Ak3SVx", 83 | "outputId": "4cf97e30-c1c4-4d94-b3ad-65697f2df18f", 84 | "colab": { 85 | "base_uri": "https://localhost:8080/" 86 | } 87 | }, 88 | "execution_count": null, 89 | "outputs": [ 90 | { 91 | "output_type": "stream", 92 | "name": "stdout", 93 | "text": [ 94 | "Player name : Lionel Andres Messi\n", 95 | "\n", 96 | "Birth Year : 1987\n", 97 | "\n", 98 | "Height : 5.6\n", 99 | "\n", 100 | "Is he the greatest of all time? True\n", 101 | "\n" 102 | ] 103 | } 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "source": [ 109 | "#### Mathematical Operations" 110 | ], 111 | "metadata": { 112 | "id": "VXFfTU4z3pIV" 113 | } 114 | }, 115 | { 116 | "cell_type": "code", 117 | "source": [ 118 | "a = 2 #integer\n", 119 | "b = 3 #integer \n", 120 | "\n", 121 | "# Addition\n", 122 | "print(\"a+b = \", a+b) \n", 123 | "\n", 124 | "# Multiplication\n", 125 | "print(\"a*b = \", a*b) \n", 126 | "\n", 127 | "# Subtraction\n", 128 | "print(\"a-b = \", a-b) \n", 129 | "\n", 130 | "# Modulus: Divides left hand operand by right hand operand and returns remainder\n", 131 | "print(\"a%b = \", a%b) \n", 132 | "\n", 133 | "# Division\n", 134 | "print(\"a/b = \", a/b)\n", 135 | "\n", 136 | "# Floor Division - The division of operands \n", 137 | "# where the result is the quotient in which \n", 138 | "# the digits after the decimal point are removed. \n", 139 | "# But if one of the operands is negative, the result is floored, \n", 140 | "# i.e., rounded away from zero (towards negative infinity)\n", 141 | "print(\"a//b = \", a//b) \n", 142 | "\n", 143 | "# Exponent: Performs exponential (power) calculation on operators\n", 144 | "print(\"a**b = \", a**b) " 145 | ], 146 | "metadata": { 147 | "id": "sHlbJ7zq3rPX", 148 | "outputId": "438e09f1-c69b-486d-8253-56a6d171c125", 149 | "colab": { 150 | "base_uri": "https://localhost:8080/" 151 | } 152 | }, 153 | "execution_count": null, 154 | "outputs": [ 155 | { 156 | "output_type": "stream", 157 | "name": "stdout", 158 | "text": [ 159 | "a+b = 5\n", 160 | "a*b = 6\n", 161 | "a-b = -1\n", 162 | "a%b = 2\n", 163 | "a/b = 0.6666666666666666\n", 164 | "a//b = 0\n", 165 | "a**b = 8\n" 166 | ] 167 | } 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "source": [ 173 | "b = 2\n", 174 | "c = 3\n", 175 | "d = 4\n", 176 | "\n", 177 | "print(b*c-d)\n", 178 | "print(b-c*d)\n", 179 | "# definitely multiplication will occur first. To avoid confusion, please use brackets such as b - (c*d). Make it readable." 180 | ], 181 | "metadata": { 182 | "id": "nfskvY7vRGH6", 183 | "outputId": "67f276fd-10b8-433b-b952-dccd7a57913a", 184 | "colab": { 185 | "base_uri": "https://localhost:8080/" 186 | } 187 | }, 188 | "execution_count": null, 189 | "outputs": [ 190 | { 191 | "output_type": "stream", 192 | "name": "stdout", 193 | "text": [ 194 | "2\n", 195 | "-10\n" 196 | ] 197 | } 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "source": [ 203 | "#### Comparison Operators" 204 | ], 205 | "metadata": { 206 | "id": "JqykIP3X3sPO" 207 | } 208 | }, 209 | { 210 | "cell_type": "code", 211 | "source": [ 212 | "a = -1\n", 213 | "b = 1\n", 214 | "\n", 215 | "print(\"a == b : \", a == b)\n", 216 | "print(\"a != b : \", a != b)\n", 217 | "print(\"a >= b : \", a >= b)\n", 218 | "print(\"a <= b : \", a <= b)\n", 219 | "print(\"a > b : \", a > b)\n", 220 | "print(\"a < b : \", a < b)" 221 | ], 222 | "metadata": { 223 | "id": "DCKnf-s43vUe", 224 | "outputId": "5441d249-304f-4671-fc71-6c5a020a67a7", 225 | "colab": { 226 | "base_uri": "https://localhost:8080/" 227 | } 228 | }, 229 | "execution_count": null, 230 | "outputs": [ 231 | { 232 | "output_type": "stream", 233 | "name": "stdout", 234 | "text": [ 235 | "a == b : False\n", 236 | "a != b : True\n", 237 | "a >= b : False\n", 238 | "a <= b : True\n", 239 | "a > b : False\n", 240 | "a < b : True\n" 241 | ] 242 | } 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "source": [ 248 | "#### Logical operators" 249 | ], 250 | "metadata": { 251 | "id": "Ir6PPHIm3wF6" 252 | } 253 | }, 254 | { 255 | "cell_type": "code", 256 | "source": [ 257 | "# and, or\n", 258 | "\n", 259 | "# ang logic\n", 260 | "print(True and True) # 1 and 1\n", 261 | "print(True and False) # 1 and 0\n", 262 | "print(False and True) # 0 and 1\n", 263 | "print(False and False) # 0 and 0\n", 264 | "\n", 265 | "# or logic\n", 266 | "print(True or True) # 1 or 1\n", 267 | "print(True or False) # 1 or 0\n", 268 | "print(False or True) # 0 or 1\n", 269 | "print(False or False) # 0 or 0" 270 | ], 271 | "metadata": { 272 | "id": "2E0-Cuih31yh", 273 | "outputId": "b77e0f4b-c2f2-4081-eae7-fe752cb76358", 274 | "colab": { 275 | "base_uri": "https://localhost:8080/" 276 | } 277 | }, 278 | "execution_count": null, 279 | "outputs": [ 280 | { 281 | "output_type": "stream", 282 | "name": "stdout", 283 | "text": [ 284 | "True\n", 285 | "False\n", 286 | "False\n", 287 | "False\n", 288 | "True\n", 289 | "True\n", 290 | "True\n", 291 | "False\n" 292 | ] 293 | } 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "source": [ 299 | "a = 1\n", 300 | "b = 1\n", 301 | "c = 2\n", 302 | "d = 2\n", 303 | "\n", 304 | "print(\"a == b and c == d : \", a == b and c == d)\n", 305 | "print(\"a == b or c == d : \", a == b or c == d)\n", 306 | "print(\"not c == d : \", not c == d)" 307 | ], 308 | "metadata": { 309 | "id": "peZcS5VHMTP3", 310 | "outputId": "48f6f21a-d30d-456d-c25d-4c77393cf4f6", 311 | "colab": { 312 | "base_uri": "https://localhost:8080/" 313 | } 314 | }, 315 | "execution_count": null, 316 | "outputs": [ 317 | { 318 | "output_type": "stream", 319 | "name": "stdout", 320 | "text": [ 321 | "a == b and c == d : True\n", 322 | "a == b or c == d : True\n", 323 | "not c == d : False\n" 324 | ] 325 | } 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "source": [ 331 | "#### Membership operators" 332 | ], 333 | "metadata": { 334 | "id": "PT3epyIy32N3" 335 | } 336 | }, 337 | { 338 | "cell_type": "code", 339 | "source": [ 340 | "a = \"abul\"\n", 341 | "b = \"babul\"\n", 342 | "\n", 343 | "print(\"a in b : \", a in b)\n", 344 | "print(\"b not in a : \", b not in a)" 345 | ], 346 | "metadata": { 347 | "id": "Qnc3tGJQ36S1", 348 | "outputId": "9aad8277-ce71-4614-930f-44e5ddfbeac1", 349 | "colab": { 350 | "base_uri": "https://localhost:8080/" 351 | } 352 | }, 353 | "execution_count": null, 354 | "outputs": [ 355 | { 356 | "output_type": "stream", 357 | "name": "stdout", 358 | "text": [ 359 | "a in b : True\n", 360 | "b not in a : True\n" 361 | ] 362 | } 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "source": [ 368 | "#### String Manipulation\n", 369 | "- How to capitalize the first character of a string?" 370 | ], 371 | "metadata": { 372 | "id": "KOp4aqNez1YL" 373 | } 374 | }, 375 | { 376 | "cell_type": "code", 377 | "source": [ 378 | "name = \"ronaldo\"\n", 379 | "updated_name = name.capitalize()\n", 380 | "\n", 381 | "print(updated_name)" 382 | ], 383 | "metadata": { 384 | "id": "N3JF3EVHzxqz", 385 | "outputId": "a5fddff7-2799-474a-d547-75c1e131bfe7", 386 | "colab": { 387 | "base_uri": "https://localhost:8080/" 388 | } 389 | }, 390 | "execution_count": null, 391 | "outputs": [ 392 | { 393 | "output_type": "stream", 394 | "name": "stdout", 395 | "text": [ 396 | "Ronaldo\n" 397 | ] 398 | } 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "source": [ 404 | "- How to find the frequency of the substring in a string?" 405 | ], 406 | "metadata": { 407 | "id": "gYcrwKZW0iu8" 408 | } 409 | }, 410 | { 411 | "cell_type": "code", 412 | "source": [ 413 | "name_count = name.count(\"o\")\n", 414 | "print(name_count)" 415 | ], 416 | "metadata": { 417 | "id": "t0wn6ZNRzxxP", 418 | "outputId": "979e149d-89de-4c62-b158-d82185614ef7", 419 | "colab": { 420 | "base_uri": "https://localhost:8080/" 421 | } 422 | }, 423 | "execution_count": null, 424 | "outputs": [ 425 | { 426 | "output_type": "stream", 427 | "name": "stdout", 428 | "text": [ 429 | "2\n" 430 | ] 431 | } 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "source": [ 437 | "\n", 438 | "- How to find if a string starts or ends with a specific substring?" 439 | ], 440 | "metadata": { 441 | "id": "Q2STTCkO0k-L" 442 | } 443 | }, 444 | { 445 | "cell_type": "code", 446 | "source": [ 447 | "name_startswith = name.startswith(\"o\")\n", 448 | "print(name_startswith)\n", 449 | "\n", 450 | "name_endswith = name.endswith(\"o\")\n", 451 | "print(name_endswith)" 452 | ], 453 | "metadata": { 454 | "id": "_WnxOORvzyKE", 455 | "outputId": "5da29e0b-8d30-45e8-e7ef-949039041128", 456 | "colab": { 457 | "base_uri": "https://localhost:8080/" 458 | } 459 | }, 460 | "execution_count": null, 461 | "outputs": [ 462 | { 463 | "output_type": "stream", 464 | "name": "stdout", 465 | "text": [ 466 | "False\n", 467 | "True\n" 468 | ] 469 | } 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "source": [ 475 | "#### String slicing" 476 | ], 477 | "metadata": { 478 | "id": "OYnKrQBh0dIY" 479 | } 480 | }, 481 | { 482 | "cell_type": "code", 483 | "source": [ 484 | "# python is a 0-indexed based programming language\n", 485 | "# the index of the last character = len(var) -1\n", 486 | "# the last index in a range is always exclusive\n", 487 | "var = \"I love Bangladesh\"\n", 488 | "\n", 489 | "print(\"length of var = \", len(var))\n", 490 | "\n", 491 | "print(var)\n", 492 | "print(var[0])\n", 493 | "print(var[7])\n", 494 | "print(var[16])\n", 495 | "print(var[-1])\n", 496 | "print(var[-5])\n", 497 | "\n", 498 | "print(var[2:6])\n", 499 | "print(var[2:])\n", 500 | "print(var[7:17])\n", 501 | "print(var[7:])\n", 502 | "\n", 503 | "print(var[0:6])\n", 504 | "print(var[:6])\n", 505 | "\n", 506 | "print(var[-10:])" 507 | ], 508 | "metadata": { 509 | "id": "FQMGLSnQ0et8", 510 | "outputId": "a4af1ee5-eeb5-4709-e18b-7618c6f9f0c4", 511 | "colab": { 512 | "base_uri": "https://localhost:8080/" 513 | } 514 | }, 515 | "execution_count": null, 516 | "outputs": [ 517 | { 518 | "output_type": "stream", 519 | "name": "stdout", 520 | "text": [ 521 | "length of var = 17\n", 522 | "I love Bangladesh\n", 523 | "I\n", 524 | "B\n", 525 | "h\n", 526 | "h\n", 527 | "a\n", 528 | "love\n", 529 | "love Bangladesh\n", 530 | "Bangladesh\n", 531 | "Bangladesh\n", 532 | "I love\n", 533 | "I love\n", 534 | "Bangladesh\n" 535 | ] 536 | } 537 | ] 538 | }, 539 | { 540 | "cell_type": "markdown", 541 | "source": [ 542 | "\n", 543 | "- How to find the index of a specific character in a string?" 544 | ], 545 | "metadata": { 546 | "id": "N1ko6f9u0qXA" 547 | } 548 | }, 549 | { 550 | "cell_type": "code", 551 | "source": [ 552 | "text = \"Happy New Year\"\n", 553 | "\n", 554 | "print(text.find(\"p\"))\n", 555 | "print(text.index(\"a\"))" 556 | ], 557 | "metadata": { 558 | "id": "F2jQZws9zyNF", 559 | "outputId": "7081cfcf-3f09-4380-d581-44dd2b8a0fed", 560 | "colab": { 561 | "base_uri": "https://localhost:8080/" 562 | } 563 | }, 564 | "execution_count": 5, 565 | "outputs": [ 566 | { 567 | "output_type": "stream", 568 | "name": "stdout", 569 | "text": [ 570 | "2\n", 571 | "1\n" 572 | ] 573 | } 574 | ] 575 | }, 576 | { 577 | "cell_type": "markdown", 578 | "source": [ 579 | "\n", 580 | "- How to convert a string into lower case and upper case characters?\n" 581 | ], 582 | "metadata": { 583 | "id": "VWIn_WJp0s0R" 584 | } 585 | }, 586 | { 587 | "cell_type": "code", 588 | "source": [ 589 | "name = \"ShakibAlHasan\"\n", 590 | "\n", 591 | "print(name.lower())\n", 592 | "print(name.upper())" 593 | ], 594 | "metadata": { 595 | "id": "5GY9xut-zyRD", 596 | "outputId": "c5e1d3f0-2f6d-4e83-d289-cc7ea2a9e939", 597 | "colab": { 598 | "base_uri": "https://localhost:8080/" 599 | } 600 | }, 601 | "execution_count": 7, 602 | "outputs": [ 603 | { 604 | "output_type": "stream", 605 | "name": "stdout", 606 | "text": [ 607 | "shakibalhasan\n", 608 | "SHAKIBALHASAN\n" 609 | ] 610 | } 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "source": [ 616 | "print(\"electrical engineering\" == \"Electrical Engineering\".lower())\n", 617 | "print(\"electrical engineering \".rstrip() == \"Electrical Engineering\".lower())\n", 618 | "print(\" electrical engineering\".lstrip() == \"Electrical Engineering\".lower())" 619 | ], 620 | "metadata": { 621 | "id": "lFydnuj-5fcE", 622 | "outputId": "14bc5862-fc41-40ee-8a88-a77c09752ea8", 623 | "colab": { 624 | "base_uri": "https://localhost:8080/" 625 | } 626 | }, 627 | "execution_count": 15, 628 | "outputs": [ 629 | { 630 | "output_type": "stream", 631 | "name": "stdout", 632 | "text": [ 633 | "True\n", 634 | "True\n", 635 | "True\n" 636 | ] 637 | } 638 | ] 639 | }, 640 | { 641 | "cell_type": "markdown", 642 | "source": [ 643 | "I eat rice.\n", 644 | "Eating rice regulary is not a good habit.\n", 645 | "\n", 646 | "eat, ate, eaten => eat\n", 647 | "\n", 648 | "Electrical Engineering " 649 | ], 650 | "metadata": { 651 | "id": "2Ss7uG8X5fxE" 652 | } 653 | }, 654 | { 655 | "cell_type": "markdown", 656 | "source": [ 657 | "\n", 658 | "- How to remove spaces from a string?" 659 | ], 660 | "metadata": { 661 | "id": "OoyvIzgX0xgK" 662 | } 663 | }, 664 | { 665 | "cell_type": "code", 666 | "source": [ 667 | "# see lecture 4" 668 | ], 669 | "metadata": { 670 | "id": "oRxmJdYqzyTo" 671 | }, 672 | "execution_count": null, 673 | "outputs": [] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "source": [ 678 | "\n", 679 | "- How to replace a substring inside a string?\n" 680 | ], 681 | "metadata": { 682 | "id": "2nv15dSl0z1B" 683 | } 684 | }, 685 | { 686 | "cell_type": "code", 687 | "source": [ 688 | "text = \"Electrical Enginering\"\n", 689 | "\n", 690 | "print(text.replace(\"Enginering\", \"Engineering\"))" 691 | ], 692 | "metadata": { 693 | "id": "685H-nkkzyV8", 694 | "outputId": "021855bf-08df-4942-dc03-12393d0f4f46", 695 | "colab": { 696 | "base_uri": "https://localhost:8080/" 697 | } 698 | }, 699 | "execution_count": 19, 700 | "outputs": [ 701 | { 702 | "output_type": "stream", 703 | "name": "stdout", 704 | "text": [ 705 | "Electrical Engineering\n" 706 | ] 707 | } 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "source": [ 713 | "text = \"electrical enginering\"\n", 714 | "print(text.capitalize())" 715 | ], 716 | "metadata": { 717 | "id": "KSYefQlK8l6L", 718 | "outputId": "1d0a425a-690d-4e0f-a748-e2f1bb4b7bd3", 719 | "colab": { 720 | "base_uri": "https://localhost:8080/" 721 | } 722 | }, 723 | "execution_count": 21, 724 | "outputs": [ 725 | { 726 | "output_type": "stream", 727 | "name": "stdout", 728 | "text": [ 729 | "Electrical enginering\n" 730 | ] 731 | } 732 | ] 733 | }, 734 | { 735 | "cell_type": "markdown", 736 | "source": [ 737 | "- How to find the presence of an alphanumeric character, alphabet or digit?" 738 | ], 739 | "metadata": { 740 | "id": "PRL4pSo402h9" 741 | } 742 | }, 743 | { 744 | "cell_type": "code", 745 | "source": [ 746 | "name = \"shakibalhasan\"\n", 747 | "\n", 748 | "print(name.isalnum())\n", 749 | "print(name.isdigit())\n", 750 | "print(name.isalpha())\n", 751 | "print(name.isupper())\n", 752 | "print(name.islower())" 753 | ], 754 | "metadata": { 755 | "id": "8qsLA0ee04oR", 756 | "outputId": "5a965dfd-4cb7-498f-e040-98a5b034048e", 757 | "colab": { 758 | "base_uri": "https://localhost:8080/" 759 | } 760 | }, 761 | "execution_count": 31, 762 | "outputs": [ 763 | { 764 | "output_type": "stream", 765 | "name": "stdout", 766 | "text": [ 767 | "True\n", 768 | "False\n", 769 | "True\n", 770 | "False\n", 771 | "True\n" 772 | ] 773 | } 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "source": [ 779 | "" 780 | ], 781 | "metadata": { 782 | "id": "uuTKBjAFU2Ob" 783 | }, 784 | "execution_count": null, 785 | "outputs": [] 786 | } 787 | ] 788 | } 789 | -------------------------------------------------------------------------------- /8 Data Analysis in Pandas.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 | "Details about Kawsar :) \n", 15 | "If you are interested to find me on Linkedin, here's my profile link: https://www.linkedin.com/in/mdshahidullahkawsar/\n", 16 | "Please include a message with the connection request: \"I am your student at Python Fundamentals course\"\n", 17 | "It will help me to recognize you. Besides, I encourage you to connect with your classmates and don't forget to include a text message with the connection request.\n", 18 | "\n", 19 | "Here are my additional profile links if you are interested to visit:\n", 20 | "
Datacamp Profile: https://www.datacamp.com/profile/mdshahidullahkawsar\n", 21 | "
Github Profile: https://github.com/SKawsar \n", 22 | "
Medium profile: https://kawsar34.medium.com/\n", 23 | "
Tableau profile: https://public.tableau.com/profile/kawsar\n", 24 | "
Instagram: @tasnimandkawsar\n", 25 | "\n", 26 | "# What's Next?\n", 27 | "1. Module 2: Data Visualization with Python\n", 28 | "2. Udemy: LeetCode In Python: 50 Algorithms Coding Interview Questions: https://www.udemy.com/share/102Kpi3@Jsrgad5rUToBCj4VA34b0aUmfa4AKB_Ct59lEHzAYhYSBBrMuxAqQrTMwpFb5146/\n", 29 | "3. datacamp.com\n", 30 | "\n", 31 | "\n", 32 | "#### Objectives:\n", 33 | "1. How to look at the data?\n", 34 | "2. Good data or bad data?\n", 35 | "3. Data Statistics\n", 36 | "\n", 37 | "### What is customer churn?\n", 38 | "Source: DataCamp and Mark Peterson\n", 39 | "\n", 40 | "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", 41 | "\n", 42 | "**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", 43 | "\n", 44 | "**Voluntary churn:** When a user voluntarily cancels a service and includes prepaid cell phones, streaming subscriptions.\n", 45 | "\n", 46 | "**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", 47 | "\n", 48 | "**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", 49 | "\n", 50 | "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", 51 | "\n", 52 | "Dataset: cellular usage dataset that consists of records of actual cell phone that include specific features such as\n", 53 | "\n", 54 | "1. **Account_Length**: the number of days the customer has the subscription with the telecom company\n", 55 | "\n", 56 | "2. **Vmail_Message**: the total number of voicemails the customer has sent\n", 57 | "\n", 58 | "3. **Total_mins**: the total number of minutes the customer has talked over the phone\n", 59 | "\n", 60 | "4. **CustServ_Calls**: the number of customer service calls the customer made\n", 61 | "\n", 62 | "5. **Churn**: yes and no - indicating whether or not the customer has churned\n", 63 | "\n", 64 | "6. **Intl_Plan**: yes and no - indicating whether or not the customer has international plan or not\n", 65 | "\n", 66 | "7. **Vmail_Plan**: yes and no - indicating whether or not the customer has voicemail plan or not\n", 67 | "\n", 68 | "8. **Total_calls**: the total number of calls the customer has made\n", 69 | "\n", 70 | "9. **Total_charges**: the total amount of bill in $ the customer has paid" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 24, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# pip install pandas\n", 80 | "import pandas as pd" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 25, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "# reading a csv file as dataframe\n", 90 | "# dataset source: datacamp\n", 91 | "df = pd.read_csv(\"telecom_data.csv\")\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 26, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/html": [ 102 | "
\n", 103 | "\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 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | "
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", 194 | "
" 195 | ], 196 | "text/plain": [ 197 | " Account_Length Vmail_Message CustServ_Calls Churn Intl_Plan Vmail_Plan \\\n", 198 | "0 128 25 1 no no yes \n", 199 | "1 107 26 1 no no yes \n", 200 | "2 137 0 0 no no no \n", 201 | "3 84 0 2 no yes no \n", 202 | "4 75 0 3 no yes no \n", 203 | "\n", 204 | " Total_mins Total_calls Total_charges \n", 205 | "0 717.2 303 320.26 \n", 206 | "1 625.2 332 313.64 \n", 207 | "2 539.4 333 224.89 \n", 208 | "3 564.8 255 263.70 \n", 209 | "4 512.0 359 238.99 " 210 | ] 211 | }, 212 | "metadata": {}, 213 | "output_type": "display_data" 214 | } 215 | ], 216 | "source": [ 217 | "display(df.head())" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 27, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/html": [ 228 | "
\n", 229 | "\n", 242 | "\n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \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 | "
Account_LengthVmail_MessageCustServ_CallsChurnIntl_PlanVmail_PlanTotal_minsTotal_callsTotal_charges
3328192362nonoyes660.7292339.20
33296803nonono585.4239254.83
33302802nonono675.6264259.64
333118402noyesno517.6336196.73
333274250nonoyes755.4276318.41
\n", 320 | "
" 321 | ], 322 | "text/plain": [ 323 | " Account_Length Vmail_Message CustServ_Calls Churn Intl_Plan \\\n", 324 | "3328 192 36 2 no no \n", 325 | "3329 68 0 3 no no \n", 326 | "3330 28 0 2 no no \n", 327 | "3331 184 0 2 no yes \n", 328 | "3332 74 25 0 no no \n", 329 | "\n", 330 | " Vmail_Plan Total_mins Total_calls Total_charges \n", 331 | "3328 yes 660.7 292 339.20 \n", 332 | "3329 no 585.4 239 254.83 \n", 333 | "3330 no 675.6 264 259.64 \n", 334 | "3331 no 517.6 336 196.73 \n", 335 | "3332 yes 755.4 276 318.41 " 336 | ] 337 | }, 338 | "metadata": {}, 339 | "output_type": "display_data" 340 | } 341 | ], 342 | "source": [ 343 | "display(df.tail())" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 28, 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "name": "stdout", 353 | "output_type": "stream", 354 | "text": [ 355 | "\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "print(type(df))" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 29, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "(3333, 9)\n", 373 | "number of rows = 3333\n", 374 | "number of columns = 9\n" 375 | ] 376 | } 377 | ], 378 | "source": [ 379 | "print(df.shape)\n", 380 | "\n", 381 | "print(\"number of rows = \", df.shape[0])\n", 382 | "print(\"number of columns = \", df.shape[1])" 383 | ] 384 | }, 385 | { 386 | "cell_type": "markdown", 387 | "metadata": {}, 388 | "source": [ 389 | "#### check the column types and look for missing values" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 30, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "name": "stdout", 399 | "output_type": "stream", 400 | "text": [ 401 | "\n", 402 | "RangeIndex: 3333 entries, 0 to 3332\n", 403 | "Data columns (total 9 columns):\n", 404 | " # Column Non-Null Count Dtype \n", 405 | "--- ------ -------------- ----- \n", 406 | " 0 Account_Length 3333 non-null int64 \n", 407 | " 1 Vmail_Message 3333 non-null int64 \n", 408 | " 2 CustServ_Calls 3333 non-null int64 \n", 409 | " 3 Churn 3333 non-null object \n", 410 | " 4 Intl_Plan 3333 non-null object \n", 411 | " 5 Vmail_Plan 3333 non-null object \n", 412 | " 6 Total_mins 3333 non-null float64\n", 413 | " 7 Total_calls 3333 non-null int64 \n", 414 | " 8 Total_charges 3333 non-null float64\n", 415 | "dtypes: float64(2), int64(4), object(3)\n", 416 | "memory usage: 234.5+ KB\n", 417 | "None\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "print(df.info())" 423 | ] 424 | }, 425 | { 426 | "cell_type": "markdown", 427 | "metadata": {}, 428 | "source": [ 429 | "#### Number of unique values in each column" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 31, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "data": { 439 | "text/plain": [ 440 | "no 2850\n", 441 | "yes 483\n", 442 | "Name: Churn, dtype: int64" 443 | ] 444 | }, 445 | "execution_count": 31, 446 | "metadata": {}, 447 | "output_type": "execute_result" 448 | } 449 | ], 450 | "source": [ 451 | "df[\"Churn\"].value_counts()" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 32, 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "no 3010\n", 463 | "yes 323\n", 464 | "Name: Intl_Plan, dtype: int64" 465 | ] 466 | }, 467 | "execution_count": 32, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "df[\"Intl_Plan\"].value_counts()" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 33, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "data": { 483 | "text/plain": [ 484 | "no 2411\n", 485 | "yes 922\n", 486 | "Name: Vmail_Plan, dtype: int64" 487 | ] 488 | }, 489 | "execution_count": 33, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "df[\"Vmail_Plan\"].value_counts()" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 34, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "data": { 505 | "text/plain": [ 506 | "105 43\n", 507 | "87 42\n", 508 | "93 40\n", 509 | "101 40\n", 510 | "90 39\n", 511 | " ..\n", 512 | "191 1\n", 513 | "199 1\n", 514 | "215 1\n", 515 | "221 1\n", 516 | "2 1\n", 517 | "Name: Account_Length, Length: 212, dtype: int64" 518 | ] 519 | }, 520 | "execution_count": 34, 521 | "metadata": {}, 522 | "output_type": "execute_result" 523 | } 524 | ], 525 | "source": [ 526 | "df[\"Account_Length\"].value_counts()" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 35, 532 | "metadata": {}, 533 | "outputs": [ 534 | { 535 | "data": { 536 | "text/plain": [ 537 | "212" 538 | ] 539 | }, 540 | "execution_count": 35, 541 | "metadata": {}, 542 | "output_type": "execute_result" 543 | } 544 | ], 545 | "source": [ 546 | "len(df[\"Account_Length\"].unique())" 547 | ] 548 | }, 549 | { 550 | "cell_type": "markdown", 551 | "metadata": {}, 552 | "source": [ 553 | "#### Data Statistics" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 36, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "data": { 563 | "text/html": [ 564 | "
\n", 565 | "\n", 578 | "\n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | " \n", 603 | " \n", 604 | " \n", 605 | " \n", 606 | " \n", 607 | " \n", 608 | " \n", 609 | " \n", 610 | " \n", 611 | " \n", 612 | " \n", 613 | " \n", 614 | " \n", 615 | " \n", 616 | " \n", 617 | " \n", 618 | " \n", 619 | " \n", 620 | " \n", 621 | " \n", 622 | " \n", 623 | " \n", 624 | " \n", 625 | " \n", 626 | " \n", 627 | " \n", 628 | " \n", 629 | " \n", 630 | " \n", 631 | " \n", 632 | " \n", 633 | " \n", 634 | " \n", 635 | " \n", 636 | " \n", 637 | " \n", 638 | " \n", 639 | " \n", 640 | " \n", 641 | " \n", 642 | " \n", 643 | " \n", 644 | " \n", 645 | " \n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | "
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", 665 | "
" 666 | ], 667 | "text/plain": [ 668 | " Account_Length Vmail_Message CustServ_Calls Total_mins \\\n", 669 | "count 3333.000000 3333.000000 3333.000000 3333.000000 \n", 670 | "mean 101.064806 8.099010 1.562856 591.864776 \n", 671 | "std 39.822106 13.688365 1.315491 89.954251 \n", 672 | "min 1.000000 0.000000 0.000000 284.300000 \n", 673 | "25% 74.000000 0.000000 1.000000 531.500000 \n", 674 | "50% 101.000000 0.000000 1.000000 593.600000 \n", 675 | "75% 127.000000 20.000000 2.000000 652.400000 \n", 676 | "max 243.000000 51.000000 9.000000 885.000000 \n", 677 | "\n", 678 | " Total_calls Total_charges \n", 679 | "count 3333.000000 3333.000000 \n", 680 | "mean 305.137114 260.321791 \n", 681 | "std 34.448164 53.810896 \n", 682 | "min 191.000000 68.370000 \n", 683 | "25% 282.000000 224.220000 \n", 684 | "50% 305.000000 260.560000 \n", 685 | "75% 328.000000 295.410000 \n", 686 | "max 416.000000 460.630000 " 687 | ] 688 | }, 689 | "metadata": {}, 690 | "output_type": "display_data" 691 | } 692 | ], 693 | "source": [ 694 | "display(df.describe())" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": 37, 700 | "metadata": {}, 701 | "outputs": [ 702 | { 703 | "data": { 704 | "text/html": [ 705 | "
\n", 706 | "\n", 719 | "\n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | "
ChurnIntl_PlanVmail_Plan
count333333333333
unique222
topnonono
freq285030102411
\n", 755 | "
" 756 | ], 757 | "text/plain": [ 758 | " Churn Intl_Plan Vmail_Plan\n", 759 | "count 3333 3333 3333\n", 760 | "unique 2 2 2\n", 761 | "top no no no\n", 762 | "freq 2850 3010 2411" 763 | ] 764 | }, 765 | "execution_count": 37, 766 | "metadata": {}, 767 | "output_type": "execute_result" 768 | } 769 | ], 770 | "source": [ 771 | "df.describe(include=['O'])" 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "metadata": {}, 777 | "source": [ 778 | "If you have attended/watched all the class lectures, submitted all the HW, and got the full grade of your HW, you can share a post on LinkedIn. \n", 779 | "\n", 780 | "#### Course outcome / LinkedIn Post:\n", 781 | "\n", 782 | "I have successfully completed module 1: Python Fundamentals organized by @EMK Center and conducted by @Md Shahidullah Kawsar. This module is a part of Expedition to Data Science and Machine Learning with Python.\n", 783 | "\n", 784 | "\n", 785 | "Number of live classes attended: 8\n", 786 | "\n", 787 | "Total time investment: 25 hours in one month\n", 788 | "\n", 789 | "In this module, I have learned:\n", 790 | "1. How to use Anaconda, jupyter notebook, Google Colab, and Github\n", 791 | "2. if-else if-else condition, while, for loop by playing blockly games: Maze, Bird, Turtle\n", 792 | "3. Python data types, and Data Structure: List, tuple, dictionary, set\n", 793 | "4. Mathematical Operations, Comparison Operators, Logical Operators, and Membership Operators\n", 794 | "5. String manipulation\n", 795 | "6. Writing functions in Python \n", 796 | "7. Solved 10 LeetCode problems from multiple perspectives\n", 797 | "8. math functions and NumPy operations\n", 798 | "9. Comparison between List and NumPy array\n", 799 | "10. String, List and NumPy array slicing\n", 800 | "11. Reading a csv file using Pandas, finding missing values, and data statistics\n", 801 | "\n", 802 | "#python #leetcode #datascience #dataanalysis #numpy #pandas" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": null, 808 | "metadata": {}, 809 | "outputs": [], 810 | "source": [] 811 | } 812 | ], 813 | "metadata": { 814 | "kernelspec": { 815 | "display_name": "Python 3", 816 | "language": "python", 817 | "name": "python3" 818 | }, 819 | "language_info": { 820 | "codemirror_mode": { 821 | "name": "ipython", 822 | "version": 3 823 | }, 824 | "file_extension": ".py", 825 | "mimetype": "text/x-python", 826 | "name": "python", 827 | "nbconvert_exporter": "python", 828 | "pygments_lexer": "ipython3", 829 | "version": "3.8.5" 830 | } 831 | }, 832 | "nbformat": 4, 833 | "nbformat_minor": 4 834 | } 835 | --------------------------------------------------------------------------------