├── .gitignore ├── 00-Python-Basics ├── 01-Built-in-Scalar-Types.ipynb ├── 02-Built-in-Data-Structures.ipynb ├── 03-Control-Flow-Statements.ipynb ├── 04-Functions.ipynb ├── 04-IPython-and-Shell-Commands.ipynb ├── 05-Errors-and-Debugging.ipynb ├── 06-Iterators.ipynb ├── 07-Generators.ipynb ├── 08-Modules-and-Packages.ipynb ├── 09-Strings-and-Regular-Expressions.ipynb ├── Exercise-01-Built-in-Scalar-Types.ipynb ├── Exercise-02-Built-in-Data-Structures.ipynb ├── Exercise-03-Control-Flow-Statements.ipynb ├── Exercise-04-Functions.ipynb ├── Exercise-05-Errors-and-Debugging.ipynb ├── Exercise-06-Iterators.ipynb ├── Exercise-07-Generators.ipynb ├── Exercise-08-Modules-and-Packages.ipynb └── Exercise-09-Strings-and-Regular-Expressions.ipynb ├── 01-Introduction ├── 01-Introduction.ipynb ├── 02-Colab-Intro.ipynb ├── 03-Python-Syntax-and-Semantics.ipynb ├── 04-Magic-Commands.ipynb ├── 05-A-Preview-of-Data-Science-Tools.ipynb ├── Exercise-01-Introduction.ipynb ├── Exercise-03-Python-Syntax-and-Semantics.ipynb └── Exercise-05-Preview-of-Data-Science-Tools.ipynb ├── 02-Data-Import-Export ├── 01-File-Import-Export-in-Colab.ipynb ├── 02-Reading-Tabular-Data-into-DataFrames.ipynb ├── Assignment-01.ipynb └── sample_data │ ├── Pandas-Example.xlsx │ ├── anscombe.json │ ├── california_housing_test.csv │ ├── datasets.xlsx │ ├── electric-car-sales.csv │ └── mtcars.csv ├── 03-Data-Transformation ├── 01-Missing-Values.ipynb ├── 02-Tidy-Data.ipynb ├── 03-Aggregation-and-Grouping.ipynb ├── Exercise-01-Missing-Values.ipynb ├── Exercise-03-Aggregation-and-Grouping.ipynb └── loc-pandas-Guide.ipynb ├── 04-Visualization ├── 01-Matplotlib.ipynb ├── 02-Seaborn.ipynb ├── Assignment-02.ipynb ├── Exercise-02-Seaborn.ipynb └── Mevgal-Cleaning.ipynb ├── 05-EDA ├── 01-Exploratory-Data-Analysis.ipynb ├── README.md └── Sephora-EDA-Exercise.ipynb ├── 06-Joins └── 01-Merge-and-Join.ipynb ├── 07-Bonus ├── 01-Machine-Learning-Overview.ipynb ├── 02-Introducing-Scikit-Learn.ipynb ├── 03-Classification.ipynb ├── 04-Regression.ipynb ├── 05-Exploring-Hand-written-Digits.ipynb ├── 06-Working-with-APIs.ipynb ├── 07-BigQuery.ipynb └── 08-Working-with-Time-Series.ipynb ├── Docs ├── Cheatsheets │ ├── K-Means.png │ ├── Matplotlib-CheatSheet.pdf │ ├── Pandas-CheatSheet.pdf │ ├── Scikit-learn-CheatSheet.pdf │ ├── Seaborn-CheatSheet.pdf │ ├── python_cheatsheet_dictionaries.pdf │ ├── python_cheatsheet_functions.pdf │ ├── python_cheatsheet_if_while.pdf │ └── python_cheatsheet_lists.pdf └── a-whirlwind-tour-of-python.pdf ├── LICENSE ├── README.md └── data ├── 120-years-of-olympic-history-athletes-and-results ├── athlete_events.csv └── noc_regions.csv ├── 2017_StPaul_MN_Real_Estate.csv ├── FremontBridge.csv ├── GOOGL.csv ├── GOT-battles.csv ├── GOT-character-deaths.csv ├── MEVGAL-Dairy-Sales ├── product1-2020.xlsx ├── product1-2021.xlsx ├── product1-2022.xlsx ├── product2-2020.xlsx ├── product2-2021.xlsx ├── product2-2022.xlsx ├── product3-2020.xlsx ├── product3-2021.xlsx ├── product3-2022.xlsx ├── product4-2020.xlsx ├── product4-2021.xlsx ├── product4-2022.xlsx ├── product5-2020.xlsx ├── product5-2021.xlsx ├── product5-2022.xlsx ├── product6-2020.xlsx ├── product6-2021.xlsx ├── product6-2022.xlsx ├── product7-2020.xlsx ├── product7-2021.xlsx ├── product7-2022.xlsx └── products-2020-2022.csv ├── Pandas-Example.xlsx ├── Telco-Customer-Churn.csv ├── anscombe.json ├── california_housing_test.csv ├── datasets.xlsx ├── movies_metadata.csv ├── movies_ratings.csv ├── mtcars.csv ├── state-abbrevs.csv ├── state-areas.csv └── state-population.csv /.gitignore: -------------------------------------------------------------------------------- 1 | materials 2 | *.docx 3 | *.swp 4 | KEY-*.ipynb 5 | *.html 6 | 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | build/ 18 | develop-eggs/ 19 | dist/ 20 | downloads/ 21 | eggs/ 22 | .eggs/ 23 | lib/ 24 | lib64/ 25 | parts/ 26 | sdist/ 27 | var/ 28 | wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *.cover 53 | .hypothesis/ 54 | .pytest_cache/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | 65 | # Flask stuff: 66 | instance/ 67 | .webassets-cache 68 | 69 | # Scrapy stuff: 70 | .scrapy 71 | 72 | # Sphinx documentation 73 | docs/_build/ 74 | 75 | # PyBuilder 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # pyenv 82 | .python-version 83 | 84 | # celery beat schedule file 85 | celerybeat-schedule 86 | 87 | # SageMath parsed files 88 | *.sage.py 89 | 90 | # Environments 91 | .env 92 | .venv 93 | env/ 94 | venv/ 95 | ENV/ 96 | env.bak/ 97 | venv.bak/ 98 | 99 | # Spyder project settings 100 | .spyderproject 101 | .spyproject 102 | 103 | # Rope project settings 104 | .ropeproject 105 | 106 | # mkdocs documentation 107 | /site 108 | 109 | # mypy 110 | .mypy_cache/ 111 | 112 | notebooks 113 | -------------------------------------------------------------------------------- /00-Python-Basics/04-Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Defining and Using Functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "So far, our scripts have been simple, single-use code blocks.\n", 15 | "One way to organize our Python code and to make it more readable and reusable is to factor-out useful pieces into reusable *functions*.\n", 16 | "Here we'll cover two ways of creating functions: the ``def`` statement, useful for any type of function, and the ``lambda`` statement, useful for creating short anonymous functions." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Using Functions\n", 24 | "\n", 25 | "Functions are groups of code that have a name, and can be called using parentheses.\n", 26 | "We've seen functions before. For example, ``print`` in Python 3 is a function:" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "abc\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "print('abc')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "Here ``print`` is the function name, and ``'abc'`` is the function's input *argument*.\n", 51 | "\n", 52 | "In addition to arguments, there are *keyword arguments* that are specified by name.\n", 53 | "One available keyword argument for the ``print()`` function (in Python 3) is ``sep``, which tells what character or characters should be used to separate multiple items:" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "1 2 3\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "print(1, 2, 3)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "1--2--3\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "print(1, 2, 3, sep='--')" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "When non-keyword arguments are used together with keyword arguments, the keyword arguments must come at the end." 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "## Defining Functions\n", 102 | "Functions become even more useful when we begin to define our own, organizing functionality to be used in multiple places.\n", 103 | "In Python, functions are defined with the ``def`` statement." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "def add_1(x):\n", 113 | " return x + 1" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 5, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "11" 125 | ] 126 | }, 127 | "execution_count": 5, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "add_1(10)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "We can even create a docstring for our functions:" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 6, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "def add_1(x):\n", 150 | " \"\"\"Adds 1 to the input.\n", 151 | " \n", 152 | " Args:\n", 153 | " x (int/float): A numberical value\n", 154 | " \n", 155 | " Returns:\n", 156 | " x + 1\n", 157 | " \"\"\"\n", 158 | " return x + 1" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "Using `?` or `help()` one can access this docstring:" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 7, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "\u001b[0;31mSignature:\u001b[0m \u001b[0madd_1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 177 | "\u001b[0;31mDocstring:\u001b[0m\n", 178 | "Adds 1 to the input.\n", 179 | "\n", 180 | "Args:\n", 181 | " x (int/float): A numberical value\n", 182 | "\n", 183 | "Returns:\n", 184 | " x + 1\n", 185 | "\u001b[0;31mFile:\u001b[0m ~/Drive/projects/Business-Analytics/02-Python-Overview/\n", 186 | "\u001b[0;31mType:\u001b[0m function\n" 187 | ] 188 | }, 189 | "metadata": {}, 190 | "output_type": "display_data" 191 | } 192 | ], 193 | "source": [ 194 | "add_1?" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "Help on function add_1 in module __main__:\n", 207 | "\n", 208 | "add_1(x)\n", 209 | " Adds 1 to the input.\n", 210 | " \n", 211 | " Args:\n", 212 | " x (int/float): A numberical value\n", 213 | " \n", 214 | " Returns:\n", 215 | " x + 1\n", 216 | "\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "help(add_1)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "For example, we can encapsulate a version of our Fibonacci sequence code from the previous section as follows:" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 9, 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "def fibonacci(N):\n", 238 | " \"\"\"Calculates the first N Fibonacci numbers, with initial condition a, b = 0, 1.\n", 239 | " \n", 240 | " Args:\n", 241 | " N (int): Number of Fibonacci numbers to be returned\n", 242 | " \n", 243 | " Returns:\n", 244 | " L (list): A list of Fibonacci numbers\n", 245 | " \"\"\"\n", 246 | " L = []\n", 247 | " a, b = 0, 1\n", 248 | " while len(L) < N:\n", 249 | " a, b = b, a + b\n", 250 | " L.append(a)\n", 251 | " return L" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 10, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "Help on function fibonacci in module __main__:\n", 264 | "\n", 265 | "fibonacci(N)\n", 266 | " Calculates the first N Fibonacci numbers, with initial condition a, b = 0, 1.\n", 267 | " \n", 268 | " Args:\n", 269 | " N (int): Number of Fibonacci numbers to be returned\n", 270 | " \n", 271 | " Returns:\n", 272 | " L (list): A list of Fibonacci numbers\n", 273 | "\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "help(fibonacci)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "Now we have a function named ``fibonacci`` which takes a single argument ``N``, does something with this argument, and ``return``s a value; in this case, a list of the first ``N`` Fibonacci numbers:" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 11, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" 297 | ] 298 | }, 299 | "execution_count": 11, 300 | "metadata": {}, 301 | "output_type": "execute_result" 302 | } 303 | ], 304 | "source": [ 305 | "fibonacci(10)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "If you're familiar with strongly-typed languages like ``C``, you'll immediately notice that there is no type information associated with the function inputs or outputs.\n", 313 | "Python functions can return any Python object, simple or compound, which means constructs that may be difficult in other languages are straightforward in Python.\n", 314 | "\n", 315 | "For example, multiple return values are simply put in a tuple, which is indicated by commas:" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 12, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "3.0 4.0 (3-4j)\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "def real_imag_conj(val):\n", 333 | " \"\"\"Returns real, imaginary, and complex conjugate of a complex number.\n", 334 | " \n", 335 | " Args:\n", 336 | " val (complex): A complex number\n", 337 | " \n", 338 | " Returns:\n", 339 | " val.real, val.imag, val.conjugate()\n", 340 | " \"\"\"\n", 341 | " return val.real, val.imag, val.conjugate()\n", 342 | "\n", 343 | "r, i, c = real_imag_conj(3 + 4j)\n", 344 | "print(r, i, c)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "## Default Argument Values\n", 352 | "\n", 353 | "Often when defining a function, there are certain values that we want the function to use *most* of the time, but we'd also like to give the user some flexibility.\n", 354 | "In this case, we can use *default values* for arguments.\n", 355 | "Consider the ``fibonacci`` function from before.\n", 356 | "What if we would like the user to be able to play with the starting values?\n", 357 | "We could do that as follows:" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 13, 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [ 366 | "def fibonacci(N, a=0, b=1):\n", 367 | " L = []\n", 368 | " while len(L) < N:\n", 369 | " a, b = b, a + b\n", 370 | " L.append(a)\n", 371 | " return L" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "With a single argument, the result of the function call is identical to before:" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 14, 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "data": { 388 | "text/plain": [ 389 | "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" 390 | ] 391 | }, 392 | "execution_count": 14, 393 | "metadata": {}, 394 | "output_type": "execute_result" 395 | } 396 | ], 397 | "source": [ 398 | "fibonacci(10)" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "But now we can use the function to explore new things, such as the effect of new starting values:" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 15, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "data": { 415 | "text/plain": [ 416 | "[2, 2, 4, 6, 10, 16, 26, 42, 68, 110]" 417 | ] 418 | }, 419 | "execution_count": 15, 420 | "metadata": {}, 421 | "output_type": "execute_result" 422 | } 423 | ], 424 | "source": [ 425 | "fibonacci(10, 0, 2)" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "The values can also be specified by name if desired, in which case the order of the named values does not matter:" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 16, 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "data": { 442 | "text/plain": [ 443 | "[3, 4, 7, 11, 18, 29, 47, 76, 123, 199]" 444 | ] 445 | }, 446 | "execution_count": 16, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "fibonacci(10, b=3, a=1)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "## ``*args`` and ``**kwargs``: Flexible Arguments\n", 460 | "Sometimes you might wish to write a function in which you don't initially know how many arguments the user will pass.\n", 461 | "In this case, you can use the special form ``*args`` and ``**kwargs`` to catch all arguments that are passed.\n", 462 | "Here is an example:" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 17, 468 | "metadata": {}, 469 | "outputs": [], 470 | "source": [ 471 | "def catch_all(*args, **kwargs):\n", 472 | " print(\"args =\", args)\n", 473 | " print(\"kwargs = \", kwargs)" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 18, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "args = (1, 2, 3)\n", 486 | "kwargs = {'a': 4, 'b': 5}\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "catch_all(1, 2, 3, a=4, b=5)" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 19, 497 | "metadata": {}, 498 | "outputs": [ 499 | { 500 | "name": "stdout", 501 | "output_type": "stream", 502 | "text": [ 503 | "args = ('a',)\n", 504 | "kwargs = {'keyword': 2}\n" 505 | ] 506 | } 507 | ], 508 | "source": [ 509 | "catch_all('a', keyword=2)" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": {}, 515 | "source": [ 516 | "Here it is not the names ``args`` and ``kwargs`` that are important, but the ``*`` characters preceding them.\n", 517 | "``args`` and ``kwargs`` are just the variable names often used by convention, short for \"arguments\" and \"keyword arguments\".\n", 518 | "The operative difference is the asterisk characters: a single ``*`` before a variable means \"expand this as a sequence\", while a double ``**`` before a variable means \"expand this as a dictionary\".\n", 519 | "In fact, this syntax can be used not only with the function definition, but with the function call as well!" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 20, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "name": "stdout", 529 | "output_type": "stream", 530 | "text": [ 531 | "args = (1, 2, 3)\n", 532 | "kwargs = {'pi': 3.14}\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "inputs = (1, 2, 3)\n", 538 | "keywords = {'pi': 3.14}\n", 539 | "\n", 540 | "catch_all(*inputs, **keywords)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "## ``lambda`` Functions (anonymous functions)\n", 548 | "Earlier we quickly covered the most common way of defining functions, the ``def`` statement.\n", 549 | "You'll likely come across another way of defining short, one-off functions with the ``lambda`` statement.\n", 550 | "It looks something like this:" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 21, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "3" 562 | ] 563 | }, 564 | "execution_count": 21, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "add = lambda x, y: x + y\n", 571 | "add(1, 2)" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "This lambda function is roughly equivalent to" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 22, 584 | "metadata": {}, 585 | "outputs": [], 586 | "source": [ 587 | "def add(x, y):\n", 588 | " return x + y" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "So why would you ever want to use such a thing?\n", 596 | "Primarily, it comes down to the fact that *everything is an object* in Python, even functions themselves!\n", 597 | "That means that functions can be passed as arguments to functions.\n", 598 | "\n", 599 | "As an example of this, suppose we have some data stored in a list of dictionaries:" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 23, 605 | "metadata": {}, 606 | "outputs": [], 607 | "source": [ 608 | "data = [{'first':'Guido', 'last':'Van Rossum', 'YOB':1956},\n", 609 | " {'first':'Grace', 'last':'Hopper', 'YOB':1906},\n", 610 | " {'first':'Alan', 'last':'Turing', 'YOB':1912}]" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "Now suppose we want to sort this data.\n", 618 | "Python has a ``sorted`` function that does this:" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 24, 624 | "metadata": {}, 625 | "outputs": [ 626 | { 627 | "data": { 628 | "text/plain": [ 629 | "[1, 2, 3, 4, 5, 6]" 630 | ] 631 | }, 632 | "execution_count": 24, 633 | "metadata": {}, 634 | "output_type": "execute_result" 635 | } 636 | ], 637 | "source": [ 638 | "sorted([2,4,3,5,1,6])" 639 | ] 640 | }, 641 | { 642 | "cell_type": "markdown", 643 | "metadata": {}, 644 | "source": [ 645 | "But dictionaries are not orderable: we need a way to tell the function *how* to sort our data.\n", 646 | "We can do this by specifying the ``key`` function, a function which given an item returns the sorting key for that item:" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 25, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "data": { 656 | "text/plain": [ 657 | "[{'first': 'Alan', 'last': 'Turing', 'YOB': 1912},\n", 658 | " {'first': 'Grace', 'last': 'Hopper', 'YOB': 1906},\n", 659 | " {'first': 'Guido', 'last': 'Van Rossum', 'YOB': 1956}]" 660 | ] 661 | }, 662 | "execution_count": 25, 663 | "metadata": {}, 664 | "output_type": "execute_result" 665 | } 666 | ], 667 | "source": [ 668 | "# sort alphabetically by first name\n", 669 | "sorted(data, key=lambda item: item['first'])" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": 26, 675 | "metadata": {}, 676 | "outputs": [ 677 | { 678 | "data": { 679 | "text/plain": [ 680 | "[{'first': 'Grace', 'last': 'Hopper', 'YOB': 1906},\n", 681 | " {'first': 'Alan', 'last': 'Turing', 'YOB': 1912},\n", 682 | " {'first': 'Guido', 'last': 'Van Rossum', 'YOB': 1956}]" 683 | ] 684 | }, 685 | "execution_count": 26, 686 | "metadata": {}, 687 | "output_type": "execute_result" 688 | } 689 | ], 690 | "source": [ 691 | "# sort by year of birth\n", 692 | "sorted(data, key=lambda item: item['YOB'])" 693 | ] 694 | }, 695 | { 696 | "cell_type": "markdown", 697 | "metadata": {}, 698 | "source": [ 699 | "While these key functions could certainly be created by the normal, ``def`` syntax, the ``lambda`` syntax is convenient for such short one-off functions like these." 700 | ] 701 | } 702 | ], 703 | "metadata": { 704 | "kernelspec": { 705 | "display_name": "Python 3", 706 | "language": "python", 707 | "name": "python3" 708 | }, 709 | "language_info": { 710 | "codemirror_mode": { 711 | "name": "ipython", 712 | "version": 3 713 | }, 714 | "file_extension": ".py", 715 | "mimetype": "text/x-python", 716 | "name": "python", 717 | "nbconvert_exporter": "python", 718 | "pygments_lexer": "ipython3", 719 | "version": "3.7.2" 720 | } 721 | }, 722 | "nbformat": 4, 723 | "nbformat_minor": 2 724 | } 725 | -------------------------------------------------------------------------------- /00-Python-Basics/04-IPython-and-Shell-Commands.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# IPython and Shell Commands" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "When working interactively with the standard Python interpreter, one of the frustrations is the need to switch between multiple windows to access Python tools and system command-line tools.\n", 15 | "IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal.\n", 16 | "The magic happens with the exclamation point: anything appearing after ``!`` on a line will be executed not by the Python kernel, but by the system command-line.\n", 17 | "\n", 18 | "The following assumes you're on a Unix-like system, such as Linux or Mac OSX.\n", 19 | "Some of the examples that follow will fail on Windows, which uses a different type of shell by default (though with the 2016 announcement of native Bash shells on Windows, soon this may no longer be an issue!).\n", 20 | "If you're unfamiliar with shell commands, I'd suggest reviewing the [Shell Tutorial](http://swcarpentry.github.io/shell-novice/) put together by the always excellent Software Carpentry Foundation." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "## Quick Introduction to the Shell\n", 28 | "\n", 29 | "A full intro to using the shell/terminal/command-line is well beyond the scope of this chapter, but for the uninitiated we will offer a quick introduction here.\n", 30 | "The shell is a way to interact textually with your computer.\n", 31 | "Ever since the mid 1980s, when Microsoft and Apple introduced the first versions of their now ubiquitous graphical operating systems, most computer users have interacted with their operating system through familiar clicking of menus and drag-and-drop movements.\n", 32 | "But operating systems existed long before these graphical user interfaces, and were primarily controlled through sequences of text input: at the prompt, the user would type a command, and the computer would do what the user told it to.\n", 33 | "Those early prompt systems are the precursors of the shells and terminals that most active data scientists still use today.\n", 34 | "\n", 35 | "Someone unfamiliar with the shell might ask why you would bother with this, when many results can be accomplished by simply clicking on icons and menus.\n", 36 | "A shell user might reply with another question: why hunt icons and click menus when you can accomplish things much more easily by typing?\n", 37 | "While it might sound like a typical tech preference impasse, when moving beyond basic tasks it quickly becomes clear that the shell offers much more control of advanced tasks, though admittedly the learning curve can intimidate the average computer user.\n", 38 | "\n", 39 | "As an example, here is a sample of a Linux/OSX shell session where a user explores, creates, and modifies directories and files on their system (``osx:~ $`` is the prompt, and everything after the ``$`` sign is the typed command; text that is preceded by a ``#`` is meant just as description, rather than something you would actually type in):\n", 40 | "\n", 41 | "```bash\n", 42 | "osx:~ $ echo \"hello world\" # echo is like Python's print function\n", 43 | "hello world\n", 44 | "\n", 45 | "osx:~ $ pwd # pwd = print working directory\n", 46 | "/home/jake # this is the \"path\" that we're sitting in\n", 47 | "\n", 48 | "osx:~ $ ls # ls = list working directory contents\n", 49 | "notebooks projects \n", 50 | "\n", 51 | "osx:~ $ cd projects/ # cd = change directory\n", 52 | "\n", 53 | "osx:projects $ pwd\n", 54 | "/home/jake/projects\n", 55 | "\n", 56 | "osx:projects $ ls\n", 57 | "datasci_book mpld3 myproject.txt\n", 58 | "\n", 59 | "osx:projects $ mkdir myproject # mkdir = make new directory\n", 60 | "\n", 61 | "osx:projects $ cd myproject/\n", 62 | "\n", 63 | "osx:myproject $ mv ../myproject.txt ./ # mv = move file. Here we're moving the\n", 64 | " # file myproject.txt from one directory\n", 65 | " # up (../) to the current directory (./)\n", 66 | "osx:myproject $ ls\n", 67 | "myproject.txt\n", 68 | "```\n", 69 | "\n", 70 | "Notice that all of this is just a compact way to do familiar operations (navigating a directory structure, creating a directory, moving a file, etc.) by typing commands rather than clicking icons and menus.\n", 71 | "Note that with just a few commands (``pwd``, ``ls``, ``cd``, ``mkdir``, and ``cp``) you can do many of the most common file operations.\n", 72 | "It's when you go beyond these basics that the shell approach becomes really powerful." 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## Shell Commands in IPython\n", 80 | "\n", 81 | "Any command that works at the command-line can be used in IPython by prefixing it with the ``!`` character.\n", 82 | "For example, the ``ls``, ``pwd``, and ``echo`` commands can be run as follows:\n", 83 | "\n", 84 | "```ipython\n", 85 | "In [1]: !ls\n", 86 | "myproject.txt\n", 87 | "\n", 88 | "In [2]: !pwd\n", 89 | "/home/jake/projects/myproject\n", 90 | "\n", 91 | "In [3]: !echo \"printing from the shell\"\n", 92 | "printing from the shell\n", 93 | "```" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "## Passing Values to and from the Shell\n", 101 | "\n", 102 | "Shell commands can not only be called from IPython, but can also be made to interact with the IPython namespace.\n", 103 | "For example, you can save the output of any shell command to a Python list using the assignment operator:\n", 104 | "\n", 105 | "```ipython\n", 106 | "In [4]: contents = !ls\n", 107 | "\n", 108 | "In [5]: print(contents)\n", 109 | "['myproject.txt']\n", 110 | "\n", 111 | "In [6]: directory = !pwd\n", 112 | "\n", 113 | "In [7]: print(directory)\n", 114 | "['/Users/jakevdp/notebooks/tmp/myproject']\n", 115 | "```\n", 116 | "\n", 117 | "Note that these results are not returned as lists, but as a special shell return type defined in IPython:\n", 118 | "\n", 119 | "```ipython\n", 120 | "In [8]: type(directory)\n", 121 | "IPython.utils.text.SList\n", 122 | "```\n", 123 | "\n", 124 | "This looks and acts a lot like a Python list, but has additional functionality, such as\n", 125 | "the ``grep`` and ``fields`` methods and the ``s``, ``n``, and ``p`` properties that allow you to search, filter, and display the results in convenient ways.\n", 126 | "For more information on these, you can use IPython's built-in help features." 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Communication in the other direction–passing Python variables into the shell–is possible using the ``{varname}`` syntax:\n", 134 | "\n", 135 | "```ipython\n", 136 | "In [9]: message = \"hello from Python\"\n", 137 | "\n", 138 | "In [10]: !echo {message}\n", 139 | "hello from Python\n", 140 | "```\n", 141 | "\n", 142 | "The curly braces contain the variable name, which is replaced by the variable's contents in the shell command." 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "# Shell-Related Magic Commands\n", 150 | "\n", 151 | "If you play with IPython's shell commands for a while, you might notice that you cannot use ``!cd`` to navigate the filesystem:\n", 152 | "\n", 153 | "```ipython\n", 154 | "In [11]: !pwd\n", 155 | "/home/jake/projects/myproject\n", 156 | "\n", 157 | "In [12]: !cd ..\n", 158 | "\n", 159 | "In [13]: !pwd\n", 160 | "/home/jake/projects/myproject\n", 161 | "```\n", 162 | "\n", 163 | "The reason is that shell commands in the notebook are executed in a temporary subshell.\n", 164 | "If you'd like to change the working directory in a more enduring way, you can use the ``%cd`` magic command:\n", 165 | "\n", 166 | "```ipython\n", 167 | "In [14]: %cd ..\n", 168 | "/home/jake/projects\n", 169 | "```\n", 170 | "\n", 171 | "In fact, by default you can even use this without the ``%`` sign:\n", 172 | "\n", 173 | "```ipython\n", 174 | "In [15]: cd myproject\n", 175 | "/home/jake/projects/myproject\n", 176 | "```\n", 177 | "\n", 178 | "This is known as an ``automagic`` function, and this behavior can be toggled with the ``%automagic`` magic function.\n", 179 | "\n", 180 | "Besides ``%cd``, other available shell-like magic functions are ``%cat``, ``%cp``, ``%env``, ``%ls``, ``%man``, ``%mkdir``, ``%more``, ``%mv``, ``%pwd``, ``%rm``, and ``%rmdir``, any of which can be used without the ``%`` sign if ``automagic`` is on.\n", 181 | "This makes it so that you can almost treat the IPython prompt as if it's a normal shell:\n", 182 | "\n", 183 | "```ipython\n", 184 | "In [16]: mkdir tmp\n", 185 | "\n", 186 | "In [17]: ls\n", 187 | "myproject.txt tmp/\n", 188 | "\n", 189 | "In [18]: cp myproject.txt tmp/\n", 190 | "\n", 191 | "In [19]: ls tmp\n", 192 | "myproject.txt\n", 193 | "\n", 194 | "In [20]: rm -r tmp\n", 195 | "```\n", 196 | "\n", 197 | "This access to the shell from within the same terminal window as your Python session means that there is a lot less switching back and forth between interpreter and shell as you write your Python code." 198 | ] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 3", 204 | "language": "python", 205 | "name": "python3" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 3 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython3", 217 | "version": "3.7.2" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 2 222 | } 223 | -------------------------------------------------------------------------------- /00-Python-Basics/07-Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Generators" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Here we'll take a deeper dive into Python generators, including *generator expressions* and *generator functions*." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Generator Expressions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "The difference between list comprehensions and generator expressions is sometimes confusing; here we'll quickly outline the differences between them:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "### List comprehensions use square brackets, while generator expressions use parentheses\n", 36 | "This is a representative list comprehension:" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "[n ** 2 for n in range(12)]" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "While this is a representative generator expression:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 2, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | " at 0x111a84930>" 75 | ] 76 | }, 77 | "execution_count": 2, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "(n ** 2 for n in range(12))" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Notice that printing the generator expression does not print the contents; one way to print the contents of a generator expression is to pass it to the ``list`` constructor:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 102 | ] 103 | }, 104 | "execution_count": 3, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "G = (n ** 2 for n in range(12))\n", 111 | "list(G)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### A list is a collection of values, while a generator is a recipe for producing values\n", 119 | "When you create a list, you are actually building a collection of values, and there is some memory cost associated with that.\n", 120 | "When you create a generator, you are not building a collection of values, but a recipe for producing those values.\n", 121 | "Both expose the same iterator interface, as we can see here:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 4, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "0 1 4 9 16 25 36 49 64 81 100 121 " 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "L = [n ** 2 for n in range(12)]\n", 139 | "for val in L:\n", 140 | " print(val, end=' ')" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 5, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "0 1 4 9 16 25 36 49 64 81 100 121 " 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "G = (n ** 2 for n in range(12))\n", 158 | "for val in G:\n", 159 | " print(val, end=' ')" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "The difference is that a generator expression does not actually compute the values until they are needed.\n", 167 | "This not only leads to memory efficiency, but to computational efficiency as well!\n", 168 | "This also means that while the size of a list is limited by available memory, the size of a generator expression is unlimited!\n", 169 | "\n", 170 | "An example of an infinite generator expression can be created using the ``count`` iterator defined in ``itertools``:" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 6, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "count(0)" 182 | ] 183 | }, 184 | "execution_count": 6, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "from itertools import count\n", 191 | "count()" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 7, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "0 1 2 3 4 5 6 7 8 9 10 " 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "for i in count():\n", 209 | " print(i, end=' ')\n", 210 | " if i >= 10: break" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "The ``count`` iterator will go on happily counting forever until you tell it to stop; this makes it convenient to create generators that will also go on forever:" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 8, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "1 11 13 17 19 23 29 31 37 41 " 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "factors = [2, 3, 5, 7]\n", 235 | "G = (i for i in count() if all(i % n > 0 for n in factors))\n", 236 | "for val in G:\n", 237 | " print(val, end=' ')\n", 238 | " if val > 40: break" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "Here we have used `all()` function: Returns True if bool(x) is True for all values x in the iterable.\n", 246 | "\n", 247 | "You might see what we're getting at here: if we were to expand the list of factors appropriately, what we would have the beginnings of is a prime number generator, using the Sieve of Eratosthenes algorithm. We'll explore this more momentarily." 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### A list can be iterated multiple times; a generator expression is single-use\n", 255 | "This is one of those potential gotchas of generator expressions.\n", 256 | "With a list, we can straightforwardly do this:" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 9, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "0 1 4 9 16 25 36 49 64 81 100 121 \n", 269 | "0 1 4 9 16 25 36 49 64 81 100 121 " 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "L = [n ** 2 for n in range(12)]\n", 275 | "for val in L:\n", 276 | " print(val, end=' ')\n", 277 | "print()\n", 278 | "\n", 279 | "for val in L:\n", 280 | " print(val, end=' ')" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "A generator expression, on the other hand, is used-up after one iteration:" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 10, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 299 | ] 300 | }, 301 | "execution_count": 10, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "G = (n ** 2 for n in range(12))\n", 308 | "list(G)" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 11, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "[]" 320 | ] 321 | }, 322 | "execution_count": 11, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "list(G)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "This can be very useful because it means iteration can be stopped and started:" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 12, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "name": "stdout", 345 | "output_type": "stream", 346 | "text": [ 347 | "0 1 4 9 16 25 36 \n", 348 | "doing something in between\n", 349 | "49 64 81 100 121 " 350 | ] 351 | } 352 | ], 353 | "source": [ 354 | "G = (n**2 for n in range(12))\n", 355 | "for n in G:\n", 356 | " print(n, end=' ')\n", 357 | " if n > 30: break\n", 358 | "\n", 359 | "print(\"\\ndoing something in between\")\n", 360 | "\n", 361 | "for n in G:\n", 362 | " print(n, end=' ')" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "One place I've found this useful is when working with collections of data files on disk; it means that you can quite easily analyze them in batches, letting the generator keep track of which ones you have yet to see." 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "## Generator Functions: Using ``yield``\n", 377 | "We saw in the previous section that list comprehensions are best used to create relatively simple lists, while using a normal ``for`` loop can be better in more complicated situations.\n", 378 | "The same is true of generator expressions: we can make more complicated generators using *generator functions*, which make use of the ``yield`` statement.\n", 379 | "\n", 380 | "Here we have two ways of constructing the same list:" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 13, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]\n", 393 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]\n" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "L1 = [n ** 2 for n in range(12)]\n", 399 | "\n", 400 | "L2 = []\n", 401 | "for n in range(12):\n", 402 | " L2.append(n ** 2)\n", 403 | "\n", 404 | "print(L1)\n", 405 | "print(L2)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "Similarly, here we have two ways of constructing equivalent generators:" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 14, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "name": "stdout", 422 | "output_type": "stream", 423 | "text": [ 424 | "0 1 4 9 16 25 36 49 64 81 100 121\n", 425 | "0 1 4 9 16 25 36 49 64 81 100 121\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "G1 = (n ** 2 for n in range(12))\n", 431 | "\n", 432 | "def gen():\n", 433 | " for n in range(12):\n", 434 | " yield n ** 2\n", 435 | "\n", 436 | "G2 = gen()\n", 437 | "print(*G1)\n", 438 | "print(*G2)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "A generator function is a function that, rather than using ``return`` to return a value once, uses ``yield`` to yield a (potentially infinite) sequence of values.\n", 446 | "Just as in generator expressions, the state of the generator is preserved between partial iterations, but if we want a fresh copy of the generator we can simply call the function again." 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": {}, 452 | "source": [ 453 | "## Example: Prime Number Generator\n", 454 | "Here I'll show my favorite example of a generator function: a function to generate an unbounded series of prime numbers.\n", 455 | "A classic algorithm for this is the *Sieve of Eratosthenes*, which works something like this:" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 15, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "name": "stdout", 465 | "output_type": "stream", 466 | "text": [ 467 | "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "# Generate a list of candidates\n", 473 | "L = [n for n in range(2, 40)]\n", 474 | "print(L)" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 16, 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "name": "stdout", 484 | "output_type": "stream", 485 | "text": [ 486 | "[2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "# Remove all multiples of the first value\n", 492 | "L = [n for n in L if n == L[0] or n % L[0] > 0]\n", 493 | "print(L)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 17, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37]\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "# Remove all multiples of the second value\n", 511 | "L = [n for n in L if n == L[1] or n % L[1] > 0]\n", 512 | "print(L)" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 18, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "name": "stdout", 522 | "output_type": "stream", 523 | "text": [ 524 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "# Remove all multiples of the third value\n", 530 | "L = [n for n in L if n == L[2] or n % L[2] > 0]\n", 531 | "print(L)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "markdown", 536 | "metadata": {}, 537 | "source": [ 538 | "If we repeat this procedure enough times on a large enough list, we can generate as many primes as we wish.\n", 539 | "\n", 540 | "Let's encapsulate this logic in a generator function:" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 19, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "name": "stdout", 550 | "output_type": "stream", 551 | "text": [ 552 | "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97\n" 553 | ] 554 | } 555 | ], 556 | "source": [ 557 | "def gen_primes(N):\n", 558 | " \"\"\"Generate primes up to N\"\"\"\n", 559 | " primes = set()\n", 560 | " for n in range(2, N):\n", 561 | " if all(n % p > 0 for p in primes):\n", 562 | " primes.add(n)\n", 563 | " yield n\n", 564 | "\n", 565 | "print(*gen_primes(100))" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "That's all there is to it!\n", 573 | "While this is certainly not the most computationally efficient implementation of the Sieve of Eratosthenes, it illustrates how convenient the generator function syntax can be for building more complicated sequences." 574 | ] 575 | } 576 | ], 577 | "metadata": { 578 | "kernelspec": { 579 | "display_name": "Python 3", 580 | "language": "python", 581 | "name": "python3" 582 | }, 583 | "language_info": { 584 | "codemirror_mode": { 585 | "name": "ipython", 586 | "version": 3 587 | }, 588 | "file_extension": ".py", 589 | "mimetype": "text/x-python", 590 | "name": "python", 591 | "nbconvert_exporter": "python", 592 | "pygments_lexer": "ipython3", 593 | "version": "3.7.2" 594 | } 595 | }, 596 | "nbformat": 4, 597 | "nbformat_minor": 2 598 | } 599 | -------------------------------------------------------------------------------- /00-Python-Basics/08-Modules-and-Packages.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Modules and Packages" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "One feature of Python that makes it useful for a wide range of tasks is the fact that it comes \"batteries included\" – that is, the Python standard library contains useful tools for a wide range of tasks.\n", 15 | "On top of this, there is a broad ecosystem of third-party tools and packages that offer more specialized functionality.\n", 16 | "Here we'll take a look at importing standard library modules, tools for installing third-party modules, and a description of how you can make your own modules." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Loading Modules: the ``import`` Statement\n", 24 | "\n", 25 | "For loading built-in and third-party modules, Python provides the ``import`` statement.\n", 26 | "There are a few ways to use the statement, which we will mention briefly here, from most recommended to least recommended." 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "### Explicit module import\n", 34 | "\n", 35 | "Explicit import of a module preserves the module's content in a namespace.\n", 36 | "The namespace is then used to refer to its contents with a \"``.``\" between them.\n", 37 | "For example, here we'll import the built-in ``math`` module and compute the cosine of pi:" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "-1.0" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "import math\n", 58 | "math.cos(math.pi)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### Explicit module import by alias\n", 66 | "\n", 67 | "For longer module names, it's not convenient to use the full module name each time you access some element.\n", 68 | "For this reason, we'll commonly use the \"``import ... as ...``\" pattern to create a shorter alias for the namespace.\n", 69 | "For example, the NumPy (Numerical Python) package, a popular third-party package useful for data science, is by convention imported under the alias ``np``:" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 2, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "-1.0" 81 | ] 82 | }, 83 | "execution_count": 2, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "import numpy as np\n", 90 | "np.cos(np.pi)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### Explicit import of module contents\n", 98 | "\n", 99 | "Sometimes rather than importing the module namespace, you would just like to import a few particular items from the module.\n", 100 | "This can be done with the \"``from ... import ...``\" pattern.\n", 101 | "For example, we can import just the ``cos`` function and the ``pi`` constant from the ``math`` module:" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 3, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "-1.0" 113 | ] 114 | }, 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "from math import cos, pi\n", 122 | "cos(pi)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Implicit import of module contents\n", 130 | "\n", 131 | "Finally, it is sometimes useful to import the entirety of the module contents into the local namespace.\n", 132 | "This can be done with the \"``from ... import *``\" pattern:" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 4, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "1.0" 144 | ] 145 | }, 146 | "execution_count": 4, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "from math import *\n", 153 | "sin(pi) ** 2 + cos(pi) ** 2" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "This pattern should be used sparingly, if at all.\n", 161 | "The problem is that such imports can sometimes overwrite function names that you do not intend to overwrite, and the implicitness of the statement makes it difficult to determine what has changed.\n", 162 | "\n", 163 | "For example, Python has a built-in ``sum`` function that can be used for various operations:" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 5, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "Help on built-in function sum in module builtins:\n", 176 | "\n", 177 | "sum(iterable, start=0, /)\n", 178 | " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", 179 | " \n", 180 | " When the iterable is empty, return the start value.\n", 181 | " This function is intended specifically for use with numeric values and may\n", 182 | " reject non-numeric types.\n", 183 | "\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "help(sum)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "We can use this to compute the sum of a sequence, starting with a certain value (here, we'll start with ``-1``):" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 6, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "9" 207 | ] 208 | }, 209 | "execution_count": 6, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "sum(range(5), -1)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Now observe what happens if we make the *exact same function call* after importing ``*`` from ``numpy``:" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 7, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "from numpy import *" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 8, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "10" 243 | ] 244 | }, 245 | "execution_count": 8, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "sum(range(5), -1)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "The result is off by one!\n", 259 | "The reason for this is that the ``import *`` statement *replaces* the built-in ``sum`` function with the ``numpy.sum`` function, which has a different call signature: in the former, we're summing ``range(5)`` starting at ``-1``; in the latter, we're summing ``range(5)`` along the last axis (indicated by ``-1``).\n", 260 | "This is the type of situation that may arise if care is not taken when using \"``import *``\" – for this reason, it is best to avoid this unless you know exactly what you are doing." 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "## Importing from Python's Standard Library\n", 268 | "\n", 269 | "Python's standard library contains many useful built-in modules, which you can read about fully in [Python's documentation](https://docs.python.org/3/library/).\n", 270 | "Any of these can be imported with the ``import`` statement, and then explored using the help function seen in the previous section.\n", 271 | "Here is an extremely incomplete list of some of the modules you might wish to explore and learn about:\n", 272 | "\n", 273 | "- ``os`` and ``sys``: Tools for interfacing with the operating system, including navigating file directory structures and executing shell commands\n", 274 | "- ``math`` and ``cmath``: Mathematical functions and operations on real and complex numbers\n", 275 | "- ``itertools``: Tools for constructing and interacting with iterators and generators\n", 276 | "- ``functools``: Tools that assist with functional programming\n", 277 | "- ``random``: Tools for generating pseudorandom numbers\n", 278 | "- ``pickle``: Tools for object persistence: saving objects to and loading objects from disk\n", 279 | "- ``json`` and ``csv``: Tools for reading JSON-formatted and CSV-formatted files.\n", 280 | "- ``urllib``: Tools for doing HTTP and other web requests.\n", 281 | "\n", 282 | "You can find information on these, and many more, in the Python standard library documentation: https://docs.python.org/3/library/." 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "## Importing from Third-Party Modules\n", 290 | "\n", 291 | "One of the things that makes Python useful, especially within the world of data science, is its ecosystem of third-party modules.\n", 292 | "These can be imported just as the built-in modules, but first the modules must be installed on your system. To install common third-party packages one can use one of the following approaches:\n", 293 | "\n", 294 | "### Conda\n", 295 | "Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. \n", 296 | "For example, if you'd like to install the ``numpy`` package, all that is required is to type the following at the command line:\n", 297 | "```\n", 298 | "$ conda install numpy\n", 299 | "```\n", 300 | "\n", 301 | "As we saw in the first class you can access **Conda** by installing Miniconda. [Miniconda](http://conda.pydata.org/miniconda.html) gives you Python interpreter itself, along with a command-line tool called ``conda`` which operates as a cross-platform package manager geared toward Python packages\n", 302 | "\n", 303 | "### pip\n", 304 | "Another standard registry for such modules is the Python Package Index (*PyPI* for short), found on the Web at http://pypi.python.org/.\n", 305 | "For convenience, Python comes with a program called ``pip`` (a recursive acronym meaning \"pip installs packages\"), which will automatically fetch packages released and listed on PyPI (if you use Python version 2, ``pip`` must be installed separately).\n", 306 | "For example, if you'd like to install the ``numpy`` package, all that is required is to type the following at the command line:\n", 307 | "```\n", 308 | "$ pip install numpy\n", 309 | "```\n", 310 | "The source code for the package will be automatically downloaded from the PyPI repository, and the package installed in the standard Python path (assuming you have permission to do so on the computer you're using).\n", 311 | "\n", 312 | "For more information about PyPI and the ``pip`` installer, refer to the documentation at http://pypi.python.org/." 313 | ] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 3", 319 | "language": "python", 320 | "name": "python3" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 3 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython3", 332 | "version": "3.7.2" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 2 337 | } 338 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-01-Built-in-Scalar-Types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise\n", 8 | "Capitalize the following string:" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "text1 = \"python\"" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "'Python'" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "# Your answer goes here\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "Convert the following string to lowercase:" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "text2 = \"Python-Bootcamp\"" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "'python-bootcamp'" 65 | ] 66 | }, 67 | "execution_count": 4, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Your answer goes here\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Did these methods change the original string? If not, how can you ensure that your changes are saved?" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "python-bootcamp\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "# Your answer goes here\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "How can you access the third character of `text2`?" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 6, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "'T'" 116 | ] 117 | }, 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "# Your answer goes here\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Print the second half of text2:\n", 132 | "\n", 133 | "Hint 1: strings have a method called `split()`. This method will split the string into pieces based on the delimiter provided. \n", 134 | "\n", 135 | "Hint 2: One way to access the help documentation in notebook is by pressing Shift + Tab while the cursor is on the word. Try typing `text2.split` and then hit Shift + Tab." 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "'bootcamp'" 147 | ] 148 | }, 149 | "execution_count": 7, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "# Your answer goes here\n" 156 | ] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.7.2" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 2 180 | } 181 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-02-Built-in-Data-Structures.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a Python program which accepts a sequence of comma-separated numbers from user and generates a list and a tuple with those numbers.\n", 9 | "\n", 10 | "Sample data: 4,6,9\n", 11 | "\n", 12 | "Output: \n", 13 | "\n", 14 | "List : ['4', '6', '9']\n", 15 | "\n", 16 | "Tuple : ('4', '6', '9')\n", 17 | "\n", 18 | "Hint: you can use `input()` function to get user's input through keyboard. The input values can be stored to a variable. E.g.: \n", 19 | "\n", 20 | "```values = input(\"Input some comma seprated numbers : \")```" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Input some comma seprated numbers : 4,6,9\n", 33 | "List : ['4', '6', '9']\n", 34 | "Tuple : ('4', '6', '9')\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# Your answer goes here\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "What is the size of the tuple?" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "3" 58 | ] 59 | }, 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "# Your answer goes here\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "Add a new element '999' to the end of the list and print it:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "['4', '6', '9', '999']\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# Your answer goes here\n" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Can you do the same thing with the tuple?" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# Your answer goes here\n" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "# Exercise 2\n", 114 | "Given this nested list, use indexing to grab the word \"hello\"" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "hello\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "# Your answer goes here\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "# Exercise 3\n", 148 | "Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 8, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "hello\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "# Your answer goes here\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "# Exercise 4\n", 182 | "Write a Python program to accept a filename from the user and print the extension of that.\n", 183 | "\n", 184 | "Example:\n", 185 | "\n", 186 | "Input the Filename: my.file.csv\n", 187 | "\n", 188 | "The extension of the file is : 'csv'" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "Input the Filename: my.file.csv\n", 201 | "The extension of the file is : 'csv'\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# Your answer goes here\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "# Exercise 5\n", 214 | "Display the first and last colors from the following list:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 10, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "color_list = [\"Red\",\"Green\",\"White\" ,\"Black\"]" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 11, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "'Red Black'" 235 | ] 236 | }, 237 | "execution_count": 11, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "# Your answer goes here\n" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "# Exercise 6\n", 251 | "Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.\n", 252 | "\n", 253 | "Example: n = 10 ==> Output: 10+1010+101010 = 102030" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 12, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "Input an integer : 10\n", 266 | "102030\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# Your answer goes here\n" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "# Exercise 7\n", 279 | "Write a Python program that can detect whether a sting appears in another string. Make it case insensitive.\n", 280 | "\n", 281 | "Example:\n", 282 | "\n", 283 | "a = \"Cat\"\n", 284 | "\n", 285 | "b = \"Do cats dream?\"\n", 286 | "\n", 287 | "Does a appear in b? ==> Output: True|" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 13, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "True" 299 | ] 300 | }, 301 | "execution_count": 13, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "# Your answer goes here\n" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "How many string characters are in b? (white space counts)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 14, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "14" 326 | ] 327 | }, 328 | "execution_count": 14, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "# Your answer goes here\n" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "# Exercise 8\n", 342 | "Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).\n", 343 | "\n", 344 | "Try to find an answer online.\n", 345 | "\n", 346 | "Example input: print\n", 347 | "\n", 348 | "```\n", 349 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 350 | "\n", 351 | "Prints the values to a stream, or to sys.stdout by default.\n", 352 | "Optional keyword arguments:\n", 353 | "file: a file-like object (stream); defaults to the current sys.stdout.\n", 354 | "sep: string inserted between values, default a space.\n", 355 | "end: string appended after the last value, default a newline.\n", 356 | "flush: whether to forcibly flush the stream.\n", 357 | "```" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 15, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 370 | "\n", 371 | "Prints the values to a stream, or to sys.stdout by default.\n", 372 | "Optional keyword arguments:\n", 373 | "file: a file-like object (stream); defaults to the current sys.stdout.\n", 374 | "sep: string inserted between values, default a space.\n", 375 | "end: string appended after the last value, default a newline.\n", 376 | "flush: whether to forcibly flush the stream.\n" 377 | ] 378 | } 379 | ], 380 | "source": [ 381 | "# Your answer goes here\n" 382 | ] 383 | } 384 | ], 385 | "metadata": { 386 | "kernelspec": { 387 | "display_name": "Python 3", 388 | "language": "python", 389 | "name": "python3" 390 | }, 391 | "language_info": { 392 | "codemirror_mode": { 393 | "name": "ipython", 394 | "version": 3 395 | }, 396 | "file_extension": ".py", 397 | "mimetype": "text/x-python", 398 | "name": "python", 399 | "nbconvert_exporter": "python", 400 | "pygments_lexer": "ipython3", 401 | "version": "3.7.2" 402 | } 403 | }, 404 | "nbformat": 4, 405 | "nbformat_minor": 2 406 | } 407 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-03-Control-Flow-Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a program to sum all the items in a list.\n", 9 | "\n", 10 | "L = [1, 6, 10] ==> Output: 17" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "17\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "# Your answer goes here\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "# Exercise 2\n", 35 | "Find all the numbers between 1800 and 1995 (both included) that are divisible by 7 and multiple of 5.\n", 36 | "\n", 37 | "How many are there?" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "[1820, 1855, 1890, 1925, 1960, 1995]\n", 50 | "6\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "# Your answer goes here\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "# Exercise 3\n", 63 | "Write a program that prints each item and its corresponding type from the following list." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "datalist = [1452, 11.23, 1+2j, True, 'Python', (0, -1), [5, 12], {\"class\":'V', \"section\":'A'}, {1,2,3}]" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Type of 1452 is: \n", 85 | "Type of 11.23 is: \n", 86 | "Type of (1+2j) is: \n", 87 | "Type of True is: \n", 88 | "Type of Python is: \n", 89 | "Type of (0, -1) is: \n", 90 | "Type of [5, 12] is: \n", 91 | "Type of {'class': 'V', 'section': 'A'} is: \n", 92 | "Type of {1, 2, 3} is: \n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "# Your answer goes here\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "# Exercise 4\n", 105 | "Write a program which iterates the integers from 1 to 20. For multiples of three print \"Fizz\" instead of the number and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".\n", 106 | "\n", 107 | "Sample Output : \n", 108 | "\n", 109 | "1\n", 110 | "\n", 111 | "2\n", 112 | "\n", 113 | "fizz\n", 114 | "\n", 115 | "4 \n", 116 | "\n", 117 | "buzz" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 5, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "1\n", 130 | "2\n", 131 | "fizz\n", 132 | "4\n", 133 | "buzz\n", 134 | "fizz\n", 135 | "7\n", 136 | "8\n", 137 | "fizz\n", 138 | "buzz\n", 139 | "11\n", 140 | "fizz\n", 141 | "13\n", 142 | "14\n", 143 | "fizzbuzz\n", 144 | "16\n", 145 | "17\n", 146 | "fizz\n", 147 | "19\n", 148 | "buzz\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "# Your answer goes here\n" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "# Exercise 5\n", 161 | "Write a program to convert temperatures to and from celsius, fahrenheit.\n", 162 | "\n", 163 | "[ Formula : c = (f - 32) * 5/9 [ where c = temperature in celsius and f = temperature in fahrenheit ] " 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 6, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "Input the temperature you like to convert? (e.g., 45F, 102C, etc.) : 70f\n", 176 | "The temperature in Celsius is 21 degrees.\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# Your answer goes here\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "# Exercise 6\n", 189 | "Convert the following for loop to a one-liner using list comprehension:" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 1, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "numbers = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 2, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]" 210 | ] 211 | }, 212 | "execution_count": 2, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "L = []\n", 219 | "for n in numbers:\n", 220 | " L.append(n ** 2)\n", 221 | "L" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 3, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]" 233 | ] 234 | }, 235 | "execution_count": 3, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "# Your answer goes here\n" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "# Exercise 7\n", 249 | "Convert the following for loop to a one-liner using list comprehension:" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 4, 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "numbers = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 5, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "[0, 1, 4, 9, 16, 25]" 270 | ] 271 | }, 272 | "execution_count": 5, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "L = []\n", 279 | "for val in numbers:\n", 280 | " if val <= 0:\n", 281 | " L.append(val ** 2)\n", 282 | "L" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 6, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "[0, 1, 4, 9, 16, 25]" 294 | ] 295 | }, 296 | "execution_count": 6, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "# Your answer goes here\n" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "# Exercise 8\n", 310 | "Using a list comprehension, create a new list called `newlist` out of the list `numbers`, which contains only the positive numbers from the list, as integers." 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 7, 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [ 319 | "numbers = [4.2, -0.01, 1.1, -1.5, -2.2, 5.4, -3.5, 3.7, 2.8, -4.3, -5]" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 8, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "data": { 329 | "text/plain": [ 330 | "[4, 1, 5, 3, 2]" 331 | ] 332 | }, 333 | "execution_count": 8, 334 | "metadata": {}, 335 | "output_type": "execute_result" 336 | } 337 | ], 338 | "source": [ 339 | "# Your answer goes here\n" 340 | ] 341 | } 342 | ], 343 | "metadata": { 344 | "kernelspec": { 345 | "display_name": "Python 3", 346 | "language": "python", 347 | "name": "python3" 348 | }, 349 | "language_info": { 350 | "codemirror_mode": { 351 | "name": "ipython", 352 | "version": 3 353 | }, 354 | "file_extension": ".py", 355 | "mimetype": "text/x-python", 356 | "name": "python", 357 | "nbconvert_exporter": "python", 358 | "pygments_lexer": "ipython3", 359 | "version": "3.7.2" 360 | } 361 | }, 362 | "nbformat": 4, 363 | "nbformat_minor": 2 364 | } 365 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-04-Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a function that sums all the numbers in a list and name it `list_sum`." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "# Your answer goes here\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "L = [8, 2, 13, 0, 7]\n", 27 | "list_sum(L)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "# Exercise 2\n", 35 | "Write a function to reverse a string." 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "# Your answer goes here\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "s = \"Hello World!\"\n", 54 | "str_reverse(s)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "# Exercise 3\n", 62 | "Write a function that takes a list and returns a new list with unique elements of the first list." 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 5, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# Your answer goes here\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 6, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "[0, 'dog', 4, 'cat']" 83 | ] 84 | }, 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "L = ['dog',0,4,'cat','dog',0,'dog','cat',4,0]\n", 92 | "unique_elements(L)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "# Exercise 4\n", 100 | "Write a function to print all the even numbers from a given list." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Your answer goes here\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 8, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "[2, 4, 6, 8, 10]" 121 | ] 122 | }, 123 | "execution_count": 8, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 130 | "is_even_num(L)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "# Exercise 5\n", 138 | "Write a function that takes a number as a parameter and checks the number is prime or not.\n", 139 | "\n", 140 | "Note : A prime number is a natural number greater than 1 and that has no positive divisors other than 1 and itself." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 9, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# Your answer goes here\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 10, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "False" 161 | ] 162 | }, 163 | "execution_count": 10, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "test_prime(9)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 11, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "True" 181 | ] 182 | }, 183 | "execution_count": 11, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "test_prime(15487271)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "# Exercise 6\n", 197 | "Write a function that takes an unknown number of parameters, converts them to string (if they are not), capitalizes them and prints one string separated by a space `\" \"`. " 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 12, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# Your answer goes here\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 13, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "2 Cats Are Sleeping Under The Blanket! " 219 | ] 220 | } 221 | ], 222 | "source": [ 223 | "print_unknown(2, 'cats', 'are', 'sleeping', 'under', 'the', 'blanket!')" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "# Exercise 7\n", 231 | "Use lambda expressions and `filter()` function to filter words from a list that don't start with the letter 's'. For example:\n", 232 | "\n", 233 | " seq = ['soup','dog','salad','cat','great']\n", 234 | "\n", 235 | "**should be filtered down to:**\n", 236 | "\n", 237 | " ['soup','salad']" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 14, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "seq = ['soup','dog','salad','cat','great']" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 15, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "['soup', 'salad']" 258 | ] 259 | }, 260 | "execution_count": 15, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "# Your answer goes here\n" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "kernelspec": { 272 | "display_name": "Python 3", 273 | "language": "python", 274 | "name": "python3" 275 | }, 276 | "language_info": { 277 | "codemirror_mode": { 278 | "name": "ipython", 279 | "version": 3 280 | }, 281 | "file_extension": ".py", 282 | "mimetype": "text/x-python", 283 | "name": "python", 284 | "nbconvert_exporter": "python", 285 | "pygments_lexer": "ipython3", 286 | "version": "3.7.2" 287 | } 288 | }, 289 | "nbformat": 4, 290 | "nbformat_minor": 2 291 | } 292 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-05-Errors-and-Debugging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise\n", 8 | "Write a program that asks for users input and if the input is not a valid integer repeats the question until a valid input is provided. Then print the integer input.\n", 9 | "\n", 10 | "Hint: You can use a format similar to `x = int(input(\"Please enter a number: \"))`. `int()` function will try to convert the user input (which is a string by default, i.e., even input 1 will be proceeded as '1') to an int. See what kind of error you get by providing bad inputs and use that for your exception (`except`) argument.\n", 11 | "\n", 12 | "For instance in the following cell I have provided 3.14 which results in a \"ValueError\" error:" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdin", 22 | "output_type": "stream", 23 | "text": [ 24 | "Please enter a number: 3.14\n" 25 | ] 26 | }, 27 | { 28 | "ename": "ValueError", 29 | "evalue": "invalid literal for int() with base 10: '3.14'", 30 | "output_type": "error", 31 | "traceback": [ 32 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 33 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 34 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please enter a number: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 35 | "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '3.14'" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "x = int(input(\"Please enter a number: \"))" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Use a while loop similar to the following:\n", 48 | "\n", 49 | "```python\n", 50 | "while True:\n", 51 | " try:\n", 52 | " # your code here\n", 53 | " break\n", 54 | " except ValueError:\n", 55 | " # your code here\n", 56 | "\n", 57 | "print(\"x:\", x)\n", 58 | "```" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 1, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdin", 68 | "output_type": "stream", 69 | "text": [ 70 | "Please enter a number: 3.14\n" 71 | ] 72 | }, 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "Oops! That was not a valid number. Try again...\n" 78 | ] 79 | }, 80 | { 81 | "name": "stdin", 82 | "output_type": "stream", 83 | "text": [ 84 | "Please enter a number: Hello\n" 85 | ] 86 | }, 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "Oops! That was not a valid number. Try again...\n" 92 | ] 93 | }, 94 | { 95 | "name": "stdin", 96 | "output_type": "stream", 97 | "text": [ 98 | "Please enter a number: 20\n" 99 | ] 100 | }, 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "x: 20\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "# Your answer goes here\n" 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.7.2" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 2 135 | } 136 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-06-Iterators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a function that takes a list and prints a corresponding enumerate object of this list, i.e., index and value of each element. You can do this by using the function `enumerate(L)`.\n", 9 | "\n", 10 | "Make sure to include a docstring for this function. Name this function `print_enumerate`." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "L = [4, 'A', 6, 0, 'Z']" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# Your answer goes here\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "0 4\n", 41 | "1 A\n", 42 | "2 6\n", 43 | "3 0\n", 44 | "4 Z\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print_enumerate(L)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "# Exercise 2\n", 57 | "Write a function that accepts a list of words and returns a list with only the ones that are capitalized. \n", 58 | "\n", 59 | "Use `filter` in conjunction with a for loop. You can use `isupper()` to identify whether a single letter is capitalized or not.\n", 60 | "\n", 61 | "Include a docstring." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "R = ['computers', 'month', 'English', 'digital', 'Questrom']" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# Your answer goes here\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 6, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "['English', 'Questrom']" 91 | ] 92 | }, 93 | "execution_count": 6, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "filter_capitalized(R)" 100 | ] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.7.2" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 2 124 | } 125 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-07-Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Using generator expression create a generator that generates values that are multiples of 3, then use this generator in a list comprehension to subset a list that gives the elements with indices that are multiples of 3." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "L = [3, 37, 72, 3, 75, 44, 3, 8, 59, 3, 50, 15, 3, 10, 29, 3, 98, 39, 3, 7, 4, 3]" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "[3, 3, 3, 3, 3, 3, 3, 3]" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "# Your answer goes here\n" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.7.2" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 2 62 | } 63 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-08-Modules-and-Packages.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "From `math` package import function `ceil()`. `ceil(x)` is equivalent of ceiling function in mathematics where it gives the closest integer bigger than x." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 8, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "# Your answer goes here\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 10, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "6" 29 | ] 30 | }, 31 | "execution_count": 10, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "ceil(5.1)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 12, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "-1" 49 | ] 50 | }, 51 | "execution_count": 12, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "ceil(-1.5)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Note: If you are getting an error in evaluating the above expressions it could be because you haven't explicitly imported the module content." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "# Exercise 2\n", 72 | "Import package `peewee`." 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 17, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "ename": "ModuleNotFoundError", 82 | "evalue": "No module named 'peewee'", 83 | "output_type": "error", 84 | "traceback": [ 85 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 86 | "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 87 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your answer goes here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpeewee\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 88 | "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'peewee'" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "# Your answer goes here\n" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "You will most likely get an error indicating that this packages does not exist. Try to install it by\n", 101 | "\n", 102 | "`conda install peewee` or `pip install peewee`. Note that these commands are for command line.\n", 103 | "\n", 104 | "Hint: You can use a `!` prior to any command in a Jupyter cell to make that line a command-line command." 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 16, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "Solving environment: failed\n", 117 | "\n", 118 | "PackagesNotFoundError: The following packages are not available from current channels:\n", 119 | "\n", 120 | " - peewee\n", 121 | "\n", 122 | "Current channels:\n", 123 | "\n", 124 | " - https://repo.anaconda.com/pkgs/main/osx-64\n", 125 | " - https://repo.anaconda.com/pkgs/main/noarch\n", 126 | " - https://repo.anaconda.com/pkgs/free/osx-64\n", 127 | " - https://repo.anaconda.com/pkgs/free/noarch\n", 128 | " - https://repo.anaconda.com/pkgs/r/osx-64\n", 129 | " - https://repo.anaconda.com/pkgs/r/noarch\n", 130 | " - https://repo.anaconda.com/pkgs/pro/osx-64\n", 131 | " - https://repo.anaconda.com/pkgs/pro/noarch\n", 132 | "\n", 133 | "To search for alternate channels that may provide the conda package you're\n", 134 | "looking for, navigate to\n", 135 | "\n", 136 | " https://anaconda.org\n", 137 | "\n", 138 | "and use the search bar at the top of the page.\n", 139 | "\n", 140 | "\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "# Your answer goes here\n" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "After a successful installation you should be able to import it." 153 | ] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-09-Strings-and-Regular-Expressions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Given the current date and dictionary below format the following string to produce custom message:\n", 9 | "\n", 10 | "Hello xxx xxx. As of xxx, your current balance is $xxx." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import datetime\n", 20 | "now = datetime.datetime.now()\n", 21 | "current_date = now.strftime(\"%Y-%m-%d\")\n", 22 | "\n", 23 | "d = {\"first\":\"John\", \"last\":\"Smith\", \"balance\":120.55}" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello John Smith. As of 2019-02-03, your current balance is $120.55.\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "# Your answer goes here\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "# Exercise 2\n", 48 | "Using `center()`, can you produce a similar output?" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "A = 'This is a centered text'\n", 58 | "B = 'x = 60'\n", 59 | "C = 'E = m c ^ 2'" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | " This is a centered text \n", 72 | " x = 60 \n", 73 | " E = m c ^ 2 \n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "# Your answer goes here\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "# Exercise 3\n", 86 | "In the following string replace \"skills\" with \"experience\"." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "text = 'Put your skills to use!'" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "'Put your experience to use!'" 107 | ] 108 | }, 109 | "execution_count": 6, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "# Your answer goes here\n" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.7.2" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 2 140 | } 141 | -------------------------------------------------------------------------------- /01-Introduction/04-Magic-Commands.ipynb: -------------------------------------------------------------------------------- 1 | {"cells":[{"cell_type":"markdown","metadata":{"id":"M7PudKu51NxW"},"source":["# IPython Magic Commands"]},{"cell_type":"markdown","metadata":{"id":"cHl-Sr0E1NxX"},"source":["Here we'll begin discussing some of the enhancements that IPython adds on top of the normal Python syntax.\n","These are known in IPython as *magic commands*, and are prefixed by the ``%`` character.\n","These magic commands are designed to succinctly solve various common problems in standard data analysis.\n","Magic commands come in two flavors: *line magics*, which are denoted by a single ``%`` prefix and operate on a single line of input, and *cell magics*, which are denoted by a double ``%%`` prefix and operate on multiple lines of input.\n","We'll demonstrate and discuss a few brief examples here, and come back to more focused discussion of several useful magic commands later in the chapter."]},{"cell_type":"markdown","metadata":{"id":"U-ahcWZQ1NxX"},"source":["## Running External Code: ``%run``\n","As you begin developing more extensive code, you will likely find yourself working in both IPython for interactive exploration, as well as a text editor to store code that you want to reuse.\n","Rather than running this code in a new window, it can be convenient to run it within your IPython session.\n","This can be done with the ``%run`` magic.\n","\n","For example, imagine you've created a ``myscript.py`` file with the following contents:\n","\n","```python\n","#-------------------------------------\n","# file: myscript.py\n","\n","def square(x):\n"," \"\"\"square a number\"\"\"\n"," return x ** 2\n","\n","for N in range(1, 4):\n"," print(N, \"squared is\", square(N))\n","```\n","\n","You can execute this from your IPython session as follows:\n","\n","```ipython\n","In [6]: %run myscript.py\n","1 squared is 1\n","2 squared is 4\n","3 squared is 9\n","```\n","\n","Note also that after you've run this script, any functions defined within it are available for use in your IPython session:\n","\n","```ipython\n","In [7]: square(5)\n","Out[7]: 25\n","```\n","\n","There are several options to fine-tune how your code is run; you can see the documentation in the normal way, by typing **``%run?``** in the IPython interpreter."]},{"cell_type":"markdown","metadata":{"id":"ro2_qcCo1NxX"},"source":["## Timing Code Execution: ``%timeit``\n","Another example of a useful magic function is ``%timeit``, which will automatically determine the execution time of the single-line Python statement that follows it.\n","For example, we may want to check the performance of a list comprehension:\n","\n","```ipython\n","In [8]: %timeit L = [n ** 2 for n in range(1000)]\n","1000 loops, best of 3: 325 µs per loop\n","```\n","\n","The benefit of ``%timeit`` is that for short commands it will automatically perform multiple runs in order to attain more robust results.\n","For multi line statements, adding a second ``%`` sign will turn this into a cell magic that can handle multiple lines of input.\n","For example, here's the equivalent construction with a ``for``-loop:\n","\n","```ipython\n","In [9]: %%timeit\n"," ...: L = []\n"," ...: for n in range(1000):\n"," ...: L.append(n ** 2)\n"," ...:\n","1000 loops, best of 3: 373 µs per loop\n","```\n","\n","We can immediately see that list comprehensions are about 10% faster than the equivalent ``for``-loop construction in this case."]},{"cell_type":"markdown","metadata":{"id":"mJIWZGHi1NxX"},"source":["## Help on Magic Functions: ``?``, ``%magic``, and ``%lsmagic``\n","\n","Like normal Python functions, IPython magic functions have docstrings, and this useful\n","documentation can be accessed in the standard manner.\n","So, for example, to read the documentation of the ``%timeit`` magic simply type this:\n","\n","```ipython\n","In [10]: %timeit?\n","```\n","\n","Documentation for other functions can be accessed similarly.\n","To access a general description of available magic functions, including some examples, you can type this:\n","\n","```ipython\n","In [11]: %magic\n","```\n","\n","For a quick and simple list of all available magic functions, type this:\n","\n","```ipython\n","In [12]: %lsmagic\n","```"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.2"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":0} -------------------------------------------------------------------------------- /01-Introduction/Exercise-01-Introduction.ipynb: -------------------------------------------------------------------------------- 1 | {"cells":[{"cell_type":"markdown","id":"cb8c7567","metadata":{"id":"cb8c7567"},"source":["\n","# Getting Started with Google Colaboratory (Colab)\n","\n","In this exercise, we will get familiar with Google Colaboratory (Colab), an online platform for writing and running Python code. Follow these steps to complete the exercise and learn basic operations in Colab.\n","\n","## Step 1: Open Google Colab\n","Visit [Google Colab](https://colab.research.google.com/) and sign in with your BU account.\n","\n","## Step 2: Create a New Notebook\n","Click on \"File\" -> \"New Notebook\" to create a new Colab notebook.\n","\n","## Step 3: Add a Markdown Cell\n","Click on \"+ Text\" to add a new Markdown cell. Write a description similar to the following and press `Shift + Enter` to render it:\n","\n","```markdown\n","# Python Version\n","The following code provides the version of the Python that this notebook is running on:\n","```\n","\n","## Step 4: Add a Code Cell to Display Python Version\n","Click on \"+ Code\" to add a new code cell and type the following code. Run the cell by pressing `Shift +\n","\n"," Enter` to display the Python version.\n"]},{"cell_type":"code","execution_count":1,"id":"f89d6545","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"f89d6545","executionInfo":{"status":"ok","timestamp":1725307736961,"user_tz":240,"elapsed":291,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"57cd7a2a-cc2c-402d-e6a8-ebec8c3e9482"},"outputs":[{"output_type":"stream","name":"stdout","text":["3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]\n"]}],"source":["import sys\n","print(sys.version)"]},{"cell_type":"markdown","id":"03158492","metadata":{"id":"03158492"},"source":["\n","## Step 5: Rename the Notebook\n","Click on the notebook name at the top left and give it a meaningful name, such as \"Exercise 1 - Colab Introduction\".\n","\n","## Step 6: Install and Import a Library\n","Add a new code cell and write code to install a new library using `pip`. After installing, import the library and display its version:\n","\n","```python\n","!pip install numpy\n","import numpy as np\n","print(\"NumPy version:\", np.__version__)\n","```\n","\n","## Step 7: Save the Notebook to Google Drive\n","Click on \"File\" -> \"Save a Copy in Drive\" to save your notebook in your Google Drive for future reference.\n"]},{"cell_type":"code","source":[],"metadata":{"id":"7ZPPFDjjW0EY"},"id":"7ZPPFDjjW0EY","execution_count":null,"outputs":[]}],"metadata":{"colab":{"provenance":[]},"language_info":{"name":"python"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"nbformat":4,"nbformat_minor":5} -------------------------------------------------------------------------------- /01-Introduction/Exercise-03-Python-Syntax-and-Semantics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.2" 21 | }, 22 | "colab": { 23 | "name": "Exercise-01-Python-Syntax-and-Semantics.ipynb", 24 | "provenance": [], 25 | "collapsed_sections": [] 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "Qz8NhXDlPRQz" 33 | }, 34 | "source": [ 35 | "# This cell is used to load the hints and solutions, if needed\n", 36 | "from IPython.display import Pretty as disp\n", 37 | "hint = 'https://raw.githubusercontent.com/soltaniehha/Business-Analytics/master/docs/hints/' # path to hints on GitHub" 38 | ], 39 | "execution_count": 1, 40 | "outputs": [] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": { 45 | "id": "Dgz80FppOMoX" 46 | }, 47 | "source": [ 48 | "# Exercise 1\n", 49 | "How can you improve the syntax used in the following code cells?\n", 50 | "\n", 51 | "Consider conventions, and code readability." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "metadata": { 57 | "id": "0ERoHwbIOMoc" 58 | }, 59 | "source": [ 60 | "x= 4+ 1" 61 | ], 62 | "execution_count": null, 63 | "outputs": [] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "metadata": { 68 | "id": "fsyiyWbjP0vQ" 69 | }, 70 | "source": [ 71 | "# HINT: Uncomment and execute the cell below to get help\n", 72 | "#disp(hint + '02-01-ex01-a-hint')" 73 | ], 74 | "execution_count": 7, 75 | "outputs": [] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "metadata": { 80 | "id": "aDi883v0PDTH" 81 | }, 82 | "source": [ 83 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 84 | "#disp(hint + '02-01-ex01-a')" 85 | ], 86 | "execution_count": 8, 87 | "outputs": [] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "metadata": { 92 | "id": "mHCVNBkrOMoc", 93 | "outputId": "0432c903-40d9-4a7a-81c8-2dd171ba1a6e" 94 | }, 95 | "source": [ 96 | "print(x); x = x + 1; print(x); x += 1; print(x)" 97 | ], 98 | "execution_count": null, 99 | "outputs": [ 100 | { 101 | "output_type": "stream", 102 | "text": [ 103 | "5\n", 104 | "6\n", 105 | "7\n" 106 | ], 107 | "name": "stdout" 108 | } 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "metadata": { 114 | "id": "T6-uLe86Pmes" 115 | }, 116 | "source": [ 117 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 118 | "#disp(hint + '02-01-ex01-b')" 119 | ], 120 | "execution_count": 10, 121 | "outputs": [] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "metadata": { 126 | "id": "LsF9qusFOMod", 127 | "outputId": "ebe49471-2a86-4af1-ac09-b1f460a5ebd6" 128 | }, 129 | "source": [ 130 | "for i in range(10):\n", 131 | " if i % 3 == 0 :#checks to see if i is a multiple of 3\n", 132 | " print(i, 'is a multiple of 3!')" 133 | ], 134 | "execution_count": null, 135 | "outputs": [ 136 | { 137 | "output_type": "stream", 138 | "text": [ 139 | "0 is a multiple of 3!\n", 140 | "3 is a multiple of 3!\n", 141 | "6 is a multiple of 3!\n", 142 | "9 is a multiple of 3!\n" 143 | ], 144 | "name": "stdout" 145 | } 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "id": "sSJ15iZ9QFgI" 152 | }, 153 | "source": [ 154 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 155 | "#disp(hint + '02-01-ex01-c')" 156 | ], 157 | "execution_count": null, 158 | "outputs": [] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "metadata": { 163 | "id": "RfRd1xUQOMod", 164 | "outputId": "6199dfbb-4dcd-4968-be01-126fc30fdb10" 165 | }, 166 | "source": [ 167 | "2*8+8/4-9+2*1-1" 168 | ], 169 | "execution_count": null, 170 | "outputs": [ 171 | { 172 | "output_type": "execute_result", 173 | "data": { 174 | "text/plain": [ 175 | "10.0" 176 | ] 177 | }, 178 | "metadata": { 179 | "tags": [] 180 | }, 181 | "execution_count": 4 182 | } 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "metadata": { 188 | "id": "i_eeAZNeQNuw" 189 | }, 190 | "source": [ 191 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 192 | "#disp(hint + '02-01-ex01-d')" 193 | ], 194 | "execution_count": null, 195 | "outputs": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "id": "H_kbXQrGOMoe" 201 | }, 202 | "source": [ 203 | "# Exercise 2\n", 204 | "Create a variable `savings` and give it value 200" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "metadata": { 210 | "id": "hlGLw_2EOMoe" 211 | }, 212 | "source": [ 213 | "# Your answer goes here\n" 214 | ], 215 | "execution_count": null, 216 | "outputs": [] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "metadata": { 221 | "id": "fYsFWqrlQgW5" 222 | }, 223 | "source": [ 224 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 225 | "#disp(hint + '02-01-ex02-a')" 226 | ], 227 | "execution_count": null, 228 | "outputs": [] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": { 233 | "id": "SlS2MEcCOMoe" 234 | }, 235 | "source": [ 236 | "Create a variable `factor` and assign it value 1.1" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "metadata": { 242 | "id": "Yp6mrZfbOMoe" 243 | }, 244 | "source": [ 245 | "# Your answer goes here\n" 246 | ], 247 | "execution_count": null, 248 | "outputs": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "metadata": { 253 | "id": "ts0YnFSEQovL" 254 | }, 255 | "source": [ 256 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 257 | "#disp(hint + '02-01-ex02-b')" 258 | ], 259 | "execution_count": null, 260 | "outputs": [] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": { 265 | "id": "zbI5XvEYOMof" 266 | }, 267 | "source": [ 268 | "Calculate `balance`:" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "metadata": { 274 | "id": "FBBxiSsJOMof" 275 | }, 276 | "source": [ 277 | "balance = savings * factor ** 5" 278 | ], 279 | "execution_count": null, 280 | "outputs": [] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": { 285 | "id": "Xru-33ZgOMof" 286 | }, 287 | "source": [ 288 | "Now print a statement that says something like \"Your account balance after 5 years would be: $xxx\" and use the value of `balance` to populate xxx:" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "G4BEOYnfOMof", 295 | "outputId": "f3063638-bcc3-42d0-9b6d-f614a270c6d9" 296 | }, 297 | "source": [ 298 | "# Your answer goes here\n" 299 | ], 300 | "execution_count": null, 301 | "outputs": [ 302 | { 303 | "output_type": "stream", 304 | "text": [ 305 | "Your account balance after 5 years would be: $ 322.1020000000001\n" 306 | ], 307 | "name": "stdout" 308 | } 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "metadata": { 314 | "id": "pfgud1TOQx0o" 315 | }, 316 | "source": [ 317 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 318 | "#disp(hint + '02-01-ex02-c')" 319 | ], 320 | "execution_count": null, 321 | "outputs": [] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": { 326 | "id": "j5z9BSj9OMof" 327 | }, 328 | "source": [ 329 | "Let's make two modifications to this:\n", 330 | "* use `sep = ''` in print function to avoid the extra space between dollar sign and balance value.\n", 331 | "* use `round()` function to round to two decimals\n", 332 | "\n", 333 | "Note: Use `?round` in a new cell to get help from the help function." 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "metadata": { 339 | "id": "wd8hnFKDOMog", 340 | "outputId": "67ed92bc-9905-464d-f25f-da0d7f03723e" 341 | }, 342 | "source": [ 343 | "# Your answer goes here\n" 344 | ], 345 | "execution_count": null, 346 | "outputs": [ 347 | { 348 | "output_type": "stream", 349 | "text": [ 350 | "Your account balance after 5 years would be: $322.1\n" 351 | ], 352 | "name": "stdout" 353 | } 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "metadata": { 359 | "id": "K1ZQC7TfQ35D" 360 | }, 361 | "source": [ 362 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 363 | "#disp(hint + '02-01-ex02-d')" 364 | ], 365 | "execution_count": null, 366 | "outputs": [] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": { 371 | "id": "RyNTgcOoOMog" 372 | }, 373 | "source": [ 374 | "# Exercise 3\n", 375 | "The value of pi is given below in a string/character format:" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "metadata": { 381 | "id": "UOcoQsflOMog", 382 | "outputId": "effc0b01-7154-4400-892a-c5717520e91b" 383 | }, 384 | "source": [ 385 | "pi_char = \"3.1415926535\"\n", 386 | "type(pi_char)" 387 | ], 388 | "execution_count": null, 389 | "outputs": [ 390 | { 391 | "output_type": "execute_result", 392 | "data": { 393 | "text/plain": [ 394 | "str" 395 | ] 396 | }, 397 | "metadata": { 398 | "tags": [] 399 | }, 400 | "execution_count": 6 401 | } 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": { 407 | "id": "Dosji50-OMog" 408 | }, 409 | "source": [ 410 | "Google and find a function that would convert it to float so we can use it in math equations, call this new converted value `pi`" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "metadata": { 416 | "id": "R8FOKf03OMog" 417 | }, 418 | "source": [ 419 | "# Your answer goes here\n" 420 | ], 421 | "execution_count": null, 422 | "outputs": [] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "metadata": { 427 | "id": "gJ_335Y2Q-Tz" 428 | }, 429 | "source": [ 430 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 431 | "#disp(hint + '02-01-ex03-a')" 432 | ], 433 | "execution_count": null, 434 | "outputs": [] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": { 439 | "id": "tiTydXSNOMog" 440 | }, 441 | "source": [ 442 | "Using this variable calculate the area of a circle with radius `r = 2` and print a rounded value for area (up to 3 decimals):" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "metadata": { 448 | "id": "mUbqtQIDOMoh", 449 | "outputId": "43d794e2-058f-41e3-fb69-0332622f768f" 450 | }, 451 | "source": [ 452 | "# Your answer goes here\n" 453 | ], 454 | "execution_count": null, 455 | "outputs": [ 456 | { 457 | "output_type": "stream", 458 | "text": [ 459 | "The area of a circle with radius 2 is 12.566\n" 460 | ], 461 | "name": "stdout" 462 | } 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "metadata": { 468 | "id": "yHPKuQLzRH95" 469 | }, 470 | "source": [ 471 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 472 | "#disp(hint + '02-01-ex03-b')" 473 | ], 474 | "execution_count": null, 475 | "outputs": [] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": { 480 | "id": "U_YrXEEEOMoh" 481 | }, 482 | "source": [ 483 | "# Exercise 4\n", 484 | "What is the quotient and remainder of 67432/98?" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "metadata": { 490 | "id": "EylwRuyfOMoh", 491 | "outputId": "fe3b29f8-c536-454c-9c4d-97d5b1d06e69" 492 | }, 493 | "source": [ 494 | "# Your answer goes here\n" 495 | ], 496 | "execution_count": null, 497 | "outputs": [ 498 | { 499 | "output_type": "stream", 500 | "text": [ 501 | "Quotient of 67432/98: 688\n", 502 | "Remainder of 67432/98: 8\n" 503 | ], 504 | "name": "stdout" 505 | } 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "metadata": { 511 | "id": "_5UAAU-VRLb9" 512 | }, 513 | "source": [ 514 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 515 | "#disp(hint + '02-01-ex04')" 516 | ], 517 | "execution_count": null, 518 | "outputs": [] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "metadata": { 523 | "id": "eGjD1P5hOMoh" 524 | }, 525 | "source": [ 526 | "# Exercise 5\n", 527 | "Write a statement to check whether `a` is a multiple of 5 and within the range of 100-150 or 0-50. Check your statement with the following values and it should give you the following values:\n", 528 | "\n", 529 | "55 --> False\n", 530 | "\n", 531 | "110 --> True\n", 532 | "\n", 533 | "-25 --> False\n", 534 | "\n", 535 | "150 --> False\n", 536 | "\n", 537 | "100 --> True" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "metadata": { 543 | "id": "0cTTpug-OMoh" 544 | }, 545 | "source": [ 546 | "# Your answer goes here\n" 547 | ], 548 | "execution_count": null, 549 | "outputs": [] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "metadata": { 554 | "id": "2AKItRrRRR5A" 555 | }, 556 | "source": [ 557 | "# SOLUTION: Uncomment and execute the cell below to get help\n", 558 | "#disp(hint + '02-01-ex05')" 559 | ], 560 | "execution_count": null, 561 | "outputs": [] 562 | } 563 | ] 564 | } -------------------------------------------------------------------------------- /02-Data-Import-Export/01-File-Import-Export-in-Colab.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.2"}},"cells":[{"cell_type":"markdown","metadata":{"id":"9Q5oBKSDEVv2"},"source":["# File Import/Export in Google Colab\n","\n","## Uploading files from your local file system\n","\n","`files.upload` returns a dictionary of the files which were uploaded.\n","The dictionary is keyed by the file name, the value is the data which was uploaded."]},{"cell_type":"code","metadata":{"id":"6Q-alKUZ_gqa","outputId":"48d5976f-0452-4f36-b9cd-13ce558e0de9","colab":{"base_uri":"https://localhost:8080/","height":92},"executionInfo":{"status":"ok","timestamp":1726113485387,"user_tz":240,"elapsed":13153,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"source":["from google.colab import files\n","\n","uploaded = files.upload()\n","\n","for fn in uploaded.keys():\n"," print('User uploaded file \"{name}\" with length {length} bytes'.format(\n"," name=fn, length=len(uploaded[fn])))"],"execution_count":1,"outputs":[{"output_type":"display_data","data":{"text/plain":[""],"text/html":["\n"," \n"," \n"," Upload widget is only available when the cell has been executed in the\n"," current browser session. Please rerun this cell to enable.\n"," \n"," "]},"metadata":{}},{"output_type":"stream","name":"stdout","text":["Saving electric-car-sales.csv to electric-car-sales.csv\n","User uploaded file \"electric-car-sales.csv\" with length 10144 bytes\n"]}]},{"cell_type":"code","metadata":{"id":"ECHNVDXAEmfY","outputId":"b8082eb3-1d9e-4756-b192-549d918c4ece","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1726113489927,"user_tz":240,"elapsed":178,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"source":["ls"],"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["electric-car-sales.csv \u001b[0m\u001b[01;34msample_data\u001b[0m/\n"]}]},{"cell_type":"markdown","metadata":{"id":"Ve7wL-QFE4Ss"},"source":["Please note that any file that is being uploaded to Google Colab will not persist after the session is terminated (In general, free Colab notebooks can run for at most 12 hours, depending on availability and your usage patterns.). This should be used with caution.\n","\n","## Downloading files to your local file system\n","\n","`files.download` will invoke a browser download of the file to the user's local computer."]},{"cell_type":"code","metadata":{"id":"SewvUVW4EvVc","colab":{"base_uri":"https://localhost:8080/","height":17},"executionInfo":{"status":"ok","timestamp":1726113658658,"user_tz":240,"elapsed":184,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"b17383e0-2c0b-4ed0-89c8-d89c0901d370"},"source":["from google.colab import files\n","\n","files.download('electric-car-sales.csv')"],"execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/plain":[""],"application/javascript":["\n"," async function download(id, filename, size) {\n"," if (!google.colab.kernel.accessAllowed) {\n"," return;\n"," }\n"," const div = document.createElement('div');\n"," const label = document.createElement('label');\n"," label.textContent = `Downloading \"${filename}\": `;\n"," div.appendChild(label);\n"," const progress = document.createElement('progress');\n"," progress.max = size;\n"," div.appendChild(progress);\n"," document.body.appendChild(div);\n","\n"," const buffers = [];\n"," let downloaded = 0;\n","\n"," const channel = await google.colab.kernel.comms.open(id);\n"," // Send a message to notify the kernel that we're ready.\n"," channel.send({})\n","\n"," for await (const message of channel.messages) {\n"," // Send a message to notify the kernel that we're ready.\n"," channel.send({})\n"," if (message.buffers) {\n"," for (const buffer of message.buffers) {\n"," buffers.push(buffer);\n"," downloaded += buffer.byteLength;\n"," progress.value = downloaded;\n"," }\n"," }\n"," }\n"," const blob = new Blob(buffers, {type: 'application/binary'});\n"," const a = document.createElement('a');\n"," a.href = window.URL.createObjectURL(blob);\n"," a.download = filename;\n"," div.appendChild(a);\n"," a.click();\n"," div.remove();\n"," }\n"," "]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":[""],"application/javascript":["download(\"download_1868bcbe-2962-4e29-b608-34e25861f58e\", \"electric-car-sales.csv\", 10144)"]},"metadata":{}}]},{"cell_type":"markdown","metadata":{"id":"Pco6vppIFhLI"},"source":["## Removing a file from the Colab container\n","\n","We can use the linux command `rm` to remove the file we just uploaded:"]},{"cell_type":"code","metadata":{"id":"n4vm1bSFFW0-","executionInfo":{"status":"ok","timestamp":1726113675061,"user_tz":240,"elapsed":164,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"source":["rm electric-car-sales.csv"],"execution_count":4,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"lGW3DYjsFyZm"},"source":["We can check to see if the file was removed by listing the files in the current directory (`content`) by the linux command `ls`:"]},{"cell_type":"code","metadata":{"id":"cABLAUYdFwM0","outputId":"80fb51a6-cb9d-4123-952c-5575c3c58064","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1726113683617,"user_tz":240,"elapsed":164,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"source":["ls"],"execution_count":5,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[0m\u001b[01;34msample_data\u001b[0m/\n"]}]},{"cell_type":"markdown","metadata":{"id":"3uLYpYmhGCen"},"source":["## Manual upload/download\n","\n","This can be done by clicking on the \"Files\" icon on the top left of your Colab notebook then \"Upload\".\n","\n","One can right click on any file to download it manually.\n","\n","**Note:** The working directory for Colab notebooks is \"`content`\" and we will only use that folder to avoid breaking anything.\n","\n","Linux command `pwd` returns the `p`resent `w`orking `d`irectory:"]},{"cell_type":"code","source":["pwd"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"id":"t83MY6JEZRRi","executionInfo":{"status":"ok","timestamp":1726113718952,"user_tz":240,"elapsed":4,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"495483ad-f6c6-449a-a691-fb7bfc26a28c"},"execution_count":6,"outputs":[{"output_type":"execute_result","data":{"text/plain":["'/content'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":6}]},{"cell_type":"markdown","metadata":{"id":"vHYGPEJRG-fp"},"source":["## Mounting Google Drive locally\n","\n","The example below shows how to mount your Google Drive in your virtual machine using an authorization process, and shows a couple of ways to write & read files there. Once executed, check the new file is visible in https://drive.google.com/ as well."]},{"cell_type":"code","metadata":{"id":"qcLbA_Z5F_ZE","executionInfo":{"status":"ok","timestamp":1726113910970,"user_tz":240,"elapsed":16180,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"9075e5b6-90bd-4675-a7e2-97244a856560","colab":{"base_uri":"https://localhost:8080/"}},"source":["# Mount the drive\n","from google.colab import drive\n","drive.mount('/content/gdrive', force_remount=True)"],"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/gdrive\n"]}]},{"cell_type":"code","source":["ls gdrive"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"qxyBBHkCaBLi","executionInfo":{"status":"ok","timestamp":1726113915529,"user_tz":240,"elapsed":194,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"36ae5807-0f82-49ed-d151-f72a25b3648a"},"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[0m\u001b[01;34mMyDrive\u001b[0m/ \u001b[01;34mOthercomputers\u001b[0m/ \u001b[01;34mShareddrives\u001b[0m/\n"]}]},{"cell_type":"code","metadata":{"id":"E_xmjbSBHTQq","executionInfo":{"status":"ok","timestamp":1726113991310,"user_tz":240,"elapsed":154,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"5445bc8d-134e-47b0-ab96-fc9f78371038","colab":{"base_uri":"https://localhost:8080/"}},"source":["ls gdrive/MyDrive/_01_Teaching/BA780-Introduction-to-Data-Analytics/Intro-to-Data-Analytics/data"],"execution_count":11,"outputs":[{"output_type":"stream","name":"stdout","text":["2017_StPaul_MN_Real_Estate.csv movies_metadata.csv table3.csv\n","\u001b[0m\u001b[01;34mAnalyticsEdge-Datasets\u001b[0m/ movies_ratings.csv table4a.csv\n","athlete_events.csv pollution.csv table4b.csv\n","FremontBridge.csv state-abbrevs.csv table5.csv\n","GOOGL.csv state-areas.csv Telco-Customer-Churn.csv\n","GOT-battles.csv state-population.csv weatherHistory.csv\n","GOT-character-deaths.csv table1.csv\n","\u001b[01;34mHouse-Prices-Kaggle\u001b[0m/ table2.csv\n"]}]},{"cell_type":"markdown","metadata":{"id":"VCqmYjE-IFIE"},"source":["We can now use this path (or any other Google Drive path) to store data that we want to keep. For example, in the cell below, we copy `sample_data/california_housing_train.csv` to a chosen folder in Google Drive."]},{"cell_type":"code","metadata":{"id":"5_wI9MWzHZ0a","executionInfo":{"status":"ok","timestamp":1726114328754,"user_tz":240,"elapsed":209,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"source":["cp sample_data/california_housing_train.csv gdrive/MyDrive/_01_Teaching/BA780-Introduction-to-Data-Analytics/Intro-to-Data-Analytics/data"],"execution_count":13,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"z3tXLcjTIaFP"},"source":["The `cp` command copies files or directories from a source path to a destination path, using the syntax:\n","\n","```cp [OPTION]... ```\n","\n","Option could be something like `-r` which is for recursive copying (an entire folder).\n","\n","Let's confirm the file was copied properly. We can also go to Drive and visually inspect it:"]},{"cell_type":"code","metadata":{"id":"VdsJalyVH99I","executionInfo":{"status":"ok","timestamp":1726114330546,"user_tz":240,"elapsed":171,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"f1dfa11b-233b-457d-8b19-587eb8a485c9","colab":{"base_uri":"https://localhost:8080/"}},"source":["ls gdrive/MyDrive/_01_Teaching/BA780-Introduction-to-Data-Analytics/Intro-to-Data-Analytics/data"],"execution_count":14,"outputs":[{"output_type":"stream","name":"stdout","text":["2017_StPaul_MN_Real_Estate.csv \u001b[0m\u001b[01;34mHouse-Prices-Kaggle\u001b[0m/ table2.csv\n","\u001b[01;34mAnalyticsEdge-Datasets\u001b[0m/ movies_metadata.csv table3.csv\n","athlete_events.csv movies_ratings.csv table4a.csv\n","california_housing_train.csv pollution.csv table4b.csv\n","FremontBridge.csv state-abbrevs.csv table5.csv\n","GOOGL.csv state-areas.csv Telco-Customer-Churn.csv\n","GOT-battles.csv state-population.csv weatherHistory.csv\n","GOT-character-deaths.csv table1.csv\n"]}]},{"cell_type":"markdown","metadata":{"id":"Jw5MvJ-RIqVK"},"source":["## Your Trun\n","\n","1. From https://grouplens.org/datasets/movielens/ download `ml-latest-small.zip`. Unzip it and upload the following files to your Colab's `content` folder:\n"," * movies.csv\n"," * ratings.csv\n","2. Mount Google Drive locally and copy these two files into a folder called `movie_rating` within your BA780 folder.\n"," * You can use the following code example to copy movies.csv to a folder called tmp_BA780 (change it to your desired desitination)\n","```\n","cp movies.csv gdrive/MyDrive/tmp_BA780/movie_rating\n","```\n"," * **Note** that `gdrive/MyDrive` is the homepage of your Google Drive. If the specified path doesn't exist, you will encounter an error. To create a new path, you can use the `mkdir` command in the command line. The `-p` option allows you to create a new folder within another new folder. For example, the command below creates `movie_rating` inside `tmp_BA780`; if `tmp_BA780` doesn't exist, it will create the parent directory first, then the child directory.:\n"," ```mkdir -p gdrive/MyDrive/tmp_BA780/movie_rating/```\n","3. Check your Drive and make sure the files are there.\n","4. Download these files to your local laptop."]},{"cell_type":"code","metadata":{"id":"NQEpAkUHH_hK"},"source":["# Your code goes here (use as many cells as needed)"],"execution_count":null,"outputs":[]}]} -------------------------------------------------------------------------------- /02-Data-Import-Export/Assignment-01.ipynb: -------------------------------------------------------------------------------- 1 | {"cells":[{"cell_type":"markdown","metadata":{"id":"wWqKAC5o9b2W"},"source":["# Assignment 1\n","\n","This assignment is to test your understanding of Python basics.\n","\n","You won't need to upload/email your notebook, but you will need to submit your code and answers on Blackboard.\n","\n","When applicable, only use the specific method requested.\n","\n","**Note:** spend some time reviewing the basics of python notebooks under the folder 00-Python-Basics in the course repo or A Whirlwind Tour of Python.\n","\n","**Generative AI rule:** For this assignment, you are allowed to use generative AI tools for assistance, but the code must be your original work—any code that is not your own will be considered cheating."]},{"cell_type":"markdown","metadata":{"id":"-WvDcI8r9e8N"},"source":["# Question 1\n","**Projected Revenue Growth Analysis**\n","\n","You are an analyst at a consulting firm tasked with projecting the 9-year compounded revenue growth for a tech company client. The company has experienced steady growth in recent years, but as a tech firm, it also faces significant market volatility. Your goal is to calculate the 9-year projected growth, adjust it based on market volatility, and assess the growth potential.\n","\n","Calculate the 9-Year Projected Growth: The company's annual growth rate is 12%. To estimate the compounded revenue growth over 9 years, calculate\n","$(1 + 0.12)^9$ and multiply by an initial revenue value of 1 (for simplicity, we assume the initial value is normalized to 1).\n","\n","Adjust for Market Volatility: Market volatility can impact growth predictions. To adjust for this, multiply the projected growth by a volatility index factor. Assume a Volatility Index (VIX) factor of 1.25, meaning the projection could be increased by 25% due to market optimism or risk. Multiply the projected growth by 1.25 and round the result to two decimal places.\n","\n","Classify the Growth Potential based on the adjusted result:\n","\n","If the adjusted projected growth is greater than 3, categorize it as **'High Growth Potential'**.\n","If the adjusted projected growth is between 1.5 and 3, categorize it as **'Moderate Growth Potential'**.\n","If the adjusted projected growth is less than 1.5, categorize it as **'Low Growth Potential'**.\n","\n","Provide the final adjusted growth figure and the categorized growth potential. Make sure to use variables and assign values to them to get full credit."]},{"cell_type":"code","execution_count":1,"metadata":{"id":"XzisjM4h9b2Z","executionInfo":{"status":"ok","timestamp":1725507366076,"user_tz":240,"elapsed":641,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"iw1F3q3M9b2d"},"source":["# Question 2\n","\n","** Inventory Optimization for a Retail Company**\n","\n","You are a supply chain analyst at a retail company responsible for optimizing the packing process for a new shipment of products. The company has received a bulk order of 45,000 units of a popular product that needs to be packed into standard-sized boxes for shipping to various distribution centers. Each standard box has a capacity of 74 units.\n","\n","Calculate the Number of Full Boxes: Determine how many full boxes can be packed using the available 45,000 units. This will help the company plan for transportation and storage space.\n","\n","Calculate the Remaining Inventory: Calculate how many units will remain unpacked after filling as many full boxes as possible. This leftover inventory needs special handling or alternative packaging solutions."]},{"cell_type":"code","execution_count":2,"metadata":{"id":"-8V2Prt_9b2e","executionInfo":{"status":"ok","timestamp":1725507366076,"user_tz":240,"elapsed":2,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"uhIcHdCv9b2h"},"source":["# Question 3\n","\n","You are a data analyst working for an e-commerce company. The company wants to understand customer purchasing behavior by analyzing a list of recent transactions. Each number in the list represents the number of items purchased by different customers in a single transaction. The list is sorted chronologically from the earliest to the latest transaction.\n","\n","Given the transaction data below, perform the following operations:"]},{"cell_type":"code","source":["transactions = [2, 5, 1, 7, 3, 1, 8, 6, 1, 4, 9, 2, 5, 1, 1, 7, 3, 1, 6, 10]"],"metadata":{"id":"VdD954nSFOrK"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["1. **Identify Early Customer Behavior:** Extract the first 5 transactions to analyze the purchasing behavior of the earliest customers.\n","\n","2. **Identify Recent Customer Behavior:** Extract the last 5 transactions to analyze the purchasing behavior of the most recent customers.\n","\n","3. **Reverse Transaction History:** Reverse the entire list to see the transactions in descending chronological order (from the most recent to the earliest)."],"metadata":{"id":"F4-d-BFXFWK5"}},{"cell_type":"code","execution_count":3,"metadata":{"id":"Oaje5oiQ9b2k","executionInfo":{"status":"ok","timestamp":1725507366076,"user_tz":240,"elapsed":2,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"2c3a5121"},"source":["# Question 4\n","\n","**Marketing Campaign Text Analysis**\n","\n","You are a digital marketing analyst working on refining the text for an upcoming promotional campaign. Your team wants to make the text more eye-catching and analyze certain characteristics of the text.\n","\n","Given the promotional text below, perform the following operations:"]},{"cell_type":"code","source":["promo_text = 'a whirlwind tour of python'"],"metadata":{"id":"b2HSWyieGK3G"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["1. **Convert the Text to Uppercase:** Convert the entire string to uppercase to ensure consistency in the promotional material.\n","\n","2. **Count Key Letters:** Count the total number of occurrences of the letter 'o' in the text. The frequency of specific letters can be used to analyze the readability and tone of the message.\n","\n","3. **Analyze Text Content:** Find the position of the first occurrence of the letter 'p' to determine where important keywords might start appearing in the text.\n","\n","Provide the code, and output text for each step."],"metadata":{"id":"GQy9vnfsGLR5"}},{"cell_type":"code","execution_count":4,"metadata":{"id":"3ab38431","executionInfo":{"status":"ok","timestamp":1725507366076,"user_tz":240,"elapsed":2,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"3VKo4KqC9b2n"},"source":["# Question 5\n","\n","**Complex Data Retrieval from a Business Data Structure**\n","\n","You are a data analyst at a retail company analyzing customer feedback and product details stored in a nested data structure. This data contains various information, such as customer reviews, product ratings, and detailed attributes.\n","\n","Given the nested data structure below, determine the indexing steps needed to retrieve the word 'satisfied', which represents a customer's sentiment."]},{"cell_type":"code","source":["data_structure = [\n"," ['Product A',\n"," {'reviews': [\n"," {'rating': 5, 'comment': 'Excellent', 'sentiment': 'positive'},\n"," {'rating': 3, 'comment': 'Average', 'sentiment': 'satisfied'}\n"," ]},\n"," 299.99\n"," ],\n"," ['Product B',\n"," {'reviews': [\n"," {'rating': 2, 'comment': 'Bad', 'sentiment': 'negative'},\n"," ]},\n"," 399.99\n"," ]\n","]"],"metadata":{"id":"MbiK1dKkHfyp"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Task:** Provide the exact indexing sequence required to access the word 'satisfied' from within the data_structure.\n","\n","Consider this exercise as navigating through nested data to extract specific customer sentiments, a task you might encounter when working with un- or semi-structured data."],"metadata":{"id":"7dPh9Cd_HgYK"}},{"cell_type":"code","execution_count":5,"metadata":{"id":"ltr4AnhM9b2r","executionInfo":{"status":"ok","timestamp":1725507369824,"user_tz":240,"elapsed":202,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"xkxmCuKC9b2t"},"source":["# Question 6\n","\n","**Data Filtering for Financial Analysis**\n","\n","You are a financial analyst tasked with analyzing transaction data. Your team has collected a list of transaction amounts (both positive and negative), and you need to perform some data filtering and transformation to prepare the data for further analysis.\n","\n","Given a list of transaction amounts, L1, perform the following tasks:\n","\n","Create a Filtered List: Using a list comprehension, create a new list L2 that contains the absolute values of the even transaction amounts that are greater than 50. This will help isolate significant transactions for further analysis.\n","\n","Calculate the Total of Significant Transactions: Calculate the sum of the elements in L2 to determine the total value of these significant transactions.\n","\n","Hint: Use sum(L2) to get the sum of all the elements in L2.\n","\n","Provide the filtered list L2 and the sum of its elements."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Bf9v6gex9b2u"},"outputs":[],"source":["L1 = [34, -78, 65, 102, -53, 50, -92, 61, 150, -45, 60, -88]"]},{"cell_type":"code","execution_count":6,"metadata":{"id":"M6nhacwT9b2w","executionInfo":{"status":"ok","timestamp":1725507372518,"user_tz":240,"elapsed":965,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]},{"cell_type":"markdown","metadata":{"id":"mA7_C2bT9b2z"},"source":["# Question 7\n","\n","**Data Transformation for Quality Control Analysis**\n","\n","You are working as a quality control analyst at a manufacturing company. Your task is to analyze sensor data to identify potential anomalies in the production line. The sensor data is represented as a list of integers, where each integer corresponds to a specific measurement.\n","\n","Your goal is to write a function that filters out and analyzes specific measurements that are multiples of 4 and exceed a threshold value.\n","\n","Function Definition: Write a function named mult4_squares_filter() that receives a list of integers and returns a new list containing the squares of the numbers that are multiples of 4 and greater than 10. This function will help identify significant measurements that require attention.\n","\n","Analyze Sensor Data: Given the list L3 below, use your function to determine how many measurements meet the criteria.\n","\n","Hint: Use len(mult4_squares_filter(L3)) to get the number of elements that satisfy the conditions.\n","\n","**Task:** Provide the number of elements in the outcome of mult4_squares_filter(L3).\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RkQKcRWv9b2z"},"outputs":[],"source":["L3 = [5, 12, 7, 16, 24, 9, 8, 40, 15, 64, 100, 18, 50, 4, 80]"]},{"cell_type":"code","execution_count":7,"metadata":{"id":"OLSlhDsf9b23","executionInfo":{"status":"ok","timestamp":1725507374980,"user_tz":240,"elapsed":180,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[],"source":["# Your answer goes here"]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.7"}},"nbformat":4,"nbformat_minor":0} -------------------------------------------------------------------------------- /02-Data-Import-Export/sample_data/Pandas-Example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/02-Data-Import-Export/sample_data/Pandas-Example.xlsx -------------------------------------------------------------------------------- /02-Data-Import-Export/sample_data/anscombe.json: -------------------------------------------------------------------------------- 1 | [ 2 | {"Series":"I", "X":10.0, "Y":8.04}, 3 | {"Series":"I", "X":8.0, "Y":6.95}, 4 | {"Series":"I", "X":13.0, "Y":7.58}, 5 | {"Series":"I", "X":9.0, "Y":8.81}, 6 | {"Series":"I", "X":11.0, "Y":8.33}, 7 | {"Series":"I", "X":14.0, "Y":9.96}, 8 | {"Series":"I", "X":6.0, "Y":7.24}, 9 | {"Series":"I", "X":4.0, "Y":4.26}, 10 | {"Series":"I", "X":12.0, "Y":10.84}, 11 | {"Series":"I", "X":7.0, "Y":4.81}, 12 | {"Series":"I", "X":5.0, "Y":5.68}, 13 | 14 | {"Series":"II", "X":10.0, "Y":9.14}, 15 | {"Series":"II", "X":8.0, "Y":8.14}, 16 | {"Series":"II", "X":13.0, "Y":8.74}, 17 | {"Series":"II", "X":9.0, "Y":8.77}, 18 | {"Series":"II", "X":11.0, "Y":9.26}, 19 | {"Series":"II", "X":14.0, "Y":8.10}, 20 | {"Series":"II", "X":6.0, "Y":6.13}, 21 | {"Series":"II", "X":4.0, "Y":3.10}, 22 | {"Series":"II", "X":12.0, "Y":9.13}, 23 | {"Series":"II", "X":7.0, "Y":7.26}, 24 | {"Series":"II", "X":5.0, "Y":4.74}, 25 | 26 | {"Series":"III", "X":10.0, "Y":7.46}, 27 | {"Series":"III", "X":8.0, "Y":6.77}, 28 | {"Series":"III", "X":13.0, "Y":12.74}, 29 | {"Series":"III", "X":9.0, "Y":7.11}, 30 | {"Series":"III", "X":11.0, "Y":7.81}, 31 | {"Series":"III", "X":14.0, "Y":8.84}, 32 | {"Series":"III", "X":6.0, "Y":6.08}, 33 | {"Series":"III", "X":4.0, "Y":5.39}, 34 | {"Series":"III", "X":12.0, "Y":8.15}, 35 | {"Series":"III", "X":7.0, "Y":6.42}, 36 | {"Series":"III", "X":5.0, "Y":5.73}, 37 | 38 | {"Series":"IV", "X":8.0, "Y":6.58}, 39 | {"Series":"IV", "X":8.0, "Y":5.76}, 40 | {"Series":"IV", "X":8.0, "Y":7.71}, 41 | {"Series":"IV", "X":8.0, "Y":8.84}, 42 | {"Series":"IV", "X":8.0, "Y":8.47}, 43 | {"Series":"IV", "X":8.0, "Y":7.04}, 44 | {"Series":"IV", "X":8.0, "Y":5.25}, 45 | {"Series":"IV", "X":19.0, "Y":12.50}, 46 | {"Series":"IV", "X":8.0, "Y":5.56}, 47 | {"Series":"IV", "X":8.0, "Y":7.91}, 48 | {"Series":"IV", "X":8.0, "Y":6.89} 49 | ] 50 | -------------------------------------------------------------------------------- /02-Data-Import-Export/sample_data/datasets.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/02-Data-Import-Export/sample_data/datasets.xlsx -------------------------------------------------------------------------------- /02-Data-Import-Export/sample_data/electric-car-sales.csv: -------------------------------------------------------------------------------- 1 | Entity,Code,Year,Share of new cars that are electric 2 | Belgium,BEL,2010,0.01 3 | China,CHN,2010,0.01 4 | Europe,0,2010,14 5 | European Union (27),0,2010,12 6 | France,FRA,2010,85 7 | Germany,DEU,2010,49 8 | India,IND,2010,21 9 | Italy,ITA,2010,2 10 | Japan,JPN,2010,58 11 | Netherlands,NLD,2010,25 12 | New Zealand,NZL,2010,63 13 | Norway,NOR,2010,0.28 14 | Other Europe,0,2010,21 15 | Poland,POL,2010,48 16 | Portugal,PRT,2010,0.32 17 | South Korea,KOR,2010,5 18 | Spain,ESP,2010,77 19 | Sweden,SWE,2010,13 20 | United Kingdom,GBR,2010,13 21 | United States,USA,2010,11 22 | World,OWID_WRL,2010,11 23 | Australia,AUS,2011,65 24 | Belgium,BEL,2011,52 25 | Canada,CAN,2011,41 26 | Chile,CHL,2011,24 27 | China,CHN,2011,34 28 | Denmark,DNK,2011,0.25 29 | Europe,0,2011,73 30 | European Union (27),0,2011,63 31 | Finland,FIN,2011,25 32 | France,FRA,2011,0.13 33 | Germany,DEU,2011,52 34 | India,IND,2011,56 35 | Israel,ISR,2011,13 36 | Italy,ITA,2011,65 37 | Japan,JPN,2011,0.31 38 | Mexico,MEX,2011,26 39 | Netherlands,NLD,2011,0.16 40 | New Zealand,NZL,2011,99 41 | Norway,NOR,2011,1.4 42 | Other Europe,0,2011,18 43 | Poland,POL,2011,14 44 | Portugal,PRT,2011,0.12 45 | Rest of World,0,2011,33 46 | South Korea,KOR,2011,22 47 | Spain,ESP,2011,49 48 | Sweden,SWE,2011,54 49 | Switzerland,CHE,2011,0.14 50 | United Kingdom,GBR,2011,63 51 | United States,USA,2011,0.17 52 | World,OWID_WRL,2011,0.07 53 | Australia,AUS,2012,0.03 54 | Belgium,BEL,2012,0.19 55 | Brazil,BRA,2012,3 56 | Canada,CAN,2012,0.14 57 | Chile,CHL,2012,2 58 | China,CHN,2012,73 59 | Denmark,DNK,2012,0.29 60 | Europe,0,2012,0.2 61 | European Union (27),0,2012,0.19 62 | Finland,FIN,2012,0.17 63 | France,FRA,2012,0.34 64 | Germany,DEU,2012,0.11 65 | Iceland,ISL,2012,0.31 66 | India,IND,2012,72 67 | Israel,ISR,2012,0.27 68 | Italy,ITA,2012,33 69 | Japan,JPN,2012,0.53 70 | Mexico,MEX,2012,76 71 | Netherlands,NLD,2012,1 72 | New Zealand,NZL,2012,17 73 | Norway,NOR,2012,3.1 74 | Other Europe,0,2012,74 75 | Poland,POL,2012,18 76 | Portugal,PRT,2012,55 77 | Rest of World,0,2012,27 78 | South Korea,KOR,2012,0.04 79 | Spain,ESP,2012,76 80 | Sweden,SWE,2012,0.31 81 | Switzerland,CHE,2012,0.23 82 | Turkey,TUR,2012,16 83 | United Kingdom,GBR,2012,0.13 84 | United States,USA,2012,0.41 85 | World,OWID_WRL,2012,0.16 86 | Australia,AUS,2013,34 87 | Austria,AUT,2013,0.2 88 | Belgium,BEL,2013,0.12 89 | Brazil,BRA,2013,55 90 | Canada,CAN,2013,0.21 91 | Chile,CHL,2013,19 92 | China,CHN,2013,95 93 | Denmark,DNK,2013,0.28 94 | Europe,0,2013,0.44 95 | European Union (27),0,2013,0.47 96 | Finland,FIN,2013,0.21 97 | France,FRA,2013,0.55 98 | Germany,DEU,2013,0.23 99 | Greece,GRC,2013,51 100 | Iceland,ISL,2013,1.3 101 | India,IND,2013,16 102 | Israel,ISR,2013,0.21 103 | Italy,ITA,2013,0.08 104 | Japan,JPN,2013,0.63 105 | Mexico,MEX,2013,88 106 | Netherlands,NLD,2013,5.5 107 | New Zealand,NZL,2013,19 108 | Norway,NOR,2013,5.8 109 | Other Europe,0,2013,23 110 | Poland,POL,2013,13 111 | Portugal,PRT,2013,0.19 112 | Rest of World,0,2013,12 113 | South Africa,ZAF,2013,75 114 | South Korea,KOR,2013,47 115 | Spain,ESP,2013,0.12 116 | Sweden,SWE,2013,0.53 117 | Switzerland,CHE,2013,0.43 118 | Turkey,TUR,2013,16 119 | United Kingdom,GBR,2013,0.17 120 | United States,USA,2013,0.74 121 | World,OWID_WRL,2013,0.27 122 | Australia,AUS,2014,0.16 123 | Austria,AUT,2014,0.57 124 | Belgium,BEL,2014,0.41 125 | Brazil,BRA,2014,22 126 | Canada,CAN,2014,0.33 127 | Chile,CHL,2014,71 128 | China,CHN,2014,0.4 129 | Denmark,DNK,2014,0.9 130 | Europe,0,2014,0.68 131 | European Union (27),0,2014,0.56 132 | Finland,FIN,2014,0.41 133 | France,FRA,2014,0.72 134 | Germany,DEU,2014,0.45 135 | Greece,GRC,2014,55 136 | Iceland,ISL,2014,2.1 137 | India,IND,2014,38 138 | Israel,ISR,2014,22 139 | Italy,ITA,2014,0.1 140 | Japan,JPN,2014,0.69 141 | Mexico,MEX,2014,38 142 | Netherlands,NLD,2014,3.9 143 | New Zealand,NZL,2014,0.14 144 | Norway,NOR,2014,15 145 | Other Europe,0,2014,0.13 146 | Poland,POL,2014,62 147 | Portugal,PRT,2014,0.14 148 | Rest of World,0,2014,42 149 | South Africa,ZAF,2014,34 150 | South Korea,KOR,2014,0.09 151 | Spain,ESP,2014,0.2 152 | Sweden,SWE,2014,1.4 153 | Switzerland,CHE,2014,0.96 154 | Turkey,TUR,2014,12 155 | United Kingdom,GBR,2014,0.6 156 | United States,USA,2014,0.89 157 | World,OWID_WRL,2014,0.43 158 | Australia,AUS,2015,0.2 159 | Austria,AUT,2015,0.9 160 | Belgium,BEL,2015,0.77 161 | Brazil,BRA,2015,41 162 | Canada,CAN,2015,0.43 163 | Chile,CHL,2015,19 164 | China,CHN,2015,1 165 | Denmark,DNK,2015,2.3 166 | Europe,0,2015,1.2 167 | European Union (27),0,2015,1.1 168 | Finland,FIN,2015,0.6 169 | France,FRA,2015,1.2 170 | Germany,DEU,2015,0.73 171 | Greece,GRC,2015,75 172 | Iceland,ISL,2015,3.6 173 | India,IND,2015,17 174 | Israel,ISR,2015,24 175 | Italy,ITA,2015,0.14 176 | Japan,JPN,2015,0.58 177 | Mexico,MEX,2015,75 178 | Netherlands,NLD,2015,10 179 | New Zealand,NZL,2015,0.2 180 | Norway,NOR,2015,22 181 | Other Europe,0,2015,0.15 182 | Poland,POL,2015,62 183 | Portugal,PRT,2015,0.57 184 | Rest of World,0,2015,36 185 | South Africa,ZAF,2015,58 186 | South Korea,KOR,2015,0.21 187 | Spain,ESP,2015,0.21 188 | Sweden,SWE,2015,2.4 189 | Switzerland,CHE,2015,1.7 190 | Turkey,TUR,2015,31 191 | United Kingdom,GBR,2015,1.1 192 | United States,USA,2015,0.78 193 | World,OWID_WRL,2015,0.7 194 | Australia,AUS,2016,0.15 195 | Austria,AUT,2016,1.5 196 | Belgium,BEL,2016,1.7 197 | Brazil,BRA,2016,0.01 198 | Canada,CAN,2016,0.72 199 | Chile,CHL,2016,15 200 | China,CHN,2016,1.5 201 | Denmark,DNK,2016,0.85 202 | Europe,0,2016,1.3 203 | European Union (27),0,2016,0.98 204 | Finland,FIN,2016,1.2 205 | France,FRA,2016,1.5 206 | Germany,DEU,2016,0.73 207 | Greece,GRC,2016,0.12 208 | Iceland,ISL,2016,5.6 209 | India,IND,2016,26 210 | Israel,ISR,2016,22 211 | Italy,ITA,2016,0.15 212 | Japan,JPN,2016,0.6 213 | Mexico,MEX,2016,51 214 | Netherlands,NLD,2016,6 215 | New Zealand,NZL,2016,0.57 216 | Norway,NOR,2016,29 217 | Other Europe,0,2016,0.18 218 | Poland,POL,2016,0.12 219 | Portugal,PRT,2016,0.81 220 | Rest of World,0,2016,13 221 | South Africa,ZAF,2016,0.1 222 | South Korea,KOR,2016,0.32 223 | Spain,ESP,2016,0.31 224 | Sweden,SWE,2016,3.4 225 | Switzerland,CHE,2016,1.9 226 | Turkey,TUR,2016,17 227 | United Kingdom,GBR,2016,1.4 228 | United States,USA,2016,1 229 | World,OWID_WRL,2016,0.89 230 | Australia,AUS,2017,0.26 231 | Austria,AUT,2017,2 232 | Belgium,BEL,2017,2.6 233 | Brazil,BRA,2017,19 234 | Canada,CAN,2017,0.99 235 | Chile,CHL,2017,61 236 | China,CHN,2017,2.4 237 | Denmark,DNK,2017,0.6 238 | Europe,0,2017,1.7 239 | European Union (27),0,2017,1.3 240 | Finland,FIN,2017,2.6 241 | France,FRA,2017,1.8 242 | Germany,DEU,2017,1.6 243 | Greece,GRC,2017,0.22 244 | Iceland,ISL,2017,14 245 | India,IND,2017,31 246 | Israel,ISR,2017,0.52 247 | Italy,ITA,2017,0.25 248 | Japan,JPN,2017,1.2 249 | Mexico,MEX,2017,83 250 | Netherlands,NLD,2017,2.5 251 | New Zealand,NZL,2017,1.2 252 | Norway,NOR,2017,39 253 | Other Europe,0,2017,0.41 254 | Poland,POL,2017,0.22 255 | Portugal,PRT,2017,1.9 256 | Rest of World,0,2017,21 257 | South Africa,ZAF,2017,53 258 | South Korea,KOR,2017,0.92 259 | Spain,ESP,2017,0.6 260 | Sweden,SWE,2017,5.1 261 | Switzerland,CHE,2017,2.7 262 | Turkey,TUR,2017,15 263 | United Kingdom,GBR,2017,1.9 264 | United States,USA,2017,1.2 265 | World,OWID_WRL,2017,1.4 266 | Australia,AUS,2018,0.42 267 | Austria,AUT,2018,2.6 268 | Belgium,BEL,2018,2.4 269 | Brazil,BRA,2018,14 270 | Canada,CAN,2018,2.7 271 | Chile,CHL,2018,72 272 | China,CHN,2018,4.9 273 | Denmark,DNK,2018,2.1 274 | Europe,0,2018,2.3 275 | European Union (27),0,2018,1.9 276 | Finland,FIN,2018,4.7 277 | France,FRA,2018,2.1 278 | Germany,DEU,2018,1.9 279 | Greece,GRC,2018,0.29 280 | Iceland,ISL,2018,20 281 | India,IND,2018,0.03 282 | Israel,ISR,2018,1.2 283 | Italy,ITA,2018,0.51 284 | Japan,JPN,2018,1.1 285 | Mexico,MEX,2018,0.13 286 | Netherlands,NLD,2018,6.3 287 | New Zealand,NZL,2018,1.9 288 | Norway,NOR,2018,49 289 | Other Europe,0,2018,0.67 290 | Poland,POL,2018,0.26 291 | Portugal,PRT,2018,3.5 292 | Rest of World,0,2018,35 293 | South Africa,ZAF,2018,41 294 | South Korea,KOR,2018,3.6 295 | Spain,ESP,2018,0.88 296 | Sweden,SWE,2018,7.6 297 | Switzerland,CHE,2018,3.2 298 | Turkey,TUR,2018,45 299 | United Kingdom,GBR,2018,2.6 300 | United States,USA,2018,2 301 | World,OWID_WRL,2018,2.3 302 | Australia,AUS,2019,1.2 303 | Austria,AUT,2019,3.5 304 | Belgium,BEL,2019,3.2 305 | Brazil,BRA,2019,84 306 | Canada,CAN,2019,3.2 307 | Chile,CHL,2019,0.14 308 | China,CHN,2019,5 309 | Denmark,DNK,2019,4.2 310 | Europe,0,2019,3.4 311 | European Union (27),0,2019,3 312 | Finland,FIN,2019,6.9 313 | France,FRA,2019,2.8 314 | Germany,DEU,2019,2.9 315 | Greece,GRC,2019,0.42 316 | Iceland,ISL,2019,23 317 | India,IND,2019,24 318 | Israel,ISR,2019,1.6 319 | Italy,ITA,2019,0.9 320 | Japan,JPN,2019,0.9 321 | Mexico,MEX,2019,0.13 322 | Netherlands,NLD,2019,15 323 | New Zealand,NZL,2019,2.8 324 | Norway,NOR,2019,56 325 | Other Europe,0,2019,1.1 326 | Poland,POL,2019,0.49 327 | Portugal,PRT,2019,5.7 328 | Rest of World,0,2019,65 329 | South Africa,ZAF,2019,64 330 | South Korea,KOR,2019,2.2 331 | Spain,ESP,2019,1.4 332 | Sweden,SWE,2019,11 333 | Switzerland,CHE,2019,5.6 334 | Turkey,TUR,2019,84 335 | United Kingdom,GBR,2019,3.2 336 | United States,USA,2019,2.1 337 | World,OWID_WRL,2019,2.6 338 | Australia,AUS,2020,1.1 339 | Austria,AUT,2020,9.5 340 | Belgium,BEL,2020,11 341 | Brazil,BRA,2020,0.15 342 | Canada,CAN,2020,4.1 343 | Chile,CHL,2020,0.16 344 | China,CHN,2020,5.8 345 | Denmark,DNK,2020,16 346 | Europe,0,2020,10 347 | European Union (27),0,2020,10 348 | Finland,FIN,2020,18 349 | France,FRA,2020,11 350 | Germany,DEU,2020,13 351 | Greece,GRC,2020,2.6 352 | Iceland,ISL,2020,52 353 | India,IND,2020,0.13 354 | Israel,ISR,2020,2.6 355 | Italy,ITA,2020,4.3 356 | Japan,JPN,2020,0.77 357 | Mexico,MEX,2020,0.42 358 | Netherlands,NLD,2020,25 359 | New Zealand,NZL,2020,2.8 360 | Norway,NOR,2020,75 361 | Other Europe,0,2020,3 362 | Poland,POL,2020,1.9 363 | Portugal,PRT,2020,14 364 | Rest of World,0,2020,84 365 | South Africa,ZAF,2020,98 366 | South Korea,KOR,2020,2.5 367 | Spain,ESP,2020,4.9 368 | Sweden,SWE,2020,32 369 | Switzerland,CHE,2020,14 370 | Turkey,TUR,2020,0.17 371 | United Kingdom,GBR,2020,11 372 | United States,USA,2020,2.2 373 | World,OWID_WRL,2020,4.2 374 | Australia,AUS,2021,2.8 375 | Austria,AUT,2021,20 376 | Belgium,BEL,2021,18 377 | Brazil,BRA,2021,0.74 378 | Canada,CAN,2021,6.5 379 | Chile,CHL,2021,0.19 380 | China,CHN,2021,16 381 | Denmark,DNK,2021,35 382 | Europe,0,2021,18 383 | European Union (27),0,2021,18 384 | Finland,FIN,2021,31 385 | France,FRA,2021,19 386 | Germany,DEU,2021,26 387 | Greece,GRC,2021,6.9 388 | Iceland,ISL,2021,72 389 | India,IND,2021,0.4 390 | Israel,ISR,2021,6.8 391 | Italy,ITA,2021,9.5 392 | Japan,JPN,2021,1.2 393 | Mexico,MEX,2021,0.75 394 | Netherlands,NLD,2021,30 395 | New Zealand,NZL,2021,3.6 396 | Norway,NOR,2021,86 397 | Other Europe,0,2021,5.2 398 | Poland,POL,2021,3.7 399 | Portugal,PRT,2021,20 400 | Rest of World,0,2021,0.18 401 | South Africa,ZAF,2021,89 402 | South Korea,KOR,2021,6.2 403 | Spain,ESP,2021,7.8 404 | Sweden,SWE,2021,43 405 | Switzerland,CHE,2021,23 406 | Turkey,TUR,2021,0.55 407 | United Kingdom,GBR,2021,19 408 | United States,USA,2021,4.5 409 | World,OWID_WRL,2021,8.7 410 | Australia,AUS,2022,5.1 411 | Austria,AUT,2022,22 412 | Belgium,BEL,2022,26 413 | Brazil,BRA,2022,0.98 414 | Canada,CAN,2022,9.4 415 | Chile,CHL,2022,0.49 416 | China,CHN,2022,29 417 | Denmark,DNK,2022,39 418 | Europe,0,2022,21 419 | European Union (27),0,2022,21 420 | Finland,FIN,2022,38 421 | France,FRA,2022,21 422 | Germany,DEU,2022,31 423 | Greece,GRC,2022,7.9 424 | Iceland,ISL,2022,70 425 | India,IND,2022,1.5 426 | Israel,ISR,2022,13 427 | Italy,ITA,2022,9 428 | Japan,JPN,2022,3 429 | Mexico,MEX,2022,0.91 430 | Netherlands,NLD,2022,35 431 | New Zealand,NZL,2022,13 432 | Norway,NOR,2022,88 433 | Other Europe,0,2022,7 434 | Poland,POL,2022,6 435 | Portugal,PRT,2022,22 436 | Rest of World,0,2022,0.28 437 | South Africa,ZAF,2022,0.17 438 | South Korea,KOR,2022,9.4 439 | Spain,ESP,2022,8.9 440 | Sweden,SWE,2022,54 441 | Switzerland,CHE,2022,25 442 | Turkey,TUR,2022,1 443 | United Kingdom,GBR,2022,23 444 | United States,USA,2022,7.7 445 | World,OWID_WRL,2022,14 -------------------------------------------------------------------------------- /02-Data-Import-Export/sample_data/mtcars.csv: -------------------------------------------------------------------------------- 1 | ,mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb 2 | 0,21.0,6,160.0,110,3.9,2.62,16.46,0,1,4,4 3 | 1,21.0,6,160.0,110,3.9,2.875,17.02,0,1,4,4 4 | 2,22.8,4,108.0,93,3.85,2.32,18.61,1,1,4,1 5 | 3,21.4,6,258.0,110,3.08,3.215,19.44,1,0,3,1 6 | 4,18.7,8,360.0,175,3.15,3.44,17.02,0,0,3,2 7 | 5,18.1,6,225.0,105,2.76,3.46,20.22,1,0,3,1 8 | 6,14.3,8,360.0,245,3.21,3.57,15.84,0,0,3,4 9 | 7,24.4,4,146.7,62,3.69,3.19,20.0,1,0,4,2 10 | 8,22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2 11 | 9,19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4 12 | 10,17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4 13 | 11,16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3 14 | 12,17.3,8,275.8,180,3.07,3.73,17.6,0,0,3,3 15 | 13,15.2,8,275.8,180,3.07,3.78,18.0,0,0,3,3 16 | 14,10.4,8,472.0,205,2.93,5.25,17.98,0,0,3,4 17 | 15,10.4,8,460.0,215,3.0,5.424,17.82,0,0,3,4 18 | 16,14.7,8,440.0,230,3.23,5.345,17.42,0,0,3,4 19 | 17,32.4,4,78.7,66,4.08,2.2,19.47,1,1,4,1 20 | 18,30.4,4,75.7,52,4.93,1.615,18.52,1,1,4,2 21 | 19,33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1 22 | 20,21.5,4,120.1,97,3.7,2.465,20.01,1,0,3,1 23 | 21,15.5,8,318.0,150,2.76,3.52,16.87,0,0,3,2 24 | 22,15.2,8,304.0,150,3.15,3.435,17.3,0,0,3,2 25 | 23,13.3,8,350.0,245,3.73,3.84,15.41,0,0,3,4 26 | 24,19.2,8,400.0,175,3.08,3.845,17.05,0,0,3,2 27 | 25,27.3,4,79.0,66,4.08,1.935,18.9,1,1,4,1 28 | 26,26.0,4,120.3,91,4.43,2.14,16.7,0,1,5,2 29 | 27,30.4,4,95.1,113,3.77,1.513,16.9,1,1,5,2 30 | 28,15.8,8,351.0,264,4.22,3.17,14.5,0,1,5,4 31 | 29,19.7,6,145.0,175,3.62,2.77,15.5,0,1,5,6 32 | 30,15.0,8,301.0,335,3.54,3.57,14.6,0,1,5,8 33 | 31,21.4,4,121.0,109,4.11,2.78,18.6,1,1,4,2 34 | -------------------------------------------------------------------------------- /03-Data-Transformation/Exercise-03-Aggregation-and-Grouping.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Exercise - Aggregation and Grouping\n","\n","Now it's your turn to practice what we learned in the previous notebook.\n","\n","We will be using the same dataset as the previous exercise.\n","\n","The dataset for this exercise comes from [Dairy Supply Chain Sales dataset](https://zenodo.org/records/7853252) from [Evaluating the Effect of Volatile Federated Timeseries on Modern DNNs: Attention over Long/Short Memory](https://ieeexplore.ieee.org/document/10176585), Siniosoglou et al."],"metadata":{"id":"-0MNcD7lQPaY"}},{"cell_type":"markdown","source":["Run the first cell as is and then proceed with the exercise."],"metadata":{"id":"NrZkFJFIRNq8"}},{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MzSvhDwwQDAR","outputId":"37f6bffb-8029-40ae-d669-d224e42011ba","executionInfo":{"status":"ok","timestamp":1726717662916,"user_tz":240,"elapsed":2531,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["\n","Index: 364 entries, 0 to 364\n","Data columns (total 13 columns):\n"," # Column Non-Null Count Dtype \n","--- ------ -------------- ----- \n"," 0 Day 364 non-null int64 \n"," 1 Month 364 non-null object \n"," 2 Year 364 non-null int64 \n"," 3 daily_unit_sales 364 non-null float64\n"," 4 previous_year_daily_unit_sales 364 non-null float64\n"," 5 percentage_difference_daily_unit_sales 364 non-null float64\n"," 6 daily_unit_sales_kg 364 non-null float64\n"," 7 previous_year_daily_unit_sales_kg 364 non-null float64\n"," 8 percentage_difference_daily_unit_sales_kg 364 non-null float64\n"," 9 daily_unit_returns_kg 364 non-null float64\n"," 10 previous_year_daily_unit_returns_kg 364 non-null object \n"," 11 points_of_distribution 364 non-null float64\n"," 12 previous_year_points_of_distribution 364 non-null float64\n","dtypes: float64(9), int64(2), object(2)\n","memory usage: 39.8+ KB\n"]}],"source":["import numpy as np\n","import pandas as pd\n","\n","df = pd.read_excel('https://raw.githubusercontent.com/soltaniehha/Intro-to-Data-Analytics/main/data/MEVGAL-Dairy-Sales/product1-2022.xlsx')\n","\n","df.loc[:, 'previous_year_daily_unit_returns_kg'] = df['previous_year_daily_unit_returns_kg'].replace([np.inf, -np.inf], pd.NA)\n","df.loc[df['daily_unit_sales']<0, 'daily_unit_sales'] = pd.NA\n","df = df.dropna(subset = ['daily_unit_sales'])\n","df[['percentage_difference_daily_unit_sales','percentage_difference_daily_unit_sales_kg']] = df[['percentage_difference_daily_unit_sales', 'percentage_difference_daily_unit_sales_kg']].apply(lambda x: x.fillna(x.mean()))\n","df[['daily_unit_sales_kg', 'daily_unit_returns_kg']] = df[['daily_unit_sales_kg', 'daily_unit_returns_kg']].apply(lambda x: x.fillna(x.median()))\n","df[['previous_year_daily_unit_sales_kg', 'previous_year_daily_unit_returns_kg']] = df[['previous_year_daily_unit_sales_kg', 'previous_year_daily_unit_returns_kg']].ffill()\n","for col in ['points_of_distribution', 'previous_year_points_of_distribution']:\n"," missing = df[col].isna()\n"," df.loc[missing, col] = np.random.choice(df[col].dropna(), size=missing.sum(), replace=True)\n","df.loc[:, 'previous_year_daily_unit_returns_kg'] = df['previous_year_daily_unit_returns_kg'].fillna(0)\n","\n","df.info()"]},{"cell_type":"markdown","source":["1. What is the average amount of yogurt, in kilograms, sold and returned this year compared to last year?\n","\n","To practice, let's first create a subset of numerical columns: `daily_unit_sales_kg`, `previous_year_daily_unit_sales_kg`, `daily_unit_returns_kg`, `previous_year_daily_unit_returns_kg` before performing the aggregations.\n","\n","Hint: Do not create a new dataframe, you simply need to specify (subset) the columns you want the statistics for."],"metadata":{"id":"GHrxqzAVWH3S"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":210},"id":"disI-yCeSoey","outputId":"2e21e78d-f47c-4f5c-e177-8af353f864a3","executionInfo":{"status":"ok","timestamp":1726717662916,"user_tz":240,"elapsed":4,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":2,"outputs":[{"output_type":"execute_result","data":{"text/plain":["daily_unit_sales_kg 1569.187912\n","previous_year_daily_unit_sales_kg 1816.796703\n","daily_unit_returns_kg 0.024513\n","previous_year_daily_unit_returns_kg 0.021489\n","dtype: object"],"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
0
daily_unit_sales_kg1569.187912
previous_year_daily_unit_sales_kg1816.796703
daily_unit_returns_kg0.024513
previous_year_daily_unit_returns_kg0.021489
\n","

