├── .github └── workflows │ └── python-test.yml ├── Colab link ├── Python_Exercises.py ├── Python_guide.ipynb ├── README.md ├── requirements.txt └── test.py /.github/workflows/python-test.yml: -------------------------------------------------------------------------------- 1 | name: Python_Assignment 2 | 3 | on: pull_request 4 | jobs: 5 | tests: 6 | name: Python_Exercises 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: Set up Python 3.x 12 | uses: actions/setup-python@v2 13 | with: 14 | python-version: '3.6' 15 | architecture: 'x64' 16 | - name: Install dependencies 17 | run: | 18 | python3 -m pip install --upgrade pip 19 | python3 -m pip install pytest wheel 20 | if [ -f requirements.txt ]; then python3 -m pip install -r requirements.txt; fi 21 | - name: Assignment check 22 | run: chmod +x ./test.py & python3 ./test.py 23 | 24 | -------------------------------------------------------------------------------- /Colab link: -------------------------------------------------------------------------------- 1 | Colab link of python workshop: 2 | 3 | Copy paste this link in your browser and start exploring python !!! 4 | 5 | https://colab.research.google.com/drive/1mgNg-H0E5-s7SxrHWTSC23tSKEgzu-Al?usp=sharing 6 | -------------------------------------------------------------------------------- /Python_Exercises.py: -------------------------------------------------------------------------------- 1 | 2 | # ## Exercises 3 | 4 | # Answer the questions or complete the tasks outlined in bold below. 5 | 6 | 7 | def power(a,b): 8 | 9 | # ** What is 7 to the power of 4?** 10 | 11 | return None 12 | 13 | 14 | 15 | def split_str(s): 16 | 17 | # ** Split this string:** 18 | # 19 | # s = "Hi there Sam!" 20 | # 21 | # **into a list. ** 22 | 23 | return None 24 | 25 | 26 | def format(planet,diameter): 27 | 28 | # ** Given the variables:** 29 | # 30 | # planet = "Earth" 31 | # diameter = 12742 32 | # 33 | # ** Use .format() to print the following string: ** 34 | # 35 | # The diameter of Earth is 12742 kilometers. 36 | 37 | return None 38 | 39 | 40 | 41 | def indexing(lst): 42 | 43 | # ** Given this nested list, use indexing to grab the word "hello" ** 44 | 45 | #lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] 46 | 47 | return None 48 | 49 | 50 | def dictionary(d): 51 | 52 | # ** Given this nested dictionary grab the word "hello". Be prepared, this will be annoying/tricky ** 53 | 54 | # d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} 55 | 56 | 57 | return None 58 | 59 | 60 | def subjective(): 61 | 62 | # ** What is the main difference between a tuple and a list? ** 63 | # Tuple is _______ 64 | 65 | return None 66 | 67 | 68 | 69 | 70 | def domainGet(email): 71 | 72 | # ** Create a function that grabs the email website domain from a string in the form: ** 73 | # 74 | # user@domain.com 75 | # 76 | # **So for example, passing "user@domain.com" would return: domain.com** 77 | 78 | return None 79 | 80 | 81 | def findDog(st): 82 | 83 | # ** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. ** 84 | 85 | return None 86 | 87 | 88 | def countDog(st): 89 | 90 | # ** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. ** 91 | 92 | return None 93 | 94 | 95 | 96 | def lambdafunc(seq): 97 | 98 | # ** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:** 99 | # 100 | # seq = ['soup','dog','salad','cat','great'] 101 | # 102 | # **should be filtered down to:** 103 | # 104 | # ['soup','salad'] 105 | 106 | return None 107 | 108 | 109 | def caught_speeding(speed, is_birthday): 110 | 111 | 112 | # **You are driving a little too fast, and a police officer stops you. Write a function 113 | # to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". 114 | # If your speed is 60 or less, the result is "No Ticket". If speed is between 61 115 | # and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all 116 | # cases. ** 117 | 118 | return None 119 | 120 | 121 | ## Numpy Exercises 122 | 123 | import numpy as np 124 | 125 | 126 | def create_arr_of_fives(): 127 | 128 | #### Create an array of 10 fives 129 | #### Convert your output into list 130 | #### e.g return list(arr) 131 | 132 | return None 133 | 134 | 135 | 136 | def even_num(): 137 | 138 | ### Create an array of all the even integers from 10 to 50 139 | ### Convert your output into list 140 | ### e.g return list(arr) 141 | 142 | return None 143 | 144 | 145 | 146 | def create_matrix(): 147 | 148 | ### Create a 3x3 matrix with values ranging from 0 to 8 149 | ### Convert your output into list 150 | ### e.g return (arr).tolist() 151 | 152 | return None 153 | 154 | 155 | 156 | def linear_space(): 157 | 158 | ### Create an array of 20 linearly spaced points between 0 and 1 159 | ### Convert your output into list 160 | ### e.g return list(arr) 161 | 162 | return None 163 | 164 | 165 | 166 | def decimal_mat(): 167 | 168 | ### Create an array of size 10*10 consisting of numbers from 0.01 to 1 169 | ### Convert your output into list 170 | ### e.g return (arr).tolist() 171 | 172 | return None 173 | 174 | 175 | 176 | def slices_1(): 177 | 178 | # This is a given array 179 | arr = np.arange(1,26).reshape(5,5) 180 | # array([[ 1, 2, 3, 4, 5], 181 | # [ 6, 7, 8, 9, 10], 182 | # [11, 12, 13, 14, 15], 183 | # [16, 17, 18, 19, 20], 184 | # [21, 22, 23, 24, 25]]) 185 | 186 | # Write a code to slice this given array 187 | ### Convert your output into list 188 | ### e.g return (arr).tolist() 189 | # array([[12, 13, 14, 15], 190 | # [17, 18, 19, 20], 191 | # [22, 23, 24, 25]]) 192 | 193 | return None 194 | 195 | 196 | 197 | def slices_2(): 198 | 199 | # This is a given array 200 | arr = np.arange(1,26).reshape(5,5) 201 | # array([[ 1, 2, 3, 4, 5], 202 | # [ 6, 7, 8, 9, 10], 203 | # [11, 12, 13, 14, 15], 204 | # [16, 17, 18, 19, 20], 205 | # [21, 22, 23, 24, 25]]) 206 | 207 | # Write a code to slice this given array 208 | ### Convert your output into list 209 | ### e.g return (arr).tolist() 210 | # array([[ 2], 211 | # [ 7], 212 | # [12]]) 213 | 214 | return None 215 | 216 | 217 | 218 | def slices_3(): 219 | 220 | # This is a given array 221 | arr = np.arange(1,26).reshape(5,5) 222 | # array([[ 1, 2, 3, 4, 5], 223 | # [ 6, 7, 8, 9, 10], 224 | # [11, 12, 13, 14, 15], 225 | # [16, 17, 18, 19, 20], 226 | # [21, 22, 23, 24, 25]]) 227 | 228 | # Write a code to slice this given array 229 | ### Convert your output into list 230 | ### e.g return (arr).tolist() 231 | # array([[16, 17, 18, 19, 20], 232 | # [21, 22, 23, 24, 25]]) 233 | 234 | return None 235 | 236 | 237 | # Great job! 238 | -------------------------------------------------------------------------------- /Python_guide.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "hCNWKmR8MzA4" 7 | }, 8 | "source": [ 9 | "# Python walk-through\n", 10 | "\n", 11 | "This notebook will just go through the basic topics in order:\n", 12 | "\n", 13 | "\n", 14 | "###Data types\n", 15 | " * Numbers\n", 16 | " * Strings\n", 17 | " * Printing\n", 18 | " * Lists\n", 19 | " * Dictionaries\n", 20 | " * Booleans\n", 21 | " * Tuples \n", 22 | " * Sets\n", 23 | "\n", 24 | "###General\n", 25 | " * Comparison Operators\n", 26 | " * if, elif, else Statements\n", 27 | " * for Loops\n", 28 | " * while Loops\n", 29 | " * range()\n", 30 | " * list comprehension\n", 31 | " * functions\n", 32 | " * lambda expressions\n", 33 | " * map and filter\n", 34 | " * methods\n", 35 | " * Error handling\n", 36 | "\n", 37 | "\n", 38 | "### Numpy\n", 39 | " * Built in methods\n", 40 | " * Attributes\n", 41 | " * Indexing\n", 42 | " * Selection\n", 43 | " * Broadcasting\n", 44 | " * 2D-arrays\n", 45 | " * Slicing\n", 46 | " * Selection\n", 47 | " * Arithmetic operations\n", 48 | " * Universal functions\n", 49 | "\n", 50 | "* Object oriented programming\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": { 56 | "id": "e2HJZcFqMzBA" 57 | }, 58 | "source": [ 59 | "## Data types\n", 60 | "\n", 61 | "### Numbers" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": { 68 | "id": "8Raide1eMzBB", 69 | "outputId": "d6ea440f-076e-44ea-9d9a-4fcbc69c94ac", 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | } 73 | }, 74 | "outputs": [ 75 | { 76 | "output_type": "execute_result", 77 | "data": { 78 | "text/plain": [ 79 | "2" 80 | ] 81 | }, 82 | "metadata": {}, 83 | "execution_count": 23 84 | } 85 | ], 86 | "source": [ 87 | "1 + 1" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": { 94 | "id": "p8LCBJJnMzBE", 95 | "outputId": "fff51637-b99a-4aa0-cc52-127e1d68c642" 96 | }, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "3" 102 | ] 103 | }, 104 | "execution_count": 7, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "1 * 3" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": { 117 | "id": "w5MN13HtMzBF", 118 | "outputId": "0795709a-90bb-45a0-c1d4-0a7c8c62572d" 119 | }, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "0.5" 125 | ] 126 | }, 127 | "execution_count": 8, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "1 / 2" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "id": "D2PNR14UMzBG", 141 | "outputId": "c6881dff-deda-4d99-fd43-83fe0d75003b" 142 | }, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "16" 148 | ] 149 | }, 150 | "execution_count": 9, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "2 ** 4" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": { 163 | "id": "86b7NVwAMzBH", 164 | "outputId": "360dd019-160b-408d-a925-16d74a0bbdd2" 165 | }, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "0" 171 | ] 172 | }, 173 | "execution_count": 10, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "4 % 2" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": { 186 | "id": "yo1V44YCMzBJ", 187 | "outputId": "388a78db-2ab7-4717-de9e-ea0a5ffa4d3f" 188 | }, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "1" 194 | ] 195 | }, 196 | "execution_count": 11, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "5 % 2" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": null, 208 | "metadata": { 209 | "id": "SWUo9PuoMzBK", 210 | "outputId": "495b3a1d-eac4-4a26-de7e-82f9e96240ad" 211 | }, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "50" 217 | ] 218 | }, 219 | "execution_count": 12, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "(2 + 3) * (5 + 5)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": { 231 | "id": "3DwEhgiVMzBL" 232 | }, 233 | "source": [ 234 | "### Variable Assignment" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "collapsed": true, 242 | "id": "mJTxFsh_MzBM" 243 | }, 244 | "outputs": [], 245 | "source": [ 246 | "# Can not start with number or special characters\n", 247 | "name_of_var = 2" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": { 254 | "collapsed": true, 255 | "id": "grHr76_jMzBM" 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "x = 2\n", 260 | "y = 3" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": { 267 | "collapsed": true, 268 | "id": "TMTKNWifMzBN" 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "z = x + y" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "metadata": { 279 | "id": "B65B4dksMzBN", 280 | "outputId": "2de46542-6ab2-4a5c-9b54-2c77dc800ed8" 281 | }, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "5" 287 | ] 288 | }, 289 | "execution_count": 16, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "z" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": { 301 | "id": "9wtxrvdlMzBO" 302 | }, 303 | "source": [ 304 | "### Strings" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "id": "1vA9vSbsMzBO", 312 | "outputId": "3555df1e-2968-4a81-d442-756a5ab74d2e" 313 | }, 314 | "outputs": [ 315 | { 316 | "data": { 317 | "text/plain": [ 318 | "'single quotes'" 319 | ] 320 | }, 321 | "execution_count": 17, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "'single quotes'" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": { 334 | "id": "AuPQHAzZMzBP", 335 | "outputId": "cffb2d6f-3adb-4135-b0ed-bac9cdb5eaaa" 336 | }, 337 | "outputs": [ 338 | { 339 | "data": { 340 | "text/plain": [ 341 | "'double quotes'" 342 | ] 343 | }, 344 | "execution_count": 18, 345 | "metadata": {}, 346 | "output_type": "execute_result" 347 | } 348 | ], 349 | "source": [ 350 | "\"double quotes\"" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": { 357 | "id": "sRyG-pElMzBP", 358 | "outputId": "08227231-64c6-43d2-b08f-affacfb4d3de", 359 | "colab": { 360 | "base_uri": "https://localhost:8080/", 361 | "height": 36 362 | } 363 | }, 364 | "outputs": [ 365 | { 366 | "output_type": "execute_result", 367 | "data": { 368 | "application/vnd.google.colaboratory.intrinsic+json": { 369 | "type": "string" 370 | }, 371 | "text/plain": [ 372 | "\" wrap lot's of other quotes\"" 373 | ] 374 | }, 375 | "metadata": {}, 376 | "execution_count": 20 377 | } 378 | ], 379 | "source": [ 380 | "\" wrap lot's of other quotes\"\n" 381 | ] 382 | }, 383 | { 384 | "cell_type": "markdown", 385 | "metadata": { 386 | "id": "LwzdirJJMzBQ" 387 | }, 388 | "source": [ 389 | "### Printing" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": null, 395 | "metadata": { 396 | "collapsed": true, 397 | "id": "3-LtFtXeMzBQ" 398 | }, 399 | "outputs": [], 400 | "source": [ 401 | "x = 'hello'" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": null, 407 | "metadata": { 408 | "id": "_-Av5FtqMzBR", 409 | "outputId": "8baed049-ee94-43ac-ba1e-e8436b05800d" 410 | }, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "'hello'" 416 | ] 417 | }, 418 | "execution_count": 21, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "x" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": null, 430 | "metadata": { 431 | "id": "glF4F7IfMzBR", 432 | "outputId": "26427621-a8d2-4d09-8f63-8c20be0bb233" 433 | }, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "hello\n" 440 | ] 441 | } 442 | ], 443 | "source": [ 444 | "print(x)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": null, 450 | "metadata": { 451 | "collapsed": true, 452 | "id": "A8XUhI_DMzBR" 453 | }, 454 | "outputs": [], 455 | "source": [ 456 | "num = 12\n", 457 | "name = 'Sam'" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": { 464 | "id": "uEElWLs_MzBS", 465 | "outputId": "c3f00ec6-7191-4ecd-8a76-efdbe5daff35", 466 | "colab": { 467 | "base_uri": "https://localhost:8080/" 468 | } 469 | }, 470 | "outputs": [ 471 | { 472 | "output_type": "stream", 473 | "name": "stdout", 474 | "text": [ 475 | "12 this is my number\n" 476 | ] 477 | } 478 | ], 479 | "source": [ 480 | "# print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))\n", 481 | "print(\"{var} this is my number\".format(var = num))" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": null, 487 | "metadata": { 488 | "id": "HdiPACuZMzBT", 489 | "outputId": "dc5b4098-92cd-4951-f29b-a3fc347baa61", 490 | "colab": { 491 | "base_uri": "https://localhost:8080/" 492 | } 493 | }, 494 | "outputs": [ 495 | { 496 | "output_type": "stream", 497 | "name": "stdout", 498 | "text": [ 499 | "My number is: 14, and my name is: Sam\n" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "num = 14\n", 505 | "print('My number is: {}, and my name is: {}'.format(num,name))" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": { 511 | "id": "gGO0RBRCMzBT" 512 | }, 513 | "source": [ 514 | "### Lists" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": null, 520 | "metadata": { 521 | "id": "UoCsR5HdMzBT", 522 | "outputId": "e27ddaf4-ca67-436b-a002-70ed63ff4064", 523 | "colab": { 524 | "base_uri": "https://localhost:8080/" 525 | } 526 | }, 527 | "outputs": [ 528 | { 529 | "output_type": "execute_result", 530 | "data": { 531 | "text/plain": [ 532 | "[1, 2, 3]" 533 | ] 534 | }, 535 | "metadata": {}, 536 | "execution_count": 25 537 | } 538 | ], 539 | "source": [ 540 | "[1,2,3]" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": null, 546 | "metadata": { 547 | "id": "6bETqIHRMzBU", 548 | "outputId": "f1acc42d-75cd-457a-e737-7a1c4465d6c4" 549 | }, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "['hi', 1, [1, 2]]" 555 | ] 556 | }, 557 | "execution_count": 27, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "[ 'hi' , 1 , [1,2] ]" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": null, 569 | "metadata": { 570 | "collapsed": true, 571 | "id": "928F_ygiMzBU" 572 | }, 573 | "outputs": [], 574 | "source": [ 575 | "my_list = ['a','b','c']" 576 | ] 577 | }, 578 | { 579 | "cell_type": "code", 580 | "execution_count": null, 581 | "metadata": { 582 | "collapsed": true, 583 | "id": "s8gwzHWtMzBU" 584 | }, 585 | "outputs": [], 586 | "source": [ 587 | "my_list.append('d')" 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": null, 593 | "metadata": { 594 | "id": "-1m_7NqFMzBV", 595 | "outputId": "4b6299ee-f5bf-4f8e-f6ba-b5e34ac024b2", 596 | "colab": { 597 | "base_uri": "https://localhost:8080/" 598 | } 599 | }, 600 | "outputs": [ 601 | { 602 | "output_type": "execute_result", 603 | "data": { 604 | "text/plain": [ 605 | "['a', 'b', 'c', 'd']" 606 | ] 607 | }, 608 | "metadata": {}, 609 | "execution_count": 29 610 | } 611 | ], 612 | "source": [ 613 | "my_list" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": null, 619 | "metadata": { 620 | "id": "ydi10r2cMzBV", 621 | "outputId": "845d4970-710f-4b73-c0a8-698c41a19519" 622 | }, 623 | "outputs": [ 624 | { 625 | "data": { 626 | "text/plain": [ 627 | "'a'" 628 | ] 629 | }, 630 | "execution_count": 31, 631 | "metadata": {}, 632 | "output_type": "execute_result" 633 | } 634 | ], 635 | "source": [ 636 | "my_list[0]" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": null, 642 | "metadata": { 643 | "id": "ahJh5NytMzBV", 644 | "outputId": "65abaebf-c67f-4867-f2fc-92fea68e9412" 645 | }, 646 | "outputs": [ 647 | { 648 | "data": { 649 | "text/plain": [ 650 | "'b'" 651 | ] 652 | }, 653 | "execution_count": 32, 654 | "metadata": {}, 655 | "output_type": "execute_result" 656 | } 657 | ], 658 | "source": [ 659 | "my_list[1]" 660 | ] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "execution_count": null, 665 | "metadata": { 666 | "id": "nxlUBfQjMzBV", 667 | "outputId": "a4c8d3a5-74b0-4563-8367-c293d3ff820e", 668 | "colab": { 669 | "base_uri": "https://localhost:8080/" 670 | } 671 | }, 672 | "outputs": [ 673 | { 674 | "output_type": "execute_result", 675 | "data": { 676 | "text/plain": [ 677 | "['b', 'c', 'd']" 678 | ] 679 | }, 680 | "metadata": {}, 681 | "execution_count": 30 682 | } 683 | ], 684 | "source": [ 685 | "my_list[ 1 : ]" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": null, 691 | "metadata": { 692 | "id": "fnLbrv9qMzBW", 693 | "outputId": "f8d977d3-f249-486d-9722-39b5e9f494f4" 694 | }, 695 | "outputs": [ 696 | { 697 | "data": { 698 | "text/plain": [ 699 | "['a']" 700 | ] 701 | }, 702 | "execution_count": 34, 703 | "metadata": {}, 704 | "output_type": "execute_result" 705 | } 706 | ], 707 | "source": [ 708 | "my_list[ : 1]" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": null, 714 | "metadata": { 715 | "collapsed": true, 716 | "id": "3jsomaTEMzBW" 717 | }, 718 | "outputs": [], 719 | "source": [ 720 | "my_list[0] = 'NEW'" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": null, 726 | "metadata": { 727 | "id": "A-65W4B4MzBW", 728 | "outputId": "781dab63-07d1-4284-e97e-636dff6be3ea", 729 | "colab": { 730 | "base_uri": "https://localhost:8080/" 731 | } 732 | }, 733 | "outputs": [ 734 | { 735 | "output_type": "execute_result", 736 | "data": { 737 | "text/plain": [ 738 | "['NEW', 'b', 'c', 'd']" 739 | ] 740 | }, 741 | "metadata": {}, 742 | "execution_count": 32 743 | } 744 | ], 745 | "source": [ 746 | "my_list" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": null, 752 | "metadata": { 753 | "collapsed": true, 754 | "id": "CRnTenJ9MzBX" 755 | }, 756 | "outputs": [], 757 | "source": [ 758 | "nest = [1,2,3,[4,5,['target']]]" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": null, 764 | "metadata": { 765 | "id": "ADuJbxTpMzBX", 766 | "outputId": "84594434-8413-42cc-e97b-c1c9128a5cac", 767 | "colab": { 768 | "base_uri": "https://localhost:8080/" 769 | } 770 | }, 771 | "outputs": [ 772 | { 773 | "output_type": "execute_result", 774 | "data": { 775 | "text/plain": [ 776 | "[4, 5, ['target']]" 777 | ] 778 | }, 779 | "metadata": {}, 780 | "execution_count": 34 781 | } 782 | ], 783 | "source": [ 784 | "nest[3]" 785 | ] 786 | }, 787 | { 788 | "cell_type": "code", 789 | "execution_count": null, 790 | "metadata": { 791 | "id": "k8jBXuJPMzBX", 792 | "outputId": "07638884-2940-48d1-ea0e-7e278ad684c2" 793 | }, 794 | "outputs": [ 795 | { 796 | "data": { 797 | "text/plain": [ 798 | "['target']" 799 | ] 800 | }, 801 | "execution_count": 101, 802 | "metadata": {}, 803 | "output_type": "execute_result" 804 | } 805 | ], 806 | "source": [ 807 | "nest[3][2]" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": null, 813 | "metadata": { 814 | "id": "73Bnf0opMzBY", 815 | "outputId": "f9b4bb58-bfda-4ae6-9341-a86ea206ae33" 816 | }, 817 | "outputs": [ 818 | { 819 | "data": { 820 | "text/plain": [ 821 | "'target'" 822 | ] 823 | }, 824 | "execution_count": 102, 825 | "metadata": {}, 826 | "output_type": "execute_result" 827 | } 828 | ], 829 | "source": [ 830 | "nest[3][2][0]" 831 | ] 832 | }, 833 | { 834 | "cell_type": "markdown", 835 | "metadata": { 836 | "id": "2MbG65ABMzBY" 837 | }, 838 | "source": [ 839 | "### Dictionaries" 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": null, 845 | "metadata": { 846 | "collapsed": true, 847 | "id": "e2fVVkBfMzBZ" 848 | }, 849 | "outputs": [], 850 | "source": [ 851 | "d = { 'key1':'item1' , 'key2':'item2' }" 852 | ] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "execution_count": null, 857 | "metadata": { 858 | "id": "K-rEbyhJMzBZ", 859 | "outputId": "cc2c7974-8d8d-4069-f055-9e9560e320a1", 860 | "colab": { 861 | "base_uri": "https://localhost:8080/" 862 | } 863 | }, 864 | "outputs": [ 865 | { 866 | "output_type": "execute_result", 867 | "data": { 868 | "text/plain": [ 869 | "{'key1': 'item1', 'key2': 'item2'}" 870 | ] 871 | }, 872 | "metadata": {}, 873 | "execution_count": 39 874 | } 875 | ], 876 | "source": [ 877 | "d" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": null, 883 | "metadata": { 884 | "id": "0uhRWvDUMzBZ", 885 | "outputId": "e46784db-cd16-4dec-d80d-cb4237ce1b97", 886 | "colab": { 887 | "base_uri": "https://localhost:8080/", 888 | "height": 36 889 | } 890 | }, 891 | "outputs": [ 892 | { 893 | "output_type": "execute_result", 894 | "data": { 895 | "application/vnd.google.colaboratory.intrinsic+json": { 896 | "type": "string" 897 | }, 898 | "text/plain": [ 899 | "'item1'" 900 | ] 901 | }, 902 | "metadata": {}, 903 | "execution_count": 40 904 | } 905 | ], 906 | "source": [ 907 | "d['key1']" 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "source": [ 913 | "# The values in dictionary items can be of any data type\n", 914 | "dict_ = {\n", 915 | " \"brand\": \"Ford\" , # String\n", 916 | " \"electric\": False, # Boolean\n", 917 | " \"year\": 1964, # Int\n", 918 | " \"colors\": [\"red\", \"white\", \"blue\"] # List \n", 919 | "}" 920 | ], 921 | "metadata": { 922 | "id": "aVC5fL4ayr7d" 923 | }, 924 | "execution_count": null, 925 | "outputs": [] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "source": [ 930 | "dict_" 931 | ], 932 | "metadata": { 933 | "colab": { 934 | "base_uri": "https://localhost:8080/" 935 | }, 936 | "id": "e_SeIBtYP-P_", 937 | "outputId": "678c1932-a96b-4846-913d-6f67418c3b6a" 938 | }, 939 | "execution_count": null, 940 | "outputs": [ 941 | { 942 | "output_type": "execute_result", 943 | "data": { 944 | "text/plain": [ 945 | "{'brand': 'Ford',\n", 946 | " 'colors': ['red', 'white', 'blue'],\n", 947 | " 'electric': False,\n", 948 | " 'year': 1964}" 949 | ] 950 | }, 951 | "metadata": {}, 952 | "execution_count": 42 953 | } 954 | ] 955 | }, 956 | { 957 | "cell_type": "code", 958 | "source": [ 959 | "print(type(dict_[\"year\"]))" 960 | ], 961 | "metadata": { 962 | "colab": { 963 | "base_uri": "https://localhost:8080/" 964 | }, 965 | "id": "2s63LbT5zCUd", 966 | "outputId": "0fd6c616-ec8d-4378-f0d3-1813e4298727" 967 | }, 968 | "execution_count": null, 969 | "outputs": [ 970 | { 971 | "output_type": "stream", 972 | "name": "stdout", 973 | "text": [ 974 | "\n" 975 | ] 976 | } 977 | ] 978 | }, 979 | { 980 | "cell_type": "markdown", 981 | "metadata": { 982 | "id": "eF8n8yHaMzBa" 983 | }, 984 | "source": [ 985 | "### Booleans" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": null, 991 | "metadata": { 992 | "id": "lNmHK3LDMzBa", 993 | "outputId": "15920277-6a3d-4477-c57b-558ea6f14c66" 994 | }, 995 | "outputs": [ 996 | { 997 | "data": { 998 | "text/plain": [ 999 | "True" 1000 | ] 1001 | }, 1002 | "execution_count": 40, 1003 | "metadata": {}, 1004 | "output_type": "execute_result" 1005 | } 1006 | ], 1007 | "source": [ 1008 | "True" 1009 | ] 1010 | }, 1011 | { 1012 | "cell_type": "code", 1013 | "execution_count": null, 1014 | "metadata": { 1015 | "id": "kcLcT2tTMzBa", 1016 | "outputId": "bdebf763-2449-44c3-ad7d-06c957521eab" 1017 | }, 1018 | "outputs": [ 1019 | { 1020 | "data": { 1021 | "text/plain": [ 1022 | "False" 1023 | ] 1024 | }, 1025 | "execution_count": 41, 1026 | "metadata": {}, 1027 | "output_type": "execute_result" 1028 | } 1029 | ], 1030 | "source": [ 1031 | "False" 1032 | ] 1033 | }, 1034 | { 1035 | "cell_type": "markdown", 1036 | "metadata": { 1037 | "id": "mFYDT7a-MzBb" 1038 | }, 1039 | "source": [ 1040 | "### Tuples " 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "execution_count": null, 1046 | "metadata": { 1047 | "collapsed": true, 1048 | "id": "MZ2TZnZaMzBb" 1049 | }, 1050 | "outputs": [], 1051 | "source": [ 1052 | "t = (1,2,3)" 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "code", 1057 | "execution_count": null, 1058 | "metadata": { 1059 | "id": "BiYDc89mMzBb", 1060 | "outputId": "540f3f13-9afa-4f54-dbbf-c6c4332850f5", 1061 | "colab": { 1062 | "base_uri": "https://localhost:8080/" 1063 | } 1064 | }, 1065 | "outputs": [ 1066 | { 1067 | "output_type": "execute_result", 1068 | "data": { 1069 | "text/plain": [ 1070 | "1" 1071 | ] 1072 | }, 1073 | "metadata": {}, 1074 | "execution_count": 44 1075 | } 1076 | ], 1077 | "source": [ 1078 | "t[0]" 1079 | ] 1080 | }, 1081 | { 1082 | "cell_type": "code", 1083 | "execution_count": null, 1084 | "metadata": { 1085 | "id": "dKxygCdKMzBb", 1086 | "outputId": "7dddc809-c215-4e03-85af-469162549631", 1087 | "colab": { 1088 | "base_uri": "https://localhost:8080/", 1089 | "height": 165 1090 | } 1091 | }, 1092 | "outputs": [ 1093 | { 1094 | "output_type": "error", 1095 | "ename": "TypeError", 1096 | "evalue": "ignored", 1097 | "traceback": [ 1098 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1099 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1100 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'NEW'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1101 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "t[0] = 'NEW'" 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "markdown", 1111 | "metadata": { 1112 | "id": "msmiPgUDMzBc" 1113 | }, 1114 | "source": [ 1115 | "### Sets" 1116 | ] 1117 | }, 1118 | { 1119 | "cell_type": "code", 1120 | "execution_count": null, 1121 | "metadata": { 1122 | "id": "yD9XcecGMzBc", 1123 | "outputId": "7b8e3c6b-d119-4c1f-f724-a12dae2bdd97", 1124 | "colab": { 1125 | "base_uri": "https://localhost:8080/" 1126 | } 1127 | }, 1128 | "outputs": [ 1129 | { 1130 | "output_type": "execute_result", 1131 | "data": { 1132 | "text/plain": [ 1133 | "{1, 2, 3}" 1134 | ] 1135 | }, 1136 | "metadata": {}, 1137 | "execution_count": 46 1138 | } 1139 | ], 1140 | "source": [ 1141 | "{1,2,3}" 1142 | ] 1143 | }, 1144 | { 1145 | "cell_type": "code", 1146 | "execution_count": null, 1147 | "metadata": { 1148 | "id": "j4oBU1pQMzBc", 1149 | "outputId": "611ecbcb-1e1b-421b-9bf7-466845f316f7", 1150 | "colab": { 1151 | "base_uri": "https://localhost:8080/" 1152 | } 1153 | }, 1154 | "outputs": [ 1155 | { 1156 | "output_type": "execute_result", 1157 | "data": { 1158 | "text/plain": [ 1159 | "{1, 2, 3}" 1160 | ] 1161 | }, 1162 | "metadata": {}, 1163 | "execution_count": 47 1164 | } 1165 | ], 1166 | "source": [ 1167 | "{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}" 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "code", 1172 | "source": [ 1173 | "s = set([1,1,1,2,3,3,3,3,3])\n", 1174 | "s" 1175 | ], 1176 | "metadata": { 1177 | "colab": { 1178 | "base_uri": "https://localhost:8080/" 1179 | }, 1180 | "id": "MKXWb6eHLRig", 1181 | "outputId": "20954c38-2a96-4463-bbe4-0f099b6c419a" 1182 | }, 1183 | "execution_count": null, 1184 | "outputs": [ 1185 | { 1186 | "output_type": "execute_result", 1187 | "data": { 1188 | "text/plain": [ 1189 | "{1, 2, 3}" 1190 | ] 1191 | }, 1192 | "metadata": {}, 1193 | "execution_count": 48 1194 | } 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "code", 1199 | "source": [ 1200 | "s.add(4)\n", 1201 | "s" 1202 | ], 1203 | "metadata": { 1204 | "colab": { 1205 | "base_uri": "https://localhost:8080/" 1206 | }, 1207 | "id": "P8wyEk6OLazO", 1208 | "outputId": "daa798cc-e5aa-436b-a525-4c45e831a189" 1209 | }, 1210 | "execution_count": null, 1211 | "outputs": [ 1212 | { 1213 | "output_type": "execute_result", 1214 | "data": { 1215 | "text/plain": [ 1216 | "{1, 2, 3, 4}" 1217 | ] 1218 | }, 1219 | "metadata": {}, 1220 | "execution_count": 49 1221 | } 1222 | ] 1223 | }, 1224 | { 1225 | "cell_type": "code", 1226 | "source": [ 1227 | "s.add(4)\n", 1228 | "# It wont add another 4 \n", 1229 | "s" 1230 | ], 1231 | "metadata": { 1232 | "colab": { 1233 | "base_uri": "https://localhost:8080/" 1234 | }, 1235 | "id": "g7odMsCnLdhH", 1236 | "outputId": "4aafa11b-5ac7-47ce-822e-999ed2bf322b" 1237 | }, 1238 | "execution_count": null, 1239 | "outputs": [ 1240 | { 1241 | "output_type": "execute_result", 1242 | "data": { 1243 | "text/plain": [ 1244 | "{1, 2, 3, 4}" 1245 | ] 1246 | }, 1247 | "metadata": {}, 1248 | "execution_count": 50 1249 | } 1250 | ] 1251 | }, 1252 | { 1253 | "cell_type": "markdown", 1254 | "metadata": { 1255 | "id": "grPyNlBnMzBc" 1256 | }, 1257 | "source": [ 1258 | "## Comparison Operators" 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "code", 1263 | "execution_count": null, 1264 | "metadata": { 1265 | "id": "16FZfeOsMzBd", 1266 | "outputId": "47818d01-27a5-4038-a85b-1d944957ad51", 1267 | "colab": { 1268 | "base_uri": "https://localhost:8080/" 1269 | } 1270 | }, 1271 | "outputs": [ 1272 | { 1273 | "output_type": "execute_result", 1274 | "data": { 1275 | "text/plain": [ 1276 | "False" 1277 | ] 1278 | }, 1279 | "metadata": {}, 1280 | "execution_count": 51 1281 | } 1282 | ], 1283 | "source": [ 1284 | "1 > 2" 1285 | ] 1286 | }, 1287 | { 1288 | "cell_type": "code", 1289 | "execution_count": null, 1290 | "metadata": { 1291 | "id": "HGUryzNLMzBd", 1292 | "outputId": "8d66c286-1011-4402-9965-9e5d1127bf19" 1293 | }, 1294 | "outputs": [ 1295 | { 1296 | "data": { 1297 | "text/plain": [ 1298 | "True" 1299 | ] 1300 | }, 1301 | "execution_count": 48, 1302 | "metadata": {}, 1303 | "output_type": "execute_result" 1304 | } 1305 | ], 1306 | "source": [ 1307 | "1 < 2" 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "code", 1312 | "execution_count": null, 1313 | "metadata": { 1314 | "id": "ScMArxscMzBd", 1315 | "outputId": "23ef47c1-ab2c-476c-9713-eb21c8330f75" 1316 | }, 1317 | "outputs": [ 1318 | { 1319 | "data": { 1320 | "text/plain": [ 1321 | "True" 1322 | ] 1323 | }, 1324 | "execution_count": 49, 1325 | "metadata": {}, 1326 | "output_type": "execute_result" 1327 | } 1328 | ], 1329 | "source": [ 1330 | "1 >= 1" 1331 | ] 1332 | }, 1333 | { 1334 | "cell_type": "code", 1335 | "execution_count": null, 1336 | "metadata": { 1337 | "id": "6NqB_EmVMzBe", 1338 | "outputId": "870489b3-a555-4eb9-dbcc-927ac146196d" 1339 | }, 1340 | "outputs": [ 1341 | { 1342 | "data": { 1343 | "text/plain": [ 1344 | "True" 1345 | ] 1346 | }, 1347 | "execution_count": 50, 1348 | "metadata": {}, 1349 | "output_type": "execute_result" 1350 | } 1351 | ], 1352 | "source": [ 1353 | "1 <= 4" 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "code", 1358 | "execution_count": null, 1359 | "metadata": { 1360 | "id": "X-jXhtebMzBe", 1361 | "outputId": "efb2af39-4585-45e8-e8dc-915cfea6f0e0", 1362 | "colab": { 1363 | "base_uri": "https://localhost:8080/" 1364 | } 1365 | }, 1366 | "outputs": [ 1367 | { 1368 | "output_type": "execute_result", 1369 | "data": { 1370 | "text/plain": [ 1371 | "True" 1372 | ] 1373 | }, 1374 | "metadata": {}, 1375 | "execution_count": 44 1376 | } 1377 | ], 1378 | "source": [ 1379 | "1 == 1" 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "source": [ 1385 | "# It will give you error\n", 1386 | "# Coz here you are doing a variable assigment\n", 1387 | "1 = 1 " 1388 | ], 1389 | "metadata": { 1390 | "colab": { 1391 | "base_uri": "https://localhost:8080/", 1392 | "height": 130 1393 | }, 1394 | "id": "5i0443aJLopn", 1395 | "outputId": "74c79657-a7f3-48ab-af12-b3bb3d01af8a" 1396 | }, 1397 | "execution_count": null, 1398 | "outputs": [ 1399 | { 1400 | "output_type": "error", 1401 | "ename": "SyntaxError", 1402 | "evalue": "ignored", 1403 | "traceback": [ 1404 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m 1 = 1\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to literal\n" 1405 | ] 1406 | } 1407 | ] 1408 | }, 1409 | { 1410 | "cell_type": "code", 1411 | "execution_count": null, 1412 | "metadata": { 1413 | "id": "IB42Cq_rMzBe", 1414 | "outputId": "d1007e80-5def-4158-a94c-2a25d7518625" 1415 | }, 1416 | "outputs": [ 1417 | { 1418 | "data": { 1419 | "text/plain": [ 1420 | "False" 1421 | ] 1422 | }, 1423 | "execution_count": 52, 1424 | "metadata": {}, 1425 | "output_type": "execute_result" 1426 | } 1427 | ], 1428 | "source": [ 1429 | "'hi' == 'bye'" 1430 | ] 1431 | }, 1432 | { 1433 | "cell_type": "markdown", 1434 | "metadata": { 1435 | "id": "2fNO3LlQMzBf" 1436 | }, 1437 | "source": [ 1438 | "## Logic Operators" 1439 | ] 1440 | }, 1441 | { 1442 | "cell_type": "code", 1443 | "source": [ 1444 | "# T and T -> T # T or T -> T\n", 1445 | "# T and F -> F # T or F -> T\n", 1446 | "# F and T -> F # F or T -> T\n", 1447 | "# F and F -> F # F or F -> F" 1448 | ], 1449 | "metadata": { 1450 | "id": "DBIZs1TNziJm" 1451 | }, 1452 | "execution_count": null, 1453 | "outputs": [] 1454 | }, 1455 | { 1456 | "cell_type": "code", 1457 | "execution_count": null, 1458 | "metadata": { 1459 | "id": "rPvGSmpxMzBf", 1460 | "outputId": "180a6d31-5c40-4e49-d115-2b5c898eccab", 1461 | "colab": { 1462 | "base_uri": "https://localhost:8080/" 1463 | } 1464 | }, 1465 | "outputs": [ 1466 | { 1467 | "output_type": "execute_result", 1468 | "data": { 1469 | "text/plain": [ 1470 | "False" 1471 | ] 1472 | }, 1473 | "metadata": {}, 1474 | "execution_count": 36 1475 | } 1476 | ], 1477 | "source": [ 1478 | "(1 > 2) and (2 < 3)" 1479 | ] 1480 | }, 1481 | { 1482 | "cell_type": "code", 1483 | "execution_count": null, 1484 | "metadata": { 1485 | "id": "z8pGCrhtMzBf", 1486 | "outputId": "7262ba0e-57d2-4b2d-a9ee-29b7eeeb70f2" 1487 | }, 1488 | "outputs": [ 1489 | { 1490 | "data": { 1491 | "text/plain": [ 1492 | "True" 1493 | ] 1494 | }, 1495 | "execution_count": 54, 1496 | "metadata": {}, 1497 | "output_type": "execute_result" 1498 | } 1499 | ], 1500 | "source": [ 1501 | "(1 > 2) or (2 < 3)" 1502 | ] 1503 | }, 1504 | { 1505 | "cell_type": "code", 1506 | "execution_count": null, 1507 | "metadata": { 1508 | "id": "mGYQ6_-PMzBf", 1509 | "outputId": "b9677ec3-a8f0-4527-c8a7-40f0d33ab88c" 1510 | }, 1511 | "outputs": [ 1512 | { 1513 | "data": { 1514 | "text/plain": [ 1515 | "True" 1516 | ] 1517 | }, 1518 | "execution_count": 55, 1519 | "metadata": {}, 1520 | "output_type": "execute_result" 1521 | } 1522 | ], 1523 | "source": [ 1524 | "(1 == 2) or (2 == 3) or (4 == 4)\n", 1525 | "# F or F \n", 1526 | "# F \n", 1527 | "# F or T\n", 1528 | "# T\n" 1529 | ] 1530 | }, 1531 | { 1532 | "cell_type": "markdown", 1533 | "metadata": { 1534 | "id": "P1vNSet5MzBg" 1535 | }, 1536 | "source": [ 1537 | "## if,elif, else Statements" 1538 | ] 1539 | }, 1540 | { 1541 | "cell_type": "code", 1542 | "execution_count": null, 1543 | "metadata": { 1544 | "id": "kEVsk0p2MzBg", 1545 | "outputId": "162b21ab-ee11-4665-cc90-378cd95d23cd", 1546 | "colab": { 1547 | "base_uri": "https://localhost:8080/" 1548 | } 1549 | }, 1550 | "outputs": [ 1551 | { 1552 | "output_type": "stream", 1553 | "name": "stdout", 1554 | "text": [ 1555 | "Yep!\n" 1556 | ] 1557 | } 1558 | ], 1559 | "source": [ 1560 | "if (1 < 2):\n", 1561 | " print('Yep!')" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": null, 1567 | "metadata": { 1568 | "id": "nBQ84f91MzBg" 1569 | }, 1570 | "outputs": [], 1571 | "source": [ 1572 | "if (1 > 2):\n", 1573 | " print('yep!')\n" 1574 | ] 1575 | }, 1576 | { 1577 | "cell_type": "code", 1578 | "execution_count": null, 1579 | "metadata": { 1580 | "id": "oLdVq_YHMzBg", 1581 | "outputId": "ee29b190-e731-45bc-9bb5-daa6189a3792", 1582 | "colab": { 1583 | "base_uri": "https://localhost:8080/" 1584 | } 1585 | }, 1586 | "outputs": [ 1587 | { 1588 | "output_type": "stream", 1589 | "name": "stdout", 1590 | "text": [ 1591 | "last\n" 1592 | ] 1593 | } 1594 | ], 1595 | "source": [ 1596 | "if (1 > 2):\n", 1597 | " print('first')\n", 1598 | "else:\n", 1599 | " print('last')" 1600 | ] 1601 | }, 1602 | { 1603 | "cell_type": "code", 1604 | "execution_count": null, 1605 | "metadata": { 1606 | "id": "kdHXqeGmMzBo", 1607 | "outputId": "3b8f909f-c6a6-4f2d-b3b8-f92d110d7ce1" 1608 | }, 1609 | "outputs": [ 1610 | { 1611 | "name": "stdout", 1612 | "output_type": "stream", 1613 | "text": [ 1614 | "last\n" 1615 | ] 1616 | } 1617 | ], 1618 | "source": [ 1619 | "if 1 > 2:\n", 1620 | " print('first')\n", 1621 | "else:\n", 1622 | " print('last')" 1623 | ] 1624 | }, 1625 | { 1626 | "cell_type": "code", 1627 | "execution_count": null, 1628 | "metadata": { 1629 | "id": "hMSnIrbpMzBp", 1630 | "outputId": "19b9c76a-bfd7-4508-bd3c-b84585244ece", 1631 | "colab": { 1632 | "base_uri": "https://localhost:8080/" 1633 | } 1634 | }, 1635 | "outputs": [ 1636 | { 1637 | "output_type": "stream", 1638 | "name": "stdout", 1639 | "text": [ 1640 | "middle\n", 1641 | "DOne\n" 1642 | ] 1643 | } 1644 | ], 1645 | "source": [ 1646 | "if 1 == 2:\n", 1647 | " print('first')\n", 1648 | "elif 3 == 3:\n", 1649 | " print('middle')\n", 1650 | "elif 4==3:\n", 1651 | " print(\"four\")\n", 1652 | "else:\n", 1653 | " print('Last')\n", 1654 | "\n", 1655 | "\n", 1656 | "print('DOne')" 1657 | ] 1658 | }, 1659 | { 1660 | "cell_type": "markdown", 1661 | "metadata": { 1662 | "id": "yv88aEzhMzBp" 1663 | }, 1664 | "source": [ 1665 | "## for Loops" 1666 | ] 1667 | }, 1668 | { 1669 | "cell_type": "code", 1670 | "execution_count": null, 1671 | "metadata": { 1672 | "id": "uLEnEtsnMzBp" 1673 | }, 1674 | "outputs": [], 1675 | "source": [ 1676 | "seq = [1,2,3,4,5]" 1677 | ] 1678 | }, 1679 | { 1680 | "cell_type": "code", 1681 | "execution_count": null, 1682 | "metadata": { 1683 | "id": "8HgEDDb7MzBq", 1684 | "outputId": "580a941a-67d3-4983-a7da-98b9212aa28b", 1685 | "colab": { 1686 | "base_uri": "https://localhost:8080/" 1687 | } 1688 | }, 1689 | "outputs": [ 1690 | { 1691 | "output_type": "stream", 1692 | "name": "stdout", 1693 | "text": [ 1694 | "1\n", 1695 | "2\n", 1696 | "3\n", 1697 | "4\n", 1698 | "5\n" 1699 | ] 1700 | } 1701 | ], 1702 | "source": [ 1703 | "for item in seq:\n", 1704 | " print(item)" 1705 | ] 1706 | }, 1707 | { 1708 | "cell_type": "code", 1709 | "execution_count": null, 1710 | "metadata": { 1711 | "id": "7WZwMswYMzBq", 1712 | "outputId": "573afaf9-899d-4061-8b74-0329c89e7541", 1713 | "colab": { 1714 | "base_uri": "https://localhost:8080/" 1715 | } 1716 | }, 1717 | "outputs": [ 1718 | { 1719 | "output_type": "stream", 1720 | "name": "stdout", 1721 | "text": [ 1722 | "Yep\n", 1723 | "Yep\n", 1724 | "Yep\n", 1725 | "Yep\n", 1726 | "Yep\n" 1727 | ] 1728 | } 1729 | ], 1730 | "source": [ 1731 | "for item in seq:\n", 1732 | " print('Yep')" 1733 | ] 1734 | }, 1735 | { 1736 | "cell_type": "code", 1737 | "execution_count": null, 1738 | "metadata": { 1739 | "id": "KF55MV7FMzBq", 1740 | "outputId": "73323cb6-db88-4cba-cf71-f73a5e3a6f51" 1741 | }, 1742 | "outputs": [ 1743 | { 1744 | "name": "stdout", 1745 | "output_type": "stream", 1746 | "text": [ 1747 | "2\n", 1748 | "4\n", 1749 | "6\n", 1750 | "8\n", 1751 | "10\n" 1752 | ] 1753 | } 1754 | ], 1755 | "source": [ 1756 | "for jelly in seq:\n", 1757 | " print(jelly+jelly)" 1758 | ] 1759 | }, 1760 | { 1761 | "cell_type": "markdown", 1762 | "metadata": { 1763 | "id": "E4yI7jLuMzBr" 1764 | }, 1765 | "source": [ 1766 | "## while Loops" 1767 | ] 1768 | }, 1769 | { 1770 | "cell_type": "code", 1771 | "execution_count": null, 1772 | "metadata": { 1773 | "id": "S-FPHcfRMzBr", 1774 | "outputId": "f86ad142-3485-4c2b-b8a0-a8d98a0c5d0f", 1775 | "colab": { 1776 | "base_uri": "https://localhost:8080/" 1777 | } 1778 | }, 1779 | "outputs": [ 1780 | { 1781 | "output_type": "stream", 1782 | "name": "stdout", 1783 | "text": [ 1784 | "i is: 1\n", 1785 | "i is: 2\n", 1786 | "i is: 3\n", 1787 | "i is: 4\n", 1788 | "i is: 5\n" 1789 | ] 1790 | } 1791 | ], 1792 | "source": [ 1793 | "i = 1\n", 1794 | "while (i <= 5):\n", 1795 | " print('i is: {}'.format(i))\n", 1796 | " i = i+1" 1797 | ] 1798 | }, 1799 | { 1800 | "cell_type": "markdown", 1801 | "metadata": { 1802 | "id": "hI_N68yqMzBr" 1803 | }, 1804 | "source": [ 1805 | "## range()" 1806 | ] 1807 | }, 1808 | { 1809 | "cell_type": "code", 1810 | "execution_count": null, 1811 | "metadata": { 1812 | "id": "TL6nLEbMMzBr", 1813 | "outputId": "2dad7095-93b6-48a9-e463-0c73e03d8768", 1814 | "colab": { 1815 | "base_uri": "https://localhost:8080/" 1816 | } 1817 | }, 1818 | "outputs": [ 1819 | { 1820 | "output_type": "execute_result", 1821 | "data": { 1822 | "text/plain": [ 1823 | "range(0, 5)" 1824 | ] 1825 | }, 1826 | "metadata": {}, 1827 | "execution_count": 67 1828 | } 1829 | ], 1830 | "source": [ 1831 | "range(5)" 1832 | ] 1833 | }, 1834 | { 1835 | "cell_type": "code", 1836 | "execution_count": null, 1837 | "metadata": { 1838 | "id": "5QhshTzTMzBs", 1839 | "outputId": "fb41c8c1-b2e7-47c1-8bc4-8aa2e43d82ea", 1840 | "colab": { 1841 | "base_uri": "https://localhost:8080/" 1842 | } 1843 | }, 1844 | "outputs": [ 1845 | { 1846 | "output_type": "stream", 1847 | "name": "stdout", 1848 | "text": [ 1849 | "0\n", 1850 | "1\n", 1851 | "2\n", 1852 | "3\n", 1853 | "4\n" 1854 | ] 1855 | } 1856 | ], 1857 | "source": [ 1858 | "for i in range(5):\n", 1859 | " print(i)" 1860 | ] 1861 | }, 1862 | { 1863 | "cell_type": "code", 1864 | "execution_count": null, 1865 | "metadata": { 1866 | "id": "XCMe40uYMzBs", 1867 | "outputId": "0d1019a8-2e5c-46a9-9fe1-09a920cd1115", 1868 | "colab": { 1869 | "base_uri": "https://localhost:8080/" 1870 | } 1871 | }, 1872 | "outputs": [ 1873 | { 1874 | "output_type": "execute_result", 1875 | "data": { 1876 | "text/plain": [ 1877 | "[0, 1, 2, 3, 4]" 1878 | ] 1879 | }, 1880 | "metadata": {}, 1881 | "execution_count": 69 1882 | } 1883 | ], 1884 | "source": [ 1885 | "list(range(5))" 1886 | ] 1887 | }, 1888 | { 1889 | "cell_type": "markdown", 1890 | "metadata": { 1891 | "id": "YaBgpSYlMzBs" 1892 | }, 1893 | "source": [ 1894 | "## list comprehension" 1895 | ] 1896 | }, 1897 | { 1898 | "cell_type": "code", 1899 | "execution_count": null, 1900 | "metadata": { 1901 | "collapsed": true, 1902 | "id": "IzDsjXocMzBs" 1903 | }, 1904 | "outputs": [], 1905 | "source": [ 1906 | "x = [1,2,3,4]" 1907 | ] 1908 | }, 1909 | { 1910 | "cell_type": "code", 1911 | "execution_count": null, 1912 | "metadata": { 1913 | "id": "FwwYiYQFMzBt", 1914 | "outputId": "7b173b93-3ce1-4ae4-a356-f1eff44eced9", 1915 | "colab": { 1916 | "base_uri": "https://localhost:8080/" 1917 | } 1918 | }, 1919 | "outputs": [ 1920 | { 1921 | "output_type": "stream", 1922 | "name": "stdout", 1923 | "text": [ 1924 | "[1, 4, 9, 16]\n" 1925 | ] 1926 | } 1927 | ], 1928 | "source": [ 1929 | "out = []\n", 1930 | "for item in x:\n", 1931 | " out.append(item**2)\n", 1932 | "print(out)" 1933 | ] 1934 | }, 1935 | { 1936 | "cell_type": "code", 1937 | "execution_count": null, 1938 | "metadata": { 1939 | "id": "_ugpHhmAMzBt", 1940 | "outputId": "1f93dd68-2a12-4f72-c4ad-5ea00cc456cc", 1941 | "colab": { 1942 | "base_uri": "https://localhost:8080/" 1943 | } 1944 | }, 1945 | "outputs": [ 1946 | { 1947 | "output_type": "execute_result", 1948 | "data": { 1949 | "text/plain": [ 1950 | "[1, 8, 27, 64]" 1951 | ] 1952 | }, 1953 | "metadata": {}, 1954 | "execution_count": 74 1955 | } 1956 | ], 1957 | "source": [ 1958 | "store = [item**3 for item in x]\n", 1959 | "store" 1960 | ] 1961 | }, 1962 | { 1963 | "cell_type": "markdown", 1964 | "metadata": { 1965 | "id": "HyxbSTNWMzBt" 1966 | }, 1967 | "source": [ 1968 | "## functions" 1969 | ] 1970 | }, 1971 | { 1972 | "cell_type": "code", 1973 | "execution_count": null, 1974 | "metadata": { 1975 | "collapsed": true, 1976 | "id": "G82hKB7GMzBt" 1977 | }, 1978 | "outputs": [], 1979 | "source": [ 1980 | "def my_func(param1='default'):\n", 1981 | " \"\"\"\n", 1982 | " Docstring goes here.\n", 1983 | "\n", 1984 | " Add numbers \n", 1985 | " \n", 1986 | " \"\"\"\n", 1987 | " print(param1)" 1988 | ] 1989 | }, 1990 | { 1991 | "cell_type": "code", 1992 | "execution_count": null, 1993 | "metadata": { 1994 | "id": "iQbNT28yMzBu", 1995 | "outputId": "2c876775-4b50-4b1f-d7db-ed2683f65f23", 1996 | "colab": { 1997 | "base_uri": "https://localhost:8080/" 1998 | } 1999 | }, 2000 | "outputs": [ 2001 | { 2002 | "output_type": "execute_result", 2003 | "data": { 2004 | "text/plain": [ 2005 | "" 2006 | ] 2007 | }, 2008 | "metadata": {}, 2009 | "execution_count": 76 2010 | } 2011 | ], 2012 | "source": [ 2013 | "my_func" 2014 | ] 2015 | }, 2016 | { 2017 | "cell_type": "code", 2018 | "execution_count": null, 2019 | "metadata": { 2020 | "id": "uFLVU_n5MzBu", 2021 | "outputId": "aab0bed1-7211-4a50-8854-0a3755064928", 2022 | "colab": { 2023 | "base_uri": "https://localhost:8080/" 2024 | } 2025 | }, 2026 | "outputs": [ 2027 | { 2028 | "output_type": "stream", 2029 | "name": "stdout", 2030 | "text": [ 2031 | "default\n" 2032 | ] 2033 | } 2034 | ], 2035 | "source": [ 2036 | "my_func()" 2037 | ] 2038 | }, 2039 | { 2040 | "cell_type": "code", 2041 | "execution_count": null, 2042 | "metadata": { 2043 | "id": "Tt_0Uq2nMzBu", 2044 | "outputId": "eea40aa6-e3b8-47ac-c8f6-e5ee8bd6cacc", 2045 | "colab": { 2046 | "base_uri": "https://localhost:8080/" 2047 | } 2048 | }, 2049 | "outputs": [ 2050 | { 2051 | "output_type": "stream", 2052 | "name": "stdout", 2053 | "text": [ 2054 | "new param\n" 2055 | ] 2056 | } 2057 | ], 2058 | "source": [ 2059 | "my_func('new param')" 2060 | ] 2061 | }, 2062 | { 2063 | "cell_type": "code", 2064 | "execution_count": null, 2065 | "metadata": { 2066 | "id": "BMSXtU4iMzBu", 2067 | "outputId": "f7f9a8e9-50d5-4ded-c7b4-2807f19fdfe0" 2068 | }, 2069 | "outputs": [ 2070 | { 2071 | "name": "stdout", 2072 | "output_type": "stream", 2073 | "text": [ 2074 | "new param\n" 2075 | ] 2076 | } 2077 | ], 2078 | "source": [ 2079 | "my_func(param1='new param')" 2080 | ] 2081 | }, 2082 | { 2083 | "cell_type": "code", 2084 | "execution_count": null, 2085 | "metadata": { 2086 | "collapsed": true, 2087 | "id": "PiSmkjpiMzBv" 2088 | }, 2089 | "outputs": [], 2090 | "source": [ 2091 | "def square(x):\n", 2092 | " return x**2" 2093 | ] 2094 | }, 2095 | { 2096 | "cell_type": "code", 2097 | "execution_count": null, 2098 | "metadata": { 2099 | "collapsed": true, 2100 | "id": "SeTljqO5MzBv" 2101 | }, 2102 | "outputs": [], 2103 | "source": [ 2104 | "out = square(34)" 2105 | ] 2106 | }, 2107 | { 2108 | "cell_type": "code", 2109 | "execution_count": null, 2110 | "metadata": { 2111 | "scrolled": true, 2112 | "id": "BbzvPUIhMzBv", 2113 | "outputId": "a75ab8a8-7e99-4e4e-ed75-e5e9090daaa8", 2114 | "colab": { 2115 | "base_uri": "https://localhost:8080/" 2116 | } 2117 | }, 2118 | "outputs": [ 2119 | { 2120 | "output_type": "stream", 2121 | "name": "stdout", 2122 | "text": [ 2123 | "1156\n" 2124 | ] 2125 | } 2126 | ], 2127 | "source": [ 2128 | "print(out)" 2129 | ] 2130 | }, 2131 | { 2132 | "cell_type": "markdown", 2133 | "metadata": { 2134 | "id": "vehYCw-OMzBv" 2135 | }, 2136 | "source": [ 2137 | "## lambda expressions" 2138 | ] 2139 | }, 2140 | { 2141 | "cell_type": "code", 2142 | "execution_count": null, 2143 | "metadata": { 2144 | "collapsed": true, 2145 | "id": "1grx4VyhMzBw" 2146 | }, 2147 | "outputs": [], 2148 | "source": [ 2149 | "def times2(var):\n", 2150 | " return var*2" 2151 | ] 2152 | }, 2153 | { 2154 | "cell_type": "code", 2155 | "execution_count": null, 2156 | "metadata": { 2157 | "id": "-0x8g4rJMzBw", 2158 | "outputId": "9e46bb80-908e-4332-e41a-ad8dc6b00b37", 2159 | "colab": { 2160 | "base_uri": "https://localhost:8080/" 2161 | } 2162 | }, 2163 | "outputs": [ 2164 | { 2165 | "output_type": "execute_result", 2166 | "data": { 2167 | "text/plain": [ 2168 | "4" 2169 | ] 2170 | }, 2171 | "metadata": {}, 2172 | "execution_count": 20 2173 | } 2174 | ], 2175 | "source": [ 2176 | "times2(2)" 2177 | ] 2178 | }, 2179 | { 2180 | "cell_type": "code", 2181 | "source": [ 2182 | "def times2(var):return var*2" 2183 | ], 2184 | "metadata": { 2185 | "id": "tmokRZwH8A-J" 2186 | }, 2187 | "execution_count": null, 2188 | "outputs": [] 2189 | }, 2190 | { 2191 | "cell_type": "code", 2192 | "execution_count": null, 2193 | "metadata": { 2194 | "id": "rq5gcVWyMzBw", 2195 | "outputId": "fe36c3c3-d36e-4c0d-c3f9-568433904dfe", 2196 | "colab": { 2197 | "base_uri": "https://localhost:8080/" 2198 | } 2199 | }, 2200 | "outputs": [ 2201 | { 2202 | "output_type": "execute_result", 2203 | "data": { 2204 | "text/plain": [ 2205 | ">" 2206 | ] 2207 | }, 2208 | "metadata": {}, 2209 | "execution_count": 85 2210 | } 2211 | ], 2212 | "source": [ 2213 | "lambda var: var*2" 2214 | ] 2215 | }, 2216 | { 2217 | "cell_type": "code", 2218 | "source": [ 2219 | "func = lambda var: var*3\n", 2220 | "func(3)" 2221 | ], 2222 | "metadata": { 2223 | "id": "x8czzRgVcIc5", 2224 | "colab": { 2225 | "base_uri": "https://localhost:8080/" 2226 | }, 2227 | "outputId": "3940c62b-1357-47fc-cb45-eabe90696d33" 2228 | }, 2229 | "execution_count": null, 2230 | "outputs": [ 2231 | { 2232 | "output_type": "execute_result", 2233 | "data": { 2234 | "text/plain": [ 2235 | "9" 2236 | ] 2237 | }, 2238 | "metadata": {}, 2239 | "execution_count": 86 2240 | } 2241 | ] 2242 | }, 2243 | { 2244 | "cell_type": "code", 2245 | "source": [ 2246 | "x = lambda a : a + 10\n", 2247 | "print(x(5))" 2248 | ], 2249 | "metadata": { 2250 | "colab": { 2251 | "base_uri": "https://localhost:8080/" 2252 | }, 2253 | "id": "QctnNayo30of", 2254 | "outputId": "2cc0c6e4-645c-46da-d0cc-8ef981b06e54" 2255 | }, 2256 | "execution_count": null, 2257 | "outputs": [ 2258 | { 2259 | "output_type": "stream", 2260 | "name": "stdout", 2261 | "text": [ 2262 | "15\n" 2263 | ] 2264 | } 2265 | ] 2266 | }, 2267 | { 2268 | "cell_type": "code", 2269 | "source": [ 2270 | "x = lambda a , b : a * b\n", 2271 | "print(x(5, 6))" 2272 | ], 2273 | "metadata": { 2274 | "colab": { 2275 | "base_uri": "https://localhost:8080/" 2276 | }, 2277 | "id": "DqAIjSfJ327_", 2278 | "outputId": "f5943e0c-0618-40f5-db4f-b9ac8af2bea0" 2279 | }, 2280 | "execution_count": null, 2281 | "outputs": [ 2282 | { 2283 | "output_type": "stream", 2284 | "name": "stdout", 2285 | "text": [ 2286 | "30\n" 2287 | ] 2288 | } 2289 | ] 2290 | }, 2291 | { 2292 | "cell_type": "markdown", 2293 | "metadata": { 2294 | "id": "5Pt7LcbxMzBw" 2295 | }, 2296 | "source": [ 2297 | "## map and filter" 2298 | ] 2299 | }, 2300 | { 2301 | "cell_type": "code", 2302 | "source": [ 2303 | "def times2(var):\n", 2304 | " return var*2\n", 2305 | "\n", 2306 | "times2(4)" 2307 | ], 2308 | "metadata": { 2309 | "colab": { 2310 | "base_uri": "https://localhost:8080/" 2311 | }, 2312 | "id": "VC-wNFC_7P-h", 2313 | "outputId": "8232dce4-f136-4676-f122-99934d65dda1" 2314 | }, 2315 | "execution_count": null, 2316 | "outputs": [ 2317 | { 2318 | "output_type": "execute_result", 2319 | "data": { 2320 | "text/plain": [ 2321 | "8" 2322 | ] 2323 | }, 2324 | "metadata": {}, 2325 | "execution_count": 89 2326 | } 2327 | ] 2328 | }, 2329 | { 2330 | "cell_type": "code", 2331 | "execution_count": null, 2332 | "metadata": { 2333 | "collapsed": true, 2334 | "id": "kxcPmWZpMzBx" 2335 | }, 2336 | "outputs": [], 2337 | "source": [ 2338 | "seq = [1,2,3,4,5]" 2339 | ] 2340 | }, 2341 | { 2342 | "cell_type": "code", 2343 | "execution_count": null, 2344 | "metadata": { 2345 | "id": "knWquEWLMzBx", 2346 | "outputId": "79d6f3c7-5398-4512-8c77-a2717537faf4", 2347 | "colab": { 2348 | "base_uri": "https://localhost:8080/" 2349 | } 2350 | }, 2351 | "outputs": [ 2352 | { 2353 | "output_type": "execute_result", 2354 | "data": { 2355 | "text/plain": [ 2356 | "" 2357 | ] 2358 | }, 2359 | "metadata": {}, 2360 | "execution_count": 90 2361 | } 2362 | ], 2363 | "source": [ 2364 | "# Apply times2 func on each element\n", 2365 | "map( times2 , seq)\n", 2366 | "# Returns map at a particular place at memory" 2367 | ] 2368 | }, 2369 | { 2370 | "cell_type": "code", 2371 | "execution_count": null, 2372 | "metadata": { 2373 | "id": "yIEch0lCMzBy", 2374 | "outputId": "40b071ab-c4f8-4646-e462-c760dae05589", 2375 | "colab": { 2376 | "base_uri": "https://localhost:8080/" 2377 | } 2378 | }, 2379 | "outputs": [ 2380 | { 2381 | "output_type": "execute_result", 2382 | "data": { 2383 | "text/plain": [ 2384 | "[2, 4, 6, 8, 10]" 2385 | ] 2386 | }, 2387 | "metadata": {}, 2388 | "execution_count": 91 2389 | } 2390 | ], 2391 | "source": [ 2392 | "# Returns the list \n", 2393 | "list(map(times2,seq))" 2394 | ] 2395 | }, 2396 | { 2397 | "cell_type": "code", 2398 | "execution_count": null, 2399 | "metadata": { 2400 | "id": "WV-c7FeLMzBy", 2401 | "outputId": "75e766c6-2d74-48dd-d4ee-5cf3d77ca897", 2402 | "colab": { 2403 | "base_uri": "https://localhost:8080/" 2404 | } 2405 | }, 2406 | "outputs": [ 2407 | { 2408 | "output_type": "execute_result", 2409 | "data": { 2410 | "text/plain": [ 2411 | "[3, 6, 9, 12, 15]" 2412 | ] 2413 | }, 2414 | "metadata": {}, 2415 | "execution_count": 92 2416 | } 2417 | ], 2418 | "source": [ 2419 | "list(map(lambda var: var*3 ,seq))" 2420 | ] 2421 | }, 2422 | { 2423 | "cell_type": "code", 2424 | "execution_count": null, 2425 | "metadata": { 2426 | "id": "bCEHO_k0MzBy", 2427 | "outputId": "2002c30d-eed3-430e-b23e-8dfe7cfb4007", 2428 | "colab": { 2429 | "base_uri": "https://localhost:8080/" 2430 | } 2431 | }, 2432 | "outputs": [ 2433 | { 2434 | "output_type": "execute_result", 2435 | "data": { 2436 | "text/plain": [ 2437 | "" 2438 | ] 2439 | }, 2440 | "metadata": {}, 2441 | "execution_count": 93 2442 | } 2443 | ], 2444 | "source": [ 2445 | "# Filters out or returns boolean value\n", 2446 | "filter(lambda item: item%2 == 0, seq)" 2447 | ] 2448 | }, 2449 | { 2450 | "cell_type": "code", 2451 | "execution_count": null, 2452 | "metadata": { 2453 | "id": "HGq3Z6F2MzBz", 2454 | "outputId": "1276c308-4099-4d9c-d0b3-3eba44918e1d", 2455 | "colab": { 2456 | "base_uri": "https://localhost:8080/" 2457 | } 2458 | }, 2459 | "outputs": [ 2460 | { 2461 | "output_type": "execute_result", 2462 | "data": { 2463 | "text/plain": [ 2464 | "[2, 4]" 2465 | ] 2466 | }, 2467 | "metadata": {}, 2468 | "execution_count": 94 2469 | } 2470 | ], 2471 | "source": [ 2472 | "list(filter(lambda item: item%2 == 0,seq))" 2473 | ] 2474 | }, 2475 | { 2476 | "cell_type": "markdown", 2477 | "metadata": { 2478 | "id": "c9Vk0RMTMzBz" 2479 | }, 2480 | "source": [ 2481 | "## methods" 2482 | ] 2483 | }, 2484 | { 2485 | "cell_type": "code", 2486 | "execution_count": null, 2487 | "metadata": { 2488 | "collapsed": true, 2489 | "id": "I3_5LYPcMzBz" 2490 | }, 2491 | "outputs": [], 2492 | "source": [ 2493 | "st = 'hello mY name is Sam'" 2494 | ] 2495 | }, 2496 | { 2497 | "cell_type": "code", 2498 | "execution_count": null, 2499 | "metadata": { 2500 | "id": "GRQ4Ns25MzBz", 2501 | "outputId": "bfaccf37-ff91-420f-e1e0-a3ef286550a5", 2502 | "colab": { 2503 | "base_uri": "https://localhost:8080/", 2504 | "height": 36 2505 | } 2506 | }, 2507 | "outputs": [ 2508 | { 2509 | "output_type": "execute_result", 2510 | "data": { 2511 | "application/vnd.google.colaboratory.intrinsic+json": { 2512 | "type": "string" 2513 | }, 2514 | "text/plain": [ 2515 | "'hello my name is sam'" 2516 | ] 2517 | }, 2518 | "metadata": {}, 2519 | "execution_count": 98 2520 | } 2521 | ], 2522 | "source": [ 2523 | "st.lower()" 2524 | ] 2525 | }, 2526 | { 2527 | "cell_type": "code", 2528 | "execution_count": null, 2529 | "metadata": { 2530 | "id": "i57OKEQFMzB0", 2531 | "outputId": "9704a7ff-9c19-41ef-9843-4ace61142f3b", 2532 | "colab": { 2533 | "base_uri": "https://localhost:8080/", 2534 | "height": 36 2535 | } 2536 | }, 2537 | "outputs": [ 2538 | { 2539 | "output_type": "execute_result", 2540 | "data": { 2541 | "application/vnd.google.colaboratory.intrinsic+json": { 2542 | "type": "string" 2543 | }, 2544 | "text/plain": [ 2545 | "'HELLO MY NAME IS SAM'" 2546 | ] 2547 | }, 2548 | "metadata": {}, 2549 | "execution_count": 99 2550 | } 2551 | ], 2552 | "source": [ 2553 | "st.upper()" 2554 | ] 2555 | }, 2556 | { 2557 | "cell_type": "code", 2558 | "execution_count": null, 2559 | "metadata": { 2560 | "id": "pevrNFZnMzB1", 2561 | "outputId": "cb51e6fa-7858-4297-b513-9fb9113fe66f", 2562 | "colab": { 2563 | "base_uri": "https://localhost:8080/" 2564 | } 2565 | }, 2566 | "outputs": [ 2567 | { 2568 | "output_type": "execute_result", 2569 | "data": { 2570 | "text/plain": [ 2571 | "['hello', 'mY', 'name', 'is', 'Sam']" 2572 | ] 2573 | }, 2574 | "metadata": {}, 2575 | "execution_count": 100 2576 | } 2577 | ], 2578 | "source": [ 2579 | "st.split()" 2580 | ] 2581 | }, 2582 | { 2583 | "cell_type": "code", 2584 | "execution_count": null, 2585 | "metadata": { 2586 | "collapsed": true, 2587 | "id": "d9Y-IHclMzB1" 2588 | }, 2589 | "outputs": [], 2590 | "source": [ 2591 | "tweet = 'Go Sports! #Sports'" 2592 | ] 2593 | }, 2594 | { 2595 | "cell_type": "code", 2596 | "execution_count": null, 2597 | "metadata": { 2598 | "id": "X5a7IDZNMzB1", 2599 | "outputId": "100564a8-d2bd-4647-c8d1-c2fe171f8a98" 2600 | }, 2601 | "outputs": [ 2602 | { 2603 | "data": { 2604 | "text/plain": [ 2605 | "['Go Sports! ', 'Sports']" 2606 | ] 2607 | }, 2608 | "execution_count": 106, 2609 | "metadata": {}, 2610 | "output_type": "execute_result" 2611 | } 2612 | ], 2613 | "source": [ 2614 | "tweet.split('#')" 2615 | ] 2616 | }, 2617 | { 2618 | "cell_type": "code", 2619 | "execution_count": null, 2620 | "metadata": { 2621 | "id": "PY-OvnMmMzB2", 2622 | "outputId": "ee6e2b12-2e41-4226-cd60-e1e7af2d035e", 2623 | "colab": { 2624 | "base_uri": "https://localhost:8080/", 2625 | "height": 36 2626 | } 2627 | }, 2628 | "outputs": [ 2629 | { 2630 | "output_type": "execute_result", 2631 | "data": { 2632 | "application/vnd.google.colaboratory.intrinsic+json": { 2633 | "type": "string" 2634 | }, 2635 | "text/plain": [ 2636 | "'Sports'" 2637 | ] 2638 | }, 2639 | "metadata": {}, 2640 | "execution_count": 102 2641 | } 2642 | ], 2643 | "source": [ 2644 | "tweet.split('#')[1]" 2645 | ] 2646 | }, 2647 | { 2648 | "cell_type": "code", 2649 | "execution_count": null, 2650 | "metadata": { 2651 | "id": "ANLBerDzMzB2", 2652 | "outputId": "478730c5-6d3b-427e-c209-90e2e2517a20", 2653 | "colab": { 2654 | "base_uri": "https://localhost:8080/" 2655 | } 2656 | }, 2657 | "outputs": [ 2658 | { 2659 | "output_type": "execute_result", 2660 | "data": { 2661 | "text/plain": [ 2662 | "{'key1': 'item1', 'key2': 'item2'}" 2663 | ] 2664 | }, 2665 | "metadata": {}, 2666 | "execution_count": 103 2667 | } 2668 | ], 2669 | "source": [ 2670 | "d " 2671 | ] 2672 | }, 2673 | { 2674 | "cell_type": "code", 2675 | "execution_count": null, 2676 | "metadata": { 2677 | "id": "IvrIiMFcMzB2", 2678 | "outputId": "f5f882b0-172d-47ae-e05b-b22f0b169d75", 2679 | "colab": { 2680 | "base_uri": "https://localhost:8080/" 2681 | } 2682 | }, 2683 | "outputs": [ 2684 | { 2685 | "output_type": "execute_result", 2686 | "data": { 2687 | "text/plain": [ 2688 | "dict_keys(['key1', 'key2'])" 2689 | ] 2690 | }, 2691 | "metadata": {}, 2692 | "execution_count": 104 2693 | } 2694 | ], 2695 | "source": [ 2696 | "d.keys()" 2697 | ] 2698 | }, 2699 | { 2700 | "cell_type": "code", 2701 | "execution_count": null, 2702 | "metadata": { 2703 | "id": "_sqeVQgqMzB3", 2704 | "outputId": "96e23dba-73d5-45ea-b6c0-0793cb28ab80", 2705 | "colab": { 2706 | "base_uri": "https://localhost:8080/" 2707 | } 2708 | }, 2709 | "outputs": [ 2710 | { 2711 | "output_type": "execute_result", 2712 | "data": { 2713 | "text/plain": [ 2714 | "dict_items([('key1', 'item1'), ('key2', 'item2')])" 2715 | ] 2716 | }, 2717 | "metadata": {}, 2718 | "execution_count": 105 2719 | } 2720 | ], 2721 | "source": [ 2722 | "d.items()" 2723 | ] 2724 | }, 2725 | { 2726 | "cell_type": "code", 2727 | "execution_count": null, 2728 | "metadata": { 2729 | "collapsed": true, 2730 | "id": "rcbP3o6ZMzB3" 2731 | }, 2732 | "outputs": [], 2733 | "source": [ 2734 | "lst = [1,2,3]" 2735 | ] 2736 | }, 2737 | { 2738 | "cell_type": "code", 2739 | "execution_count": null, 2740 | "metadata": { 2741 | "id": "Z0-VAz22MzB3", 2742 | "outputId": "6b9e4c46-9c8f-412c-bc14-3b3786e93663", 2743 | "colab": { 2744 | "base_uri": "https://localhost:8080/" 2745 | } 2746 | }, 2747 | "outputs": [ 2748 | { 2749 | "output_type": "execute_result", 2750 | "data": { 2751 | "text/plain": [ 2752 | "2" 2753 | ] 2754 | }, 2755 | "metadata": {}, 2756 | "execution_count": 108 2757 | } 2758 | ], 2759 | "source": [ 2760 | "lst.pop()" 2761 | ] 2762 | }, 2763 | { 2764 | "cell_type": "code", 2765 | "execution_count": null, 2766 | "metadata": { 2767 | "id": "fZTFz_1NMzB4" 2768 | }, 2769 | "outputs": [], 2770 | "source": [ 2771 | "lst.append(5)" 2772 | ] 2773 | }, 2774 | { 2775 | "cell_type": "code", 2776 | "source": [ 2777 | "lst" 2778 | ], 2779 | "metadata": { 2780 | "colab": { 2781 | "base_uri": "https://localhost:8080/" 2782 | }, 2783 | "id": "SSBBD5wmJEyn", 2784 | "outputId": "af7f0443-a79a-4985-81d7-b964db2bafa7" 2785 | }, 2786 | "execution_count": null, 2787 | "outputs": [ 2788 | { 2789 | "output_type": "execute_result", 2790 | "data": { 2791 | "text/plain": [ 2792 | "[1, 5]" 2793 | ] 2794 | }, 2795 | "metadata": {}, 2796 | "execution_count": 110 2797 | } 2798 | ] 2799 | }, 2800 | { 2801 | "cell_type": "code", 2802 | "execution_count": null, 2803 | "metadata": { 2804 | "id": "n-P3iW8jMzB4", 2805 | "outputId": "c1b1dc01-460d-45bf-9b0c-50ca9ca10019", 2806 | "colab": { 2807 | "base_uri": "https://localhost:8080/" 2808 | } 2809 | }, 2810 | "outputs": [ 2811 | { 2812 | "output_type": "execute_result", 2813 | "data": { 2814 | "text/plain": [ 2815 | "False" 2816 | ] 2817 | }, 2818 | "metadata": {}, 2819 | "execution_count": 111 2820 | } 2821 | ], 2822 | "source": [ 2823 | "'x' in [1,2,3]" 2824 | ] 2825 | }, 2826 | { 2827 | "cell_type": "code", 2828 | "execution_count": null, 2829 | "metadata": { 2830 | "id": "7YGFYUbdMzB4", 2831 | "outputId": "900c9879-dee9-4007-aa9a-ee493ee35c7b", 2832 | "colab": { 2833 | "base_uri": "https://localhost:8080/" 2834 | } 2835 | }, 2836 | "outputs": [ 2837 | { 2838 | "output_type": "execute_result", 2839 | "data": { 2840 | "text/plain": [ 2841 | "True" 2842 | ] 2843 | }, 2844 | "metadata": {}, 2845 | "execution_count": 112 2846 | } 2847 | ], 2848 | "source": [ 2849 | "'x' in ['x','y','z']" 2850 | ] 2851 | }, 2852 | { 2853 | "cell_type": "markdown", 2854 | "metadata": { 2855 | "id": "jsiXgqaAMzB5" 2856 | }, 2857 | "source": [ 2858 | "# Error Handling" 2859 | ] 2860 | }, 2861 | { 2862 | "cell_type": "code", 2863 | "execution_count": null, 2864 | "metadata": { 2865 | "id": "3xuSf-fLMzB5", 2866 | "outputId": "aa9b5c1a-1524-4c35-92cd-88c340ecf26c", 2867 | "colab": { 2868 | "base_uri": "https://localhost:8080/" 2869 | } 2870 | }, 2871 | "outputs": [ 2872 | { 2873 | "name": "stdout", 2874 | "output_type": "stream", 2875 | "text": [ 2876 | "Enter Number: 3\n" 2877 | ] 2878 | }, 2879 | { 2880 | "output_type": "execute_result", 2881 | "data": { 2882 | "text/plain": [ 2883 | "3.3333333333333335" 2884 | ] 2885 | }, 2886 | "metadata": {}, 2887 | "execution_count": 116 2888 | } 2889 | ], 2890 | "source": [ 2891 | "var=10\n", 2892 | "answer = int(input(\"Enter Number: \"))\n", 2893 | "var/answer" 2894 | ] 2895 | }, 2896 | { 2897 | "cell_type": "code", 2898 | "execution_count": null, 2899 | "metadata": { 2900 | "id": "GNPkAP10MzB6", 2901 | "outputId": "d33faac4-4af5-4cdb-ed7b-613f5eeb06f7", 2902 | "colab": { 2903 | "base_uri": "https://localhost:8080/" 2904 | } 2905 | }, 2906 | "outputs": [ 2907 | { 2908 | "output_type": "stream", 2909 | "name": "stdout", 2910 | "text": [ 2911 | "Enter Number: 0\n", 2912 | "division by zero\n" 2913 | ] 2914 | } 2915 | ], 2916 | "source": [ 2917 | "\n", 2918 | "# code asks user for number and divides 10 by it\n", 2919 | "# enter '0' to trigger exception\n", 2920 | "# enter any string to check for exception\n", 2921 | "try:\n", 2922 | " var=10\n", 2923 | " answer = int(input(\"Enter Number: \"))\n", 2924 | " var/answer\n", 2925 | "except ZeroDivisionError as e:\n", 2926 | " print(e)\n", 2927 | "except ValueError:\n", 2928 | " print(\"Caught any exception dont pass a string\")" 2929 | ] 2930 | }, 2931 | { 2932 | "cell_type": "code", 2933 | "execution_count": null, 2934 | "metadata": { 2935 | "id": "W1J_bcOcMzB8", 2936 | "outputId": "aa8cf6d5-693f-4ac0-d08d-521bf7484165", 2937 | "colab": { 2938 | "base_uri": "https://localhost:8080/" 2939 | } 2940 | }, 2941 | "outputs": [ 2942 | { 2943 | "output_type": "stream", 2944 | "name": "stdout", 2945 | "text": [ 2946 | "Enter Number: 45\n", 2947 | "Exit code\n" 2948 | ] 2949 | } 2950 | ], 2951 | "source": [ 2952 | "try:\n", 2953 | " var=10\n", 2954 | " answer = int(input(\"Enter Number: \"))\n", 2955 | " var/answer\n", 2956 | "except ZeroDivisionError as e:\n", 2957 | " print(e)\n", 2958 | "except ValueError:\n", 2959 | " print(\"Caught any exception\")\n", 2960 | "except:\n", 2961 | " pass\n", 2962 | "\n", 2963 | " \n", 2964 | "finally:\n", 2965 | " print(\"Exit code\")\n" 2966 | ] 2967 | }, 2968 | { 2969 | "cell_type": "markdown", 2970 | "metadata": { 2971 | "id": "8Bsrtc3OMzB9" 2972 | }, 2973 | "source": [ 2974 | "# NumPy \n", 2975 | "\n", 2976 | "NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks.\n", 2977 | "\n" 2978 | ] 2979 | }, 2980 | { 2981 | "cell_type": "markdown", 2982 | "metadata": { 2983 | "id": "IOKc9i8sMzB9" 2984 | }, 2985 | "source": [ 2986 | "## Install numpy " 2987 | ] 2988 | }, 2989 | { 2990 | "cell_type": "code", 2991 | "execution_count": null, 2992 | "metadata": { 2993 | "id": "g4HIyvNNMzB9", 2994 | "outputId": "357d6472-7967-4319-c57b-b7ed8367ae90", 2995 | "colab": { 2996 | "base_uri": "https://localhost:8080/" 2997 | } 2998 | }, 2999 | "outputs": [ 3000 | { 3001 | "output_type": "stream", 3002 | "name": "stdout", 3003 | "text": [ 3004 | "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (1.19.5)\n" 3005 | ] 3006 | } 3007 | ], 3008 | "source": [ 3009 | "# !pip install numpy" 3010 | ] 3011 | }, 3012 | { 3013 | "cell_type": "markdown", 3014 | "metadata": { 3015 | "id": "WcKJ8vA6MzB-" 3016 | }, 3017 | "source": [ 3018 | "## Using NumPy\n", 3019 | "\n", 3020 | "Once you've installed NumPy you can import it as a library:" 3021 | ] 3022 | }, 3023 | { 3024 | "cell_type": "code", 3025 | "execution_count": null, 3026 | "metadata": { 3027 | "id": "KZG07uSCMzB-" 3028 | }, 3029 | "outputs": [], 3030 | "source": [ 3031 | "import numpy as np" 3032 | ] 3033 | }, 3034 | { 3035 | "cell_type": "markdown", 3036 | "metadata": { 3037 | "id": "o1lFDQK8MzB-" 3038 | }, 3039 | "source": [ 3040 | "Numpy has many built-in functions and capabilities. We won't cover them all but instead we will focus on some of the most important aspects of Numpy: vectors,arrays,matrices, and number generation. Let's start by discussing arrays.\n", 3041 | "\n", 3042 | "# Numpy Arrays\n", 3043 | "\n", 3044 | "NumPy arrays are the main way we will use Numpy throughout the workshop. Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).\n", 3045 | "\n", 3046 | "Let's begin our introduction by exploring how to create NumPy arrays.\n", 3047 | "\n", 3048 | "## Creating NumPy Arrays\n", 3049 | "\n", 3050 | "### From a Python List\n", 3051 | "\n", 3052 | "We can create an array by directly converting a list or list of lists:" 3053 | ] 3054 | }, 3055 | { 3056 | "cell_type": "code", 3057 | "execution_count": null, 3058 | "metadata": { 3059 | "id": "z9bqrP5XMzB-", 3060 | "outputId": "fadebdc0-4647-4f62-c4ac-4a1a83202257", 3061 | "colab": { 3062 | "base_uri": "https://localhost:8080/" 3063 | } 3064 | }, 3065 | "outputs": [ 3066 | { 3067 | "output_type": "execute_result", 3068 | "data": { 3069 | "text/plain": [ 3070 | "[1, 2, 3]" 3071 | ] 3072 | }, 3073 | "metadata": {}, 3074 | "execution_count": 123 3075 | } 3076 | ], 3077 | "source": [ 3078 | "my_list = [1,2,3]\n", 3079 | "my_list" 3080 | ] 3081 | }, 3082 | { 3083 | "cell_type": "code", 3084 | "execution_count": null, 3085 | "metadata": { 3086 | "id": "vS-tUfTjMzB_", 3087 | "outputId": "02871d54-d2eb-4dbe-d3ae-61a1843a0847", 3088 | "colab": { 3089 | "base_uri": "https://localhost:8080/" 3090 | } 3091 | }, 3092 | "outputs": [ 3093 | { 3094 | "output_type": "execute_result", 3095 | "data": { 3096 | "text/plain": [ 3097 | "array([1, 2, 3])" 3098 | ] 3099 | }, 3100 | "metadata": {}, 3101 | "execution_count": 124 3102 | } 3103 | ], 3104 | "source": [ 3105 | "ar = np.array(my_list)\n", 3106 | "ar \n" 3107 | ] 3108 | }, 3109 | { 3110 | "cell_type": "code", 3111 | "source": [ 3112 | "#Convert back array in to list\n", 3113 | "list(ar)" 3114 | ], 3115 | "metadata": { 3116 | "id": "e9QSlg3Wxcjy", 3117 | "colab": { 3118 | "base_uri": "https://localhost:8080/" 3119 | }, 3120 | "outputId": "53acd495-f907-466f-e541-bb2a284aadb1" 3121 | }, 3122 | "execution_count": null, 3123 | "outputs": [ 3124 | { 3125 | "output_type": "execute_result", 3126 | "data": { 3127 | "text/plain": [ 3128 | "[1, 2, 3]" 3129 | ] 3130 | }, 3131 | "metadata": {}, 3132 | "execution_count": 125 3133 | } 3134 | ] 3135 | }, 3136 | { 3137 | "cell_type": "code", 3138 | "execution_count": null, 3139 | "metadata": { 3140 | "id": "v0WJmLkGMzB_", 3141 | "outputId": "27c5a770-11cb-465d-d6f5-b07d761276d4", 3142 | "colab": { 3143 | "base_uri": "https://localhost:8080/" 3144 | } 3145 | }, 3146 | "outputs": [ 3147 | { 3148 | "output_type": "execute_result", 3149 | "data": { 3150 | "text/plain": [ 3151 | "array([[1, 2, 3],\n", 3152 | " [4, 5, 6],\n", 3153 | " [7, 8, 9]])" 3154 | ] 3155 | }, 3156 | "metadata": {}, 3157 | "execution_count": 126 3158 | } 3159 | ], 3160 | "source": [ 3161 | "my_matrix = [ [1,2,3] , [4,5,6] , [7,8,9] ]\n", 3162 | "arr = np.array(my_matrix)\n", 3163 | "arr" 3164 | ] 3165 | }, 3166 | { 3167 | "cell_type": "code", 3168 | "source": [ 3169 | "#Convert 2d array into list\n", 3170 | "list(arr)\n", 3171 | "# It is storing a list of arrays" 3172 | ], 3173 | "metadata": { 3174 | "colab": { 3175 | "base_uri": "https://localhost:8080/" 3176 | }, 3177 | "id": "K-cV7WncxqJv", 3178 | "outputId": "6d347614-f096-454b-97b4-be33d6d192c2" 3179 | }, 3180 | "execution_count": null, 3181 | "outputs": [ 3182 | { 3183 | "output_type": "execute_result", 3184 | "data": { 3185 | "text/plain": [ 3186 | "[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]" 3187 | ] 3188 | }, 3189 | "metadata": {}, 3190 | "execution_count": 127 3191 | } 3192 | ] 3193 | }, 3194 | { 3195 | "cell_type": "code", 3196 | "source": [ 3197 | "#Converts 2d array to list\n", 3198 | "arr.tolist()" 3199 | ], 3200 | "metadata": { 3201 | "colab": { 3202 | "base_uri": "https://localhost:8080/" 3203 | }, 3204 | "id": "6DmMzeENyEHg", 3205 | "outputId": "aa4b19b3-b444-44f0-9a91-b4f5908c26a2" 3206 | }, 3207 | "execution_count": null, 3208 | "outputs": [ 3209 | { 3210 | "output_type": "execute_result", 3211 | "data": { 3212 | "text/plain": [ 3213 | "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" 3214 | ] 3215 | }, 3216 | "metadata": {}, 3217 | "execution_count": 128 3218 | } 3219 | ] 3220 | }, 3221 | { 3222 | "cell_type": "code", 3223 | "execution_count": null, 3224 | "metadata": { 3225 | "id": "BfKIJMq7MzB_", 3226 | "outputId": "a3034860-1bb3-4be8-9ec6-015363b7c397", 3227 | "colab": { 3228 | "base_uri": "https://localhost:8080/" 3229 | } 3230 | }, 3231 | "outputs": [ 3232 | { 3233 | "output_type": "execute_result", 3234 | "data": { 3235 | "text/plain": [ 3236 | "array([[1, 2, 3],\n", 3237 | " [4, 5, 6],\n", 3238 | " [7, 8, 9]])" 3239 | ] 3240 | }, 3241 | "metadata": {}, 3242 | "execution_count": 129 3243 | } 3244 | ], 3245 | "source": [ 3246 | "np.array(my_matrix)" 3247 | ] 3248 | }, 3249 | { 3250 | "cell_type": "markdown", 3251 | "metadata": { 3252 | "id": "GI3wYdzbMzCA" 3253 | }, 3254 | "source": [ 3255 | "## Built-in Methods\n", 3256 | "\n", 3257 | "There are lots of built-in ways to generate Arrays" 3258 | ] 3259 | }, 3260 | { 3261 | "cell_type": "markdown", 3262 | "metadata": { 3263 | "id": "jDIxTmT9MzCA" 3264 | }, 3265 | "source": [ 3266 | "### arange\n", 3267 | "\n", 3268 | "Return evenly spaced values within a given interval." 3269 | ] 3270 | }, 3271 | { 3272 | "cell_type": "code", 3273 | "execution_count": null, 3274 | "metadata": { 3275 | "id": "wHuQG5IkMzCA", 3276 | "outputId": "9c8b070b-8ee8-476c-e24a-77f0d76d083a" 3277 | }, 3278 | "outputs": [ 3279 | { 3280 | "data": { 3281 | "text/plain": [ 3282 | "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" 3283 | ] 3284 | }, 3285 | "execution_count": 22, 3286 | "metadata": {}, 3287 | "output_type": "execute_result" 3288 | } 3289 | ], 3290 | "source": [ 3291 | "np.arange(0,10)" 3292 | ] 3293 | }, 3294 | { 3295 | "cell_type": "code", 3296 | "execution_count": null, 3297 | "metadata": { 3298 | "id": "DhQFNNk-MzCA", 3299 | "outputId": "ec01acf8-eb45-4426-95ac-f67b93e667ff", 3300 | "colab": { 3301 | "base_uri": "https://localhost:8080/" 3302 | } 3303 | }, 3304 | "outputs": [ 3305 | { 3306 | "output_type": "execute_result", 3307 | "data": { 3308 | "text/plain": [ 3309 | "array([0, 3, 6, 9])" 3310 | ] 3311 | }, 3312 | "metadata": {}, 3313 | "execution_count": 131 3314 | } 3315 | ], 3316 | "source": [ 3317 | "np.arange(0,11,3)" 3318 | ] 3319 | }, 3320 | { 3321 | "cell_type": "markdown", 3322 | "metadata": { 3323 | "id": "YVgr-TZsMzCB" 3324 | }, 3325 | "source": [ 3326 | "### zeros and ones\n", 3327 | "\n", 3328 | "Generate arrays of zeros or ones" 3329 | ] 3330 | }, 3331 | { 3332 | "cell_type": "code", 3333 | "execution_count": null, 3334 | "metadata": { 3335 | "id": "hBLIksLVMzCB", 3336 | "outputId": "1b7282f1-c654-4220-85cc-79adb338f26a" 3337 | }, 3338 | "outputs": [ 3339 | { 3340 | "data": { 3341 | "text/plain": [ 3342 | "array([ 0., 0., 0.])" 3343 | ] 3344 | }, 3345 | "execution_count": 24, 3346 | "metadata": {}, 3347 | "output_type": "execute_result" 3348 | } 3349 | ], 3350 | "source": [ 3351 | "np.zeros(3)" 3352 | ] 3353 | }, 3354 | { 3355 | "cell_type": "code", 3356 | "execution_count": null, 3357 | "metadata": { 3358 | "id": "CluorO6KMzCB", 3359 | "outputId": "ac6aa236-4354-4a59-a19c-1a74c0026b0d" 3360 | }, 3361 | "outputs": [ 3362 | { 3363 | "data": { 3364 | "text/plain": [ 3365 | "array([[ 0., 0., 0., 0., 0.],\n", 3366 | " [ 0., 0., 0., 0., 0.],\n", 3367 | " [ 0., 0., 0., 0., 0.],\n", 3368 | " [ 0., 0., 0., 0., 0.],\n", 3369 | " [ 0., 0., 0., 0., 0.]])" 3370 | ] 3371 | }, 3372 | "execution_count": 26, 3373 | "metadata": {}, 3374 | "output_type": "execute_result" 3375 | } 3376 | ], 3377 | "source": [ 3378 | "np.zeros((5,5))" 3379 | ] 3380 | }, 3381 | { 3382 | "cell_type": "code", 3383 | "execution_count": null, 3384 | "metadata": { 3385 | "id": "1fuNCcZSMzCB", 3386 | "outputId": "d392034c-945c-4592-92c9-d0f3fa90c7f4" 3387 | }, 3388 | "outputs": [ 3389 | { 3390 | "data": { 3391 | "text/plain": [ 3392 | "array([ 1., 1., 1.])" 3393 | ] 3394 | }, 3395 | "execution_count": 27, 3396 | "metadata": {}, 3397 | "output_type": "execute_result" 3398 | } 3399 | ], 3400 | "source": [ 3401 | "np.ones(3)" 3402 | ] 3403 | }, 3404 | { 3405 | "cell_type": "code", 3406 | "execution_count": null, 3407 | "metadata": { 3408 | "id": "AQnSj7zkMzCC", 3409 | "outputId": "11f4beee-7d46-4633-f4d2-ea3bf3645a1d" 3410 | }, 3411 | "outputs": [ 3412 | { 3413 | "data": { 3414 | "text/plain": [ 3415 | "array([[ 1., 1., 1.],\n", 3416 | " [ 1., 1., 1.],\n", 3417 | " [ 1., 1., 1.]])" 3418 | ] 3419 | }, 3420 | "execution_count": 28, 3421 | "metadata": {}, 3422 | "output_type": "execute_result" 3423 | } 3424 | ], 3425 | "source": [ 3426 | "np.ones((3,3))" 3427 | ] 3428 | }, 3429 | { 3430 | "cell_type": "markdown", 3431 | "metadata": { 3432 | "id": "JCzg9f5OMzCC" 3433 | }, 3434 | "source": [ 3435 | "### linspace\n", 3436 | "Return evenly spaced numbers over a specified interval." 3437 | ] 3438 | }, 3439 | { 3440 | "cell_type": "code", 3441 | "execution_count": null, 3442 | "metadata": { 3443 | "id": "t5KcATlSMzCC", 3444 | "outputId": "49425afa-e7fe-4e64-dbd6-ac6087677d3d", 3445 | "colab": { 3446 | "base_uri": "https://localhost:8080/" 3447 | } 3448 | }, 3449 | "outputs": [ 3450 | { 3451 | "output_type": "execute_result", 3452 | "data": { 3453 | "text/plain": [ 3454 | "array([ 0., 5., 10.])" 3455 | ] 3456 | }, 3457 | "metadata": {}, 3458 | "execution_count": 132 3459 | } 3460 | ], 3461 | "source": [ 3462 | "np.linspace(0,10,3)" 3463 | ] 3464 | }, 3465 | { 3466 | "cell_type": "code", 3467 | "execution_count": null, 3468 | "metadata": { 3469 | "id": "gIrjhFIJMzCC", 3470 | "outputId": "92e7cf06-b723-40ca-f8f6-59467eed4b34", 3471 | "colab": { 3472 | "base_uri": "https://localhost:8080/" 3473 | } 3474 | }, 3475 | "outputs": [ 3476 | { 3477 | "output_type": "execute_result", 3478 | "data": { 3479 | "text/plain": [ 3480 | "array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,\n", 3481 | " 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,\n", 3482 | " 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,\n", 3483 | " 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,\n", 3484 | " 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,\n", 3485 | " 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,\n", 3486 | " 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,\n", 3487 | " 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,\n", 3488 | " 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,\n", 3489 | " 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])" 3490 | ] 3491 | }, 3492 | "metadata": {}, 3493 | "execution_count": 133 3494 | } 3495 | ], 3496 | "source": [ 3497 | "np.linspace(0,10,50)" 3498 | ] 3499 | }, 3500 | { 3501 | "cell_type": "markdown", 3502 | "metadata": { 3503 | "id": "a8jb4Pl1MzCD" 3504 | }, 3505 | "source": [ 3506 | "## eye\n", 3507 | "\n", 3508 | "Creates an identity matrix" 3509 | ] 3510 | }, 3511 | { 3512 | "cell_type": "code", 3513 | "execution_count": null, 3514 | "metadata": { 3515 | "id": "ju9U1hv1MzCD", 3516 | "outputId": "c678e2e7-0314-4e5d-ba41-5d29a13f7b7e" 3517 | }, 3518 | "outputs": [ 3519 | { 3520 | "data": { 3521 | "text/plain": [ 3522 | "array([[ 1., 0., 0., 0.],\n", 3523 | " [ 0., 1., 0., 0.],\n", 3524 | " [ 0., 0., 1., 0.],\n", 3525 | " [ 0., 0., 0., 1.]])" 3526 | ] 3527 | }, 3528 | "execution_count": 37, 3529 | "metadata": {}, 3530 | "output_type": "execute_result" 3531 | } 3532 | ], 3533 | "source": [ 3534 | "np.eye(4)" 3535 | ] 3536 | }, 3537 | { 3538 | "cell_type": "markdown", 3539 | "metadata": { 3540 | "id": "wkD34NhOMzCE" 3541 | }, 3542 | "source": [ 3543 | "## Random \n", 3544 | "\n", 3545 | "Numpy also has lots of ways to create random number arrays:\n", 3546 | "\n", 3547 | "### rand\n", 3548 | "Create an array of the given shape and populate it with\n", 3549 | "random samples from a uniform distribution\n", 3550 | "over ``[0, 1)``." 3551 | ] 3552 | }, 3553 | { 3554 | "cell_type": "code", 3555 | "execution_count": null, 3556 | "metadata": { 3557 | "id": "321ygfyGMzCE", 3558 | "outputId": "11af9832-7875-4229-9dec-30bb103a5f0e", 3559 | "colab": { 3560 | "base_uri": "https://localhost:8080/" 3561 | } 3562 | }, 3563 | "outputs": [ 3564 | { 3565 | "output_type": "execute_result", 3566 | "data": { 3567 | "text/plain": [ 3568 | "array([0.45346703, 0.1497983 , 0.87266952])" 3569 | ] 3570 | }, 3571 | "metadata": {}, 3572 | "execution_count": 134 3573 | } 3574 | ], 3575 | "source": [ 3576 | "np.random.rand(3)" 3577 | ] 3578 | }, 3579 | { 3580 | "cell_type": "code", 3581 | "execution_count": null, 3582 | "metadata": { 3583 | "id": "dbFGzcrJMzCF", 3584 | "outputId": "bda97512-d689-423d-c721-841949c07a32", 3585 | "colab": { 3586 | "base_uri": "https://localhost:8080/" 3587 | } 3588 | }, 3589 | "outputs": [ 3590 | { 3591 | "output_type": "execute_result", 3592 | "data": { 3593 | "text/plain": [ 3594 | "array([[0.42442332, 0.2524502 , 0.42153375, 0.42342204, 0.99055167],\n", 3595 | " [0.908554 , 0.87508428, 0.19406493, 0.92307498, 0.06234637],\n", 3596 | " [0.35323966, 0.05491062, 0.39163486, 0.57011386, 0.36450496],\n", 3597 | " [0.34669702, 0.91678722, 0.41248057, 0.37321225, 0.48298827],\n", 3598 | " [0.21634103, 0.90925935, 0.46150927, 0.55268504, 0.13477428]])" 3599 | ] 3600 | }, 3601 | "metadata": {}, 3602 | "execution_count": 135 3603 | } 3604 | ], 3605 | "source": [ 3606 | "np.random.rand(5,5)" 3607 | ] 3608 | }, 3609 | { 3610 | "cell_type": "markdown", 3611 | "metadata": { 3612 | "id": "xyn_guW0MzCF" 3613 | }, 3614 | "source": [ 3615 | "### randn\n", 3616 | "\n", 3617 | "Return a sample (or samples) from the \"standard normal\" distribution. Unlike rand which is uniform:" 3618 | ] 3619 | }, 3620 | { 3621 | "cell_type": "code", 3622 | "execution_count": null, 3623 | "metadata": { 3624 | "id": "wLkPHEqZMzCF", 3625 | "outputId": "3455547f-d80a-406b-e94d-1c1de6eeb2ab", 3626 | "colab": { 3627 | "base_uri": "https://localhost:8080/" 3628 | } 3629 | }, 3630 | "outputs": [ 3631 | { 3632 | "output_type": "execute_result", 3633 | "data": { 3634 | "text/plain": [ 3635 | "array([-0.16605779, 0.65824292, -0.35998725, 0.07601771, -0.34089485,\n", 3636 | " 0.11430547])" 3637 | ] 3638 | }, 3639 | "metadata": {}, 3640 | "execution_count": 136 3641 | } 3642 | ], 3643 | "source": [ 3644 | "np.random.randn(6)" 3645 | ] 3646 | }, 3647 | { 3648 | "cell_type": "code", 3649 | "execution_count": null, 3650 | "metadata": { 3651 | "id": "-FooS9rKMzCG", 3652 | "outputId": "09ed4a6d-8078-4a8e-87ad-14a5d9004283" 3653 | }, 3654 | "outputs": [ 3655 | { 3656 | "data": { 3657 | "text/plain": [ 3658 | "array([[ 0.70154515, 0.22441999, 1.33563186, 0.82872577, -0.28247509],\n", 3659 | " [ 0.64489788, 0.61815094, -0.81693168, -0.30102424, -0.29030574],\n", 3660 | " [ 0.8695976 , 0.413755 , 2.20047208, 0.17955692, -0.82159344],\n", 3661 | " [ 0.59264235, 1.29869894, -1.18870241, 0.11590888, -0.09181687],\n", 3662 | " [-0.96924265, -1.62888685, -2.05787102, -0.29705576, 0.68915542]])" 3663 | ] 3664 | }, 3665 | "execution_count": 45, 3666 | "metadata": {}, 3667 | "output_type": "execute_result" 3668 | } 3669 | ], 3670 | "source": [ 3671 | "np.random.randn(5,5)" 3672 | ] 3673 | }, 3674 | { 3675 | "cell_type": "markdown", 3676 | "metadata": { 3677 | "id": "4MQTfhm2MzCG" 3678 | }, 3679 | "source": [ 3680 | "### randint\n", 3681 | "Return random integers from `low` (inclusive) to `high` (exclusive)." 3682 | ] 3683 | }, 3684 | { 3685 | "cell_type": "code", 3686 | "execution_count": null, 3687 | "metadata": { 3688 | "id": "YkuX6OC9MzCG", 3689 | "outputId": "1b9d4ec2-3c15-490f-9d0d-e806047034e4", 3690 | "colab": { 3691 | "base_uri": "https://localhost:8080/" 3692 | } 3693 | }, 3694 | "outputs": [ 3695 | { 3696 | "output_type": "execute_result", 3697 | "data": { 3698 | "text/plain": [ 3699 | "94" 3700 | ] 3701 | }, 3702 | "metadata": {}, 3703 | "execution_count": 137 3704 | } 3705 | ], 3706 | "source": [ 3707 | "np.random.randint(1,100)" 3708 | ] 3709 | }, 3710 | { 3711 | "cell_type": "code", 3712 | "execution_count": null, 3713 | "metadata": { 3714 | "id": "M7CjVnEfMzCG", 3715 | "outputId": "f93aaaa0-a872-48dc-ad18-de369e2cc076", 3716 | "colab": { 3717 | "base_uri": "https://localhost:8080/" 3718 | } 3719 | }, 3720 | "outputs": [ 3721 | { 3722 | "output_type": "execute_result", 3723 | "data": { 3724 | "text/plain": [ 3725 | "array([40, 77, 38, 92, 50, 22, 89, 12, 88, 92])" 3726 | ] 3727 | }, 3728 | "metadata": {}, 3729 | "execution_count": 2 3730 | } 3731 | ], 3732 | "source": [ 3733 | "np.random.randint(1,100,10)" 3734 | ] 3735 | }, 3736 | { 3737 | "cell_type": "code", 3738 | "source": [ 3739 | "from numpy.random import randint \n", 3740 | "randint(1,100,10)" 3741 | ], 3742 | "metadata": { 3743 | "colab": { 3744 | "base_uri": "https://localhost:8080/" 3745 | }, 3746 | "id": "3lgffWQUwpea", 3747 | "outputId": "77e31666-9b5b-4655-e153-f5b13e0f3cce" 3748 | }, 3749 | "execution_count": null, 3750 | "outputs": [ 3751 | { 3752 | "output_type": "execute_result", 3753 | "data": { 3754 | "text/plain": [ 3755 | "array([24, 70, 33, 67, 4, 90, 68, 63, 68, 96])" 3756 | ] 3757 | }, 3758 | "metadata": {}, 3759 | "execution_count": 138 3760 | } 3761 | ] 3762 | }, 3763 | { 3764 | "cell_type": "markdown", 3765 | "metadata": { 3766 | "id": "50hWEtl1MzCH" 3767 | }, 3768 | "source": [ 3769 | "## Array Attributes and Methods\n", 3770 | "\n", 3771 | "Let's discuss some useful attributes and methods or an array:" 3772 | ] 3773 | }, 3774 | { 3775 | "cell_type": "code", 3776 | "execution_count": null, 3777 | "metadata": { 3778 | "collapsed": true, 3779 | "id": "WBuK2fmgMzCH" 3780 | }, 3781 | "outputs": [], 3782 | "source": [ 3783 | "arr = np.arange(25)\n", 3784 | "ranarr = np.random.randint(0,50,10)" 3785 | ] 3786 | }, 3787 | { 3788 | "cell_type": "code", 3789 | "execution_count": null, 3790 | "metadata": { 3791 | "id": "DWCp4A_QMzCH", 3792 | "outputId": "cf1e5969-bff6-4a7d-d025-66c0f2b10442", 3793 | "colab": { 3794 | "base_uri": "https://localhost:8080/" 3795 | } 3796 | }, 3797 | "outputs": [ 3798 | { 3799 | "output_type": "execute_result", 3800 | "data": { 3801 | "text/plain": [ 3802 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", 3803 | " 17, 18, 19, 20, 21, 22, 23, 24])" 3804 | ] 3805 | }, 3806 | "metadata": {}, 3807 | "execution_count": 141 3808 | } 3809 | ], 3810 | "source": [ 3811 | "arr" 3812 | ] 3813 | }, 3814 | { 3815 | "cell_type": "code", 3816 | "execution_count": null, 3817 | "metadata": { 3818 | "id": "-y-KPzZIMzCH", 3819 | "outputId": "fa5d734a-9ecc-4a6f-be8f-5c3337fc04d3", 3820 | "colab": { 3821 | "base_uri": "https://localhost:8080/" 3822 | } 3823 | }, 3824 | "outputs": [ 3825 | { 3826 | "output_type": "execute_result", 3827 | "data": { 3828 | "text/plain": [ 3829 | "array([49, 9, 10, 26, 4, 34, 25, 13, 44, 40])" 3830 | ] 3831 | }, 3832 | "metadata": {}, 3833 | "execution_count": 142 3834 | } 3835 | ], 3836 | "source": [ 3837 | "ranarr" 3838 | ] 3839 | }, 3840 | { 3841 | "cell_type": "markdown", 3842 | "metadata": { 3843 | "id": "XnsPCaA-MzCI" 3844 | }, 3845 | "source": [ 3846 | "## Reshape\n", 3847 | "Returns an array containing the same data with a new shape." 3848 | ] 3849 | }, 3850 | { 3851 | "cell_type": "code", 3852 | "execution_count": null, 3853 | "metadata": { 3854 | "id": "kPqNug66MzCI", 3855 | "outputId": "d93a098f-949d-4521-b614-70bad751a1d9", 3856 | "colab": { 3857 | "base_uri": "https://localhost:8080/" 3858 | } 3859 | }, 3860 | "outputs": [ 3861 | { 3862 | "output_type": "execute_result", 3863 | "data": { 3864 | "text/plain": [ 3865 | "array([[ 0, 1, 2, 3, 4],\n", 3866 | " [ 5, 6, 7, 8, 9],\n", 3867 | " [10, 11, 12, 13, 14],\n", 3868 | " [15, 16, 17, 18, 19],\n", 3869 | " [20, 21, 22, 23, 24]])" 3870 | ] 3871 | }, 3872 | "metadata": {}, 3873 | "execution_count": 143 3874 | } 3875 | ], 3876 | "source": [ 3877 | "arr.reshape(5,5)" 3878 | ] 3879 | }, 3880 | { 3881 | "cell_type": "markdown", 3882 | "metadata": { 3883 | "id": "8UyCgkYzMzCI" 3884 | }, 3885 | "source": [ 3886 | "### max,min,argmax,argmin\n", 3887 | "\n", 3888 | "These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax" 3889 | ] 3890 | }, 3891 | { 3892 | "cell_type": "code", 3893 | "execution_count": null, 3894 | "metadata": { 3895 | "id": "duna40shMzCI", 3896 | "outputId": "a54bd2b9-3a1f-4b7a-e717-79da89a031eb", 3897 | "colab": { 3898 | "base_uri": "https://localhost:8080/" 3899 | } 3900 | }, 3901 | "outputs": [ 3902 | { 3903 | "output_type": "execute_result", 3904 | "data": { 3905 | "text/plain": [ 3906 | "array([49, 9, 10, 26, 4, 34, 25, 13, 44, 40])" 3907 | ] 3908 | }, 3909 | "metadata": {}, 3910 | "execution_count": 144 3911 | } 3912 | ], 3913 | "source": [ 3914 | "ranarr" 3915 | ] 3916 | }, 3917 | { 3918 | "cell_type": "code", 3919 | "execution_count": null, 3920 | "metadata": { 3921 | "id": "d7dL7E8yMzCJ", 3922 | "outputId": "8d758e0e-fce4-4cf6-efec-0c9154fbb386", 3923 | "colab": { 3924 | "base_uri": "https://localhost:8080/" 3925 | } 3926 | }, 3927 | "outputs": [ 3928 | { 3929 | "output_type": "execute_result", 3930 | "data": { 3931 | "text/plain": [ 3932 | "49" 3933 | ] 3934 | }, 3935 | "metadata": {}, 3936 | "execution_count": 145 3937 | } 3938 | ], 3939 | "source": [ 3940 | "ranarr.max()" 3941 | ] 3942 | }, 3943 | { 3944 | "cell_type": "code", 3945 | "execution_count": null, 3946 | "metadata": { 3947 | "id": "3LaQiUkcMzCJ", 3948 | "outputId": "5a2cd918-2be8-4383-f783-6c2cba9c5011", 3949 | "colab": { 3950 | "base_uri": "https://localhost:8080/" 3951 | } 3952 | }, 3953 | "outputs": [ 3954 | { 3955 | "output_type": "execute_result", 3956 | "data": { 3957 | "text/plain": [ 3958 | "0" 3959 | ] 3960 | }, 3961 | "metadata": {}, 3962 | "execution_count": 146 3963 | } 3964 | ], 3965 | "source": [ 3966 | "# Index of max element\n", 3967 | "ranarr.argmax()" 3968 | ] 3969 | }, 3970 | { 3971 | "cell_type": "code", 3972 | "execution_count": null, 3973 | "metadata": { 3974 | "id": "SFP-6W0QMzCJ", 3975 | "outputId": "8a2e8354-b018-4db3-a116-0b98e9d6b44b", 3976 | "colab": { 3977 | "base_uri": "https://localhost:8080/" 3978 | } 3979 | }, 3980 | "outputs": [ 3981 | { 3982 | "output_type": "execute_result", 3983 | "data": { 3984 | "text/plain": [ 3985 | "4" 3986 | ] 3987 | }, 3988 | "metadata": {}, 3989 | "execution_count": 147 3990 | } 3991 | ], 3992 | "source": [ 3993 | "ranarr.min()" 3994 | ] 3995 | }, 3996 | { 3997 | "cell_type": "code", 3998 | "execution_count": null, 3999 | "metadata": { 4000 | "id": "7fnQNxQ3MzCJ", 4001 | "outputId": "6eb06262-4d50-4b86-b681-feafb874de55", 4002 | "colab": { 4003 | "base_uri": "https://localhost:8080/" 4004 | } 4005 | }, 4006 | "outputs": [ 4007 | { 4008 | "output_type": "execute_result", 4009 | "data": { 4010 | "text/plain": [ 4011 | "4" 4012 | ] 4013 | }, 4014 | "metadata": {}, 4015 | "execution_count": 148 4016 | } 4017 | ], 4018 | "source": [ 4019 | "# Index of min element\n", 4020 | "ranarr.argmin()" 4021 | ] 4022 | }, 4023 | { 4024 | "cell_type": "markdown", 4025 | "metadata": { 4026 | "id": "wUgIcHB0MzCK" 4027 | }, 4028 | "source": [ 4029 | "## Shape\n", 4030 | "\n", 4031 | "Shape is an attribute that arrays have (not a method):" 4032 | ] 4033 | }, 4034 | { 4035 | "cell_type": "code", 4036 | "execution_count": null, 4037 | "metadata": { 4038 | "id": "1JRCrISxMzCK", 4039 | "outputId": "62e4c94c-e002-444f-8be8-7df91e772372", 4040 | "colab": { 4041 | "base_uri": "https://localhost:8080/" 4042 | } 4043 | }, 4044 | "outputs": [ 4045 | { 4046 | "output_type": "execute_result", 4047 | "data": { 4048 | "text/plain": [ 4049 | "(25,)" 4050 | ] 4051 | }, 4052 | "metadata": {}, 4053 | "execution_count": 150 4054 | } 4055 | ], 4056 | "source": [ 4057 | "# Vector\n", 4058 | "arr.shape" 4059 | ] 4060 | }, 4061 | { 4062 | "cell_type": "code", 4063 | "execution_count": null, 4064 | "metadata": { 4065 | "id": "zjgstdGqMzCL", 4066 | "outputId": "39cba7dc-2bb2-4fd9-b578-8aa176f7e54f", 4067 | "colab": { 4068 | "base_uri": "https://localhost:8080/" 4069 | } 4070 | }, 4071 | "outputs": [ 4072 | { 4073 | "output_type": "execute_result", 4074 | "data": { 4075 | "text/plain": [ 4076 | "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\n", 4077 | " 16, 17, 18, 19, 20, 21, 22, 23, 24]])" 4078 | ] 4079 | }, 4080 | "metadata": {}, 4081 | "execution_count": 151 4082 | } 4083 | ], 4084 | "source": [ 4085 | "# Notice the two sets of brackets\n", 4086 | "arr.reshape(1,25)" 4087 | ] 4088 | }, 4089 | { 4090 | "cell_type": "code", 4091 | "execution_count": null, 4092 | "metadata": { 4093 | "id": "y4FpI0tjMzCL", 4094 | "outputId": "d904179b-9765-4a94-f61c-62b7ac5308a4", 4095 | "colab": { 4096 | "base_uri": "https://localhost:8080/" 4097 | } 4098 | }, 4099 | "outputs": [ 4100 | { 4101 | "output_type": "execute_result", 4102 | "data": { 4103 | "text/plain": [ 4104 | "(1, 25)" 4105 | ] 4106 | }, 4107 | "metadata": {}, 4108 | "execution_count": 152 4109 | } 4110 | ], 4111 | "source": [ 4112 | "arr.reshape(1,25).shape" 4113 | ] 4114 | }, 4115 | { 4116 | "cell_type": "code", 4117 | "execution_count": null, 4118 | "metadata": { 4119 | "id": "3SDYV5nOMzCL", 4120 | "outputId": "20df8ce0-24ea-42b7-ef36-0a23d3a8a873", 4121 | "colab": { 4122 | "base_uri": "https://localhost:8080/" 4123 | } 4124 | }, 4125 | "outputs": [ 4126 | { 4127 | "output_type": "execute_result", 4128 | "data": { 4129 | "text/plain": [ 4130 | "array([[ 0],\n", 4131 | " [ 1],\n", 4132 | " [ 2],\n", 4133 | " [ 3],\n", 4134 | " [ 4],\n", 4135 | " [ 5],\n", 4136 | " [ 6],\n", 4137 | " [ 7],\n", 4138 | " [ 8],\n", 4139 | " [ 9],\n", 4140 | " [10],\n", 4141 | " [11],\n", 4142 | " [12],\n", 4143 | " [13],\n", 4144 | " [14],\n", 4145 | " [15],\n", 4146 | " [16],\n", 4147 | " [17],\n", 4148 | " [18],\n", 4149 | " [19],\n", 4150 | " [20],\n", 4151 | " [21],\n", 4152 | " [22],\n", 4153 | " [23],\n", 4154 | " [24]])" 4155 | ] 4156 | }, 4157 | "metadata": {}, 4158 | "execution_count": 153 4159 | } 4160 | ], 4161 | "source": [ 4162 | "arr.reshape(25,1)" 4163 | ] 4164 | }, 4165 | { 4166 | "cell_type": "code", 4167 | "execution_count": null, 4168 | "metadata": { 4169 | "id": "ZC82zen1MzCM", 4170 | "outputId": "c4e7761b-ad7f-4b28-8970-12e3b37486ba", 4171 | "colab": { 4172 | "base_uri": "https://localhost:8080/" 4173 | } 4174 | }, 4175 | "outputs": [ 4176 | { 4177 | "output_type": "execute_result", 4178 | "data": { 4179 | "text/plain": [ 4180 | "(25, 1)" 4181 | ] 4182 | }, 4183 | "metadata": {}, 4184 | "execution_count": 154 4185 | } 4186 | ], 4187 | "source": [ 4188 | "arr.reshape(25,1).shape" 4189 | ] 4190 | }, 4191 | { 4192 | "cell_type": "markdown", 4193 | "metadata": { 4194 | "id": "zz_0umGqMzCM" 4195 | }, 4196 | "source": [ 4197 | "### dtype\n", 4198 | "\n", 4199 | "You can also grab the data type of the object in the array:" 4200 | ] 4201 | }, 4202 | { 4203 | "cell_type": "code", 4204 | "execution_count": null, 4205 | "metadata": { 4206 | "id": "zwHB5B7EMzCM", 4207 | "outputId": "0d26fae7-65e2-41ff-dd43-0a10a070e970", 4208 | "colab": { 4209 | "base_uri": "https://localhost:8080/" 4210 | } 4211 | }, 4212 | "outputs": [ 4213 | { 4214 | "output_type": "execute_result", 4215 | "data": { 4216 | "text/plain": [ 4217 | "dtype('int64')" 4218 | ] 4219 | }, 4220 | "metadata": {}, 4221 | "execution_count": 155 4222 | } 4223 | ], 4224 | "source": [ 4225 | "arr.dtype" 4226 | ] 4227 | }, 4228 | { 4229 | "cell_type": "markdown", 4230 | "metadata": { 4231 | "id": "Sqdy4jUJMzCM" 4232 | }, 4233 | "source": [ 4234 | "# NumPy Indexing and Selection\n", 4235 | "\n", 4236 | "Now we will discuss how to select elements or groups of elements from an array." 4237 | ] 4238 | }, 4239 | { 4240 | "cell_type": "code", 4241 | "execution_count": null, 4242 | "metadata": { 4243 | "id": "htm6ErjEMzCN" 4244 | }, 4245 | "outputs": [], 4246 | "source": [ 4247 | "#Creating sample array\n", 4248 | "arr = np.arange(0,11)" 4249 | ] 4250 | }, 4251 | { 4252 | "cell_type": "code", 4253 | "execution_count": null, 4254 | "metadata": { 4255 | "id": "w0ajXEZ9MzCN", 4256 | "outputId": "0fa9a104-4111-482c-d049-ee57a2327616", 4257 | "colab": { 4258 | "base_uri": "https://localhost:8080/" 4259 | } 4260 | }, 4261 | "outputs": [ 4262 | { 4263 | "output_type": "execute_result", 4264 | "data": { 4265 | "text/plain": [ 4266 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 4267 | ] 4268 | }, 4269 | "metadata": {}, 4270 | "execution_count": 157 4271 | } 4272 | ], 4273 | "source": [ 4274 | "#Show\n", 4275 | "arr" 4276 | ] 4277 | }, 4278 | { 4279 | "cell_type": "markdown", 4280 | "metadata": { 4281 | "id": "1F8PtmEKMzCN" 4282 | }, 4283 | "source": [ 4284 | "## Bracket Indexing and Selection\n", 4285 | "The simplest way to pick one or some elements of an array looks very similar to python lists:" 4286 | ] 4287 | }, 4288 | { 4289 | "cell_type": "code", 4290 | "execution_count": null, 4291 | "metadata": { 4292 | "id": "ljXOuUdKMzCN", 4293 | "outputId": "c10c58c3-9ad1-4862-d1a1-d0274dca8c7c", 4294 | "colab": { 4295 | "base_uri": "https://localhost:8080/" 4296 | } 4297 | }, 4298 | "outputs": [ 4299 | { 4300 | "output_type": "execute_result", 4301 | "data": { 4302 | "text/plain": [ 4303 | "8" 4304 | ] 4305 | }, 4306 | "metadata": {}, 4307 | "execution_count": 158 4308 | } 4309 | ], 4310 | "source": [ 4311 | "#Get a value at an index\n", 4312 | "arr[8]" 4313 | ] 4314 | }, 4315 | { 4316 | "cell_type": "code", 4317 | "execution_count": null, 4318 | "metadata": { 4319 | "id": "QxXUs4cXMzCN", 4320 | "outputId": "9e50185c-25c0-453d-a6fc-9199b5947d93", 4321 | "colab": { 4322 | "base_uri": "https://localhost:8080/" 4323 | } 4324 | }, 4325 | "outputs": [ 4326 | { 4327 | "output_type": "execute_result", 4328 | "data": { 4329 | "text/plain": [ 4330 | "array([1, 2, 3, 4])" 4331 | ] 4332 | }, 4333 | "metadata": {}, 4334 | "execution_count": 159 4335 | } 4336 | ], 4337 | "source": [ 4338 | "#Get values in a range\n", 4339 | "arr[1:5]" 4340 | ] 4341 | }, 4342 | { 4343 | "cell_type": "code", 4344 | "execution_count": null, 4345 | "metadata": { 4346 | "id": "4_fIzlSNMzCO", 4347 | "outputId": "fbea3afa-2551-4439-d10a-20e9023a5bd1", 4348 | "colab": { 4349 | "base_uri": "https://localhost:8080/" 4350 | } 4351 | }, 4352 | "outputs": [ 4353 | { 4354 | "output_type": "execute_result", 4355 | "data": { 4356 | "text/plain": [ 4357 | "array([0, 1, 2, 3, 4])" 4358 | ] 4359 | }, 4360 | "metadata": {}, 4361 | "execution_count": 160 4362 | } 4363 | ], 4364 | "source": [ 4365 | "#Get values in a range\n", 4366 | "arr[0:5]" 4367 | ] 4368 | }, 4369 | { 4370 | "cell_type": "markdown", 4371 | "metadata": { 4372 | "id": "VCyo8fi_MzCO" 4373 | }, 4374 | "source": [ 4375 | "## Broadcasting\n", 4376 | "\n", 4377 | "Numpy arrays differ from a normal Python list because of their ability to broadcast:" 4378 | ] 4379 | }, 4380 | { 4381 | "cell_type": "code", 4382 | "execution_count": null, 4383 | "metadata": { 4384 | "id": "TV_hlwEmMzCO", 4385 | "outputId": "3db89da2-aa01-4967-cd15-38d568b7ff9c", 4386 | "colab": { 4387 | "base_uri": "https://localhost:8080/" 4388 | } 4389 | }, 4390 | "outputs": [ 4391 | { 4392 | "output_type": "execute_result", 4393 | "data": { 4394 | "text/plain": [ 4395 | "array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])" 4396 | ] 4397 | }, 4398 | "metadata": {}, 4399 | "execution_count": 161 4400 | } 4401 | ], 4402 | "source": [ 4403 | "#Setting a value with index range (Broadcasting)\n", 4404 | "arr[0:5]=100\n", 4405 | "\n", 4406 | "#Show\n", 4407 | "arr" 4408 | ] 4409 | }, 4410 | { 4411 | "cell_type": "code", 4412 | "execution_count": null, 4413 | "metadata": { 4414 | "id": "Y-BB-hvXMzCP", 4415 | "outputId": "102b0f12-4451-4581-8ec9-a3f6a4b24712", 4416 | "colab": { 4417 | "base_uri": "https://localhost:8080/" 4418 | } 4419 | }, 4420 | "outputs": [ 4421 | { 4422 | "output_type": "execute_result", 4423 | "data": { 4424 | "text/plain": [ 4425 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 4426 | ] 4427 | }, 4428 | "metadata": {}, 4429 | "execution_count": 162 4430 | } 4431 | ], 4432 | "source": [ 4433 | "# Reset array, we'll see why I had to reset in a moment\n", 4434 | "arr = np.arange(0,11)\n", 4435 | "\n", 4436 | "#Show\n", 4437 | "arr" 4438 | ] 4439 | }, 4440 | { 4441 | "cell_type": "code", 4442 | "execution_count": null, 4443 | "metadata": { 4444 | "id": "Spqd_rJ5MzCP", 4445 | "outputId": "a2882534-96a4-4fc9-c8cb-15de00c21707", 4446 | "colab": { 4447 | "base_uri": "https://localhost:8080/" 4448 | } 4449 | }, 4450 | "outputs": [ 4451 | { 4452 | "output_type": "execute_result", 4453 | "data": { 4454 | "text/plain": [ 4455 | "array([0, 1, 2, 3, 4, 5])" 4456 | ] 4457 | }, 4458 | "metadata": {}, 4459 | "execution_count": 163 4460 | } 4461 | ], 4462 | "source": [ 4463 | "#Important notes on Slices\n", 4464 | "slice_of_arr = arr[0:6]\n", 4465 | "\n", 4466 | "#Show slice\n", 4467 | "slice_of_arr" 4468 | ] 4469 | }, 4470 | { 4471 | "cell_type": "code", 4472 | "execution_count": null, 4473 | "metadata": { 4474 | "id": "3QkRohwyMzCP", 4475 | "outputId": "eb651ade-381c-4f08-b629-54750230eadf", 4476 | "colab": { 4477 | "base_uri": "https://localhost:8080/" 4478 | } 4479 | }, 4480 | "outputs": [ 4481 | { 4482 | "output_type": "execute_result", 4483 | "data": { 4484 | "text/plain": [ 4485 | "array([99, 99, 99, 99, 99, 99])" 4486 | ] 4487 | }, 4488 | "metadata": {}, 4489 | "execution_count": 164 4490 | } 4491 | ], 4492 | "source": [ 4493 | "#Change Slice\n", 4494 | "slice_of_arr[:]=99\n", 4495 | "\n", 4496 | "#Show Slice again\n", 4497 | "slice_of_arr" 4498 | ] 4499 | }, 4500 | { 4501 | "cell_type": "markdown", 4502 | "metadata": { 4503 | "id": "4G-JTUCQMzCP" 4504 | }, 4505 | "source": [ 4506 | "Now note the changes also occur in our original array!" 4507 | ] 4508 | }, 4509 | { 4510 | "cell_type": "code", 4511 | "execution_count": null, 4512 | "metadata": { 4513 | "id": "lrkd00JWMzCQ", 4514 | "outputId": "ae51471c-d976-414b-c182-712631834548", 4515 | "colab": { 4516 | "base_uri": "https://localhost:8080/" 4517 | } 4518 | }, 4519 | "outputs": [ 4520 | { 4521 | "output_type": "execute_result", 4522 | "data": { 4523 | "text/plain": [ 4524 | "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" 4525 | ] 4526 | }, 4527 | "metadata": {}, 4528 | "execution_count": 165 4529 | } 4530 | ], 4531 | "source": [ 4532 | "arr" 4533 | ] 4534 | }, 4535 | { 4536 | "cell_type": "markdown", 4537 | "metadata": { 4538 | "id": "c8USAxIuMzCQ" 4539 | }, 4540 | "source": [ 4541 | "Data is not copied, it's a view of the original array! This avoids memory problems!" 4542 | ] 4543 | }, 4544 | { 4545 | "cell_type": "code", 4546 | "execution_count": null, 4547 | "metadata": { 4548 | "id": "SODBmpwxMzCQ", 4549 | "outputId": "4a878f67-352e-4040-c0ce-b67ef92a3782", 4550 | "colab": { 4551 | "base_uri": "https://localhost:8080/" 4552 | } 4553 | }, 4554 | "outputs": [ 4555 | { 4556 | "output_type": "execute_result", 4557 | "data": { 4558 | "text/plain": [ 4559 | "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" 4560 | ] 4561 | }, 4562 | "metadata": {}, 4563 | "execution_count": 166 4564 | } 4565 | ], 4566 | "source": [ 4567 | "#To get a copy, need to be explicit\n", 4568 | "arr_copy = arr.copy()\n", 4569 | "\n", 4570 | "arr_copy" 4571 | ] 4572 | }, 4573 | { 4574 | "cell_type": "markdown", 4575 | "metadata": { 4576 | "id": "-YllIwk8MzCR" 4577 | }, 4578 | "source": [ 4579 | "## Indexing a 2D array (matrices)\n", 4580 | "\n", 4581 | "The general format is **arr_2d[row][col]** or **arr_2d[row,col]**. I recommend usually using the comma notation for clarity." 4582 | ] 4583 | }, 4584 | { 4585 | "cell_type": "code", 4586 | "execution_count": null, 4587 | "metadata": { 4588 | "id": "7x0MKnxwMzCS", 4589 | "outputId": "3eadecb6-240e-4c6d-a79c-ad0513eb73cb", 4590 | "colab": { 4591 | "base_uri": "https://localhost:8080/" 4592 | } 4593 | }, 4594 | "outputs": [ 4595 | { 4596 | "output_type": "execute_result", 4597 | "data": { 4598 | "text/plain": [ 4599 | "array([[ 5, 10, 15],\n", 4600 | " [20, 25, 30],\n", 4601 | " [35, 40, 45]])" 4602 | ] 4603 | }, 4604 | "metadata": {}, 4605 | "execution_count": 167 4606 | } 4607 | ], 4608 | "source": [ 4609 | "arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))\n", 4610 | "\n", 4611 | "#Show\n", 4612 | "arr_2d" 4613 | ] 4614 | }, 4615 | { 4616 | "cell_type": "code", 4617 | "execution_count": null, 4618 | "metadata": { 4619 | "id": "-s40Kjg0MzCS", 4620 | "outputId": "f4ec897e-e35c-46c1-b696-484b366ec0fe", 4621 | "colab": { 4622 | "base_uri": "https://localhost:8080/" 4623 | } 4624 | }, 4625 | "outputs": [ 4626 | { 4627 | "output_type": "execute_result", 4628 | "data": { 4629 | "text/plain": [ 4630 | "array([20, 25, 30])" 4631 | ] 4632 | }, 4633 | "metadata": {}, 4634 | "execution_count": 168 4635 | } 4636 | ], 4637 | "source": [ 4638 | "#Indexing row\n", 4639 | "arr_2d[1]\n" 4640 | ] 4641 | }, 4642 | { 4643 | "cell_type": "code", 4644 | "execution_count": null, 4645 | "metadata": { 4646 | "id": "B0xgeIP6MzCS", 4647 | "outputId": "614b2e05-2fce-480e-cc5b-330a823cce2a", 4648 | "colab": { 4649 | "base_uri": "https://localhost:8080/" 4650 | } 4651 | }, 4652 | "outputs": [ 4653 | { 4654 | "output_type": "execute_result", 4655 | "data": { 4656 | "text/plain": [ 4657 | "20" 4658 | ] 4659 | }, 4660 | "metadata": {}, 4661 | "execution_count": 169 4662 | } 4663 | ], 4664 | "source": [ 4665 | "# Format is arr_2d[row][col] or arr_2d[row,col]\n", 4666 | "\n", 4667 | "# Getting individual element value\n", 4668 | "arr_2d[1][0]" 4669 | ] 4670 | }, 4671 | { 4672 | "cell_type": "code", 4673 | "execution_count": null, 4674 | "metadata": { 4675 | "id": "aQKiy2hFMzCT", 4676 | "outputId": "5d078540-5a07-4c7f-e71e-dfbac4cd8d7a", 4677 | "colab": { 4678 | "base_uri": "https://localhost:8080/" 4679 | } 4680 | }, 4681 | "outputs": [ 4682 | { 4683 | "output_type": "execute_result", 4684 | "data": { 4685 | "text/plain": [ 4686 | "20" 4687 | ] 4688 | }, 4689 | "metadata": {}, 4690 | "execution_count": 170 4691 | } 4692 | ], 4693 | "source": [ 4694 | "# Getting individual element value\n", 4695 | "arr_2d[1,0]" 4696 | ] 4697 | }, 4698 | { 4699 | "cell_type": "code", 4700 | "execution_count": null, 4701 | "metadata": { 4702 | "id": "ZwnhtH3MMzCT", 4703 | "outputId": "ffcabd97-93e3-4da1-ea21-0c10d9ed5ef8", 4704 | "colab": { 4705 | "base_uri": "https://localhost:8080/" 4706 | } 4707 | }, 4708 | "outputs": [ 4709 | { 4710 | "output_type": "execute_result", 4711 | "data": { 4712 | "text/plain": [ 4713 | "array([[10, 15],\n", 4714 | " [25, 30]])" 4715 | ] 4716 | }, 4717 | "metadata": {}, 4718 | "execution_count": 171 4719 | } 4720 | ], 4721 | "source": [ 4722 | "# 2D array slicing\n", 4723 | "\n", 4724 | "#Shape (2,2) from top right corner\n", 4725 | "arr_2d[ :2 , 1: ]" 4726 | ] 4727 | }, 4728 | { 4729 | "cell_type": "code", 4730 | "execution_count": null, 4731 | "metadata": { 4732 | "id": "fx9nMcjFMzCT", 4733 | "outputId": "8b32906a-4ab3-4ffe-bcc1-10b1cd6d8677" 4734 | }, 4735 | "outputs": [ 4736 | { 4737 | "data": { 4738 | "text/plain": [ 4739 | "array([35, 40, 45])" 4740 | ] 4741 | }, 4742 | "execution_count": 19, 4743 | "metadata": {}, 4744 | "output_type": "execute_result" 4745 | } 4746 | ], 4747 | "source": [ 4748 | "#Shape bottom row\n", 4749 | "arr_2d[2]" 4750 | ] 4751 | }, 4752 | { 4753 | "cell_type": "code", 4754 | "execution_count": null, 4755 | "metadata": { 4756 | "id": "chalrDy-MzCU", 4757 | "outputId": "744aa103-c546-495e-acb5-77f7e5bba58e" 4758 | }, 4759 | "outputs": [ 4760 | { 4761 | "data": { 4762 | "text/plain": [ 4763 | "array([35, 40, 45])" 4764 | ] 4765 | }, 4766 | "execution_count": 20, 4767 | "metadata": {}, 4768 | "output_type": "execute_result" 4769 | } 4770 | ], 4771 | "source": [ 4772 | "#Shape bottom row\n", 4773 | "arr_2d[ 2 , : ]" 4774 | ] 4775 | }, 4776 | { 4777 | "cell_type": "markdown", 4778 | "metadata": { 4779 | "id": "8L1Ms6D3MzCU" 4780 | }, 4781 | "source": [ 4782 | "### Fancy Indexing\n", 4783 | "\n", 4784 | "Fancy indexing allows you to select entire rows or columns out of order,to show this, let's quickly build out a numpy array:" 4785 | ] 4786 | }, 4787 | { 4788 | "cell_type": "code", 4789 | "execution_count": null, 4790 | "metadata": { 4791 | "id": "wY80JEwxMzCU" 4792 | }, 4793 | "outputs": [], 4794 | "source": [ 4795 | "#Set up matrix\n", 4796 | "arr2d = np.zeros((10,10))" 4797 | ] 4798 | }, 4799 | { 4800 | "cell_type": "code", 4801 | "execution_count": null, 4802 | "metadata": { 4803 | "id": "qFwfYvA4MzCU" 4804 | }, 4805 | "outputs": [], 4806 | "source": [ 4807 | "#Length of array\n", 4808 | "arr_length = arr2d.shape[1]" 4809 | ] 4810 | }, 4811 | { 4812 | "cell_type": "code", 4813 | "execution_count": null, 4814 | "metadata": { 4815 | "id": "qGy-7PQDMzCV", 4816 | "outputId": "29998b1a-d2c6-4db1-e29b-8c9922f62561", 4817 | "colab": { 4818 | "base_uri": "https://localhost:8080/" 4819 | } 4820 | }, 4821 | "outputs": [ 4822 | { 4823 | "output_type": "execute_result", 4824 | "data": { 4825 | "text/plain": [ 4826 | "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 4827 | " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", 4828 | " [2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", 4829 | " [3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],\n", 4830 | " [4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", 4831 | " [5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],\n", 4832 | " [6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", 4833 | " [7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],\n", 4834 | " [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n", 4835 | " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])" 4836 | ] 4837 | }, 4838 | "metadata": {}, 4839 | "execution_count": 174 4840 | } 4841 | ], 4842 | "source": [ 4843 | "#Set up array\n", 4844 | "\n", 4845 | "for i in range(arr_length):\n", 4846 | " arr2d[i] = i\n", 4847 | " \n", 4848 | "arr2d" 4849 | ] 4850 | }, 4851 | { 4852 | "cell_type": "markdown", 4853 | "metadata": { 4854 | "id": "dNG73neKMzCV" 4855 | }, 4856 | "source": [ 4857 | "Fancy indexing allows the following" 4858 | ] 4859 | }, 4860 | { 4861 | "cell_type": "code", 4862 | "execution_count": null, 4863 | "metadata": { 4864 | "id": "e9tjerW5MzCV", 4865 | "outputId": "5b43dcc7-e7e9-43fd-888c-49e03844183d" 4866 | }, 4867 | "outputs": [ 4868 | { 4869 | "data": { 4870 | "text/plain": [ 4871 | "array([[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", 4872 | " [ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", 4873 | " [ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", 4874 | " [ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]])" 4875 | ] 4876 | }, 4877 | "execution_count": 24, 4878 | "metadata": {}, 4879 | "output_type": "execute_result" 4880 | } 4881 | ], 4882 | "source": [ 4883 | "arr2d[[2,4,6,8]]" 4884 | ] 4885 | }, 4886 | { 4887 | "cell_type": "code", 4888 | "execution_count": null, 4889 | "metadata": { 4890 | "id": "r0F-OOK6MzCV", 4891 | "outputId": "f38bfb6e-2618-4b7c-a331-7ae22a02c0c9" 4892 | }, 4893 | "outputs": [ 4894 | { 4895 | "data": { 4896 | "text/plain": [ 4897 | "array([[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", 4898 | " [ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", 4899 | " [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", 4900 | " [ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.]])" 4901 | ] 4902 | }, 4903 | "execution_count": 25, 4904 | "metadata": {}, 4905 | "output_type": "execute_result" 4906 | } 4907 | ], 4908 | "source": [ 4909 | "#Allows in any order\n", 4910 | "arr2d[[6,4,2,7]]" 4911 | ] 4912 | }, 4913 | { 4914 | "cell_type": "markdown", 4915 | "metadata": { 4916 | "id": "cpWYgJYsMzCV" 4917 | }, 4918 | "source": [ 4919 | "## More Indexing Help\n", 4920 | "Indexing a 2d matrix can be a bit confusing at first, especially when you start to add in step size. \n" 4921 | ] 4922 | }, 4923 | { 4924 | "cell_type": "markdown", 4925 | "metadata": { 4926 | "id": "f3xcyh6kMzCW" 4927 | }, 4928 | "source": [ 4929 | "## Selection\n", 4930 | "\n", 4931 | "Let's briefly go over how to use brackets for selection based off of comparison operators." 4932 | ] 4933 | }, 4934 | { 4935 | "cell_type": "code", 4936 | "execution_count": null, 4937 | "metadata": { 4938 | "id": "WoBh6sbDMzCW", 4939 | "outputId": "ae68d73a-f980-4552-9b58-c7a0df6fd0aa", 4940 | "colab": { 4941 | "base_uri": "https://localhost:8080/" 4942 | } 4943 | }, 4944 | "outputs": [ 4945 | { 4946 | "output_type": "execute_result", 4947 | "data": { 4948 | "text/plain": [ 4949 | "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" 4950 | ] 4951 | }, 4952 | "metadata": {}, 4953 | "execution_count": 180 4954 | } 4955 | ], 4956 | "source": [ 4957 | "arr = np.arange(1,11)\n", 4958 | "arr" 4959 | ] 4960 | }, 4961 | { 4962 | "cell_type": "code", 4963 | "execution_count": null, 4964 | "metadata": { 4965 | "id": "yALJNuiCMzCW", 4966 | "outputId": "09a13497-d4d3-4990-e6e9-7ae8eae865b1", 4967 | "colab": { 4968 | "base_uri": "https://localhost:8080/" 4969 | } 4970 | }, 4971 | "outputs": [ 4972 | { 4973 | "output_type": "execute_result", 4974 | "data": { 4975 | "text/plain": [ 4976 | "array([False, False, False, False, True, True, True, True, True,\n", 4977 | " True])" 4978 | ] 4979 | }, 4980 | "metadata": {}, 4981 | "execution_count": 181 4982 | } 4983 | ], 4984 | "source": [ 4985 | "arr > 4" 4986 | ] 4987 | }, 4988 | { 4989 | "cell_type": "code", 4990 | "execution_count": null, 4991 | "metadata": { 4992 | "collapsed": true, 4993 | "id": "ATDwQofnMzCW" 4994 | }, 4995 | "outputs": [], 4996 | "source": [ 4997 | "bool_arr = arr>4" 4998 | ] 4999 | }, 5000 | { 5001 | "cell_type": "code", 5002 | "execution_count": null, 5003 | "metadata": { 5004 | "id": "HdvgTeJ0MzCW", 5005 | "outputId": "9ba15b4d-897e-446f-8bf6-e917e8a1976f", 5006 | "colab": { 5007 | "base_uri": "https://localhost:8080/" 5008 | } 5009 | }, 5010 | "outputs": [ 5011 | { 5012 | "output_type": "execute_result", 5013 | "data": { 5014 | "text/plain": [ 5015 | "array([False, False, False, False, True, True, True, True, True,\n", 5016 | " True])" 5017 | ] 5018 | }, 5019 | "metadata": {}, 5020 | "execution_count": 183 5021 | } 5022 | ], 5023 | "source": [ 5024 | "bool_arr" 5025 | ] 5026 | }, 5027 | { 5028 | "cell_type": "code", 5029 | "execution_count": null, 5030 | "metadata": { 5031 | "id": "1pNuuLBoMzCX", 5032 | "outputId": "207e50d6-add4-4a34-e6cc-32087f083f97", 5033 | "colab": { 5034 | "base_uri": "https://localhost:8080/" 5035 | } 5036 | }, 5037 | "outputs": [ 5038 | { 5039 | "output_type": "execute_result", 5040 | "data": { 5041 | "text/plain": [ 5042 | "array([ 5, 6, 7, 8, 9, 10])" 5043 | ] 5044 | }, 5045 | "metadata": {}, 5046 | "execution_count": 184 5047 | } 5048 | ], 5049 | "source": [ 5050 | "arr[bool_arr]" 5051 | ] 5052 | }, 5053 | { 5054 | "cell_type": "code", 5055 | "execution_count": null, 5056 | "metadata": { 5057 | "id": "R1fNpyucMzCX", 5058 | "outputId": "6b6027a7-f000-4614-e143-4c510877a57e", 5059 | "colab": { 5060 | "base_uri": "https://localhost:8080/" 5061 | } 5062 | }, 5063 | "outputs": [ 5064 | { 5065 | "output_type": "execute_result", 5066 | "data": { 5067 | "text/plain": [ 5068 | "array([ 3, 4, 5, 6, 7, 8, 9, 10])" 5069 | ] 5070 | }, 5071 | "metadata": {}, 5072 | "execution_count": 185 5073 | } 5074 | ], 5075 | "source": [ 5076 | "arr[arr>2]" 5077 | ] 5078 | }, 5079 | { 5080 | "cell_type": "code", 5081 | "execution_count": null, 5082 | "metadata": { 5083 | "id": "4fdl6u3wMzCX", 5084 | "outputId": "c2472804-1701-4c16-aec6-51f52945597e", 5085 | "colab": { 5086 | "base_uri": "https://localhost:8080/" 5087 | } 5088 | }, 5089 | "outputs": [ 5090 | { 5091 | "output_type": "execute_result", 5092 | "data": { 5093 | "text/plain": [ 5094 | "array([ 3, 4, 5, 6, 7, 8, 9, 10])" 5095 | ] 5096 | }, 5097 | "metadata": {}, 5098 | "execution_count": 186 5099 | } 5100 | ], 5101 | "source": [ 5102 | "x = 2\n", 5103 | "arr[arr>x]" 5104 | ] 5105 | }, 5106 | { 5107 | "cell_type": "markdown", 5108 | "metadata": { 5109 | "collapsed": true, 5110 | "id": "3m5wmx6QMzCY" 5111 | }, 5112 | "source": [ 5113 | "# NumPy Operations" 5114 | ] 5115 | }, 5116 | { 5117 | "cell_type": "markdown", 5118 | "metadata": { 5119 | "id": "z28LV8v3MzCY" 5120 | }, 5121 | "source": [ 5122 | "## Arithmetic\n", 5123 | "\n", 5124 | "You can easily perform array with array arithmetic, or scalar with array arithmetic. Let's see some examples:" 5125 | ] 5126 | }, 5127 | { 5128 | "cell_type": "code", 5129 | "execution_count": null, 5130 | "metadata": { 5131 | "collapsed": true, 5132 | "id": "U2AVxC9bMzCY" 5133 | }, 5134 | "outputs": [], 5135 | "source": [ 5136 | "import numpy as np\n", 5137 | "arr = np.arange(0,10)" 5138 | ] 5139 | }, 5140 | { 5141 | "cell_type": "code", 5142 | "execution_count": null, 5143 | "metadata": { 5144 | "id": "7nNsfNNbMzCY", 5145 | "outputId": "c4540a2f-b5d2-4cc8-f0b0-8b6c8f7f5bb0", 5146 | "colab": { 5147 | "base_uri": "https://localhost:8080/" 5148 | } 5149 | }, 5150 | "outputs": [ 5151 | { 5152 | "output_type": "execute_result", 5153 | "data": { 5154 | "text/plain": [ 5155 | "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" 5156 | ] 5157 | }, 5158 | "metadata": {}, 5159 | "execution_count": 188 5160 | } 5161 | ], 5162 | "source": [ 5163 | "arr + arr" 5164 | ] 5165 | }, 5166 | { 5167 | "cell_type": "code", 5168 | "execution_count": null, 5169 | "metadata": { 5170 | "id": "0Li3bHCSMzCY", 5171 | "outputId": "5ae16fd3-bd17-4ca9-e33b-e81981370466", 5172 | "colab": { 5173 | "base_uri": "https://localhost:8080/" 5174 | } 5175 | }, 5176 | "outputs": [ 5177 | { 5178 | "output_type": "execute_result", 5179 | "data": { 5180 | "text/plain": [ 5181 | "array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])" 5182 | ] 5183 | }, 5184 | "metadata": {}, 5185 | "execution_count": 189 5186 | } 5187 | ], 5188 | "source": [ 5189 | "arr * arr" 5190 | ] 5191 | }, 5192 | { 5193 | "cell_type": "code", 5194 | "execution_count": null, 5195 | "metadata": { 5196 | "id": "0SS5aG7hMzCZ", 5197 | "outputId": "29a77079-1651-479c-f63f-173c232f08da", 5198 | "colab": { 5199 | "base_uri": "https://localhost:8080/" 5200 | } 5201 | }, 5202 | "outputs": [ 5203 | { 5204 | "output_type": "execute_result", 5205 | "data": { 5206 | "text/plain": [ 5207 | "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" 5208 | ] 5209 | }, 5210 | "metadata": {}, 5211 | "execution_count": 190 5212 | } 5213 | ], 5214 | "source": [ 5215 | "arr - arr" 5216 | ] 5217 | }, 5218 | { 5219 | "cell_type": "code", 5220 | "execution_count": null, 5221 | "metadata": { 5222 | "id": "eAd83h0vMzCZ", 5223 | "outputId": "bbf3502e-76bc-44d2-a38c-33323c1ebf6e" 5224 | }, 5225 | "outputs": [ 5226 | { 5227 | "name": "stderr", 5228 | "output_type": "stream", 5229 | "text": [ 5230 | "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide\n", 5231 | " if __name__ == '__main__':\n" 5232 | ] 5233 | }, 5234 | { 5235 | "data": { 5236 | "text/plain": [ 5237 | "array([ nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])" 5238 | ] 5239 | }, 5240 | "execution_count": 5, 5241 | "metadata": {}, 5242 | "output_type": "execute_result" 5243 | } 5244 | ], 5245 | "source": [ 5246 | "# Warning on division by zero, but not an error!\n", 5247 | "# Just replaced with nan\n", 5248 | "arr/arr" 5249 | ] 5250 | }, 5251 | { 5252 | "cell_type": "code", 5253 | "execution_count": null, 5254 | "metadata": { 5255 | "id": "u3WINanJMzCZ", 5256 | "outputId": "a6c24f44-f80c-4164-fb5a-757dd90197fa", 5257 | "colab": { 5258 | "base_uri": "https://localhost:8080/" 5259 | } 5260 | }, 5261 | "outputs": [ 5262 | { 5263 | "output_type": "stream", 5264 | "name": "stderr", 5265 | "text": [ 5266 | "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide\n", 5267 | " \n" 5268 | ] 5269 | }, 5270 | { 5271 | "output_type": "execute_result", 5272 | "data": { 5273 | "text/plain": [ 5274 | "array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,\n", 5275 | " 0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])" 5276 | ] 5277 | }, 5278 | "metadata": {}, 5279 | "execution_count": 191 5280 | } 5281 | ], 5282 | "source": [ 5283 | "# Also warning, but not an error instead infinity\n", 5284 | "1/arr" 5285 | ] 5286 | }, 5287 | { 5288 | "cell_type": "code", 5289 | "execution_count": null, 5290 | "metadata": { 5291 | "id": "xcCdtzcoMzCZ", 5292 | "outputId": "6b8afba6-544c-4f44-d06b-5edf00ceaae7" 5293 | }, 5294 | "outputs": [ 5295 | { 5296 | "data": { 5297 | "text/plain": [ 5298 | "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])" 5299 | ] 5300 | }, 5301 | "execution_count": 10, 5302 | "metadata": {}, 5303 | "output_type": "execute_result" 5304 | } 5305 | ], 5306 | "source": [ 5307 | "arr**3" 5308 | ] 5309 | }, 5310 | { 5311 | "cell_type": "markdown", 5312 | "metadata": { 5313 | "id": "2jFoIf2LMzCa" 5314 | }, 5315 | "source": [ 5316 | "## Universal Array Functions\n", 5317 | "\n", 5318 | "Numpy comes with many [universal array functions](http://docs.scipy.org/doc/numpy/reference/ufuncs.html), which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:" 5319 | ] 5320 | }, 5321 | { 5322 | "cell_type": "code", 5323 | "execution_count": null, 5324 | "metadata": { 5325 | "id": "o0My8zCwMzCa", 5326 | "outputId": "05989f49-566e-41a7-b675-1f55545a03af" 5327 | }, 5328 | "outputs": [ 5329 | { 5330 | "data": { 5331 | "text/plain": [ 5332 | "array([ 0. , 1. , 1.41421356, 1.73205081, 2. ,\n", 5333 | " 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])" 5334 | ] 5335 | }, 5336 | "execution_count": 12, 5337 | "metadata": {}, 5338 | "output_type": "execute_result" 5339 | } 5340 | ], 5341 | "source": [ 5342 | "#Taking Square Roots\n", 5343 | "np.sqrt(arr)" 5344 | ] 5345 | }, 5346 | { 5347 | "cell_type": "code", 5348 | "execution_count": null, 5349 | "metadata": { 5350 | "id": "I83ifGEOMzCa", 5351 | "outputId": "da7d194f-47c9-45a0-c9e3-46564bfaf4cf" 5352 | }, 5353 | "outputs": [ 5354 | { 5355 | "data": { 5356 | "text/plain": [ 5357 | "array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00,\n", 5358 | " 2.00855369e+01, 5.45981500e+01, 1.48413159e+02,\n", 5359 | " 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,\n", 5360 | " 8.10308393e+03])" 5361 | ] 5362 | }, 5363 | "execution_count": 13, 5364 | "metadata": {}, 5365 | "output_type": "execute_result" 5366 | } 5367 | ], 5368 | "source": [ 5369 | "#Calcualting exponential (e^)\n", 5370 | "np.exp(arr)" 5371 | ] 5372 | }, 5373 | { 5374 | "cell_type": "code", 5375 | "execution_count": null, 5376 | "metadata": { 5377 | "id": "KoPb1k3oMzCa", 5378 | "outputId": "fb208d0a-9dae-424a-b390-a93efd3e29c6" 5379 | }, 5380 | "outputs": [ 5381 | { 5382 | "data": { 5383 | "text/plain": [ 5384 | "9" 5385 | ] 5386 | }, 5387 | "execution_count": 14, 5388 | "metadata": {}, 5389 | "output_type": "execute_result" 5390 | } 5391 | ], 5392 | "source": [ 5393 | "np.max(arr) #same as arr.max()" 5394 | ] 5395 | }, 5396 | { 5397 | "cell_type": "code", 5398 | "execution_count": null, 5399 | "metadata": { 5400 | "id": "R7adquk_MzCb", 5401 | "outputId": "913ce00d-35eb-4c7c-fdc3-999b622f97c5" 5402 | }, 5403 | "outputs": [ 5404 | { 5405 | "data": { 5406 | "text/plain": [ 5407 | "array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,\n", 5408 | " -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])" 5409 | ] 5410 | }, 5411 | "execution_count": 15, 5412 | "metadata": {}, 5413 | "output_type": "execute_result" 5414 | } 5415 | ], 5416 | "source": [ 5417 | "np.sin(arr)" 5418 | ] 5419 | }, 5420 | { 5421 | "cell_type": "code", 5422 | "execution_count": null, 5423 | "metadata": { 5424 | "id": "EdfBcrGqMzCb", 5425 | "outputId": "42da2fff-5fa2-47b3-fb4d-fd4d4df006f0" 5426 | }, 5427 | "outputs": [ 5428 | { 5429 | "name": "stderr", 5430 | "output_type": "stream", 5431 | "text": [ 5432 | "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in log\n", 5433 | " if __name__ == '__main__':\n" 5434 | ] 5435 | }, 5436 | { 5437 | "data": { 5438 | "text/plain": [ 5439 | "array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,\n", 5440 | " 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])" 5441 | ] 5442 | }, 5443 | "execution_count": 16, 5444 | "metadata": {}, 5445 | "output_type": "execute_result" 5446 | } 5447 | ], 5448 | "source": [ 5449 | "np.log(arr)" 5450 | ] 5451 | }, 5452 | { 5453 | "cell_type": "markdown", 5454 | "metadata": { 5455 | "id": "zXk_4JmaMzCc" 5456 | }, 5457 | "source": [ 5458 | "# Object Oriented Programming" 5459 | ] 5460 | }, 5461 | { 5462 | "cell_type": "markdown", 5463 | "metadata": { 5464 | "id": "OcGenCbrMzCc" 5465 | }, 5466 | "source": [ 5467 | "#### Python is a multi-paradigm programming language. It supports different programming approaches." 5468 | ] 5469 | }, 5470 | { 5471 | "cell_type": "markdown", 5472 | "metadata": { 5473 | "id": "AOzj8tr2MzCc" 5474 | }, 5475 | "source": [ 5476 | "One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).\n", 5477 | "\n", 5478 | "An object has two characteristics:\n", 5479 | "* attributes\n", 5480 | "* behavior\n" 5481 | ] 5482 | }, 5483 | { 5484 | "cell_type": "markdown", 5485 | "metadata": { 5486 | "id": "3qYm5lttMzCc" 5487 | }, 5488 | "source": [ 5489 | "#### Let's take an example:\n", 5490 | "A parrot is an object, as it has the following properties:\n", 5491 | "\n", 5492 | "name, age, color as attributes singing, dancing as behavior\n", 5493 | "\n", 5494 | "The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don't Repeat Yourself)." 5495 | ] 5496 | }, 5497 | { 5498 | "cell_type": "markdown", 5499 | "metadata": { 5500 | "id": "Y3SzsI8DMzCc" 5501 | }, 5502 | "source": [ 5503 | "# Class\n", 5504 | "A class is a blueprint for the object.\n", 5505 | "\n", 5506 | "We can think of class as a sketch of a parrot with labels. It contains all the details about the name, colors, size etc. Based on these descriptions, we can study about the parrot. Here, a parrot is an object.\n", 5507 | "\n", 5508 | "The example for class of parrot can be :" 5509 | ] 5510 | }, 5511 | { 5512 | "cell_type": "code", 5513 | "execution_count": null, 5514 | "metadata": { 5515 | "id": "EQ02ranYMzCd" 5516 | }, 5517 | "outputs": [], 5518 | "source": [ 5519 | "class Parrot:\n", 5520 | " pass\n" 5521 | ] 5522 | }, 5523 | { 5524 | "cell_type": "markdown", 5525 | "metadata": { 5526 | "id": "6HK0uXsoMzCd" 5527 | }, 5528 | "source": [ 5529 | "Here, we use the class keyword to define an empty class Parrot. From class, we construct instances. An instance is a specific object created from a particular class." 5530 | ] 5531 | }, 5532 | { 5533 | "cell_type": "markdown", 5534 | "metadata": { 5535 | "id": "gxZWDXLbMzCd" 5536 | }, 5537 | "source": [ 5538 | "# Object\n", 5539 | "An object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.\n", 5540 | "\n", 5541 | "The example for object of parrot class can be:" 5542 | ] 5543 | }, 5544 | { 5545 | "cell_type": "code", 5546 | "execution_count": null, 5547 | "metadata": { 5548 | "id": "VZiMxT54MzCd" 5549 | }, 5550 | "outputs": [], 5551 | "source": [ 5552 | "obj = Parrot()" 5553 | ] 5554 | }, 5555 | { 5556 | "cell_type": "markdown", 5557 | "metadata": { 5558 | "id": "dtbcp3hQMzCd" 5559 | }, 5560 | "source": [ 5561 | "Here, obj is an object of class Parrot.\n", 5562 | "\n", 5563 | "Suppose we have details of parrots. Now, we are going to show how to build the class and objects of parrots." 5564 | ] 5565 | }, 5566 | { 5567 | "cell_type": "code", 5568 | "execution_count": null, 5569 | "metadata": { 5570 | "id": "-bYnG3vCMzCe", 5571 | "colab": { 5572 | "base_uri": "https://localhost:8080/" 5573 | }, 5574 | "outputId": "f6b56ab1-45ec-42d2-b823-0eaf25837511" 5575 | }, 5576 | "outputs": [ 5577 | { 5578 | "output_type": "stream", 5579 | "name": "stdout", 5580 | "text": [ 5581 | "Check\n", 5582 | "Birdy\n", 5583 | "5\n" 5584 | ] 5585 | } 5586 | ], 5587 | "source": [ 5588 | "# We created a class with the name Parrot. \n", 5589 | "class Parrot:\n", 5590 | "\n", 5591 | " def check(self):\n", 5592 | " print(\"Check\")\n", 5593 | "\n", 5594 | "\n", 5595 | " # instance attribute\n", 5596 | " def config(self, name, age):\n", 5597 | " self.name = name\n", 5598 | " self.age = age\n", 5599 | " print(name)\n", 5600 | " print(age)\n", 5601 | "\n", 5602 | "\n", 5603 | "#Created a object\n", 5604 | "obj = Parrot()\n", 5605 | "\n", 5606 | "\n", 5607 | "# Parrot.check(obj)\n", 5608 | "# Parrot.config(obj,'Birdy',5)\n", 5609 | "\n", 5610 | "# We can directly use object\n", 5611 | "obj.check()\n", 5612 | "obj.config('Birdy',5)" 5613 | ] 5614 | }, 5615 | { 5616 | "cell_type": "markdown", 5617 | "source": [ 5618 | "##### Instance\n", 5619 | "We create instances of the Parrot class. Here, blu and woo are references (value) to our new objects" 5620 | ], 5621 | "metadata": { 5622 | "id": "YWyM6mcx5jNc" 5623 | } 5624 | }, 5625 | { 5626 | "cell_type": "code", 5627 | "source": [ 5628 | "# We created a class with the name Parrot. \n", 5629 | "# Then, we define attributes. The attributes are a characteristic of an object.\n", 5630 | "\n", 5631 | "class Parrot:\n", 5632 | "\n", 5633 | " # instance attribute\n", 5634 | " def __init__(self):\n", 5635 | " print(\"Called automatically\")\n", 5636 | "\n", 5637 | " def check(self):\n", 5638 | " print(\"Check\")\n", 5639 | "\n", 5640 | "\n", 5641 | " # instance attribute\n", 5642 | " def config(self, name, age):\n", 5643 | " self.name = name\n", 5644 | " self.age = age\n", 5645 | " print(name)\n", 5646 | " print(age)\n", 5647 | " \n", 5648 | "\n", 5649 | "# we create instances of the Parrot class. Here, blu and woo are references (value) to our new objects\n", 5650 | "# instantiate the Parrot class\n", 5651 | "blu = Parrot()\n", 5652 | "woo = Parrot()\n", 5653 | "\n" 5654 | ], 5655 | "metadata": { 5656 | "id": "cmcgl3h_4bM5", 5657 | "colab": { 5658 | "base_uri": "https://localhost:8080/" 5659 | }, 5660 | "outputId": "30778328-e999-4cbc-a1df-645a2b8ac958" 5661 | }, 5662 | "execution_count": null, 5663 | "outputs": [ 5664 | { 5665 | "output_type": "stream", 5666 | "name": "stdout", 5667 | "text": [ 5668 | "Called automatically\n", 5669 | "Called automatically\n" 5670 | ] 5671 | } 5672 | ] 5673 | }, 5674 | { 5675 | "cell_type": "code", 5676 | "source": [ 5677 | "class Parrot:\n", 5678 | "\n", 5679 | " # Now we are passing variables in our init function\n", 5680 | " def __init__(self,color,size):\n", 5681 | " self.color = color\n", 5682 | " self.size = size\n", 5683 | " print(\"show\")\n", 5684 | "\n", 5685 | " # instance attribute\n", 5686 | " def config(self):\n", 5687 | " print(\"Color is : \",self.color)\n", 5688 | " print(\"Size is : \",self.size)\n", 5689 | "\n", 5690 | "obj = Parrot('Gnree',4)\n", 5691 | "obj2 = Parrot('Blue',5)\n", 5692 | "\n", 5693 | "obj.config()\n", 5694 | "obj2.config()" 5695 | ], 5696 | "metadata": { 5697 | "colab": { 5698 | "base_uri": "https://localhost:8080/" 5699 | }, 5700 | "id": "pz04NwJtLuFO", 5701 | "outputId": "470f9017-bb56-4375-9bed-a2b28fd66942" 5702 | }, 5703 | "execution_count": null, 5704 | "outputs": [ 5705 | { 5706 | "output_type": "stream", 5707 | "name": "stdout", 5708 | "text": [ 5709 | "show\n", 5710 | "show\n", 5711 | "Color is : Green\n", 5712 | "Size is : 4\n", 5713 | "Color is : Blue\n", 5714 | "Size is : 5\n" 5715 | ] 5716 | } 5717 | ] 5718 | }, 5719 | { 5720 | "cell_type": "markdown", 5721 | "metadata": { 5722 | "id": "tN9s1052MzCe" 5723 | }, 5724 | "source": [ 5725 | "### Overview\n", 5726 | "\n", 5727 | "In the above program, we created a class with the name Parrot. \n", 5728 | "\n", 5729 | "These attributes are defined inside the init method of the class. It is the initializer method that is first run as soon as the object is created.\n", 5730 | "\n", 5731 | "Then, we create instances of the Parrot class. Here, blu and woo are references (value) to our new objects.\n", 5732 | "\n", 5733 | "Similarly, we access the instance attributes using blu.name and blu.age. " 5734 | ] 5735 | }, 5736 | { 5737 | "cell_type": "markdown", 5738 | "metadata": { 5739 | "id": "DGs9To6CMzCe" 5740 | }, 5741 | "source": [ 5742 | "# Great Job!\n", 5743 | "\n", 5744 | "That's all we need to know for now!\n", 5745 | "\n", 5746 | "Check out the github link for assignment :\n", 5747 | "https://github.com/utkarsh-iitbhu/Python-Workshop\n", 5748 | "\n", 5749 | "For git and github tutorial you can refer too : \n", 5750 | "https://youtube.com/playlist?list=PLLt4yMoVgczVgFcTzT60U5IXtNX1qjHL9\n", 5751 | "\n", 5752 | "\n" 5753 | ] 5754 | }, 5755 | { 5756 | "cell_type": "markdown", 5757 | "source": [ 5758 | "## Check out some cool projects in python \n", 5759 | "\n", 5760 | "https://github.com/recognai/rubrix\n", 5761 | "\n", 5762 | "https://github.com/httpie/httpie\n", 5763 | "\n", 5764 | "https://github.com/ActivityWatch/activitywatch\n", 5765 | "\n", 5766 | "https://github.com/adamghill/django-unicorn\n", 5767 | "\n", 5768 | "https://github.com/sherlock-project/sherlock\n", 5769 | "\n", 5770 | "https://github.com/vinta/awesome-python\n", 5771 | "\n", 5772 | "https://github.com/Ciphey/Ciphey\n", 5773 | "\n", 5774 | "https://github.com/encode/django-rest-framework" 5775 | ], 5776 | "metadata": { 5777 | "id": "35yMpBvtgXEm" 5778 | } 5779 | } 5780 | ], 5781 | "metadata": { 5782 | "kernelspec": { 5783 | "display_name": "Python 3 (ipykernel)", 5784 | "language": "python", 5785 | "name": "python3" 5786 | }, 5787 | "language_info": { 5788 | "codemirror_mode": { 5789 | "name": "ipython", 5790 | "version": 3 5791 | }, 5792 | "file_extension": ".py", 5793 | "mimetype": "text/x-python", 5794 | "name": "python", 5795 | "nbconvert_exporter": "python", 5796 | "pygments_lexer": "ipython3", 5797 | "version": "3.8.8" 5798 | }, 5799 | "colab": { 5800 | "name": "Python_guide.ipynb", 5801 | "provenance": [], 5802 | "collapsed_sections": [ 5803 | "LwzdirJJMzBQ", 5804 | "gGO0RBRCMzBT", 5805 | "2MbG65ABMzBY", 5806 | "eF8n8yHaMzBa", 5807 | "mFYDT7a-MzBb", 5808 | "msmiPgUDMzBc", 5809 | "grPyNlBnMzBc", 5810 | "2fNO3LlQMzBf", 5811 | "P1vNSet5MzBg", 5812 | "yv88aEzhMzBp", 5813 | "E4yI7jLuMzBr", 5814 | "hI_N68yqMzBr", 5815 | "YaBgpSYlMzBs", 5816 | "HyxbSTNWMzBt", 5817 | "vehYCw-OMzBv", 5818 | "5Pt7LcbxMzBw", 5819 | "c9Vk0RMTMzBz", 5820 | "jsiXgqaAMzB5", 5821 | "8Bsrtc3OMzB9", 5822 | "IOKc9i8sMzB9", 5823 | "WcKJ8vA6MzB-", 5824 | "o1lFDQK8MzB-", 5825 | "GI3wYdzbMzCA", 5826 | "jDIxTmT9MzCA", 5827 | "YVgr-TZsMzCB", 5828 | "JCzg9f5OMzCC", 5829 | "a8jb4Pl1MzCD", 5830 | "wkD34NhOMzCE", 5831 | "xyn_guW0MzCF", 5832 | "4MQTfhm2MzCG", 5833 | "50hWEtl1MzCH", 5834 | "XnsPCaA-MzCI", 5835 | "8UyCgkYzMzCI", 5836 | "wUgIcHB0MzCK", 5837 | "zz_0umGqMzCM", 5838 | "Sqdy4jUJMzCM", 5839 | "1F8PtmEKMzCN", 5840 | "VCyo8fi_MzCO", 5841 | "-YllIwk8MzCR", 5842 | "8L1Ms6D3MzCU", 5843 | "cpWYgJYsMzCV", 5844 | "f3xcyh6kMzCW", 5845 | "3m5wmx6QMzCY", 5846 | "z28LV8v3MzCY", 5847 | "2jFoIf2LMzCa", 5848 | "zXk_4JmaMzCc", 5849 | "OcGenCbrMzCc", 5850 | "3qYm5lttMzCc", 5851 | "Y3SzsI8DMzCc", 5852 | "gxZWDXLbMzCd", 5853 | "tN9s1052MzCe", 5854 | "DGs9To6CMzCe", 5855 | "35yMpBvtgXEm" 5856 | ] 5857 | } 5858 | }, 5859 | "nbformat": 4, 5860 | "nbformat_minor": 0 5861 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Workshop -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.19.3 2 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import Python_Exercises 3 | funcs = [Python_Exercises.power, Python_Exercises.split_str, Python_Exercises.format, 4 | Python_Exercises.indexing , Python_Exercises.subjective, Python_Exercises.dictionary, Python_Exercises.domainGet, 5 | Python_Exercises.findDog, Python_Exercises.countDog, Python_Exercises.lambdafunc, Python_Exercises.caught_speeding, 6 | Python_Exercises.create_arr_of_fives,Python_Exercises.even_num,Python_Exercises.create_matrix, 7 | Python_Exercises.linear_space,Python_Exercises.decimal_mat,Python_Exercises.slices_1,Python_Exercises.slices_2,Python_Exercises.slices_3] 8 | debug = False 9 | test_cases = { 10 | 'power' : [ 11 | [(7,4),2401] 12 | ], 13 | 'split_str' : [ 14 | [("Hi there Sam!",),["Hi","there","Sam!"]] 15 | ], 16 | 17 | 'format' : [ 18 | [("Earth",12742,),'The diameter of Earth is 12742 kilometers.'] 19 | ], 20 | 21 | 'indexing' : [ 22 | [ 23 | ([1,2,[3,4],[5,[100,200,["hello"]],23,11],1,7],) , 24 | 25 | ("hello") 26 | 27 | ] 28 | ], 29 | 'subjective' : [['',"immutable"]], 30 | 31 | 32 | 'dictionary' : [ 33 | [({'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]},), 34 | ("hello")] 35 | ], 36 | 37 | 38 | 'domainGet' : [ 39 | [('user@domain.com',),'domain.com'] 40 | ], 41 | 'findDog' : [ 42 | [('Is there a dog here?',),True] 43 | ], 44 | 'countDog' : [ 45 | [('This dog runs faster than the other dog dude!',),2] 46 | ], 47 | 'lambdafunc' : [[ (['soup','dog','salad','cat','great'],), 48 | ['soup','salad'], 49 | ]], 50 | 'caught_speeding' : [ 51 | [(81,True),"Small Ticket"] 52 | ], 53 | 54 | 'create_arr_of_fives': [ 55 | ['', (([ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])) 56 | ] 57 | ], 58 | 59 | 'even_num': [ 60 | ['',([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 61 | 44, 46, 48, 50])] 62 | ], 63 | 64 | 'create_matrix' : [ 65 | [ 66 | '',(([[0, 1, 2], 67 | [3, 4, 5], 68 | [6, 7, 8]])) 69 | ] 70 | ], 71 | 72 | 'linear_space' : [ 73 | [ 74 | '',(([0.0, 75 | 0.05263157894736842, 76 | 0.10526315789473684, 77 | 0.15789473684210525, 78 | 0.21052631578947367, 79 | 0.2631578947368421, 80 | 0.3157894736842105, 81 | 0.3684210526315789, 82 | 0.42105263157894735, 83 | 0.47368421052631576, 84 | 0.5263157894736842, 85 | 0.5789473684210527, 86 | 0.631578947368421, 87 | 0.6842105263157894, 88 | 0.7368421052631579, 89 | 0.7894736842105263, 90 | 0.8421052631578947, 91 | 0.894736842105263, 92 | 0.9473684210526315, 93 | 1.0]) 94 | )] 95 | ], 96 | 97 | 'decimal_mat': [ 98 | [ 99 | '',([[ 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ], 100 | [ 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ], 101 | [ 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ], 102 | [ 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ], 103 | [ 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ], 104 | [ 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ], 105 | [ 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ], 106 | [ 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ], 107 | [ 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ], 108 | [ 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]]) 109 | ] 110 | ], 111 | 112 | 'slices_1': [['',([[12, 13, 14, 15], 113 | [17, 18, 19, 20], 114 | [22, 23, 24, 25]]) 115 | ]], 116 | 117 | 'slices_2' : [[ 118 | '',([[ 2], 119 | [ 7], 120 | [12]]) 121 | ]], 122 | 123 | 'slices_3' : [[ 124 | '',([[16, 17, 18, 19, 20], 125 | [21, 22, 23, 24, 25]]) 126 | ]] 127 | 128 | } ## Will be filled after submission date 129 | 130 | score = 0 131 | 132 | for func in funcs: 133 | flag = 1 134 | for i,test_case in enumerate(test_cases[func.__name__]): 135 | 136 | x, y = test_case 137 | 138 | try: 139 | if func.__name__ == 'eqn_solver': 140 | np.testing.assert_allclose( y, func(*x) ) 141 | 142 | else: 143 | 144 | assert y==func(*x) 145 | 146 | except Exception as e: 147 | print(e) 148 | flag = 0 149 | print("function "+func.__name__+" fails on testcase "+str(i+1),end="") 150 | if debug: 151 | print("because ") 152 | 153 | else: 154 | print("") 155 | if flag==1: 156 | print("function "+func.__name__+" passes all the test cases!") 157 | score += flag 158 | 159 | print("Your score is "+str(score)+"!!,\nYou will receive extra points for smart implementation directly on the pull request") 160 | if score!=len(funcs): 161 | raise Exception("Did not pass all tests") 162 | --------------------------------------------------------------------------------