"]},"metadata":{},"execution_count":2}]},{"cell_type":"markdown","source":["2. How many more or fewer units of yogurt were sold this year compared to last year?"],"metadata":{"id":"3lY2rQRNWctd"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"nzZO3ehEYcOP","outputId":"4d0e14f4-693a-49fa-b998-321d04b27de3","executionInfo":{"status":"ok","timestamp":1726717663104,"user_tz":240,"elapsed":191,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["The difference in total sales is: -145864.00000000035\n"]}]},{"cell_type":"markdown","source":["3. What was the maximum quantity of yogurt sold in one day last year?"],"metadata":{"id":"Cu_bxfKtZAl9"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"HhECfkgdYy9l","outputId":"09f2d054-09b5-4e95-fc34-3973eebf6fe5","executionInfo":{"status":"ok","timestamp":1726717663104,"user_tz":240,"elapsed":4,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":4,"outputs":[{"output_type":"execute_result","data":{"text/plain":["4228.800000000001"]},"metadata":{},"execution_count":4}]},{"cell_type":"markdown","source":["4. What was the standard deviation observed in yogurt unit sales this year?"],"metadata":{"id":"2d1vOVTuZe5M"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ip_j6OarZea2","outputId":"274347f9-887c-409f-eb88-9f7da42cd893","executionInfo":{"status":"ok","timestamp":1726717663104,"user_tz":240,"elapsed":3,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":5,"outputs":[{"output_type":"execute_result","data":{"text/plain":["1183.207440684088"]},"metadata":{},"execution_count":5}]},{"cell_type":"markdown","source":["5. Which day had the highest average return across this year and the previous year, and what was the amount?\n","\n","Hint: You need to get the average of `daily_unit_returns_kg` and\t`previous_year_daily_unit_returns_kg` for each day. To calculate the average between two columns for each row, use `axis=1` in the aggregation function."],"metadata":{"id":"sHQgQwF7Zsy8"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"GnEzgG7CZtPi","outputId":"6321fd1c-49ec-4485-cc5c-57c68336f744","executionInfo":{"status":"ok","timestamp":1726717663104,"user_tz":240,"elapsed":2,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":6,"outputs":[{"output_type":"stream","name":"stdout","text":["Highest return day: March 13\n","Maximum average return: 0.17601993965630328\n"]}]},{"cell_type":"markdown","source":["6. What is the total unit sales for each month, sorted in descending order?"],"metadata":{"id":"CgmcJcP-WQCz"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":492},"id":"CcMdW9ZyWdGe","executionInfo":{"status":"ok","timestamp":1726717663318,"user_tz":240,"elapsed":216,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"ebc2f657-3fca-4930-cb96-7c0256f14b54"},"execution_count":7,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Month\n","June 102627.0\n","May 94959.0\n","September 83921.0\n","July 83679.0\n","October 81907.0\n","November 77869.0\n","August 77655.0\n","January 74106.0\n","February 69420.0\n","March 69013.0\n","December 66594.0\n","April 64909.0\n","Name: daily_unit_sales, dtype: float64"],"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
daily_unit_sales
Month
June102627.0
May94959.0
September83921.0
July83679.0
October81907.0
November77869.0
August77655.0
January74106.0
February69420.0
March69013.0
December66594.0
April64909.0
\n","

"]},"metadata":{},"execution_count":7}]},{"cell_type":"markdown","source":["7. What is the average daily unit sales for each month, sorted in descending order?"],"metadata":{"id":"UznYCTCecPRE"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":492},"id":"K5ETLN3jcq9l","outputId":"7ecff0e7-ae50-47ca-f3eb-d1df604d6c2d","executionInfo":{"status":"ok","timestamp":1726717663318,"user_tz":240,"elapsed":4,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":8,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Month\n","June 3420.900000\n","May 3165.300000\n","September 2797.366667\n","July 2699.322581\n","October 2642.161290\n","November 2595.633333\n","August 2505.000000\n","February 2479.285714\n","January 2390.516129\n","March 2226.225806\n","April 2163.633333\n","December 2148.193548\n","Name: daily_unit_sales, dtype: float64"],"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
daily_unit_sales
Month
June3420.900000
May3165.300000
September2797.366667
July2699.322581
October2642.161290
November2595.633333
August2505.000000
February2479.285714
January2390.516129
March2226.225806
April2163.633333
December2148.193548
\n","

"]},"metadata":{},"execution_count":8}]},{"cell_type":"markdown","source":["8. How do you evaluate the two aggregation methods mentioned above for comparing sales across different months?"],"metadata":{"id":"f6y8SU3_WqOl"}},{"cell_type":"code","source":["# Your explanation goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dDAY6WnCW-iZ","executionInfo":{"status":"ok","timestamp":1726717663318,"user_tz":240,"elapsed":4,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}},"outputId":"0c25fd15-a974-40b5-85b9-405d4aa247bc"},"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["The sum method is best for understanding total sales volume, while the average method provides a more normalized view for comparing sales performance across months with different durations.\n"]}]},{"cell_type":"markdown","source":["9. What is the average percentage difference in daily unit sales compared to the previous year for each month?\n","\n","Hint: you need to use `percentage_difference_daily_unit_sales` (The percentage difference between `daily_unit_sales` and `previous_year_daily_unit_sales`)"],"metadata":{"id":"uz4FoL8fc4I2"}},{"cell_type":"code","source":["# Your code goes here\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":492},"id":"VmyzNczGdDy-","outputId":"cf727d6b-419f-4bf3-f608-5483713dc19f","executionInfo":{"status":"ok","timestamp":1726717663318,"user_tz":240,"elapsed":3,"user":{"displayName":"Mohammad Soltanieh Ha","userId":"12308918870841825745"}}},"execution_count":10,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Month\n","April -0.077647\n","August 0.145652\n","December 0.140123\n","February -0.029261\n","January -0.087711\n","July -0.053859\n","June -0.070052\n","March -0.054208\n","May 0.332456\n","November 0.255204\n","October 0.053964\n","September 0.070395\n","Name: percentage_difference_daily_unit_sales, dtype: float64"],"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
percentage_difference_daily_unit_sales
Month
April-0.077647
August0.145652
December0.140123
February-0.029261
January-0.087711
July-0.053859
June-0.070052
March-0.054208
May0.332456
November0.255204
October0.053964
September0.070395
\n","

"]},"metadata":{},"execution_count":10}]},{"cell_type":"markdown","source":["Can you sort the results above in the natural order of the months instead of the default alphabetical ordering?"],"metadata":{"id":"a_16A5sLYqj6"}}]} -------------------------------------------------------------------------------- /05-EDA/README.md: -------------------------------------------------------------------------------- 1 | Acknowledgment: The content of notebook 01-Exploratory-Data-Analysis.ipynb comes primarily from [a Kaggle notebook](https://www.kaggle.com/jsaguiar/exploratory-analysis-with-seaborn) by *Aguiar*. 2 | -------------------------------------------------------------------------------- /Docs/Cheatsheets/K-Means.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/K-Means.png -------------------------------------------------------------------------------- /Docs/Cheatsheets/Matplotlib-CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/Matplotlib-CheatSheet.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/Pandas-CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/Pandas-CheatSheet.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/Scikit-learn-CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/Scikit-learn-CheatSheet.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/Seaborn-CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/Seaborn-CheatSheet.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/python_cheatsheet_dictionaries.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/python_cheatsheet_dictionaries.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/python_cheatsheet_functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/python_cheatsheet_functions.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/python_cheatsheet_if_while.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/python_cheatsheet_if_while.pdf -------------------------------------------------------------------------------- /Docs/Cheatsheets/python_cheatsheet_lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/Cheatsheets/python_cheatsheet_lists.pdf -------------------------------------------------------------------------------- /Docs/a-whirlwind-tour-of-python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/Docs/a-whirlwind-tour-of-python.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mohammad Soltanieh-ha 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Bootcamp 2 | This repository is comprised of notebooks, datasets, and other related materials for Python Bootcamp, an introduction course to data science in Python. 3 | 4 | This repository can be accessed via this short link: 5 | ### bit.ly/py-bootcamp 6 | 7 | Most Notebooks come from [*A Whirlwind Tour of Python*](https://jakevdp.github.io/WhirlwindTourOfPython/) (free [100-page pdf](http://www.oreilly.com/programming/free/files/a-whirlwind-tour-of-python.pdf) - backup link [here](https://drive.google.com/file/d/10dt1dtlZAwgrEW9eQEE428a8IFgRgO3t/view?usp=sharing)) by Jake VanderPlas (under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) license). Slight modifications and updates have been made in some places to keep its content up to date. *A Whirlwind Tour of Python* is a fast-paced introduction to essential components of the Python language for data science and/or scientific programming. This material was written and tested using **Python 3.7**, and should work for any Python 3.X version. 8 | 9 | Some content of a handful of notebooks come from [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas (under the [MIT license](LICENSE-CODE). Read more at the [Open Source Initiative](https://opensource.org/licenses/MIT)). Modifications and updates have been made. 10 | -------------------------------------------------------------------------------- /data/120-years-of-olympic-history-athletes-and-results/noc_regions.csv: -------------------------------------------------------------------------------- 1 | NOC,region,notes AFG,Afghanistan, AHO,Curacao,Netherlands Antilles ALB,Albania, ALG,Algeria, AND,Andorra, ANG,Angola, ANT,Antigua,Antigua and Barbuda ANZ,Australia,Australasia ARG,Argentina, ARM,Armenia, ARU,Aruba, ASA,American Samoa, AUS,Australia, AUT,Austria, AZE,Azerbaijan, BAH,Bahamas, BAN,Bangladesh, BAR,Barbados, BDI,Burundi, BEL,Belgium, BEN,Benin, BER,Bermuda, BHU,Bhutan, BIH,Bosnia and Herzegovina, BIZ,Belize, BLR,Belarus, BOH,Czech Republic,Bohemia BOL,Boliva, BOT,Botswana, BRA,Brazil, BRN,Bahrain, BRU,Brunei, BUL,Bulgaria, BUR,Burkina Faso, CAF,Central African Republic, CAM,Cambodia, CAN,Canada, CAY,Cayman Islands, CGO,Republic of Congo, CHA,Chad, CHI,Chile, CHN,China, CIV,Ivory Coast, CMR,Cameroon, COD,Democratic Republic of the Congo, COK,Cook Islands, COL,Colombia, COM,Comoros, CPV,Cape Verde, CRC,Costa Rica, CRO,Croatia, CRT,Greece,Crete CUB,Cuba, CYP,Cyprus, CZE,Czech Republic, DEN,Denmark, DJI,Djibouti, DMA,Dominica, DOM,Dominican Republic, ECU,Ecuador, EGY,Egypt, ERI,Eritrea, ESA,El Salvador, ESP,Spain, EST,Estonia, ETH,Ethiopia, EUN,Russia, FIJ,Fiji, FIN,Finland, FRA,France, FRG,Germany, FSM,Micronesia, GAB,Gabon, GAM,Gambia, GBR,UK, GBS,Guinea-Bissau, GDR,Germany, GEO,Georgia, GEQ,Equatorial Guinea, GER,Germany, GHA,Ghana, GRE,Greece, GRN,Grenada, GUA,Guatemala, GUI,Guinea, GUM,Guam, GUY,Guyana, HAI,Haiti, HKG,China,Hong Kong HON,Honduras, HUN,Hungary, INA,Indonesia, IND,India, IOA,Individual Olympic Athletes,Individual Olympic Athletes IRI,Iran, IRL,Ireland, IRQ,Iraq, ISL,Iceland, ISR,Israel, ISV,"Virgin Islands, US",Virgin Islands ITA,Italy, IVB,"Virgin Islands, British", JAM,Jamaica, JOR,Jordan, JPN,Japan, KAZ,Kazakhstan, KEN,Kenya, KGZ,Kyrgyzstan, KIR,Kiribati, KOR,South Korea, KOS,Kosovo, KSA,Saudi Arabia, KUW,Kuwait, LAO,Laos, LAT,Latvia, LBA,Libya, LBR,Liberia, LCA,Saint Lucia, LES,Lesotho, LIB,Lebanon, LIE,Liechtenstein, LTU,Lithuania, LUX,Luxembourg, MAD,Madagascar, MAL,Malaysia, MAR,Morocco, MAS,Malaysia, MAW,Malawi, MDA,Moldova, MDV,Maldives, MEX,Mexico, MGL,Mongolia, MHL,Marshall Islands, MKD,Macedonia, MLI,Mali, MLT,Malta, MNE,Montenegro, MON,Monaco, MOZ,Mozambique, MRI,Mauritius, MTN,Mauritania, MYA,Myanmar, NAM,Namibia, NBO,Malaysia,North Borneo NCA,Nicaragua, NED,Netherlands, NEP,Nepal, NFL,Canada,Newfoundland NGR,Nigeria, NIG,Niger, NOR,Norway, NRU,Nauru, NZL,New Zealand, OMA,Oman, PAK,Pakistan, PAN,Panama, PAR,Paraguay, PER,Peru, PHI,Philippines, PLE,Palestine, PLW,Palau, PNG,Papua New Guinea, POL,Poland, POR,Portugal, PRK,North Korea, PUR,Puerto Rico, QAT,Qatar, RHO,Zimbabwe, ROT,NA,Refugee Olympic Team ROU,Romania, RSA,South Africa, RUS,Russia, RWA,Rwanda, SAA,Germany, SAM,Samoa, SCG,Serbia,Serbia and Montenegro SEN,Senegal, SEY,Seychelles, SIN,Singapore, SKN,Saint Kitts,Turks and Caicos Islands SLE,Sierra Leone, SLO,Slovenia, SMR,San Marino, SOL,Solomon Islands, SOM,Somalia, SRB,Serbia, SRI,Sri Lanka, SSD,South Sudan, STP,Sao Tome and Principe, SUD,Sudan, SUI,Switzerland, SUR,Suriname, SVK,Slovakia, SWE,Sweden, SWZ,Swaziland, SYR,Syria, TAN,Tanzania, TCH,Czech Republic, TGA,Tonga, THA,Thailand, TJK,Tajikistan, TKM,Turkmenistan, TLS,Timor-Leste, TOG,Togo, TPE,Taiwan, TTO,Trinidad,Trinidad and Tobago TUN,Tunisia, TUR,Turkey, TUV,NA,Tuvalu UAE,United Arab Emirates, UAR,Syria,United Arab Republic UGA,Uganda, UKR,Ukraine, UNK,NA,Unknown URS,Russia, URU,Uruguay, USA,USA, UZB,Uzbekistan, VAN,Vanuatu, VEN,Venezuela, VIE,Vietnam, VIN,Saint Vincent, VNM,Vietnam, WIF,Trinidad,West Indies Federation YAR,Yemen,North Yemen YEM,Yemen, YMD,Yemen,South Yemen YUG,Serbia,Yugoslavia ZAM,Zambia, ZIM,Zimbabwe, -------------------------------------------------------------------------------- /data/GOT-battles.csv: -------------------------------------------------------------------------------- 1 | name,year,battle_number,attacker_king,defender_king,attacker_1,attacker_2,attacker_3,attacker_4,defender_1,defender_2,defender_3,defender_4,attacker_outcome,battle_type,major_death,major_capture,attacker_size,defender_size,attacker_commander,defender_commander,summer,location,region,note Battle of the Golden Tooth,298,1,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,win,pitched battle,1,0,15000,4000,Jaime Lannister,"Clement Piper, Vance",1,Golden Tooth,The Westerlands, Battle at the Mummer's Ford,298,2,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Baratheon,,,,win,ambush,1,0,,120,Gregor Clegane,Beric Dondarrion,1,Mummer's Ford,The Riverlands, Battle of Riverrun,298,3,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,win,pitched battle,0,1,15000,10000,"Jaime Lannister, Andros Brax","Edmure Tully, Tytos Blackwood",1,Riverrun,The Riverlands, Battle of the Green Fork,298,4,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,loss,pitched battle,1,1,18000,20000,"Roose Bolton, Wylis Manderly, Medger Cerwyn, Harrion Karstark, Halys Hornwood","Tywin Lannister, Gregor Clegane, Kevan Lannister, Addam Marbrand",1,Green Fork,The Riverlands, Battle of the Whispering Wood,298,5,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,1,1,1875,6000,"Robb Stark, Brynden Tully",Jaime Lannister,1,Whispering Wood,The Riverlands, Battle of the Camps,298,6,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,0,0,6000,12625,"Robb Stark, Tytos Blackwood, Brynden Tully","Lord Andros Brax, Forley Prester",1,Riverrun,The Riverlands, Sack of Darry,298,7,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Darry,,,,win,pitched battle,0,0,,,Gregor Clegane,Lyman Darry,1,Darry,The Riverlands, Battle of Moat Cailin,299,8,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,pitched battle,0,0,,,Victarion Greyjoy,,1,Moat Cailin,The North, Battle of Deepwood Motte,299,9,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,siege,0,0,1000,,Asha Greyjoy,,1,Deepwood Motte,The North, Battle of the Stony Shore,299,10,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,ambush,0,0,264,,Theon Greyjoy,,1,Stony Shore,The North,"Greyjoy's troop number based on the Battle of Deepwood Motte, in which Asha had 1000 soldier on 30 longships. That comes out to ~33 per longship. In the Battle of the Stony Shore, Theon has 8 longships, and just we can estimate that he has 8*33 =265 troops." Battle of Torrhen's Square,299,11,Robb Stark,Balon/Euron Greyjoy,Stark,,,,Greyjoy,,,,win,pitched battle,0,0,244,900,"Rodrik Cassel, Cley Cerwyn",Dagmer Cleftjaw,1,Torrhen's Square,The North,Greyjoy's troop number comes from the 264 estimate to have arrived on the stony shore minus the 20 Theon takes to attack Winterfell. Thus 264-20=244 Battle of Winterfell,299,12,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,ambush,0,1,20,,Theon Greyjoy,Bran Stark,1,Winterfell,The North,"It isn't mentioned how many Stark men are left in Winterfell, other than ""very few""." Sack of Torrhen's Square,299,13,Balon/Euron Greyjoy,Balon/Euron Greyjoy,Greyjoy,,,,Stark,,,,win,siege,0,1,,,Dagmer Cleftjaw,,1,Torrhen's Square,The North, Sack of Winterfell,299,14,Joffrey/Tommen Baratheon,Robb Stark,Bolton,Greyjoy,,,Stark,,,,win,ambush,1,0,618,2000,"Ramsay Snow, Theon Greyjoy ","Rodrik Cassel, Cley Cerwyn, Leobald Tallhart",1,Winterfell,The North,"Since House Bolton betrays the Starks for House Lannister, we code this battle as between these two houses. Greyjoy men, numbering only 20, don't play a major part in the fighting and end up dying anyway." Battle of Oxcross,299,15,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,1,1,6000,10000,"Robb Stark, Brynden Tully","Stafford Lannister, Roland Crakehall, Antario Jast",1,Oxcross,The Westerlands, Siege of Storm's End,299,16,Stannis Baratheon,Renly Baratheon,Baratheon,,,,Baratheon,,,,win,siege,1,0,5000,20000,"Stannis Baratheon, Davos Seaworth","Renly Baratheon, Cortnay Penrose, Loras Tyrell, Randyll Tarly, Mathis Rowan",1,Storm's End,The Stormlands, Battle of the Fords,299,17,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,loss,pitched battle,0,0,20000,10000,"Tywin Lannister, Flement Brax, Gregor Clegane, Addam Marbrand, Lyle Crakehall, Leo Lefford","Edmure Tully, Jason Mallister, Karyl Vance",1,Red Fork,The Riverlands, Sack of Harrenhal,299,18,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,win,ambush,1,0,100,100,"Roose Bolton, Vargo Hoat, Robett Glover",Amory Lorch,1,Harrenhal,The Riverlands, Battle of the Crag,299,19,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,win,ambush,0,0,6000,,"Robb Stark, Smalljon Umber, Black Walder Frey",Rolph Spicer,1,Crag,The Westerlands, Battle of the Blackwater,299,20,Stannis Baratheon,Joffrey/Tommen Baratheon,Baratheon,,,,Lannister,,,,loss,pitched battle,1,1,21000,7250,"Stannis Baratheon, Imry Florent, Guyard Morrigen, Rolland Storm, Salladhor Saan, Davos Seaworth","Tyrion Lannister, Jacelyn Bywater, Sandor Clegane, Tywin Lannister, Garlan Tyrell, Mace Tyrell, Randyll Tarly",1,King's Landing,The Crownlands, Siege of Darry,299,21,Robb Stark,Joffrey/Tommen Baratheon,Darry,,,,Lannister,,,,win,siege,0,0,,,Helman Tallhart,,1,Darry,The Riverlands, Battle of Duskendale,299,22,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,loss,pitched battle,1,0,3000,,"Robertt Glover, Helman Tallhart","Randyll Tarly, Gregor Clegane",1,Duskendale,The Crownlands, Battle of the Burning Septry,299,23,,,Brotherhood without Banners,,,,Brave Companions,,,,win,pitched battle,0,0,,,,,1,,The Riverlands, Battle of the Ruby Ford,299,24,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Stark,,,,win,pitched battle,0,0,,6000,Gregor Clegane,"Roose Bolton, Wylis Manderly",,Ruby Ford,The Riverlands, Retaking of Harrenhal,299,25,Joffrey/Tommen Baratheon,,Lannister,,,,Brave Companions,,,,win,pitched battle,1,0,,,Gregor Clegane,Vargo Hoat,1,Harrenhal,The Riverlands, The Red Wedding,299,26,Joffrey/Tommen Baratheon,Robb Stark,Frey,Bolton,,,Stark,,,,win,ambush,1,1,3500,3500,"Walder Frey, Roose Bolton, Walder Rivers",Robb Stark,1,The Twins,The Riverlands,"This observation refers to the battle against the Stark men, not the attack on the wedding" Siege of Seagard,299,27,Robb Stark,Joffrey/Tommen Baratheon,Frey,,,,Mallister,,,,win,siege,0,1,,,Walder Frey,Jason Mallister,1,Seagard,The Riverlands, Battle of Castle Black,300,28,Stannis Baratheon,Mance Rayder,Free folk,Thenns,Giants,,Night's Watch,Baratheon,,,loss,siege,1,1,100000,1240,"Mance Rayder, Tormund Giantsbane, Harma Dogshead, Magnar Styr, Varamyr","Stannis Baratheon, Jon Snow, Donal Noye, Cotter Pyke",0,Castle Black,Beyond the Wall, Fall of Moat Cailin,300,29,Joffrey/Tommen Baratheon,Balon/Euron Greyjoy,Bolton,,,,Greyjoy,,,,win,siege,0,0,,,Ramsey Bolton,,0,Moat Cailin,The North, Sack of Saltpans,300,30,,,Brave Companions,,,,,,,,win,razing,0,0,,,Rorge,,0,Saltpans,The Riverlands, Retaking of Deepwood Motte,300,31,Stannis Baratheon,Balon/Euron Greyjoy,Baratheon,Karstark,Mormont,Glover,Greyjoy,,,,win,pitched battle,0,0,4500,200,"Stannis Baratheon, Alysane Mormot",Asha Greyjoy,0,Deepwood Motte,The North, Battle of the Shield Islands,300,32,Balon/Euron Greyjoy,Joffrey/Tommen Baratheon,Greyjoy,,,,Tyrell,,,,win,pitched battle,0,0,,,"Euron Greyjoy, Victarion Greyjoy",,0,Shield Islands,The Reach, "Invasion of Ryamsport, Vinetown, and Starfish Harbor",300,33,Balon/Euron Greyjoy,Joffrey/Tommen Baratheon,Greyjoy,,,,Tyrell,,,,win,razing,0,0,,,"Euron Greyjoy, Victarion Greyjoy",,0,"Ryamsport, Vinetown, Starfish Harbor",The Reach, Second Seige of Storm's End,300,34,Joffrey/Tommen Baratheon,Stannis Baratheon,Baratheon,,,,Baratheon,,,,win,siege,0,0,,200,"Mace Tyrell, Mathis Rowan",Gilbert Farring,0,Storm's End,The Stormlands, Siege of Dragonstone,300,35,Joffrey/Tommen Baratheon,Stannis Baratheon,Baratheon,,,,Baratheon,,,,win,siege,0,0,2000,,"Loras Tyrell, Raxter Redwyne",Rolland Storm,0,Dragonstone,The Stormlands, Siege of Riverrun,300,36,Joffrey/Tommen Baratheon,Robb Stark,Lannister,Frey,,,Tully,,,,win,siege,0,0,3000,,"Daven Lannister, Ryman Fey, Jaime Lannister",Brynden Tully,0,Riverrun,The Riverlands, Siege of Raventree,300,37,Joffrey/Tommen Baratheon,Robb Stark,Bracken,Lannister,,,Blackwood,,,,win,siege,0,1,1500,,"Jonos Bracken, Jaime Lannister",Tytos Blackwood,0,Raventree,The Riverlands, Siege of Winterfell,300,38,Stannis Baratheon,Joffrey/Tommen Baratheon,Baratheon,Karstark,Mormont,Glover,Bolton,Frey,,,,,,,5000,8000,Stannis Baratheon,Roose Bolton,0,Winterfell,The North, -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product1-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product1-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product1-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product1-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product1-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product1-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product2-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product2-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product2-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product2-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product2-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product2-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product3-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product3-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product3-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product3-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product3-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product3-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product4-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product4-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product4-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product4-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product4-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product4-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product5-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product5-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product5-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product5-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product5-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product5-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product6-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product6-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product6-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product6-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product6-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product6-2022.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product7-2020.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product7-2020.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product7-2021.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product7-2021.xlsx -------------------------------------------------------------------------------- /data/MEVGAL-Dairy-Sales/product7-2022.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/MEVGAL-Dairy-Sales/product7-2022.xlsx -------------------------------------------------------------------------------- /data/Pandas-Example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/Pandas-Example.xlsx -------------------------------------------------------------------------------- /data/anscombe.json: -------------------------------------------------------------------------------- 1 | [ 2 | {"Series":"I", "X":10.0, "Y":8.04}, 3 | {"Series":"I", "X":8.0, "Y":6.95}, 4 | {"Series":"I", "X":13.0, "Y":7.58}, 5 | {"Series":"I", "X":9.0, "Y":8.81}, 6 | {"Series":"I", "X":11.0, "Y":8.33}, 7 | {"Series":"I", "X":14.0, "Y":9.96}, 8 | {"Series":"I", "X":6.0, "Y":7.24}, 9 | {"Series":"I", "X":4.0, "Y":4.26}, 10 | {"Series":"I", "X":12.0, "Y":10.84}, 11 | {"Series":"I", "X":7.0, "Y":4.81}, 12 | {"Series":"I", "X":5.0, "Y":5.68}, 13 | 14 | {"Series":"II", "X":10.0, "Y":9.14}, 15 | {"Series":"II", "X":8.0, "Y":8.14}, 16 | {"Series":"II", "X":13.0, "Y":8.74}, 17 | {"Series":"II", "X":9.0, "Y":8.77}, 18 | {"Series":"II", "X":11.0, "Y":9.26}, 19 | {"Series":"II", "X":14.0, "Y":8.10}, 20 | {"Series":"II", "X":6.0, "Y":6.13}, 21 | {"Series":"II", "X":4.0, "Y":3.10}, 22 | {"Series":"II", "X":12.0, "Y":9.13}, 23 | {"Series":"II", "X":7.0, "Y":7.26}, 24 | {"Series":"II", "X":5.0, "Y":4.74}, 25 | 26 | {"Series":"III", "X":10.0, "Y":7.46}, 27 | {"Series":"III", "X":8.0, "Y":6.77}, 28 | {"Series":"III", "X":13.0, "Y":12.74}, 29 | {"Series":"III", "X":9.0, "Y":7.11}, 30 | {"Series":"III", "X":11.0, "Y":7.81}, 31 | {"Series":"III", "X":14.0, "Y":8.84}, 32 | {"Series":"III", "X":6.0, "Y":6.08}, 33 | {"Series":"III", "X":4.0, "Y":5.39}, 34 | {"Series":"III", "X":12.0, "Y":8.15}, 35 | {"Series":"III", "X":7.0, "Y":6.42}, 36 | {"Series":"III", "X":5.0, "Y":5.73}, 37 | 38 | {"Series":"IV", "X":8.0, "Y":6.58}, 39 | {"Series":"IV", "X":8.0, "Y":5.76}, 40 | {"Series":"IV", "X":8.0, "Y":7.71}, 41 | {"Series":"IV", "X":8.0, "Y":8.84}, 42 | {"Series":"IV", "X":8.0, "Y":8.47}, 43 | {"Series":"IV", "X":8.0, "Y":7.04}, 44 | {"Series":"IV", "X":8.0, "Y":5.25}, 45 | {"Series":"IV", "X":19.0, "Y":12.50}, 46 | {"Series":"IV", "X":8.0, "Y":5.56}, 47 | {"Series":"IV", "X":8.0, "Y":7.91}, 48 | {"Series":"IV", "X":8.0, "Y":6.89} 49 | ] 50 | -------------------------------------------------------------------------------- /data/datasets.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/datasets.xlsx -------------------------------------------------------------------------------- /data/movies_metadata.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/5f580c3e2419a41a487420e21ec8d7e2c064e447/data/movies_metadata.csv -------------------------------------------------------------------------------- /data/mtcars.csv: -------------------------------------------------------------------------------- 1 | ,mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb 2 | 0,21.0,6,160.0,110,3.9,2.62,16.46,0,1,4,4 3 | 1,21.0,6,160.0,110,3.9,2.875,17.02,0,1,4,4 4 | 2,22.8,4,108.0,93,3.85,2.32,18.61,1,1,4,1 5 | 3,21.4,6,258.0,110,3.08,3.215,19.44,1,0,3,1 6 | 4,18.7,8,360.0,175,3.15,3.44,17.02,0,0,3,2 7 | 5,18.1,6,225.0,105,2.76,3.46,20.22,1,0,3,1 8 | 6,14.3,8,360.0,245,3.21,3.57,15.84,0,0,3,4 9 | 7,24.4,4,146.7,62,3.69,3.19,20.0,1,0,4,2 10 | 8,22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2 11 | 9,19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4 12 | 10,17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4 13 | 11,16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3 14 | 12,17.3,8,275.8,180,3.07,3.73,17.6,0,0,3,3 15 | 13,15.2,8,275.8,180,3.07,3.78,18.0,0,0,3,3 16 | 14,10.4,8,472.0,205,2.93,5.25,17.98,0,0,3,4 17 | 15,10.4,8,460.0,215,3.0,5.424,17.82,0,0,3,4 18 | 16,14.7,8,440.0,230,3.23,5.345,17.42,0,0,3,4 19 | 17,32.4,4,78.7,66,4.08,2.2,19.47,1,1,4,1 20 | 18,30.4,4,75.7,52,4.93,1.615,18.52,1,1,4,2 21 | 19,33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1 22 | 20,21.5,4,120.1,97,3.7,2.465,20.01,1,0,3,1 23 | 21,15.5,8,318.0,150,2.76,3.52,16.87,0,0,3,2 24 | 22,15.2,8,304.0,150,3.15,3.435,17.3,0,0,3,2 25 | 23,13.3,8,350.0,245,3.73,3.84,15.41,0,0,3,4 26 | 24,19.2,8,400.0,175,3.08,3.845,17.05,0,0,3,2 27 | 25,27.3,4,79.0,66,4.08,1.935,18.9,1,1,4,1 28 | 26,26.0,4,120.3,91,4.43,2.14,16.7,0,1,5,2 29 | 27,30.4,4,95.1,113,3.77,1.513,16.9,1,1,5,2 30 | 28,15.8,8,351.0,264,4.22,3.17,14.5,0,1,5,4 31 | 29,19.7,6,145.0,175,3.62,2.77,15.5,0,1,5,6 32 | 30,15.0,8,301.0,335,3.54,3.57,14.6,0,1,5,8 33 | 31,21.4,4,121.0,109,4.11,2.78,18.6,1,1,4,2 34 | -------------------------------------------------------------------------------- /data/state-abbrevs.csv: -------------------------------------------------------------------------------- 1 | "state","abbreviation" 2 | "Alabama","AL" 3 | "Alaska","AK" 4 | "Arizona","AZ" 5 | "Arkansas","AR" 6 | "California","CA" 7 | "Colorado","CO" 8 | "Connecticut","CT" 9 | "Delaware","DE" 10 | "District of Columbia","DC" 11 | "Florida","FL" 12 | "Georgia","GA" 13 | "Hawaii","HI" 14 | "Idaho","ID" 15 | "Illinois","IL" 16 | "Indiana","IN" 17 | "Iowa","IA" 18 | "Kansas","KS" 19 | "Kentucky","KY" 20 | "Louisiana","LA" 21 | "Maine","ME" 22 | "Montana","MT" 23 | "Nebraska","NE" 24 | "Nevada","NV" 25 | "New Hampshire","NH" 26 | "New Jersey","NJ" 27 | "New Mexico","NM" 28 | "New York","NY" 29 | "North Carolina","NC" 30 | "North Dakota","ND" 31 | "Ohio","OH" 32 | "Oklahoma","OK" 33 | "Oregon","OR" 34 | "Maryland","MD" 35 | "Massachusetts","MA" 36 | "Michigan","MI" 37 | "Minnesota","MN" 38 | "Mississippi","MS" 39 | "Missouri","MO" 40 | "Pennsylvania","PA" 41 | "Rhode Island","RI" 42 | "South Carolina","SC" 43 | "South Dakota","SD" 44 | "Tennessee","TN" 45 | "Texas","TX" 46 | "Utah","UT" 47 | "Vermont","VT" 48 | "Virginia","VA" 49 | "Washington","WA" 50 | "West Virginia","WV" 51 | "Wisconsin","WI" 52 | "Wyoming","WY" -------------------------------------------------------------------------------- /data/state-areas.csv: -------------------------------------------------------------------------------- 1 | state,area (sq. mi) 2 | Alabama,52423 3 | Alaska,656425 4 | Arizona,114006 5 | Arkansas,53182 6 | California,163707 7 | Colorado,104100 8 | Connecticut,5544 9 | Delaware,1954 10 | Florida,65758 11 | Georgia,59441 12 | Hawaii,10932 13 | Idaho,83574 14 | Illinois,57918 15 | Indiana,36420 16 | Iowa,56276 17 | Kansas,82282 18 | Kentucky,40411 19 | Louisiana,51843 20 | Maine,35387 21 | Maryland,12407 22 | Massachusetts,10555 23 | Michigan,96810 24 | Minnesota,86943 25 | Mississippi,48434 26 | Missouri,69709 27 | Montana,147046 28 | Nebraska,77358 29 | Nevada,110567 30 | New Hampshire,9351 31 | New Jersey,8722 32 | New Mexico,121593 33 | New York,54475 34 | North Carolina,53821 35 | North Dakota,70704 36 | Ohio,44828 37 | Oklahoma,69903 38 | Oregon,98386 39 | Pennsylvania,46058 40 | Rhode Island,1545 41 | South Carolina,32007 42 | South Dakota,77121 43 | Tennessee,42146 44 | Texas,268601 45 | Utah,84904 46 | Vermont,9615 47 | Virginia,42769 48 | Washington,71303 49 | West Virginia,24231 50 | Wisconsin,65503 51 | Wyoming,97818 52 | District of Columbia,68 53 | Puerto Rico,3515 54 | --------------------------------------------------------------------------------