├── Chapter-01:Google_Colab.ipynb ├── Chapter-02:Fundamentals-of-Python.ipynb ├── Chapter-03:Sequences.ipynb ├── Chapter-04:Other-Python-Data-Structures.ipynb ├── Chapter-05:Execution-control.ipynb ├── Chapter-06:Functions.ipynb ├── Chapter-07:Numpy.ipynb ├── Chapter-08:SciPy.ipynb ├── Chapter-09:Pandas.ipynb ├── Chapter-10:Visualization.ipynb ├── Chapter-11:Machine-Learning-Libraries.ipynb ├── Chapter-12:NLTK.ipynb ├── Chapter-13:Functional-Programming.ipynb ├── Chapter-14:Object-Oriented-Programming.ipynb ├── Chapter-15:Other-Topics.ipynb ├── Data-Conversion-Recipes.ipynb └── README.md /Chapter-01:Google_Colab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chpt.1.Google.Colab.ipynb", 7 | "provenance": [], 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "markdown", 18 | "metadata": { 19 | "id": "view-in-github", 20 | "colab_type": "text" 21 | }, 22 | "source": [ 23 | "\"Open" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "id": "_BVYiC2JI24d" 30 | }, 31 | "source": [ 32 | "# Introduction to Google Colab\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": { 38 | "id": "p1FX8n0icSU6" 39 | }, 40 | "source": [ 41 | "\n", 42 | "## Running Python Statements\n", 43 | "Historically Python was invoked either in an interactive Python shell, or supplying text files to the interpreter. If you have Python installed on your system, you can open the PYthon built-in interactive shell by typing 'python' on the command line:\n", 44 | "\n", 45 | "```python\n", 46 | "% python\n", 47 | "Python 3.9.1 (default, Mar 7 2021, 09:53:19) \n", 48 | "[Clang 12.0.0 (clang-1200.0.32.29)] on darwin\n", 49 | "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", 50 | ">>>\n", 51 | "```\n", 52 | "Python statements can then be typed in and run by hitting Enter:\n", 53 | "```\n", 54 | ">>> print(\"Hello\")\n", 55 | "Hello\n", 56 | "```\n", 57 | "The result of each statement is displaying directly after the statements line. The IPython project is a more feature rich version of the Python interactive shell. \n", 58 | "\n", 59 | "If Python commands are stored in a text file with the extension .py, they can be run on the command line by typing 'python' followed by the file name. If we have a file named hello.py, an in it we have the statement\n", 60 | "```\n", 61 | "print(\"Hello\")\n", 62 | "```\n", 63 | "We can invoke it on the command line and its output is displayed on the next line:\n", 64 | "```\n", 65 | "% python hello.py \n", 66 | "Hello\n", 67 | "```\n", 68 | "For traditional PYthon software projects, the interactive shell was adequete as a place to figure out syntax, or do simple expieriments. The file based code was where the real development took place. This is where software was written. THese files could be distributed to what ever environment needed to run the code. For scientic computing, neither of these solutions was ideal. Scientists wanted to have interactive engagement with data while still be able to persist and share in a document based format. That's were notebook based development filled the gap.\n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "id": "9a6HyKL1cVdO" 75 | }, 76 | "source": [ 77 | "\n", 78 | "## Jupyter Notebooks\n", 79 | "\n", 80 | "The Jupyter project sprung from the IPython project. Jupyter notebooks combine the interactive nature of the PYthon shell with the persistance of a document based format. Notebooks are executable documents, combing executable code with formatted text. THese documents are composed of cells. Cells contain code or text. When a code cell is executed, any output is displayed directly below. Any state changes performed by a code cell are shared by any cells executed subsequently. This allows you to build up your code cell by cell, without having to rerun the whole document when you make a change. This is espicially useful when you are exploring and experiementing with data. Jupyter notebooks have been widely adopted for data science work. They can be run locally from your machine, or from hosted services such as those provided by AWS, kaggle, DataBricks, or Google. \n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": { 86 | "id": "L3oZiqfCcXVB" 87 | }, 88 | "source": [ 89 | "\n", 90 | "## Google Colab\n", 91 | "\n", 92 | "Colab is Googles hosted notebook service. It is a great way to get started with Python, as you don't need to install anything, or deal with library dependencis or environment management. This book will use Colab notebooks for all of it's examples. To use Colab, you must be signed into a Google account. Follow this link to access Colab:\n", 93 | "\n", 94 | "https://colab.research.google.com\n", 95 | "\n", 96 | "From here you can create a new notebook, or open existing notebooks. The existing notebooks can include examples supplied by Google, notebooks you have previously created, or notebooks you have copied to your Google Drive.\n", 97 | "\n", 98 | "Figure 1.1-Colab.Dialogue\n", 99 | "\n", 100 | "When you choose to create a new notebook, it is opened in a new browser tab. The notebook will have a default title, 'Untitled0.ipynb' if it is your first. To change it's name, double click on the title and type the new name.\n", 101 | "\n", 102 | "Figure 1.3. nameing notebook. \n", 103 | "\n", 104 | "Colab will automatically save your notebooks to your Google Drive. THe default location will be a directory named 'Colab Notebooks'. You can access your drive at Drive.Google.com.\n", 105 | "\n", 106 | "Figure 1.4 Google Drive\n", 107 | "\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": { 113 | "id": "QL_jr1CJcm7D" 114 | }, 115 | "source": [ 116 | "### Text Cells\n", 117 | "Your new notebook will have a single code cell. Cells can be of two types, text and code. You can add new cells by using the '+ Code' and '+ Text' buttons in the upper left of the notebook interface. Text cells are formatted using a language called Markdown (https://colab.research.google.com/notebooks/markdown_guide.ipynb) . To edit a cell, double click on it. The Markdown will appear to the right, and a preview of its output to the left.\n", 118 | "\n", 119 | "\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": { 125 | "id": "liDzGlQXnkHQ" 126 | }, 127 | "source": [ 128 | "**Bold**\n", 129 | "\n", 130 | "_italic_\n", 131 | "\n", 132 | "~strikethrough~\n", 133 | "\n", 134 | "`monospace`" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": { 140 | "id": "Ximyn_F0DUi4" 141 | }, 142 | "source": [ 143 | "Lists can be created by prefacing items with either numbers or stars." 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": { 149 | "id": "U5YwrpMuDjB1" 150 | }, 151 | "source": [ 152 | "1. Ordered item\n", 153 | "2. Ordered item\n", 154 | "\n", 155 | "* Unordered item\n", 156 | "* Unordered item" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": { 162 | "id": "oIO1Mxc6p7lY" 163 | }, 164 | "source": [ 165 | "Headings are designated text preceeded by hashsigns. A single hashsign is a top level heading, two hashes a first level heading and so forth." 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": { 171 | "id": "ABw0v-XIqLjF" 172 | }, 173 | "source": [ 174 | "A heading which at the top of a cell determines its hierarchy in the document. You can view this hierarchy in the table of contents, which is opened by a button in the top left of the notebook interface. You can the table of contents to navigate the document by clicking on the displayed headings. A heading cell which has child cells will have a triangle next to the heading text. This triangle can be used to hide or view the children." 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": { 180 | "id": "F7BNm0r3hdMB" 181 | }, 182 | "source": [ 183 | "#### Latex\n", 184 | "\n", 185 | "The LaTex language (https://www.latex-project.org/about/) is a designed for the preparation of technical documents. It excels at the presentation of mathematical text using a code based approach designed to allow an author to concentrate on content rather than layout. LaTex code can be inserted in Colab text cells by surrounding it in dollar signs, $. Figure.. takes an example from the LaTex documentation and embeds it in a Colab text cell.\n" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": { 191 | "id": "zRqu8X5-GLxy" 192 | }, 193 | "source": [ 194 | "$\\begin{equation*}\n", 195 | "\\left.\\begin{aligned}\n", 196 | "B’&=-\\partial\\times E,\\\\\n", 197 | "E’&=\\partial\\times B - 4\\pi j,\n", 198 | "\\end{aligned}\n", 199 | "\\right\\}\n", 200 | "\\qquad \\text{Maxwell’s equations}\n", 201 | "\\end{equation*}$" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "metadata": { 207 | "id": "eIazTffqgbfy" 208 | }, 209 | "source": [ 210 | "" 211 | ], 212 | "execution_count": null, 213 | "outputs": [] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": { 218 | "id": "3Wlr91r1tHGl" 219 | }, 220 | "source": [ 221 | "### Code Cells\n", 222 | "\n", 223 | "Code cells are used to write and execute Python code. Python statements are typed into a cell. To execute them, either use the play button at the left of the cell, or press Shift-Enter. The latter will take you to the next cell, or create a new cell if there are none following. Any output from the code executed will be displayed below the cell itself." 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "metadata": { 229 | "colab": { 230 | "base_uri": "https://localhost:8080/" 231 | }, 232 | "id": "rOgLWSsjHinw", 233 | "outputId": "97829a8d-4464-4438-8c95-3656d972da5d" 234 | }, 235 | "source": [ 236 | "print(\"Hello\")" 237 | ], 238 | "execution_count": null, 239 | "outputs": [ 240 | { 241 | "output_type": "stream", 242 | "text": [ 243 | "Hello\n" 244 | ], 245 | "name": "stdout" 246 | } 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": { 252 | "id": "Vg6j3k-ZeVfS" 253 | }, 254 | "source": [ 255 | "We will be using only code cells for the subsequent chapters of this book." 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": { 261 | "id": "PoTYa97_ibjo" 262 | }, 263 | "source": [ 264 | "### Files\n", 265 | "To see files and folders available in Colab, click the files button on the left of the interface. By default you will have access to the sample_data folder which Google supplies. You can also use the upload button to upload files to the session \n", 266 | "**TAKE FIGURE**. These files will only be available in the current session of your document. If you come back to the same document later, you will need to re-upload them. All files available in colab have a path root of '/content/'. So if you upload a file named 'heights.over.time.csv', it's path would be '/content/heights.over.time.csv'. You can also mount your Google Drive using the Mount Drive button **TAKE FIGURE**. The contents of you drive will have the root path of '/content/drive'. " 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": { 272 | "id": "DJkS-qihK96l" 273 | }, 274 | "source": [ 275 | "#### Manage Colab Documents\n", 276 | "By default notebooks are saved to your Google Drive. Under the File menu there are other options for saveing notebooks. You can save them to GitHub, either as a Gist or as a tracked file. You can also download them either in Jupyter notebook format, .ipynb, or as Python files, .py. You can also share notebooks using the share button in the upper right of the notebook interface. \n" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": { 282 | "id": "T0NUKNVvhOsk" 283 | }, 284 | "source": [ 285 | "### Code Snippets\n", 286 | "\n", 287 | "The Code snippets section of the left navigation section lets you search and select examplte code snipper. Selected snippets can be inserted using the INSERT button. Code snippets are a great way to see examples of what can be done in Colab, including making interactive forms, downloading data, and various visualization options. **TAKE FIGURE**" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": { 293 | "id": "JDjQ-HLK-ZM9" 294 | }, 295 | "source": [ 296 | "### Existing Collections\n", 297 | "\n", 298 | "Notebooks are a great way to explain and demonstrate techniques, concepts, and workflows. There are many collections of notebooks available around the web where Data Science work is shared. Kaggle has plenty of shared notebooks, as does the Google Seedbank.\n", 299 | "https://research.google.com/seedbank/" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": { 305 | "id": "cLVqdEYajvzO" 306 | }, 307 | "source": [ 308 | "### System aliases\n", 309 | "You can run shell commands from within a code cell by prepending the command with an exclamation point, for example, to print the working directory:" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "metadata": { 315 | "colab": { 316 | "base_uri": "https://localhost:8080/" 317 | }, 318 | "id": "Y_YjIR0ecRQL", 319 | "outputId": "6c8f7da7-027a-4c77-93b4-e2bd48949011" 320 | }, 321 | "source": [ 322 | "!pwd" 323 | ], 324 | "execution_count": null, 325 | "outputs": [ 326 | { 327 | "output_type": "stream", 328 | "text": [ 329 | "/content\n" 330 | ], 331 | "name": "stdout" 332 | } 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": { 338 | "id": "Rx1p3YEKcWke" 339 | }, 340 | "source": [ 341 | "Any output from running a shell command can be captured in a Python variable and used in subsequent code, don't worry about variables yet, we will cover them in a later chapter." 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "metadata": { 347 | "id": "4zkTWpFuxePQ", 348 | "colab": { 349 | "base_uri": "https://localhost:8080/" 350 | }, 351 | "outputId": "25f32478-d0a3-48b8-d373-18c6184535b3" 352 | }, 353 | "source": [ 354 | "var = !ls sample_data\n", 355 | "print(var)" 356 | ], 357 | "execution_count": null, 358 | "outputs": [ 359 | { 360 | "output_type": "stream", 361 | "text": [ 362 | "['anscombe.json\\t\\t mnist_test.csv', 'california_housing_test.csv mnist_train_small.csv', 'california_housing_train.csv README.md']\n" 363 | ], 364 | "name": "stdout" 365 | } 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": { 371 | "id": "Gd-ORjWHdM2c" 372 | }, 373 | "source": [ 374 | "### Magic Functions\n", 375 | "Magic functions are functions which change the way a code cell is run. For example, you can time a python statement using the magic function `%timeit()`:" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "metadata": { 381 | "id": "ZE7cOOH6PHL2", 382 | "colab": { 383 | "base_uri": "https://localhost:8080/" 384 | }, 385 | "outputId": "41dfbddc-cd6a-41ab-ea94-58ffec9c7ed2" 386 | }, 387 | "source": [ 388 | "import time\n", 389 | "%timeit(time.sleep(1))" 390 | ], 391 | "execution_count": null, 392 | "outputs": [ 393 | { 394 | "output_type": "stream", 395 | "text": [ 396 | "1 loop, best of 5: 1 s per loop\n" 397 | ], 398 | "name": "stdout" 399 | } 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": { 405 | "id": "Ff1L609xdPVg" 406 | }, 407 | "source": [ 408 | "Or have a cell run html code using the magic function `%%html`:" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "metadata": { 414 | "id": "DONtrcwFPLIu" 415 | }, 416 | "source": [ 417 | "%%html\n", 418 | "Whee!" 419 | ], 420 | "execution_count": null, 421 | "outputs": [] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": { 426 | "id": "b_JhnvvXdXjy" 427 | }, 428 | "source": [ 429 | "You can find more information about magic functions in Cell Magics example notebook\n", 430 | "(https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": { 436 | "id": "9BxXUDdVduKc" 437 | }, 438 | "source": [ 439 | "## Summary\n", 440 | "Jupyter notebooks are documents which combine formatted text with executable code. They have become a very popular format for Scientific work and many example around the web. Google colab offers hosted notebooks in an environment including many popular libraries used in Data Science. Notebooks are made up of text cells, formated in markup, and code cells which can execute Python code. We will be using Colab notebooks for the examples in this book." 441 | ] 442 | } 443 | ] 444 | } 445 | -------------------------------------------------------------------------------- /Chapter-02:Fundamentals-of-Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chapter 2: Fundamentals of Python", 7 | "provenance": [], 8 | "collapsed_sections": [ 9 | "Iem3ygwKdVi5", 10 | "Wn4ATvC6dRSu", 11 | "hnUpWpI8hTgM", 12 | "moS1EdejiMvD", 13 | "3MSsxaixdf2L", 14 | "gY8pkgygdqwu", 15 | "En11dbBsibEP", 16 | "JxJWmfjzigtG", 17 | "eD45zVIpinTS", 18 | "GW1YJl_8iuo7", 19 | "y2QRhUNiizve", 20 | "TOQ9zXKtjFV5" 21 | ], 22 | "include_colab_link": true 23 | }, 24 | "kernelspec": { 25 | "name": "python3", 26 | "display_name": "Python 3" 27 | } 28 | }, 29 | "cells": [ 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "id": "view-in-github", 34 | "colab_type": "text" 35 | }, 36 | "source": [ 37 | "\"Open" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "source": [ 43 | "# Chapter 2: Fundamentals of Python" 44 | ], 45 | "metadata": { 46 | "id": "ZCErSIN4cfnG" 47 | } 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "source": [ 52 | "## Basic Types in Python" 53 | ], 54 | "metadata": { 55 | "id": "Iem3ygwKdVi5" 56 | } 57 | }, 58 | { 59 | "cell_type": "code", 60 | "metadata": { 61 | "id": "JLWqZ2R7Q1lE", 62 | "colab": { 63 | "base_uri": "https://localhost:8080/", 64 | "height": 34 65 | }, 66 | "outputId": "a5b442d7-639f-4577-af53-83157a7c8b60" 67 | }, 68 | "source": [ 69 | "type(13)" 70 | ], 71 | "execution_count": null, 72 | "outputs": [ 73 | { 74 | "output_type": "execute_result", 75 | "data": { 76 | "text/plain": [ 77 | "int" 78 | ] 79 | }, 80 | "metadata": { 81 | "tags": [] 82 | }, 83 | "execution_count": 2 84 | } 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "metadata": { 90 | "id": "ZJp_NVajQ5r9", 91 | "colab": { 92 | "base_uri": "https://localhost:8080/", 93 | "height": 34 94 | }, 95 | "outputId": "9dab9b20-3612-4e84-c790-7b35c6ac05fe" 96 | }, 97 | "source": [ 98 | "type(4.1)" 99 | ], 100 | "execution_count": null, 101 | "outputs": [ 102 | { 103 | "output_type": "execute_result", 104 | "data": { 105 | "text/plain": [ 106 | "float" 107 | ] 108 | }, 109 | "metadata": { 110 | "tags": [] 111 | }, 112 | "execution_count": 3 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "metadata": { 119 | "id": "6u5uAo44Q-U_", 120 | "colab": { 121 | "base_uri": "https://localhost:8080/", 122 | "height": 34 123 | }, 124 | "outputId": "4337ed18-89f0-4350-99dc-d74c31e32ae0" 125 | }, 126 | "source": [ 127 | "type(1.0)" 128 | ], 129 | "execution_count": null, 130 | "outputs": [ 131 | { 132 | "output_type": "execute_result", 133 | "data": { 134 | "text/plain": [ 135 | "float" 136 | ] 137 | }, 138 | "metadata": { 139 | "tags": [] 140 | }, 141 | "execution_count": 4 142 | } 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "metadata": { 148 | "id": "tcf5frSKR0eZ", 149 | "colab": { 150 | "base_uri": "https://localhost:8080/", 151 | "height": 34 152 | }, 153 | "outputId": "74f0414d-fb1e-4e1e-c071-f96c4db2d524" 154 | }, 155 | "source": [ 156 | "type(\"Hello\")" 157 | ], 158 | "execution_count": null, 159 | "outputs": [ 160 | { 161 | "output_type": "execute_result", 162 | "data": { 163 | "text/plain": [ 164 | "str" 165 | ] 166 | }, 167 | "metadata": { 168 | "tags": [] 169 | }, 170 | "execution_count": 5 171 | } 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "metadata": { 177 | "id": "cQtkhJCCSVB9", 178 | "colab": { 179 | "base_uri": "https://localhost:8080/", 180 | "height": 34 181 | }, 182 | "outputId": "0c835e6e-1cab-49e7-d02c-0dd5b934bc0b" 183 | }, 184 | "source": [ 185 | "type(True)" 186 | ], 187 | "execution_count": null, 188 | "outputs": [ 189 | { 190 | "output_type": "execute_result", 191 | "data": { 192 | "text/plain": [ 193 | "bool" 194 | ] 195 | }, 196 | "metadata": { 197 | "tags": [] 198 | }, 199 | "execution_count": 13 200 | } 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "metadata": { 206 | "id": "M6RUF_UmUxNj", 207 | "colab": { 208 | "base_uri": "https://localhost:8080/", 209 | "height": 34 210 | }, 211 | "outputId": "d9fdf859-cd50-44a9-c6cf-88ca24f6957d" 212 | }, 213 | "source": [ 214 | "type(False)" 215 | ], 216 | "execution_count": null, 217 | "outputs": [ 218 | { 219 | "output_type": "execute_result", 220 | "data": { 221 | "text/plain": [ 222 | "bool" 223 | ] 224 | }, 225 | "metadata": { 226 | "tags": [] 227 | }, 228 | "execution_count": 14 229 | } 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "metadata": { 235 | "id": "V--UtNTCUy0-", 236 | "colab": { 237 | "base_uri": "https://localhost:8080/", 238 | "height": 34 239 | }, 240 | "outputId": "811b5d68-e09d-4a9f-8cbf-8fbdf9b3e689" 241 | }, 242 | "source": [ 243 | "type(None)" 244 | ], 245 | "execution_count": null, 246 | "outputs": [ 247 | { 248 | "output_type": "execute_result", 249 | "data": { 250 | "text/plain": [ 251 | "NoneType" 252 | ] 253 | }, 254 | "metadata": { 255 | "tags": [] 256 | }, 257 | "execution_count": 15 258 | } 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "source": [ 264 | "## Statements" 265 | ], 266 | "metadata": { 267 | "id": "Wn4ATvC6dRSu" 268 | } 269 | }, 270 | { 271 | "cell_type": "code", 272 | "metadata": { 273 | "id": "8pogN432cWAL", 274 | "colab": { 275 | "base_uri": "https://localhost:8080/", 276 | "height": 34 277 | }, 278 | "outputId": "6c93d0e3-20f0-41c1-c84a-4d1e1a53bdc9" 279 | }, 280 | "source": [ 281 | "print(\"hello\")" 282 | ], 283 | "execution_count": null, 284 | "outputs": [ 285 | { 286 | "output_type": "stream", 287 | "text": [ 288 | "hello\n" 289 | ], 290 | "name": "stdout" 291 | } 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "metadata": { 297 | "id": "CqVI0nrKbB_c", 298 | "colab": { 299 | "base_uri": "https://localhost:8080/", 300 | "height": 34 301 | }, 302 | "outputId": "50ae5bc7-f566-4415-e06a-ea94327fbfe5" 303 | }, 304 | "source": [ 305 | "x, y, z = 3, 4, 6\n", 306 | "\n", 307 | "bar = x**2 if (x < y) and (y or z) else x//2\n", 308 | "bar" 309 | ], 310 | "execution_count": null, 311 | "outputs": [ 312 | { 313 | "output_type": "execute_result", 314 | "data": { 315 | "text/plain": [ 316 | "9" 317 | ] 318 | }, 319 | "metadata": { 320 | "tags": [] 321 | }, 322 | "execution_count": 5 323 | } 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "source": [ 329 | "## Multiple Statements" 330 | ], 331 | "metadata": { 332 | "id": "hnUpWpI8hTgM" 333 | } 334 | }, 335 | { 336 | "cell_type": "code", 337 | "metadata": { 338 | "id": "262sEQ95co3c", 339 | "colab": { 340 | "base_uri": "https://localhost:8080/", 341 | "height": 34 342 | }, 343 | "outputId": "eb9eea0f-0cb6-45e4-eee4-1a047912ba64" 344 | }, 345 | "source": [ 346 | "x = 23//3\n", 347 | "y = x**2\n", 348 | "print(f\"x is {x}, y is {y}\")" 349 | ], 350 | "execution_count": null, 351 | "outputs": [ 352 | { 353 | "output_type": "stream", 354 | "text": [ 355 | "x is 7, y is 49\n" 356 | ], 357 | "name": "stdout" 358 | } 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "source": [ 364 | "## Expression Statements" 365 | ], 366 | "metadata": { 367 | "id": "moS1EdejiMvD" 368 | } 369 | }, 370 | { 371 | "cell_type": "code", 372 | "metadata": { 373 | "id": "d9VsvdoWsg6U", 374 | "colab": { 375 | "base_uri": "https://localhost:8080/", 376 | "height": 34 377 | }, 378 | "outputId": "e7bb2992-f603-41ed-9b7a-796dd544564c" 379 | }, 380 | "source": [ 381 | "23 * 42" 382 | ], 383 | "execution_count": null, 384 | "outputs": [ 385 | { 386 | "output_type": "execute_result", 387 | "data": { 388 | "text/plain": [ 389 | "966" 390 | ] 391 | }, 392 | "metadata": { 393 | "tags": [] 394 | }, 395 | "execution_count": 1 396 | } 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "metadata": { 402 | "id": "CTAqytF3UEFp", 403 | "colab": { 404 | "base_uri": "https://localhost:8080/", 405 | "height": 34 406 | }, 407 | "outputId": "1f016aef-6337-4435-d040-4b81e46a616b" 408 | }, 409 | "source": [ 410 | "\"Hello\"\n" 411 | ], 412 | "execution_count": null, 413 | "outputs": [ 414 | { 415 | "output_type": "execute_result", 416 | "data": { 417 | "text/plain": [ 418 | "'Hello'" 419 | ] 420 | }, 421 | "metadata": { 422 | "tags": [] 423 | }, 424 | "execution_count": 2 425 | } 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "metadata": { 431 | "id": "fCY8YsoqVIUu", 432 | "colab": { 433 | "base_uri": "https://localhost:8080/", 434 | "height": 34 435 | }, 436 | "outputId": "a50cf909-f5b3-4709-f24a-c99c50a79512" 437 | }, 438 | "source": [ 439 | "import os\n", 440 | "os.getcwd()" 441 | ], 442 | "execution_count": null, 443 | "outputs": [ 444 | { 445 | "output_type": "execute_result", 446 | "data": { 447 | "text/plain": [ 448 | "'/content'" 449 | ] 450 | }, 451 | "metadata": { 452 | "tags": [] 453 | }, 454 | "execution_count": 3 455 | } 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "source": [ 461 | "## Assert Statements" 462 | ], 463 | "metadata": { 464 | "id": "3MSsxaixdf2L" 465 | } 466 | }, 467 | { 468 | "cell_type": "code", 469 | "metadata": { 470 | "id": "xzt0Q9Xrnwim" 471 | }, 472 | "source": [ 473 | "assert(True)\n" 474 | ], 475 | "execution_count": null, 476 | "outputs": [] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "metadata": { 481 | "id": "4eNXJ-upsWkd", 482 | "colab": { 483 | "base_uri": "https://localhost:8080/", 484 | "height": 162 485 | }, 486 | "outputId": "12b9d1f9-e8e4-4f3a-e37b-6207b2ee9a07" 487 | }, 488 | "source": [ 489 | "assert(False)" 490 | ], 491 | "execution_count": null, 492 | "outputs": [ 493 | { 494 | "output_type": "error", 495 | "ename": "AssertionError", 496 | "evalue": "ignored", 497 | "traceback": [ 498 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 499 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 500 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 501 | "\u001b[0;31mAssertionError\u001b[0m: " 502 | ] 503 | } 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "metadata": { 509 | "id": "whoB2dr0sX9m" 510 | }, 511 | "source": [ 512 | "assert('a')" 513 | ], 514 | "execution_count": null, 515 | "outputs": [] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "metadata": { 520 | "id": "s7Xyxo16sacl", 521 | "colab": { 522 | "base_uri": "https://localhost:8080/", 523 | "height": 162 524 | }, 525 | "outputId": "397dcdaf-e4fd-4e47-c136-258ec4d04480" 526 | }, 527 | "source": [ 528 | "assert(0)" 529 | ], 530 | "execution_count": null, 531 | "outputs": [ 532 | { 533 | "output_type": "error", 534 | "ename": "AssertionError", 535 | "evalue": "ignored", 536 | "traceback": [ 537 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 538 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 539 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 540 | "\u001b[0;31mAssertionError\u001b[0m: " 541 | ] 542 | } 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "metadata": { 548 | "id": "4W7SS9SrscHM", 549 | "colab": { 550 | "base_uri": "https://localhost:8080/", 551 | "height": 162 552 | }, 553 | "outputId": "d78f499f-7201-41aa-f701-0e410d5c35ad" 554 | }, 555 | "source": [ 556 | "assert([])" 557 | ], 558 | "execution_count": null, 559 | "outputs": [ 560 | { 561 | "output_type": "error", 562 | "ename": "AssertionError", 563 | "evalue": "ignored", 564 | "traceback": [ 565 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 566 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 567 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\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", 568 | "\u001b[0;31mAssertionError\u001b[0m: " 569 | ] 570 | } 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "metadata": { 576 | "id": "K5_C_XIhVWEA" 577 | }, 578 | "source": [ 579 | "assert(True)" 580 | ], 581 | "execution_count": null, 582 | "outputs": [] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "metadata": { 587 | "id": "wk_NdsU9OvgN", 588 | "colab": { 589 | "base_uri": "https://localhost:8080/", 590 | "height": 162 591 | }, 592 | "outputId": "0323bc2b-77c6-45f6-bfb6-caf0365e41d9" 593 | }, 594 | "source": [ 595 | "assert(False)" 596 | ], 597 | "execution_count": null, 598 | "outputs": [ 599 | { 600 | "output_type": "error", 601 | "ename": "AssertionError", 602 | "evalue": "ignored", 603 | "traceback": [ 604 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 605 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 606 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 607 | "\u001b[0;31mAssertionError\u001b[0m: " 608 | ] 609 | } 610 | ] 611 | }, 612 | { 613 | "cell_type": "markdown", 614 | "source": [ 615 | "## Assignment Statements" 616 | ], 617 | "metadata": { 618 | "id": "gY8pkgygdqwu" 619 | } 620 | }, 621 | { 622 | "cell_type": "code", 623 | "metadata": { 624 | "id": "92sbznDIc27M" 625 | }, 626 | "source": [ 627 | "x = 12\n", 628 | "y = 'Hello'\n" 629 | ], 630 | "execution_count": null, 631 | "outputs": [] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "metadata": { 636 | "id": "gIJrWTa4gME0", 637 | "colab": { 638 | "base_uri": "https://localhost:8080/", 639 | "height": 34 640 | }, 641 | "outputId": "da06a8eb-a5fd-417c-eccc-0d8d116cdfc9" 642 | }, 643 | "source": [ 644 | "answer = x - 3\n", 645 | "print(f\"{y} Jeff, the answer is {answer}\")\n" 646 | ], 647 | "execution_count": null, 648 | "outputs": [ 649 | { 650 | "output_type": "stream", 651 | "text": [ 652 | "Hello Jeff, the answer is 9\n" 653 | ], 654 | "name": "stdout" 655 | } 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "metadata": { 661 | "id": "-aU_NFxoU0cc" 662 | }, 663 | "source": [ 664 | "x, y, z = 1,'a',3.0" 665 | ], 666 | "execution_count": null, 667 | "outputs": [] 668 | }, 669 | { 670 | "cell_type": "markdown", 671 | "source": [ 672 | "## Delete Statements" 673 | ], 674 | "metadata": { 675 | "id": "En11dbBsibEP" 676 | } 677 | }, 678 | { 679 | "cell_type": "code", 680 | "metadata": { 681 | "id": "7k1TUe5cOxuR", 682 | "colab": { 683 | "base_uri": "https://localhost:8080/", 684 | "height": 196 685 | }, 686 | "outputId": "f9341b4f-ab0d-47cb-ac2b-d02964019d5b" 687 | }, 688 | "source": [ 689 | "polly = 'parrot'\n", 690 | "del(polly)\n", 691 | "print(polly)" 692 | ], 693 | "execution_count": null, 694 | "outputs": [ 695 | { 696 | "output_type": "error", 697 | "ename": "NameError", 698 | "evalue": "ignored", 699 | "traceback": [ 700 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 701 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 702 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mpolly\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'parrot'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpolly\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpolly\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 703 | "\u001b[0;31mNameError\u001b[0m: name 'polly' is not defined" 704 | ] 705 | } 706 | ] 707 | }, 708 | { 709 | "cell_type": "markdown", 710 | "source": [ 711 | "## Raise Statements" 712 | ], 713 | "metadata": { 714 | "id": "JxJWmfjzigtG" 715 | } 716 | }, 717 | { 718 | "cell_type": "code", 719 | "metadata": { 720 | "id": "UD1apaeOUgJO", 721 | "colab": { 722 | "base_uri": "https://localhost:8080/", 723 | "height": 162 724 | }, 725 | "outputId": "851f54b7-92c3-4d32-cd66-f11a0b59125d" 726 | }, 727 | "source": [ 728 | "raise NotImplementedError" 729 | ], 730 | "execution_count": null, 731 | "outputs": [ 732 | { 733 | "output_type": "error", 734 | "ename": "NotImplementedError", 735 | "evalue": "ignored", 736 | "traceback": [ 737 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 738 | "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", 739 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 740 | "\u001b[0;31mNotImplementedError\u001b[0m: " 741 | ] 742 | } 743 | ] 744 | }, 745 | { 746 | "cell_type": "markdown", 747 | "source": [ 748 | "## Import Statements" 749 | ], 750 | "metadata": { 751 | "id": "eD45zVIpinTS" 752 | } 753 | }, 754 | { 755 | "cell_type": "code", 756 | "metadata": { 757 | "id": "VKEtOz1Qm49G" 758 | }, 759 | "source": [ 760 | "import os\n" 761 | ], 762 | "execution_count": null, 763 | "outputs": [] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "metadata": { 768 | "id": "Yf2IntNfsbsx", 769 | "colab": { 770 | "base_uri": "https://localhost:8080/", 771 | "height": 34 772 | }, 773 | "outputId": "7dd98be3-3fa6-460c-acfc-15fdb6b8548f" 774 | }, 775 | "source": [ 776 | "os.listdir()" 777 | ], 778 | "execution_count": null, 779 | "outputs": [ 780 | { 781 | "output_type": "execute_result", 782 | "data": { 783 | "text/plain": [ 784 | "['.config', 'sample_data']" 785 | ] 786 | }, 787 | "metadata": { 788 | "tags": [] 789 | }, 790 | "execution_count": 4 791 | } 792 | ] 793 | }, 794 | { 795 | "cell_type": "code", 796 | "metadata": { 797 | "id": "A8ZnlQIish9I" 798 | }, 799 | "source": [ 800 | "import pandas" 801 | ], 802 | "execution_count": null, 803 | "outputs": [] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "metadata": { 808 | "id": "Gqw-D-Q_F6ct" 809 | }, 810 | "source": [ 811 | "import pandas as pd" 812 | ], 813 | "execution_count": null, 814 | "outputs": [] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "metadata": { 819 | "id": "OkxjIBdPF9lH", 820 | "colab": { 821 | "base_uri": "https://localhost:8080/", 822 | "height": 34 823 | }, 824 | "outputId": "b99fb6d1-72d9-47bb-cb4a-f0ceb216d970" 825 | }, 826 | "source": [ 827 | "from os import path\n", 828 | "path" 829 | ], 830 | "execution_count": null, 831 | "outputs": [ 832 | { 833 | "output_type": "execute_result", 834 | "data": { 835 | "text/plain": [ 836 | "" 837 | ] 838 | }, 839 | "metadata": { 840 | "tags": [] 841 | }, 842 | "execution_count": 2 843 | } 844 | ] 845 | }, 846 | { 847 | "cell_type": "markdown", 848 | "source": [ 849 | "## Print Statements" 850 | ], 851 | "metadata": { 852 | "id": "GW1YJl_8iuo7" 853 | } 854 | }, 855 | { 856 | "cell_type": "code", 857 | "metadata": { 858 | "id": "nhpW5qOV4007", 859 | "colab": { 860 | "base_uri": "https://localhost:8080/", 861 | "height": 101 862 | }, 863 | "outputId": "bbc17b09-4cb6-4293-f2a9-c8d5451df35a" 864 | }, 865 | "source": [ 866 | "print(1)\n", 867 | "print('a')\n", 868 | "print(1,'b')\n", 869 | "print(1,'b',sep='->')\n", 870 | "print(print)" 871 | ], 872 | "execution_count": null, 873 | "outputs": [ 874 | { 875 | "output_type": "stream", 876 | "text": [ 877 | "1\n", 878 | "a\n", 879 | "1 b\n", 880 | "1->b\n", 881 | "\n" 882 | ], 883 | "name": "stdout" 884 | } 885 | ] 886 | }, 887 | { 888 | "cell_type": "markdown", 889 | "source": [ 890 | "## Performing Basic Math Operations" 891 | ], 892 | "metadata": { 893 | "id": "y2QRhUNiizve" 894 | } 895 | }, 896 | { 897 | "cell_type": "code", 898 | "metadata": { 899 | "id": "f3s3wK53LQmW", 900 | "colab": { 901 | "base_uri": "https://localhost:8080/", 902 | "height": 34 903 | }, 904 | "outputId": "1f02534a-f810-4335-8d59-e2f8d45595e2" 905 | }, 906 | "source": [ 907 | "2 + 3" 908 | ], 909 | "execution_count": null, 910 | "outputs": [ 911 | { 912 | "output_type": "execute_result", 913 | "data": { 914 | "text/plain": [ 915 | "5" 916 | ] 917 | }, 918 | "metadata": { 919 | "tags": [] 920 | }, 921 | "execution_count": 1 922 | } 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "metadata": { 928 | "id": "fi6eMzrCOigC", 929 | "colab": { 930 | "base_uri": "https://localhost:8080/", 931 | "height": 34 932 | }, 933 | "outputId": "7e85bbe3-ac49-42c4-9cbd-c16b5e93ca06" 934 | }, 935 | "source": [ 936 | "5 - 6" 937 | ], 938 | "execution_count": null, 939 | "outputs": [ 940 | { 941 | "output_type": "execute_result", 942 | "data": { 943 | "text/plain": [ 944 | "-1" 945 | ] 946 | }, 947 | "metadata": { 948 | "tags": [] 949 | }, 950 | "execution_count": 2 951 | } 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "metadata": { 957 | "id": "nshUZ2C1Oklg", 958 | "colab": { 959 | "base_uri": "https://localhost:8080/", 960 | "height": 34 961 | }, 962 | "outputId": "74926f38-09b1-47a5-9cae-80fbefb7e668" 963 | }, 964 | "source": [ 965 | "3*4" 966 | ], 967 | "execution_count": null, 968 | "outputs": [ 969 | { 970 | "output_type": "execute_result", 971 | "data": { 972 | "text/plain": [ 973 | "12" 974 | ] 975 | }, 976 | "metadata": { 977 | "tags": [] 978 | }, 979 | "execution_count": 3 980 | } 981 | ] 982 | }, 983 | { 984 | "cell_type": "code", 985 | "metadata": { 986 | "id": "LQ0Mpl6ROmhH", 987 | "colab": { 988 | "base_uri": "https://localhost:8080/", 989 | "height": 34 990 | }, 991 | "outputId": "cbf764a0-dd71-4298-8787-2663b47116ac" 992 | }, 993 | "source": [ 994 | "9/3\n" 995 | ], 996 | "execution_count": null, 997 | "outputs": [ 998 | { 999 | "output_type": "execute_result", 1000 | "data": { 1001 | "text/plain": [ 1002 | "3.0" 1003 | ] 1004 | }, 1005 | "metadata": { 1006 | "tags": [] 1007 | }, 1008 | "execution_count": 4 1009 | } 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "code", 1014 | "metadata": { 1015 | "id": "GmyndZyGOoxE", 1016 | "colab": { 1017 | "base_uri": "https://localhost:8080/", 1018 | "height": 34 1019 | }, 1020 | "outputId": "f24b21a4-6564-4c12-d982-3c200ea054ec" 1021 | }, 1022 | "source": [ 1023 | "2**3" 1024 | ], 1025 | "execution_count": null, 1026 | "outputs": [ 1027 | { 1028 | "output_type": "execute_result", 1029 | "data": { 1030 | "text/plain": [ 1031 | "8" 1032 | ] 1033 | }, 1034 | "metadata": { 1035 | "tags": [] 1036 | }, 1037 | "execution_count": 5 1038 | } 1039 | ] 1040 | }, 1041 | { 1042 | "cell_type": "code", 1043 | "metadata": { 1044 | "id": "xSmhY6jlOqEw", 1045 | "colab": { 1046 | "base_uri": "https://localhost:8080/", 1047 | "height": 34 1048 | }, 1049 | "outputId": "77ae7598-90cf-4642-f207-c4779c8cb50d" 1050 | }, 1051 | "source": [ 1052 | "5//2" 1053 | ], 1054 | "execution_count": null, 1055 | "outputs": [ 1056 | { 1057 | "output_type": "execute_result", 1058 | "data": { 1059 | "text/plain": [ 1060 | "2" 1061 | ] 1062 | }, 1063 | "metadata": { 1064 | "tags": [] 1065 | }, 1066 | "execution_count": 6 1067 | } 1068 | ] 1069 | }, 1070 | { 1071 | "cell_type": "code", 1072 | "metadata": { 1073 | "id": "pK_Z-z9gPoZK", 1074 | "colab": { 1075 | "base_uri": "https://localhost:8080/", 1076 | "height": 34 1077 | }, 1078 | "outputId": "fe6fdee4-cecd-4af9-eb44-f375966c636c" 1079 | }, 1080 | "source": [ 1081 | "5%2" 1082 | ], 1083 | "execution_count": null, 1084 | "outputs": [ 1085 | { 1086 | "output_type": "execute_result", 1087 | "data": { 1088 | "text/plain": [ 1089 | "1" 1090 | ] 1091 | }, 1092 | "metadata": { 1093 | "tags": [] 1094 | }, 1095 | "execution_count": 7 1096 | } 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "code", 1101 | "metadata": { 1102 | "id": "Hs399g5tQ9SL", 1103 | "colab": { 1104 | "base_uri": "https://localhost:8080/", 1105 | "height": 34 1106 | }, 1107 | "outputId": "59690ad0-b0a5-42ce-ce0e-c819333e1303" 1108 | }, 1109 | "source": [ 1110 | "14 % 7 is 0\n" 1111 | ], 1112 | "execution_count": null, 1113 | "outputs": [ 1114 | { 1115 | "output_type": "execute_result", 1116 | "data": { 1117 | "text/plain": [ 1118 | "True" 1119 | ] 1120 | }, 1121 | "metadata": { 1122 | "tags": [] 1123 | }, 1124 | "execution_count": 8 1125 | } 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "markdown", 1130 | "source": [ 1131 | "## Dot Notation" 1132 | ], 1133 | "metadata": { 1134 | "id": "TOQ9zXKtjFV5" 1135 | } 1136 | }, 1137 | { 1138 | "cell_type": "code", 1139 | "metadata": { 1140 | "id": "S7gPLxzTzuJn", 1141 | "colab": { 1142 | "base_uri": "https://localhost:8080/", 1143 | "height": 34 1144 | }, 1145 | "outputId": "548261da-77ea-46c3-ab55-3485210e67e5" 1146 | }, 1147 | "source": [ 1148 | "a_number = 2\n", 1149 | "a_number.numerator" 1150 | ], 1151 | "execution_count": null, 1152 | "outputs": [ 1153 | { 1154 | "output_type": "execute_result", 1155 | "data": { 1156 | "text/plain": [ 1157 | "2" 1158 | ] 1159 | }, 1160 | "metadata": { 1161 | "tags": [] 1162 | }, 1163 | "execution_count": 10 1164 | } 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "code", 1169 | "metadata": { 1170 | "id": "YBa0FRLGz1VR", 1171 | "colab": { 1172 | "base_uri": "https://localhost:8080/", 1173 | "height": 34 1174 | }, 1175 | "outputId": "9b4a1a96-e90b-4490-c14b-c68d615d4755" 1176 | }, 1177 | "source": [ 1178 | "a_number.imag" 1179 | ], 1180 | "execution_count": null, 1181 | "outputs": [ 1182 | { 1183 | "output_type": "execute_result", 1184 | "data": { 1185 | "text/plain": [ 1186 | "0" 1187 | ] 1188 | }, 1189 | "metadata": { 1190 | "tags": [] 1191 | }, 1192 | "execution_count": 11 1193 | } 1194 | ] 1195 | }, 1196 | { 1197 | "cell_type": "code", 1198 | "metadata": { 1199 | "id": "wJDPPpraz6d7", 1200 | "colab": { 1201 | "base_uri": "https://localhost:8080/", 1202 | "height": 34 1203 | }, 1204 | "outputId": "16c44f48-cefd-4a4f-bd62-9d319d50d8e1" 1205 | }, 1206 | "source": [ 1207 | "a_number.to_bytes(8, 'little')" 1208 | ], 1209 | "execution_count": null, 1210 | "outputs": [ 1211 | { 1212 | "output_type": "execute_result", 1213 | "data": { 1214 | "text/plain": [ 1215 | "b'\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00'" 1216 | ] 1217 | }, 1218 | "metadata": { 1219 | "tags": [] 1220 | }, 1221 | "execution_count": 14 1222 | } 1223 | ] 1224 | }, 1225 | { 1226 | "cell_type": "code", 1227 | "metadata": { 1228 | "id": "1VRh_mZ00ANl" 1229 | }, 1230 | "source": [ 1231 | "" 1232 | ], 1233 | "execution_count": null, 1234 | "outputs": [] 1235 | } 1236 | ] 1237 | } 1238 | -------------------------------------------------------------------------------- /Chapter-05:Execution-control.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chapter5_Execution_control.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyNv6wrqmHXenMP2kq5iELyl", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "view-in-github", 21 | "colab_type": "text" 22 | }, 23 | "source": [ 24 | "\"Open" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "id": "x0h_M_fphoRp" 31 | }, 32 | "source": [ 33 | "### Execution Control`\n", 34 | "- Code block: group of statements to execute together\n", 35 | "- Control and repeat execution of a block of code" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "metadata": { 41 | "id": "srvWdQdYss81", 42 | "colab": { 43 | "base_uri": "https://localhost:8080/" 44 | }, 45 | "outputId": "cf8d3748-18ae-41ac-882b-5c00fc49b655" 46 | }, 47 | "source": [ 48 | "a, b, c = 1, 1, 2\n", 49 | "a == b" 50 | ], 51 | "execution_count": 1, 52 | "outputs": [ 53 | { 54 | "output_type": "execute_result", 55 | "data": { 56 | "text/plain": [ 57 | "True" 58 | ] 59 | }, 60 | "metadata": { 61 | "tags": [] 62 | }, 63 | "execution_count": 1 64 | } 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "metadata": { 70 | "id": "l8acMoQVs6h6", 71 | "colab": { 72 | "base_uri": "https://localhost:8080/" 73 | }, 74 | "outputId": "7e88197e-da93-474e-8e11-1002f0e249b9" 75 | }, 76 | "source": [ 77 | "a == c" 78 | ], 79 | "execution_count": 2, 80 | "outputs": [ 81 | { 82 | "output_type": "execute_result", 83 | "data": { 84 | "text/plain": [ 85 | "False" 86 | ] 87 | }, 88 | "metadata": { 89 | "tags": [] 90 | }, 91 | "execution_count": 2 92 | } 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "metadata": { 98 | "id": "kOg3ZFXLtDpE", 99 | "colab": { 100 | "base_uri": "https://localhost:8080/" 101 | }, 102 | "outputId": "d8c6e35c-a942-4671-829e-4ad932788268" 103 | }, 104 | "source": [ 105 | "a != b" 106 | ], 107 | "execution_count": 3, 108 | "outputs": [ 109 | { 110 | "output_type": "execute_result", 111 | "data": { 112 | "text/plain": [ 113 | "False" 114 | ] 115 | }, 116 | "metadata": { 117 | "tags": [] 118 | }, 119 | "execution_count": 3 120 | } 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "metadata": { 126 | "id": "GIMCy6was9sq", 127 | "colab": { 128 | "base_uri": "https://localhost:8080/" 129 | }, 130 | "outputId": "a90a0225-7285-42cc-c55d-63744c1aa6a0" 131 | }, 132 | "source": [ 133 | "a != c" 134 | ], 135 | "execution_count": 4, 136 | "outputs": [ 137 | { 138 | "output_type": "execute_result", 139 | "data": { 140 | "text/plain": [ 141 | "True" 142 | ] 143 | }, 144 | "metadata": { 145 | "tags": [] 146 | }, 147 | "execution_count": 4 148 | } 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "metadata": { 154 | "id": "qk_qxgyPy1c9", 155 | "colab": { 156 | "base_uri": "https://localhost:8080/" 157 | }, 158 | "outputId": "05073ba1-50ab-4c25-cf7f-aea363997195" 159 | }, 160 | "source": [ 161 | "1 == 1.0" 162 | ], 163 | "execution_count": 5, 164 | "outputs": [ 165 | { 166 | "output_type": "execute_result", 167 | "data": { 168 | "text/plain": [ 169 | "True" 170 | ] 171 | }, 172 | "metadata": { 173 | "tags": [] 174 | }, 175 | "execution_count": 5 176 | } 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "metadata": { 182 | "id": "hm6w_WXVy2zt", 183 | "colab": { 184 | "base_uri": "https://localhost:8080/" 185 | }, 186 | "outputId": "9784989c-c3b0-49dd-932d-e8bc13308044" 187 | }, 188 | "source": [ 189 | "'1' == 1" 190 | ], 191 | "execution_count": 6, 192 | "outputs": [ 193 | { 194 | "output_type": "execute_result", 195 | "data": { 196 | "text/plain": [ 197 | "False" 198 | ] 199 | }, 200 | "metadata": { 201 | "tags": [] 202 | }, 203 | "execution_count": 6 204 | } 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "metadata": { 210 | "id": "qB5WVst90bkg", 211 | "colab": { 212 | "base_uri": "https://localhost:8080/" 213 | }, 214 | "outputId": "8e2873e2-b0f2-4e86-a74b-edf5c5a005c2" 215 | }, 216 | "source": [ 217 | "a, b = 1, 1\n", 218 | "a is b" 219 | ], 220 | "execution_count": 7, 221 | "outputs": [ 222 | { 223 | "output_type": "execute_result", 224 | "data": { 225 | "text/plain": [ 226 | "True" 227 | ] 228 | }, 229 | "metadata": { 230 | "tags": [] 231 | }, 232 | "execution_count": 7 233 | } 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "metadata": { 239 | "id": "c7td4E140ZyQ" 240 | }, 241 | "source": [ 242 | "\n" 243 | ], 244 | "execution_count": 7, 245 | "outputs": [] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "metadata": { 250 | "id": "eEdQeWCj0fzP" 251 | }, 252 | "source": [ 253 | "" 254 | ], 255 | "execution_count": 7, 256 | "outputs": [] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": { 261 | "id": "fadx3sTriUox" 262 | }, 263 | "source": [ 264 | "#### For Loops\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "metadata": { 270 | "id": "PCILpeZQZi-q", 271 | "colab": { 272 | "base_uri": "https://localhost:8080/" 273 | }, 274 | "outputId": "0e2105b3-e950-43c0-e9fc-d49507430d11" 275 | }, 276 | "source": [ 277 | "for i in range(6):\n", 278 | " j = i + 1\n", 279 | " print(j)" 280 | ], 281 | "execution_count": 8, 282 | "outputs": [ 283 | { 284 | "output_type": "stream", 285 | "text": [ 286 | "1\n", 287 | "2\n", 288 | "3\n", 289 | "4\n", 290 | "5\n", 291 | "6\n" 292 | ], 293 | "name": "stdout" 294 | } 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "metadata": { 300 | "id": "lDDoznW2Z89G", 301 | "colab": { 302 | "base_uri": "https://localhost:8080/" 303 | }, 304 | "outputId": "5c7015a9-f8a8-4e15-b57d-cad31ed4d30e" 305 | }, 306 | "source": [ 307 | "colors = [\"Green\", \"Red\", \"Blue\"]\n", 308 | "for color in colors:\n", 309 | " print(f\"My favorite color is {color}\")\n", 310 | " print(\"No, wait...\")" 311 | ], 312 | "execution_count": 9, 313 | "outputs": [ 314 | { 315 | "output_type": "stream", 316 | "text": [ 317 | "My favorite color is Green\n", 318 | "No, wait...\n", 319 | "My favorite color is Red\n", 320 | "No, wait...\n", 321 | "My favorite color is Blue\n", 322 | "No, wait...\n" 323 | ], 324 | "name": "stdout" 325 | } 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": { 331 | "id": "o0KT_rHviYkp" 332 | }, 333 | "source": [ 334 | "#### While Loops" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "metadata": { 340 | "id": "BFugVZ6ua4GL", 341 | "colab": { 342 | "base_uri": "https://localhost:8080/" 343 | }, 344 | "outputId": "6e4ff0d0-16f2-4c15-afcf-68173af0be16" 345 | }, 346 | "source": [ 347 | "counter = 0\n", 348 | "while counter < 5:\n", 349 | " print(f\"I've counted {counter} so far, I hope there aren't more\")\n", 350 | " counter += 1" 351 | ], 352 | "execution_count": 10, 353 | "outputs": [ 354 | { 355 | "output_type": "stream", 356 | "text": [ 357 | "I've counted 0 so far, I hope there aren't more\n", 358 | "I've counted 1 so far, I hope there aren't more\n", 359 | "I've counted 2 so far, I hope there aren't more\n", 360 | "I've counted 3 so far, I hope there aren't more\n", 361 | "I've counted 4 so far, I hope there aren't more\n" 362 | ], 363 | "name": "stdout" 364 | } 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": { 370 | "id": "PK858dYlie4I" 371 | }, 372 | "source": [ 373 | "#### If Statements" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "metadata": { 379 | "id": "WZb0Fkjddo98", 380 | "colab": { 381 | "base_uri": "https://localhost:8080/" 382 | }, 383 | "outputId": "08a28d5e-3a4d-4ac0-ef48-2fd6b9703eb3" 384 | }, 385 | "source": [ 386 | "snack = 'apple'\n", 387 | "fruit = {'orange', 'apple', 'pear'}\n", 388 | "\n", 389 | "if snack in fruit:\n", 390 | " print(f\"Yeah, {snack} is good!\")" 391 | ], 392 | "execution_count": 11, 393 | "outputs": [ 394 | { 395 | "output_type": "stream", 396 | "text": [ 397 | "Yeah, apple is good!\n" 398 | ], 399 | "name": "stdout" 400 | } 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "metadata": { 406 | "id": "mbkw9Gr4khOC", 407 | "colab": { 408 | "base_uri": "https://localhost:8080/" 409 | }, 410 | "outputId": "7faf9ce0-820a-4cdb-9c37-b1b57acfe6c2" 411 | }, 412 | "source": [ 413 | "snack = 'cake'\n", 414 | "fruit = {'orange', 'apple', 'pear'}\n", 415 | "\n", 416 | "if snack in fruit:\n", 417 | " print(f\"Yeah, {snack} is good!\")\n", 418 | "else:\n", 419 | " print(f\"{snack}!? You should have some fruit\")" 420 | ], 421 | "execution_count": 12, 422 | "outputs": [ 423 | { 424 | "output_type": "stream", 425 | "text": [ 426 | "cake!? You should have some fruit\n" 427 | ], 428 | "name": "stdout" 429 | } 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "metadata": { 435 | "id": "BGjfNAqslEir", 436 | "colab": { 437 | "base_uri": "https://localhost:8080/" 438 | }, 439 | "outputId": "735b8cf7-1dcd-459e-eab9-7f91453efc96" 440 | }, 441 | "source": [ 442 | "balance = 2000.32\n", 443 | "account_status = None\n", 444 | "\n", 445 | "if balance > 0:\n", 446 | " account_status = 'Positive'\n", 447 | "else:\n", 448 | " if balance == 0:\n", 449 | " account_status = 'Empty' \n", 450 | " else:\n", 451 | " account_status = 'Overdrawn'\n", 452 | " \n", 453 | "print(account_status)" 454 | ], 455 | "execution_count": 13, 456 | "outputs": [ 457 | { 458 | "output_type": "stream", 459 | "text": [ 460 | "Positive\n" 461 | ], 462 | "name": "stdout" 463 | } 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "metadata": { 469 | "id": "4ufXWTZZst-0" 470 | }, 471 | "source": [ 472 | "" 473 | ], 474 | "execution_count": 13, 475 | "outputs": [] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "metadata": { 480 | "id": "OhVgANkDbYWS", 481 | "colab": { 482 | "base_uri": "https://localhost:8080/" 483 | }, 484 | "outputId": "b290e483-366a-4d67-d9ed-f807f8d91541" 485 | }, 486 | "source": [ 487 | "fish = ['mackerel', 'salmon', 'pike']\n", 488 | "beasts = ['salmon', 'pike', 'bear', 'mackerel']\n", 489 | "i = 0\n", 490 | "\n", 491 | "while True:\n", 492 | " beast = beasts[i] \n", 493 | " if beast not in fish:\n", 494 | " print(f\"Oh no! It's not a fish, it's a {beast}\")\n", 495 | " break\n", 496 | " print(f\"I caught a {beast} with my fishing net\")\n", 497 | " i += 1" 498 | ], 499 | "execution_count": 14, 500 | "outputs": [ 501 | { 502 | "output_type": "stream", 503 | "text": [ 504 | "I caught a salmon with my fishing net\n", 505 | "I caught a pike with my fishing net\n", 506 | "Oh no! It's not a fish, it's a bear\n" 507 | ], 508 | "name": "stdout" 509 | } 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "metadata": { 515 | "id": "h6NQ7ZK7eJbv", 516 | "colab": { 517 | "base_uri": "https://localhost:8080/" 518 | }, 519 | "outputId": "a80f0f22-563e-489d-baa5-a113678c91e9" 520 | }, 521 | "source": [ 522 | "for name in ['bob', 'billy', 'bonzo', 'fred', 'baxter']:\n", 523 | " if not name.startswith('b'):\n", 524 | " continue\n", 525 | " print(f\"Fine fellow that {name}\")\n" 526 | ], 527 | "execution_count": 15, 528 | "outputs": [ 529 | { 530 | "output_type": "stream", 531 | "text": [ 532 | "Fine fellow that bob\n", 533 | "Fine fellow that billy\n", 534 | "Fine fellow that bonzo\n", 535 | "Fine fellow that baxter\n" 536 | ], 537 | "name": "stdout" 538 | } 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "metadata": { 544 | "id": "KRlUDnX1G5o6", 545 | "colab": { 546 | "base_uri": "https://localhost:8080/" 547 | }, 548 | "outputId": "df8e3b88-26a6-43af-ccfc-9aaa978b41fe" 549 | }, 550 | "source": [ 551 | "import re\n", 552 | "s = '2020-12-14'\n", 553 | "match = re.search(r'(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s)\n", 554 | "if match:\n", 555 | " print(f\"Matched items: {match.groups(1)}\")\n", 556 | "else:\n", 557 | " print(f\"No match found in {s}\")" 558 | ], 559 | "execution_count": 16, 560 | "outputs": [ 561 | { 562 | "output_type": "stream", 563 | "text": [ 564 | "Matched items: ('2020', '12', '14')\n" 565 | ], 566 | "name": "stdout" 567 | } 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "metadata": { 573 | "id": "8ThNMHc5H1qC", 574 | "colab": { 575 | "base_uri": "https://localhost:8080/", 576 | "height": 130 577 | }, 578 | "outputId": "bb3ceabb-2df8-4d88-99a8-6f0cb70781d5" 579 | }, 580 | "source": [ 581 | "# Requires Python 3.8\n", 582 | "# See https://research.google.com/colaboratory/local-runtimes.html\n", 583 | "import re\n", 584 | "s = '2020-12-14'\n", 585 | "if match := re.search(r'(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s):\n", 586 | " print(f\"Matched items: {match.groups(1)}\")\n", 587 | "else:\n", 588 | " print(f\"No match found in {s}\")" 589 | ], 590 | "execution_count": 17, 591 | "outputs": [ 592 | { 593 | "output_type": "error", 594 | "ename": "SyntaxError", 595 | "evalue": "ignored", 596 | "traceback": [ 597 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m if match := re.search(r'(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 598 | ] 599 | } 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "metadata": { 605 | "id": "XRNs_WLnIOZo" 606 | }, 607 | "source": [ 608 | "a = []" 609 | ], 610 | "execution_count": 18, 611 | "outputs": [] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "metadata": { 616 | "id": "QdSaga2mM_Ev", 617 | "colab": { 618 | "base_uri": "https://localhost:8080/" 619 | }, 620 | "outputId": "c9496280-7ade-4430-81d3-39b627d68321" 621 | }, 622 | "source": [ 623 | "if a:\n", 624 | " print(\"Hiya\")\n", 625 | "else:\n", 626 | " print(\"Biya\")" 627 | ], 628 | "execution_count": 19, 629 | "outputs": [ 630 | { 631 | "output_type": "stream", 632 | "text": [ 633 | "Biya\n" 634 | ], 635 | "name": "stdout" 636 | } 637 | ] 638 | } 639 | ] 640 | } 641 | -------------------------------------------------------------------------------- /Chapter-06:Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chapter6_Functions.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyMs5PehoBKyzOLtn4nHzesS", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "view-in-github", 21 | "colab_type": "text" 22 | }, 23 | "source": [ 24 | "\"Open" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "id": "cxIh2XdSnYm4" 31 | }, 32 | "source": [ 33 | "## Notes\n", 34 | "[Functions](https://docs.python.org/3/reference/compound_stmts.html#function-definitions)\n", 35 | "\n", 36 | "[Calls](https://docs.python.org/3/reference/expressions.html#calls)\n", 37 | "\n", 38 | "[DocStrings](https://www.python.org/dev/peps/pep-0257/#:~:text=A%20docstring%20is%20a%20string,special%20attribute%20of%20that%20object.&text=String%20literals%20occurring%20elsewhere%20in%20Python%20code%20may%20also%20act%20as%20documentation.)\n", 39 | "\n", 40 | "[Positional Parameters](https://www.python.org/dev/peps/pep-0457/)\n", 41 | "\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": { 47 | "id": "2IsWbHNdkm2R" 48 | }, 49 | "source": [ 50 | "## Function Definition\n", 51 | "Function definitions define a function object. This object wraps the executable block. The definition does not run the block, just defines the function. The definition describes how the function can be called, what it is named, what parameters can be passed to it, and what will be executed when it is invoked. The building blocks of a function are the controlling statement, an optional docstring, the controlled code block and a return statement." 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": { 57 | "id": "fokV3pexnQe3" 58 | }, 59 | "source": [ 60 | "### Control Statement\n", 61 | "The first line of a function definition is the control statement. This control statement takes the form:\n", 62 | "\n", 63 | "`def ():`\n", 64 | "\n", 65 | "`def` is the keyword indicating a function definition, `` is where the name that will be used to call the function is defined and `` is where any arguments that can be passed to the function are defined. For example, the function:" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "metadata": { 71 | "id": "VoHNX1qXjDLn" 72 | }, 73 | "source": [ 74 | "def do_nothing(not_used):\n", 75 | " pass" 76 | ], 77 | "execution_count": 1, 78 | "outputs": [] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": { 83 | "id": "BJ0hfEZKyeqn" 84 | }, 85 | "source": [ 86 | "Is defined with the name `do_nothing` and a single parameter named `not_used`.\n", 87 | "\n", 88 | "\n", 89 | "### DocString\n", 90 | "\n", 91 | "The next part of a function definition is the DocString. This DocString contains documentation for the function. It can be omitted, the python compiler will not object. It is highly recommended to supply a docstring for all but the most obvious methods. The Docstring consists of a single or multiline triple quoted string. It must immediatly follow the control statement. \n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "metadata": { 97 | "id": "h5icngjNj6P6" 98 | }, 99 | "source": [ 100 | "def do_nothing(not_used):\n", 101 | " \"\"\"This function does nothing.\"\"\"\n", 102 | " pass" 103 | ], 104 | "execution_count": 2, 105 | "outputs": [] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "id": "aMrIw3pAqcmE" 111 | }, 112 | "source": [ 113 | "If it is a single line, the quotes should be on the same line as the text, for multi-line DocStrings, the quotes are generally above and below the text." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "metadata": { 119 | "id": "E9OGW3Ahqdak" 120 | }, 121 | "source": [ 122 | "def do_nothing(not_used):\n", 123 | " \"\"\"\n", 124 | " This function does nothing.\n", 125 | "\n", 126 | " This function uses a pass statement to \n", 127 | " avoid doing anything.\n", 128 | "\n", 129 | " Parameters:\n", 130 | " not_used - a parameter of any type,\n", 131 | " which is not used.\n", 132 | " \"\"\"\n", 133 | " pass\n" 134 | ], 135 | "execution_count": 3, 136 | "outputs": [] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "id": "ZZvZzmTGq7PB" 142 | }, 143 | "source": [ 144 | "The first line of the docstring should be a statement summerizing what the function does. If a more detailed explanation is desired, a blank line should be left after the first statement. There are many different possible conventions for what is contained in a DocString after the first line, but generally you want to offer an explantion of what the function does, what parameters it takes and what it is expected to return. \n", 145 | "The DocString is useful both for someone reading your code and for various utilities which read and display either the first line, or the whole DocString. For example if we call the help function on our function, the DocString is displayed." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "id": "il8Bk5r1rx28", 152 | "colab": { 153 | "base_uri": "https://localhost:8080/" 154 | }, 155 | "outputId": "7be1cfc0-5059-4f65-a1e5-78da0aecb947" 156 | }, 157 | "source": [ 158 | "help(do_nothing)" 159 | ], 160 | "execution_count": 4, 161 | "outputs": [ 162 | { 163 | "output_type": "stream", 164 | "text": [ 165 | "Help on function do_nothing in module __main__:\n", 166 | "\n", 167 | "do_nothing(not_used)\n", 168 | " This function does nothing.\n", 169 | " \n", 170 | " This function uses a pass statement to \n", 171 | " avoid doing anything.\n", 172 | " \n", 173 | " Parameters:\n", 174 | " not_used - a parameter of any type,\n", 175 | " which is not used.\n", 176 | "\n" 177 | ], 178 | "name": "stdout" 179 | } 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": { 185 | "id": "z0jowVGzt7Nj" 186 | }, 187 | "source": [ 188 | "### Parameters\n", 189 | "keyword vs position\n", 190 | "default value \n", 191 | "call using position or keyword" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "metadata": { 197 | "id": "4AjdXfZArzw7" 198 | }, 199 | "source": [ 200 | "def does_order(first, second, third):\n", 201 | " '''Prints parameters.'''\n", 202 | " print(f'First: {first}')\n", 203 | " print(f'Second: {second}')\n", 204 | " print(f'Third: {third}')" 205 | ], 206 | "execution_count": 5, 207 | "outputs": [] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "metadata": { 212 | "id": "GU-u-UZFytWL", 213 | "colab": { 214 | "base_uri": "https://localhost:8080/" 215 | }, 216 | "outputId": "193f84f2-f4d9-4ae6-8490-aae512ea9465" 217 | }, 218 | "source": [ 219 | "does_order(1, 2, 3)\n" 220 | ], 221 | "execution_count": 6, 222 | "outputs": [ 223 | { 224 | "output_type": "stream", 225 | "text": [ 226 | "First: 1\n", 227 | "Second: 2\n", 228 | "Third: 3\n" 229 | ], 230 | "name": "stdout" 231 | } 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "metadata": { 237 | "id": "rRWLAxF-yzae", 238 | "colab": { 239 | "base_uri": "https://localhost:8080/" 240 | }, 241 | "outputId": "f2ae857d-c6a3-4582-c364-f24867545db0" 242 | }, 243 | "source": [ 244 | "does_order(first=1, second=2, third=3)" 245 | ], 246 | "execution_count": 7, 247 | "outputs": [ 248 | { 249 | "output_type": "stream", 250 | "text": [ 251 | "First: 1\n", 252 | "Second: 2\n", 253 | "Third: 3\n" 254 | ], 255 | "name": "stdout" 256 | } 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "metadata": { 262 | "id": "eDZ14ioE3vTc", 263 | "colab": { 264 | "base_uri": "https://localhost:8080/" 265 | }, 266 | "outputId": "a67e71a2-669a-479c-dde2-6f511c8f9163" 267 | }, 268 | "source": [ 269 | "does_order(1, third=3, second=2)" 270 | ], 271 | "execution_count": 8, 272 | "outputs": [ 273 | { 274 | "output_type": "stream", 275 | "text": [ 276 | "First: 1\n", 277 | "Second: 2\n", 278 | "Third: 3\n" 279 | ], 280 | "name": "stdout" 281 | } 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "metadata": { 287 | "id": "FXjOCf733zp9", 288 | "colab": { 289 | "base_uri": "https://localhost:8080/", 290 | "height": 130 291 | }, 292 | "outputId": "70b74d90-c24e-474b-8612-2015426e951c" 293 | }, 294 | "source": [ 295 | "does_order(second=2, 1, 3)" 296 | ], 297 | "execution_count": 9, 298 | "outputs": [ 299 | { 300 | "output_type": "error", 301 | "ename": "SyntaxError", 302 | "evalue": "ignored", 303 | "traceback": [ 304 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m does_order(second=2, 1, 3)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" 305 | ] 306 | } 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "metadata": { 312 | "id": "aakCFwWCBezH" 313 | }, 314 | "source": [ 315 | "def does_keyword(first, second, *, third):\n", 316 | " '''Prints parameters.'''\n", 317 | " print(f'First: {first}')\n", 318 | " print(f'Second: {second}')\n", 319 | " print(f'Third: {third}')" 320 | ], 321 | "execution_count": 13, 322 | "outputs": [] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "metadata": { 327 | "id": "MaZYqX9FBnHv", 328 | "colab": { 329 | "base_uri": "https://localhost:8080/" 330 | }, 331 | "outputId": "852bfd58-0889-4149-d78c-abaa5fcfa2df" 332 | }, 333 | "source": [ 334 | "does_keyword(1, 2, third=3)" 335 | ], 336 | "execution_count": 14, 337 | "outputs": [ 338 | { 339 | "output_type": "stream", 340 | "text": [ 341 | "First: 1\n", 342 | "Second: 2\n", 343 | "Third: 3\n" 344 | ], 345 | "name": "stdout" 346 | } 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "metadata": { 352 | "id": "4NmVE5RBBsRx", 353 | "colab": { 354 | "base_uri": "https://localhost:8080/", 355 | "height": 164 356 | }, 357 | "outputId": "9212e7e3-4b42-4640-fb25-641ef5bd9036" 358 | }, 359 | "source": [ 360 | "does_keyword(1, 2, 3)" 361 | ], 362 | "execution_count": 15, 363 | "outputs": [ 364 | { 365 | "output_type": "error", 366 | "ename": "TypeError", 367 | "evalue": "ignored", 368 | "traceback": [ 369 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 370 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 371 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdoes_keyword\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 372 | "\u001b[0;31mTypeError\u001b[0m: does_keyword() takes 2 positional arguments but 3 were given" 373 | ] 374 | } 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "metadata": { 380 | "id": "74NvU1Tr3_Vk" 381 | }, 382 | "source": [ 383 | "def does_defaults(first, second, third=3):\n", 384 | " '''Prints parameters.'''\n", 385 | " print(f'First: {first}')\n", 386 | " print(f'Second: {second}')\n", 387 | " print(f'Third: {third}')" 388 | ], 389 | "execution_count": 16, 390 | "outputs": [] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "metadata": { 395 | "id": "wu9E0PTPBAq5", 396 | "colab": { 397 | "base_uri": "https://localhost:8080/" 398 | }, 399 | "outputId": "429b9b07-f0ef-4278-a2a1-64fffcb4b0d4" 400 | }, 401 | "source": [ 402 | "does_defaults(1, 2, 3)" 403 | ], 404 | "execution_count": 17, 405 | "outputs": [ 406 | { 407 | "output_type": "stream", 408 | "text": [ 409 | "First: 1\n", 410 | "Second: 2\n", 411 | "Third: 3\n" 412 | ], 413 | "name": "stdout" 414 | } 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "metadata": { 420 | "id": "XteWCqjNBEFX", 421 | "colab": { 422 | "base_uri": "https://localhost:8080/" 423 | }, 424 | "outputId": "2a16c775-3fca-4a30-f925-f7e06be982da" 425 | }, 426 | "source": [ 427 | "does_defaults(1, 2)" 428 | ], 429 | "execution_count": 18, 430 | "outputs": [ 431 | { 432 | "output_type": "stream", 433 | "text": [ 434 | "First: 1\n", 435 | "Second: 2\n", 436 | "Third: 3\n" 437 | ], 438 | "name": "stdout" 439 | } 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "metadata": { 445 | "id": "wYPqtPItBGn_", 446 | "colab": { 447 | "base_uri": "https://localhost:8080/", 448 | "height": 130 449 | }, 450 | "outputId": "f696a6cb-2016-4e91-bde4-2033562215ad" 451 | }, 452 | "source": [ 453 | "def does_defaults(first=1, second, third=3):\n", 454 | " '''Prints parameters.'''\n", 455 | " print(f'First: {first}')\n", 456 | " print(f'Second: {second}')\n", 457 | " print(f'Third: {third}')" 458 | ], 459 | "execution_count": 19, 460 | "outputs": [ 461 | { 462 | "output_type": "error", 463 | "ename": "SyntaxError", 464 | "evalue": "ignored", 465 | "traceback": [ 466 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def does_defaults(first=1, second, third=3):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" 467 | ] 468 | } 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "metadata": { 474 | "id": "ZzNKWCc6BUH7" 475 | }, 476 | "source": [ 477 | "def does_list_default(my_list=[]):\n", 478 | " '''Uses list as default.'''\n", 479 | " my_list.append(1)\n", 480 | " print(my_list)" 481 | ], 482 | "execution_count": 20, 483 | "outputs": [] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "metadata": { 488 | "id": "n_HN8ubJCnDo", 489 | "colab": { 490 | "base_uri": "https://localhost:8080/" 491 | }, 492 | "outputId": "2b33286d-ba00-4a29-faaf-9072db58a76a" 493 | }, 494 | "source": [ 495 | "does_list_default()\n", 496 | "does_list_default()\n", 497 | "does_list_default()" 498 | ], 499 | "execution_count": 21, 500 | "outputs": [ 501 | { 502 | "output_type": "stream", 503 | "text": [ 504 | "[1]\n", 505 | "[1, 1]\n", 506 | "[1, 1, 1]\n" 507 | ], 508 | "name": "stdout" 509 | } 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "metadata": { 515 | "id": "WwkKuQjxHe40" 516 | }, 517 | "source": [ 518 | "def does_list_param(my_list=None):\n", 519 | " '''Assigns default in code to avoid confusion.'''\n", 520 | " my_list = my_list or []\n", 521 | " my_list.append(1)\n", 522 | " print(my_list)" 523 | ], 524 | "execution_count": 22, 525 | "outputs": [] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "metadata": { 530 | "id": "yB2zZpCKHyAy", 531 | "colab": { 532 | "base_uri": "https://localhost:8080/" 533 | }, 534 | "outputId": "96cde62d-07dc-4eba-a46a-32bee07c39e8" 535 | }, 536 | "source": [ 537 | "does_list_param()\n", 538 | "does_list_param()\n", 539 | "does_list_param()" 540 | ], 541 | "execution_count": 23, 542 | "outputs": [ 543 | { 544 | "output_type": "stream", 545 | "text": [ 546 | "[1]\n", 547 | "[1]\n", 548 | "[1]\n" 549 | ], 550 | "name": "stdout" 551 | } 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "metadata": { 557 | "id": "anjofATZCtlw", 558 | "colab": { 559 | "base_uri": "https://localhost:8080/", 560 | "height": 130 561 | }, 562 | "outputId": "b42282a5-1c97-4446-9955-ef60223a0d61" 563 | }, 564 | "source": [ 565 | "# Requires Python 3.8\n", 566 | "# see: https://research.google.com/colaboratory/local-runtimes.html\n", 567 | "def does_positional(first, /, second, third):\n", 568 | " '''Demonstrates a positional parameter.'''\n", 569 | " print(f'First: {first}')\n", 570 | " print(f'Second: {second}')\n", 571 | " print(f'Third: {third}')" 572 | ], 573 | "execution_count": 24, 574 | "outputs": [ 575 | { 576 | "output_type": "error", 577 | "ename": "SyntaxError", 578 | "evalue": "ignored", 579 | "traceback": [ 580 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def does_positional(first, /, second, third):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 581 | ] 582 | } 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "metadata": { 588 | "id": "N73Er1X-DZeH", 589 | "colab": { 590 | "base_uri": "https://localhost:8080/", 591 | "height": 164 592 | }, 593 | "outputId": "1eeb17cc-b2bd-420d-c3e1-5166b50bbca9" 594 | }, 595 | "source": [ 596 | "does_positional(1, 2, 3)" 597 | ], 598 | "execution_count": 25, 599 | "outputs": [ 600 | { 601 | "output_type": "error", 602 | "ename": "NameError", 603 | "evalue": "ignored", 604 | "traceback": [ 605 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 606 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 607 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdoes_positional\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 608 | "\u001b[0;31mNameError\u001b[0m: name 'does_positional' is not defined" 609 | ] 610 | } 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "metadata": { 616 | "id": "O5PFF-unDc-m", 617 | "colab": { 618 | "base_uri": "https://localhost:8080/", 619 | "height": 164 620 | }, 621 | "outputId": "acc81c1b-3e7b-4f3e-bafa-2f639e712462" 622 | }, 623 | "source": [ 624 | "does_positional(first=1, second=2, third=3)" 625 | ], 626 | "execution_count": 26, 627 | "outputs": [ 628 | { 629 | "output_type": "error", 630 | "ename": "NameError", 631 | "evalue": "ignored", 632 | "traceback": [ 633 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 634 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 635 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdoes_positional\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthird\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 636 | "\u001b[0;31mNameError\u001b[0m: name 'does_positional' is not defined" 637 | ] 638 | } 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "metadata": { 644 | "id": "ekrgIc0ADh-O" 645 | }, 646 | "source": [ 647 | "def does_positional(first, /, second, *, third):\n", 648 | " '''Demonstrates a positional and keyword parameters.'''\n", 649 | " print(f'First: {first}')\n", 650 | " print(f'Second: {second}')\n", 651 | " print(f'Third: {third}')" 652 | ], 653 | "execution_count": null, 654 | "outputs": [] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "metadata": { 659 | "id": "zoVFiAibDsEW" 660 | }, 661 | "source": [ 662 | "does_positional(1, 2, third=3)" 663 | ], 664 | "execution_count": null, 665 | "outputs": [] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "metadata": { 670 | "id": "KPvNYfNDDxtV" 671 | }, 672 | "source": [ 673 | "def does_wildcard_positions(*args):\n", 674 | " '''Demonstrates wildcard for positional parameters.'''\n", 675 | " for item in args:\n", 676 | " print(item)" 677 | ], 678 | "execution_count": 27, 679 | "outputs": [] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "metadata": { 684 | "id": "VPSl-SZPETet", 685 | "colab": { 686 | "base_uri": "https://localhost:8080/" 687 | }, 688 | "outputId": "9fb332b7-e606-430c-f430-8a23da46bd96" 689 | }, 690 | "source": [ 691 | "does_wildcard_positions('Donkey', 3, ['a'])" 692 | ], 693 | "execution_count": 28, 694 | "outputs": [ 695 | { 696 | "output_type": "stream", 697 | "text": [ 698 | "Donkey\n", 699 | "3\n", 700 | "['a']\n" 701 | ], 702 | "name": "stdout" 703 | } 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "metadata": { 709 | "id": "RxSa9_JTEgYC" 710 | }, 711 | "source": [ 712 | "def does_wildcard_keywords(**kwargs):\n", 713 | " '''Demonstrates wildcard for keyword parameters.'''\n", 714 | " for key, value in kwargs.items():\n", 715 | " print(f'{key} : {value}')" 716 | ], 717 | "execution_count": 29, 718 | "outputs": [] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "metadata": { 723 | "id": "VP-AdfjGE2HK", 724 | "colab": { 725 | "base_uri": "https://localhost:8080/" 726 | }, 727 | "outputId": "19dd02b8-b10c-4505-d22b-71442d4ac9d8" 728 | }, 729 | "source": [ 730 | "does_wildcard_keywords(one=1, name='Martha')" 731 | ], 732 | "execution_count": 30, 733 | "outputs": [ 734 | { 735 | "output_type": "stream", 736 | "text": [ 737 | "one : 1\n", 738 | "name : Martha\n" 739 | ], 740 | "name": "stdout" 741 | } 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "metadata": { 747 | "id": "sRQDSxnGFCaA" 748 | }, 749 | "source": [ 750 | "def does_wildcards(*args, **kwargs):\n", 751 | " '''Demonstrates wildcard parameters.'''\n", 752 | " print(f'Positional: {args}')\n", 753 | " print(f'Keyword: {kwargs}')" 754 | ], 755 | "execution_count": 31, 756 | "outputs": [] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "metadata": { 761 | "id": "XOlMfJl7FpTi", 762 | "colab": { 763 | "base_uri": "https://localhost:8080/" 764 | }, 765 | "outputId": "44770aa5-0c3c-4ab7-ea0f-30c30688777f" 766 | }, 767 | "source": [ 768 | "does_wildcards(1, 2, a='a', b=3)" 769 | ], 770 | "execution_count": 32, 771 | "outputs": [ 772 | { 773 | "output_type": "stream", 774 | "text": [ 775 | "Positional: (1, 2)\n", 776 | "Keyword: {'a': 'a', 'b': 3}\n" 777 | ], 778 | "name": "stdout" 779 | } 780 | ] 781 | }, 782 | { 783 | "cell_type": "code", 784 | "metadata": { 785 | "id": "2KmfDf6pI5qg" 786 | }, 787 | "source": [ 788 | "def adds_one(some_number):\n", 789 | " '''Demonstrates return statement.'''\n", 790 | " return some_number + 1" 791 | ], 792 | "execution_count": 33, 793 | "outputs": [] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "metadata": { 798 | "id": "C6UxpyeEJEW_", 799 | "colab": { 800 | "base_uri": "https://localhost:8080/" 801 | }, 802 | "outputId": "b1649ae3-e148-44e2-efd1-2101731dfca8" 803 | }, 804 | "source": [ 805 | "adds_one(1)" 806 | ], 807 | "execution_count": 34, 808 | "outputs": [ 809 | { 810 | "output_type": "execute_result", 811 | "data": { 812 | "text/plain": [ 813 | "2" 814 | ] 815 | }, 816 | "metadata": { 817 | "tags": [] 818 | }, 819 | "execution_count": 34 820 | } 821 | ] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "metadata": { 826 | "id": "4YERaOgSFuME" 827 | }, 828 | "source": [ 829 | "def returns_none():\n", 830 | " '''Demonstrates default return value.'''\n", 831 | " pass" 832 | ], 833 | "execution_count": 35, 834 | "outputs": [] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "metadata": { 839 | "id": "kDCUoepPIn78", 840 | "colab": { 841 | "base_uri": "https://localhost:8080/" 842 | }, 843 | "outputId": "3a3a6868-f3fd-4dfb-fe92-1e4c7ac6a214" 844 | }, 845 | "source": [ 846 | "returns_none() == None" 847 | ], 848 | "execution_count": 36, 849 | "outputs": [ 850 | { 851 | "output_type": "execute_result", 852 | "data": { 853 | "text/plain": [ 854 | "True" 855 | ] 856 | }, 857 | "metadata": { 858 | "tags": [] 859 | }, 860 | "execution_count": 36 861 | } 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "metadata": { 867 | "id": "RtanaGVnIpgo" 868 | }, 869 | "source": [ 870 | "outer = 'Global scope'\n", 871 | "\n", 872 | "def shows_scope():\n", 873 | " '''Demonstrates local variable.'''\n", 874 | " inner = 'Local scope'\n", 875 | " print(outer)\n", 876 | " print(inner)" 877 | ], 878 | "execution_count": 37, 879 | "outputs": [] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "metadata": { 884 | "id": "oVJm5ChOJv7T", 885 | "colab": { 886 | "base_uri": "https://localhost:8080/" 887 | }, 888 | "outputId": "89febf14-fd19-45ac-a691-5d49e4bdbd38" 889 | }, 890 | "source": [ 891 | "shows_scope()" 892 | ], 893 | "execution_count": 38, 894 | "outputs": [ 895 | { 896 | "output_type": "stream", 897 | "text": [ 898 | "Global scope\n", 899 | "Local scope\n" 900 | ], 901 | "name": "stdout" 902 | } 903 | ] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "metadata": { 908 | "id": "GI8JXN3lJxGK", 909 | "colab": { 910 | "base_uri": "https://localhost:8080/", 911 | "height": 164 912 | }, 913 | "outputId": "5bd38e23-1af1-430a-9c0f-006f08bc83d7" 914 | }, 915 | "source": [ 916 | "print(inner)" 917 | ], 918 | "execution_count": 39, 919 | "outputs": [ 920 | { 921 | "output_type": "error", 922 | "ename": "NameError", 923 | "evalue": "ignored", 924 | "traceback": [ 925 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 926 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 927 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 928 | "\u001b[0;31mNameError\u001b[0m: name 'inner' is not defined" 929 | ] 930 | } 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "metadata": { 936 | "id": "2F-_HN-gJyOl", 937 | "colab": { 938 | "base_uri": "https://localhost:8080/" 939 | }, 940 | "outputId": "76cbce75-1673-40cf-bd53-90aa08a3ddd9" 941 | }, 942 | "source": [ 943 | "def add_one(n):\n", 944 | " '''Adds one to a number.'''\n", 945 | " return n + 1\n", 946 | "\n", 947 | "my_func = add_one\n", 948 | "print(my_func)" 949 | ], 950 | "execution_count": 40, 951 | "outputs": [ 952 | { 953 | "output_type": "stream", 954 | "text": [ 955 | "\n" 956 | ], 957 | "name": "stdout" 958 | } 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "metadata": { 964 | "id": "LgZ51GZXKNan", 965 | "colab": { 966 | "base_uri": "https://localhost:8080/" 967 | }, 968 | "outputId": "c8dace28-319c-4a9b-aa6e-3825c0b6cb68" 969 | }, 970 | "source": [ 971 | "my_func(2)" 972 | ], 973 | "execution_count": 41, 974 | "outputs": [ 975 | { 976 | "output_type": "execute_result", 977 | "data": { 978 | "text/plain": [ 979 | "3" 980 | ] 981 | }, 982 | "metadata": { 983 | "tags": [] 984 | }, 985 | "execution_count": 41 986 | } 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "metadata": { 992 | "id": "8X8VLlfCKPcN", 993 | "colab": { 994 | "base_uri": "https://localhost:8080/" 995 | }, 996 | "outputId": "1df0ab5f-41ae-4d9b-c94f-abb2ebaf6e45" 997 | }, 998 | "source": [ 999 | "def add_one(n):\n", 1000 | " '''Adds one to a number.'''\n", 1001 | " return n + 1\n", 1002 | "\n", 1003 | "def add_two(n):\n", 1004 | " '''Adds two to a number.'''\n", 1005 | " return n + 2\n", 1006 | "\n", 1007 | "my_functions = [add_one, add_two]\n", 1008 | "\n", 1009 | "for my_func in my_functions:\n", 1010 | " print(my_func(1))" 1011 | ], 1012 | "execution_count": 42, 1013 | "outputs": [ 1014 | { 1015 | "output_type": "stream", 1016 | "text": [ 1017 | "2\n", 1018 | "3\n" 1019 | ], 1020 | "name": "stdout" 1021 | } 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "metadata": { 1027 | "id": "TSr0v0WXKoRj", 1028 | "colab": { 1029 | "base_uri": "https://localhost:8080/" 1030 | }, 1031 | "outputId": "9f231668-ae4f-47a2-caa5-d781c68ffce9" 1032 | }, 1033 | "source": [ 1034 | "def call_nested():\n", 1035 | " '''Calls a nested function.'''\n", 1036 | " print('outer')\n", 1037 | "\n", 1038 | " def nested():\n", 1039 | " '''Prints a message.'''\n", 1040 | " print('nested')\n", 1041 | " \n", 1042 | " return nested\n", 1043 | "\n", 1044 | "my_func = call_nested()" 1045 | ], 1046 | "execution_count": 43, 1047 | "outputs": [ 1048 | { 1049 | "output_type": "stream", 1050 | "text": [ 1051 | "outer\n" 1052 | ], 1053 | "name": "stdout" 1054 | } 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "code", 1059 | "metadata": { 1060 | "id": "vFeSgL3Wh0jQ", 1061 | "colab": { 1062 | "base_uri": "https://localhost:8080/" 1063 | }, 1064 | "outputId": "8aa6be7d-05c2-4514-e0c8-dd71cbade271" 1065 | }, 1066 | "source": [ 1067 | "my_func()" 1068 | ], 1069 | "execution_count": 44, 1070 | "outputs": [ 1071 | { 1072 | "output_type": "stream", 1073 | "text": [ 1074 | "nested\n" 1075 | ], 1076 | "name": "stdout" 1077 | } 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "code", 1082 | "metadata": { 1083 | "id": "T1aEd2k9jkHM", 1084 | "colab": { 1085 | "base_uri": "https://localhost:8080/" 1086 | }, 1087 | "outputId": "49ab0bc0-9544-4df5-b3ca-3eba02e541dc" 1088 | }, 1089 | "source": [ 1090 | "def add_one(number):\n", 1091 | " '''Adds to a number.'''\n", 1092 | " print('Adding 1')\n", 1093 | " return number + 1\n", 1094 | "\n", 1095 | "def wrapper(number):\n", 1096 | " '''Wraps another function.'''\n", 1097 | " print('Before calling function')\n", 1098 | " retval = add_one(number)\n", 1099 | " print('After calling function')\n", 1100 | " return retval\n", 1101 | "\n", 1102 | "wrapper(1)" 1103 | ], 1104 | "execution_count": 45, 1105 | "outputs": [ 1106 | { 1107 | "output_type": "stream", 1108 | "text": [ 1109 | "Before calling function\n", 1110 | "Adding 1\n", 1111 | "After calling function\n" 1112 | ], 1113 | "name": "stdout" 1114 | }, 1115 | { 1116 | "output_type": "execute_result", 1117 | "data": { 1118 | "text/plain": [ 1119 | "2" 1120 | ] 1121 | }, 1122 | "metadata": { 1123 | "tags": [] 1124 | }, 1125 | "execution_count": 45 1126 | } 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "metadata": { 1132 | "id": "4hYw1L9Hh2e4", 1133 | "colab": { 1134 | "base_uri": "https://localhost:8080/" 1135 | }, 1136 | "outputId": "30b2f823-9315-4d3f-c0fd-261401f9906f" 1137 | }, 1138 | "source": [ 1139 | "def add_one(number):\n", 1140 | " '''Adds to a number.'''\n", 1141 | " print('Adding 1')\n", 1142 | " return number + 1\n", 1143 | "\n", 1144 | "def do_wrapping(some_func):\n", 1145 | " '''Returns a wrapped function.'''\n", 1146 | " print('wrapping function')\n", 1147 | "\n", 1148 | " def wrapper(number):\n", 1149 | " '''Wraps another function.'''\n", 1150 | " print('Before calling function')\n", 1151 | " retval = some_func(number)\n", 1152 | " print('After calling function')\n", 1153 | " return retval\n", 1154 | "\n", 1155 | " return wrapper\n", 1156 | "\n", 1157 | "\n", 1158 | "my_func = do_wrapping(add_one)" 1159 | ], 1160 | "execution_count": 46, 1161 | "outputs": [ 1162 | { 1163 | "output_type": "stream", 1164 | "text": [ 1165 | "wrapping function\n" 1166 | ], 1167 | "name": "stdout" 1168 | } 1169 | ] 1170 | }, 1171 | { 1172 | "cell_type": "code", 1173 | "metadata": { 1174 | "id": "vhelHppLkL5E", 1175 | "colab": { 1176 | "base_uri": "https://localhost:8080/" 1177 | }, 1178 | "outputId": "f9b6281b-dc7c-44b9-e7f4-f5b46efc5524" 1179 | }, 1180 | "source": [ 1181 | "my_func(1)" 1182 | ], 1183 | "execution_count": 47, 1184 | "outputs": [ 1185 | { 1186 | "output_type": "stream", 1187 | "text": [ 1188 | "Before calling function\n", 1189 | "Adding 1\n", 1190 | "After calling function\n" 1191 | ], 1192 | "name": "stdout" 1193 | }, 1194 | { 1195 | "output_type": "execute_result", 1196 | "data": { 1197 | "text/plain": [ 1198 | "2" 1199 | ] 1200 | }, 1201 | "metadata": { 1202 | "tags": [] 1203 | }, 1204 | "execution_count": 47 1205 | } 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "code", 1210 | "metadata": { 1211 | "id": "lvD5UaYF13xG", 1212 | "colab": { 1213 | "base_uri": "https://localhost:8080/" 1214 | }, 1215 | "outputId": "97c855d4-b76f-44b1-ceb2-e7ca10fc35ad" 1216 | }, 1217 | "source": [ 1218 | "def add_two(number):\n", 1219 | " '''Adds two to a number.'''\n", 1220 | " print('Adding 2')\n", 1221 | " return number + 2\n", 1222 | "\n", 1223 | "my_func = do_wrapping(add_two)\n", 1224 | "my_func(1)" 1225 | ], 1226 | "execution_count": 48, 1227 | "outputs": [ 1228 | { 1229 | "output_type": "stream", 1230 | "text": [ 1231 | "wrapping function\n", 1232 | "Before calling function\n", 1233 | "Adding 2\n", 1234 | "After calling function\n" 1235 | ], 1236 | "name": "stdout" 1237 | }, 1238 | { 1239 | "output_type": "execute_result", 1240 | "data": { 1241 | "text/plain": [ 1242 | "3" 1243 | ] 1244 | }, 1245 | "metadata": { 1246 | "tags": [] 1247 | }, 1248 | "execution_count": 48 1249 | } 1250 | ] 1251 | }, 1252 | { 1253 | "cell_type": "code", 1254 | "metadata": { 1255 | "id": "MZK-1DJwkSGL", 1256 | "colab": { 1257 | "base_uri": "https://localhost:8080/" 1258 | }, 1259 | "outputId": "9f990bc1-f5db-4c68-bb7b-cbc70694c5b0" 1260 | }, 1261 | "source": [ 1262 | "def do_wrapping(some_func):\n", 1263 | " '''Returns a wrapped function.'''\n", 1264 | " print('wrapping function')\n", 1265 | "\n", 1266 | " def wrapper(number):\n", 1267 | " '''Wraps another function.'''\n", 1268 | " print('Before calling function')\n", 1269 | " some_func(number)\n", 1270 | " print('After calling function')\n", 1271 | "\n", 1272 | " return wrapper\n", 1273 | "\n", 1274 | "@do_wrapping\n", 1275 | "def add_one(number):\n", 1276 | " '''Adds to a number.'''\n", 1277 | " print('Adding 1')\n", 1278 | " return number + 1\n", 1279 | "\n" 1280 | ], 1281 | "execution_count": 49, 1282 | "outputs": [ 1283 | { 1284 | "output_type": "stream", 1285 | "text": [ 1286 | "wrapping function\n" 1287 | ], 1288 | "name": "stdout" 1289 | } 1290 | ] 1291 | }, 1292 | { 1293 | "cell_type": "code", 1294 | "metadata": { 1295 | "id": "Vq7Fxwf5kmuP", 1296 | "colab": { 1297 | "base_uri": "https://localhost:8080/" 1298 | }, 1299 | "outputId": "2ffda20a-8a26-4686-8ccb-ac16d3bb477c" 1300 | }, 1301 | "source": [ 1302 | "add_one(1)" 1303 | ], 1304 | "execution_count": 50, 1305 | "outputs": [ 1306 | { 1307 | "output_type": "stream", 1308 | "text": [ 1309 | "Before calling function\n", 1310 | "Adding 1\n", 1311 | "After calling function\n" 1312 | ], 1313 | "name": "stdout" 1314 | } 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "metadata": { 1320 | "id": "V1FT4e4ekrMB", 1321 | "colab": { 1322 | "base_uri": "https://localhost:8080/" 1323 | }, 1324 | "outputId": "4f91af1e-542c-45f0-a2be-bbbd2ce87d14" 1325 | }, 1326 | "source": [ 1327 | "my_func = lambda x: x + 1\n", 1328 | "my_func(1)" 1329 | ], 1330 | "execution_count": 51, 1331 | "outputs": [ 1332 | { 1333 | "output_type": "execute_result", 1334 | "data": { 1335 | "text/plain": [ 1336 | "2" 1337 | ] 1338 | }, 1339 | "metadata": { 1340 | "tags": [] 1341 | }, 1342 | "execution_count": 51 1343 | } 1344 | ] 1345 | }, 1346 | { 1347 | "cell_type": "code", 1348 | "metadata": { 1349 | "id": "SHqJMybGlGtZ", 1350 | "colab": { 1351 | "base_uri": "https://localhost:8080/" 1352 | }, 1353 | "outputId": "f12bc5f0-b9ac-416b-ea89-2a03222dde55" 1354 | }, 1355 | "source": [ 1356 | "def apply_to_list(data, my_func):\n", 1357 | " '''Applies a function to items in a list.'''\n", 1358 | " for item in data:\n", 1359 | " print(f'{my_func(item)}')\n", 1360 | "\n", 1361 | "apply_to_list([1, 2, 3], lambda x: x + 1)" 1362 | ], 1363 | "execution_count": 52, 1364 | "outputs": [ 1365 | { 1366 | "output_type": "stream", 1367 | "text": [ 1368 | "2\n", 1369 | "3\n", 1370 | "4\n" 1371 | ], 1372 | "name": "stdout" 1373 | } 1374 | ] 1375 | }, 1376 | { 1377 | "cell_type": "code", 1378 | "metadata": { 1379 | "id": "ad0kdo88lITv" 1380 | }, 1381 | "source": [ 1382 | "def add_prefix(word, prefix='before-'):\n", 1383 | " '''Prepend a word.'''\n", 1384 | " return f'{prefix}{word}'" 1385 | ], 1386 | "execution_count": 53, 1387 | "outputs": [] 1388 | }, 1389 | { 1390 | "cell_type": "code", 1391 | "metadata": { 1392 | "id": "_Tvzu0FIXVEL" 1393 | }, 1394 | "source": [ 1395 | "def return_one():\n", 1396 | " return 1\n", 1397 | "\n", 1398 | "def wrapper():\n", 1399 | " print('a')\n", 1400 | " retval = return_one()\n", 1401 | " print('b')\n", 1402 | " print(retval)\n", 1403 | " " 1404 | ], 1405 | "execution_count": 54, 1406 | "outputs": [] 1407 | } 1408 | ] 1409 | } 1410 | -------------------------------------------------------------------------------- /Chapter-09:Pandas.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chpt09-Pandas.ipynb", 7 | "provenance": [], 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "display_name": "Python 3", 12 | "language": "python", 13 | "name": "python3" 14 | }, 15 | "language_info": { 16 | "codemirror_mode": { 17 | "name": "ipython", 18 | "version": 3 19 | }, 20 | "file_extension": ".py", 21 | "mimetype": "text/x-python", 22 | "name": "python", 23 | "nbconvert_exporter": "python", 24 | "pygments_lexer": "ipython3", 25 | "version": "3.8.0" 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "view-in-github", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "\"Open" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "id": "EWTjyen_zfYE" 43 | }, 44 | "source": [ 45 | "## About DataFrames\n", 46 | "\n", 47 | "- parts of a dataframe: series\n", 48 | "- relation to np arrays" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": { 54 | "id": "Y0qM0sOTpZVf" 55 | }, 56 | "source": [ 57 | "## Creation" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "metadata": { 63 | "id": "oYkPFyraDUJU" 64 | }, 65 | "source": [ 66 | "import pandas as pd\n", 67 | "df = pd.DataFrame()\n", 68 | "print(df)" 69 | ], 70 | "execution_count": null, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": { 76 | "id": "J7SNcde7OReB" 77 | }, 78 | "source": [ 79 | "### From dictionary" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "metadata": { 85 | "id": "a4oYxfdm2GoZ" 86 | }, 87 | "source": [ 88 | "import pandas as pd\n", 89 | "first_names = ['shanda', 'rolly', 'molly', 'frank', 'rip', 'steven', 'gwen', 'arthur']\n", 90 | "last_names = ['smith', 'brocker', 'stein', 'bach', 'spencer', 'de wilde', 'mason', 'davis']\n", 91 | "ages = [43, 23, 78, 56, 26, 14, 46, 92]" 92 | ], 93 | "execution_count": null, 94 | "outputs": [] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "metadata": { 99 | "id": "P4RVk2Mf9V3h" 100 | }, 101 | "source": [ 102 | "data = {'first':first_names,\n", 103 | " 'last':last_names,\n", 104 | " 'ages':ages}\n", 105 | "data" 106 | ], 107 | "execution_count": null, 108 | "outputs": [] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "metadata": { 113 | "id": "RMj-CeZK9hmY", 114 | "scrolled": true 115 | }, 116 | "source": [ 117 | "participants = pd.DataFrame(data)\n", 118 | "participants" 119 | ], 120 | "execution_count": null, 121 | "outputs": [] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": { 126 | "id": "1022nCWeOkON" 127 | }, 128 | "source": [ 129 | "### From lists" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "metadata": { 135 | "id": "3cZ5mTti9TTd", 136 | "scrolled": true 137 | }, 138 | "source": [ 139 | "data = [[\"shanda\", \"smith\", 43],\n", 140 | " [\"rolly\", \"brocker\", 23],\n", 141 | " [\"molly\", \"stein\", 78],\n", 142 | " [\"frank\", \"bach\", 56],\n", 143 | " [\"rip\", \"spencer\", 26],\n", 144 | " [\"steven\", \"de wilde\", 14],\n", 145 | " [\"gwen\", \"mason\", 46],\n", 146 | " [\"arthur\", \"davis\", 92]]\n", 147 | "\n", 148 | "participants = pd.DataFrame(data)\n", 149 | "participants" 150 | ], 151 | "execution_count": null, 152 | "outputs": [] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "metadata": { 157 | "id": "hfvePMqr8_pN" 158 | }, 159 | "source": [ 160 | "column_names = ['first', 'last', 'ages']\n", 161 | "participants = pd.DataFrame(data, columns=column_names)\n", 162 | "participants" 163 | ], 164 | "execution_count": null, 165 | "outputs": [] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "metadata": { 170 | "id": "JJujqrCGUC7x" 171 | }, 172 | "source": [ 173 | "index_labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", 174 | "participants = pd.DataFrame(data, \n", 175 | " columns=column_names, \n", 176 | " index=index_labels)\n", 177 | "participants" 178 | ], 179 | "execution_count": null, 180 | "outputs": [] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "metadata": { 185 | "id": "cPkA1RhC-AUg" 186 | }, 187 | "source": [ 188 | "# Data from data.fivethiryeight.com\n", 189 | "# Data set: college majors\n", 190 | "# Download the file to your workstation and then upload it to Colab\n", 191 | "college_majors = pd.read_csv('/content/all-ages.csv')\n", 192 | "\n", 193 | "\n", 194 | "college_majors = college_majors[['Major','Major_category','Total','Unemployment_rate']]\n", 195 | "college_majors" 196 | ], 197 | "execution_count": null, 198 | "outputs": [] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": { 203 | "id": "ZtMf_Ueey2lf" 204 | }, 205 | "source": [ 206 | "## Accessing Data\n", 207 | "- Head/Tail\n", 208 | "- descriptive stats\n", 209 | "\n", 210 | "- indexes(columns) named or numbered\n", 211 | "- accessing columns or indexes (brackets, iloc, loc, at, iat)\n", 212 | "- masking and filtering \n", 213 | " - single condidtion\n", 214 | " - combining conditions\n", 215 | " - conditions using multiple columns" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": { 221 | "id": "Ezeoh6PKVtq_" 222 | }, 223 | "source": [ 224 | "### Heads and tails" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "id": "ThM3XqpNSZLG" 231 | }, 232 | "source": [ 233 | "college_majors.head()" 234 | ], 235 | "execution_count": null, 236 | "outputs": [] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "metadata": { 241 | "id": "ZmtJzpIRSd9-" 242 | }, 243 | "source": [ 244 | "college_majors.head(3)" 245 | ], 246 | "execution_count": null, 247 | "outputs": [] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "metadata": { 252 | "id": "wNsXBxChSh3V" 253 | }, 254 | "source": [ 255 | "college_majors.tail()" 256 | ], 257 | "execution_count": null, 258 | "outputs": [] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": { 263 | "id": "DRTlJLytVz-T" 264 | }, 265 | "source": [ 266 | "### Descriptive statistics" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "metadata": { 272 | "id": "Dggaxh27SwAs" 273 | }, 274 | "source": [ 275 | "college_majors.describe()" 276 | ], 277 | "execution_count": null, 278 | "outputs": [] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "metadata": { 283 | "id": "Bf0kD786SyWb" 284 | }, 285 | "source": [ 286 | "college_majors.describe(percentiles=[0.1, 0.9])" 287 | ], 288 | "execution_count": null, 289 | "outputs": [] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "LBJGD9AuTQ1f" 295 | }, 296 | "source": [ 297 | "import numpy as np\n", 298 | "college_majors.describe(include=[np.object])" 299 | ], 300 | "execution_count": null, 301 | "outputs": [] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "metadata": { 306 | "id": "raxx4pozUlUn" 307 | }, 308 | "source": [ 309 | "college_majors.describe(include=['object'])" 310 | ], 311 | "execution_count": null, 312 | "outputs": [] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "metadata": { 317 | "id": "lymzkkWHVJkz" 318 | }, 319 | "source": [ 320 | "college_majors.describe(include='all')" 321 | ], 322 | "execution_count": null, 323 | "outputs": [] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "metadata": { 328 | "id": "OvXWs6KgTdy9" 329 | }, 330 | "source": [ 331 | "college_majors.describe(exclude=['int'])" 332 | ], 333 | "execution_count": null, 334 | "outputs": [] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": { 339 | "id": "Nt-Q_Dr-vx6Z" 340 | }, 341 | "source": [ 342 | "### Access data" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "metadata": { 348 | "id": "oBrhJ7XATnj8" 349 | }, 350 | "source": [ 351 | "participants" 352 | ], 353 | "execution_count": null, 354 | "outputs": [] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "metadata": { 359 | "id": "CR6SEnUVv8qm" 360 | }, 361 | "source": [ 362 | "participants['first']" 363 | ], 364 | "execution_count": null, 365 | "outputs": [] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "metadata": { 370 | "id": "LylxZ47tpn_-" 371 | }, 372 | "source": [ 373 | "df = pd.DataFrame({'one':[1,2,3],'two':[9,5,3]})\n", 374 | "df.std()" 375 | ], 376 | "execution_count": null, 377 | "outputs": [] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "metadata": { 382 | "id": "CTOWseyfppk2" 383 | }, 384 | "source": [ 385 | "df.iloc[0:2]" 386 | ], 387 | "execution_count": null, 388 | "outputs": [] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "metadata": { 393 | "id": "ToviR3Rsxw60" 394 | }, 395 | "source": [ 396 | "participants.ages" 397 | ], 398 | "execution_count": null, 399 | "outputs": [] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "metadata": { 404 | "id": "B6NN2p66y5D8" 405 | }, 406 | "source": [ 407 | "participants[['last', 'first']]" 408 | ], 409 | "execution_count": null, 410 | "outputs": [] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "metadata": { 415 | "id": "NqzMaXAhxzZ0" 416 | }, 417 | "source": [ 418 | "participants[3:6]" 419 | ], 420 | "execution_count": null, 421 | "outputs": [] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "metadata": { 426 | "id": "nP8K05QVyCVE" 427 | }, 428 | "source": [ 429 | "participants['a':'c']" 430 | ], 431 | "execution_count": null, 432 | "outputs": [] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "metadata": { 437 | "id": "hpMfx4ULzzNo" 438 | }, 439 | "source": [ 440 | "mask = [False, True, True, False, False, True, False, False]\n", 441 | "participants[mask]" 442 | ], 443 | "execution_count": null, 444 | "outputs": [] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": { 449 | "id": "PdO2ApWxTWLw" 450 | }, 451 | "source": [ 452 | "" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": { 458 | "id": "--zYZdF6zXit" 459 | }, 460 | "source": [ 461 | "### Optimized access by name" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "metadata": { 467 | "id": "EncruiIbyzpb" 468 | }, 469 | "source": [ 470 | "participants.loc['c']" 471 | ], 472 | "execution_count": null, 473 | "outputs": [] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "metadata": { 478 | "id": "Nuj0mgF-0XDS" 479 | }, 480 | "source": [ 481 | "participants.loc['c':'f']" 482 | ], 483 | "execution_count": null, 484 | "outputs": [] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "metadata": { 489 | "id": "4mSuz2iU0bpY" 490 | }, 491 | "source": [ 492 | "mask = [False, True, True, False, False, True, False, False]\n", 493 | "participants.loc[mask]" 494 | ], 495 | "execution_count": null, 496 | "outputs": [] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "metadata": { 501 | "id": "UUxLcu2K04HU" 502 | }, 503 | "source": [ 504 | "participants.loc[:, 'first']" 505 | ], 506 | "execution_count": null, 507 | "outputs": [] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "metadata": { 512 | "id": "fT5a7dw91HSD" 513 | }, 514 | "source": [ 515 | "participants.loc[:'c', ['ages', 'last']]" 516 | ], 517 | "execution_count": null, 518 | "outputs": [] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "metadata": { 523 | "id": "p7uC65-j1PYa" 524 | }, 525 | "source": [ 526 | "participants.loc[:'c', [False, True, True]]" 527 | ], 528 | "execution_count": null, 529 | "outputs": [] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": { 534 | "id": "SH78jUEv10gG" 535 | }, 536 | "source": [ 537 | "### Optimized access by index" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "metadata": { 543 | "id": "scrVkjdf1tg9" 544 | }, 545 | "source": [ 546 | "participants.iloc[3]" 547 | ], 548 | "execution_count": null, 549 | "outputs": [] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "metadata": { 554 | "id": "Rte4f7wx2FG6" 555 | }, 556 | "source": [ 557 | "participants.iloc[1:4]" 558 | ], 559 | "execution_count": null, 560 | "outputs": [] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "metadata": { 565 | "id": "XArxWNfC2NUD" 566 | }, 567 | "source": [ 568 | "participants.iloc[1:4, :2]" 569 | ], 570 | "execution_count": null, 571 | "outputs": [] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "metadata": { 576 | "id": "RO7_EENGCiFn" 577 | }, 578 | "source": [ 579 | "" 580 | ], 581 | "execution_count": null, 582 | "outputs": [] 583 | }, 584 | { 585 | "cell_type": "markdown", 586 | "metadata": { 587 | "id": "p1Njevwl5XYE" 588 | }, 589 | "source": [ 590 | "### Masking and filtering" 591 | ] 592 | }, 593 | { 594 | "cell_type": "markdown", 595 | "metadata": { 596 | "id": "RiQ4i_TIZ6F4" 597 | }, 598 | "source": [ 599 | "#### Comparison operators" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "metadata": { 605 | "id": "cLQH481YTr-K" 606 | }, 607 | "source": [ 608 | "college_majors = pd.read_csv('/content/all-ages.csv')\n", 609 | "college_majors" 610 | ], 611 | "execution_count": null, 612 | "outputs": [] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "metadata": { 617 | "id": "eRdlYtQNVq1H" 618 | }, 619 | "source": [ 620 | "college_majors.Major_category == 'Humanities & Liberal Arts'" 621 | ], 622 | "execution_count": null, 623 | "outputs": [] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "metadata": { 628 | "id": "24b3fyFjWXll" 629 | }, 630 | "source": [ 631 | "total_mask = college_majors.loc[:, 'Total'] > 1200000\n", 632 | "total_mask" 633 | ], 634 | "execution_count": null, 635 | "outputs": [] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "metadata": { 640 | "id": "80_qd6W9ZBmV" 641 | }, 642 | "source": [ 643 | "top_majors = college_majors.loc[total_mask]\n", 644 | "top_majors" 645 | ], 646 | "execution_count": null, 647 | "outputs": [] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "metadata": { 652 | "id": "2NFdv63HlulB" 653 | }, 654 | "source": [ 655 | "top_majors.Total.min()" 656 | ], 657 | "execution_count": null, 658 | "outputs": [] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "metadata": { 663 | "id": "ugXaIFdply5H" 664 | }, 665 | "source": [ 666 | "" 667 | ], 668 | "execution_count": null, 669 | "outputs": [] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "metadata": { 674 | "id": "FLrU9dZFZL7B" 675 | }, 676 | "source": [ 677 | "college_majors.Unemployment_rate.describe()" 678 | ], 679 | "execution_count": null, 680 | "outputs": [] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "metadata": { 685 | "id": "7GmxfUmQbZC1" 686 | }, 687 | "source": [ 688 | "employ_rate_mask = college_majors.loc[:, 'Unemployment_rate'] <= 0.046261\n", 689 | "employ_rate_majors = college_majors.loc[employ_rate_mask]\n", 690 | "employ_rate_majors.Major_category.unique()" 691 | ], 692 | "execution_count": null, 693 | "outputs": [] 694 | }, 695 | { 696 | "cell_type": "markdown", 697 | "metadata": { 698 | "id": "O4kaj-bjas_8" 699 | }, 700 | "source": [ 701 | "#### Pandas boolean operators\n", 702 | "| & ~" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "metadata": { 708 | "id": "6nscpZ-KcbRO" 709 | }, 710 | "source": [ 711 | "total_rate_mask = employ_rate_mask & total_mask\n", 712 | "total_rate_mask" 713 | ], 714 | "execution_count": null, 715 | "outputs": [] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "metadata": { 720 | "id": "YUORS6dRdtYg" 721 | }, 722 | "source": [ 723 | "college_majors.loc[total_rate_mask]" 724 | ], 725 | "execution_count": null, 726 | "outputs": [] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "metadata": { 731 | "id": "QBUjPrJKcPnv" 732 | }, 733 | "source": [ 734 | "lower_rate_mask = ~employ_rate_mask\n", 735 | "lower_rate_majors = college_majors.loc[lower_rate_mask]\n", 736 | "lower_rate_majors.Unemployment_rate.min()" 737 | ], 738 | "execution_count": null, 739 | "outputs": [] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "metadata": { 744 | "id": "TMmiw2EweKP7" 745 | }, 746 | "source": [ 747 | "college_majors.loc[total_mask | employ_rate_mask]" 748 | ], 749 | "execution_count": null, 750 | "outputs": [] 751 | }, 752 | { 753 | "cell_type": "markdown", 754 | "metadata": { 755 | "id": "VF06jUcrzA3Z" 756 | }, 757 | "source": [ 758 | "## Manipulating DataFrames\n", 759 | "- renaming\n", 760 | "- adding data to a dataframe\n", 761 | " - columns\n", 762 | " - from list\n", 763 | " - dataframe\n", 764 | " - operation on other columns\n", 765 | " - rows\n", 766 | " - append\n", 767 | " - concat\n", 768 | "- deleting \n", 769 | " - index (resetting)\n", 770 | " - columns drop (in_place)\n", 771 | " - rows" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "metadata": { 777 | "id": "ENm0xEEPdlvY" 778 | }, 779 | "source": [ 780 | "participants.columns" 781 | ], 782 | "execution_count": null, 783 | "outputs": [] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "metadata": { 788 | "id": "-WTDTh96d1ut" 789 | }, 790 | "source": [ 791 | "participants.rename(columns={'ages': 'Age'})" 792 | ], 793 | "execution_count": null, 794 | "outputs": [] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "metadata": { 799 | "id": "YpIHFOzCpEoB" 800 | }, 801 | "source": [ 802 | "participants.columns" 803 | ], 804 | "execution_count": null, 805 | "outputs": [] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "metadata": { 810 | "id": "6QYKW7GApH4D" 811 | }, 812 | "source": [ 813 | "participants.rename(columns={'ages':'Age'}, inplace=True)\n", 814 | "participants.columns" 815 | ], 816 | "execution_count": null, 817 | "outputs": [] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "metadata": { 822 | "id": "SGUkg6UVpRQx" 823 | }, 824 | "source": [ 825 | "" 826 | ], 827 | "execution_count": null, 828 | "outputs": [] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "metadata": { 833 | "id": "oSYpSJRMhZ64" 834 | }, 835 | "source": [ 836 | "participants['Zip Code'] = [94702, 97402, 94223, 94705, 97503, 94705, 94111, 95333]\n", 837 | "participants" 838 | ], 839 | "execution_count": null, 840 | "outputs": [] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "metadata": { 845 | "id": "QlbbgqhriACy" 846 | }, 847 | "source": [ 848 | "participants['Full Name'] = participants.loc[:, 'first'] + participants.loc[:, 'last']\n", 849 | "participants" 850 | ], 851 | "execution_count": null, 852 | "outputs": [] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "metadata": { 857 | "id": "o-8kyONVjI1R" 858 | }, 859 | "source": [ 860 | "participants['Full Name'] = participants.loc[:, 'first'] + ' ' + participants.loc[:, 'last']\n", 861 | "participants" 862 | ], 863 | "execution_count": null, 864 | "outputs": [] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": { 869 | "id": "jANdbGQCzSx-" 870 | }, 871 | "source": [ 872 | "## Manipulating Data\n", 873 | "- setting values using loc and iloc \n", 874 | "- operations on dataframes and series\n", 875 | "- map function elements in column (series)\n", 876 | "- apply function - across rows or columns\n", 877 | "\n", 878 | "- pivot, pivot_table?" 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "metadata": { 884 | "id": "YP5wkRBxlh35" 885 | }, 886 | "source": [ 887 | "participants.loc['h', 'first'] = 'Paul'\n", 888 | "participants" 889 | ], 890 | "execution_count": null, 891 | "outputs": [] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "metadata": { 896 | "id": "lVjiM14Mlqxj" 897 | }, 898 | "source": [ 899 | "participants.iloc[3, 2] = 99\n", 900 | "participants" 901 | ], 902 | "execution_count": null, 903 | "outputs": [] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "metadata": { 908 | "id": "7wvNc17BmRx2" 909 | }, 910 | "source": [ 911 | "participants.Age -= 1\n", 912 | "participants" 913 | ], 914 | "execution_count": null, 915 | "outputs": [] 916 | }, 917 | { 918 | "cell_type": "code", 919 | "metadata": { 920 | "id": "XWgZeJ9amqZh" 921 | }, 922 | "source": [ 923 | "" 924 | ], 925 | "execution_count": null, 926 | "outputs": [] 927 | }, 928 | { 929 | "cell_type": "markdown", 930 | "metadata": { 931 | "id": "vreWwDdhhR8b" 932 | }, 933 | "source": [ 934 | "#### Replace" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "metadata": { 940 | "id": "PHyFvDJueQDg" 941 | }, 942 | "source": [ 943 | "participants.replace('rolly', 'Smiley')" 944 | ], 945 | "execution_count": null, 946 | "outputs": [] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "metadata": { 951 | "id": "IAgk0p0Reu7M" 952 | }, 953 | "source": [ 954 | "participants.replace(r'(s)([a-z]+)', r'S\\2', regex=True)" 955 | ], 956 | "execution_count": null, 957 | "outputs": [] 958 | }, 959 | { 960 | "cell_type": "code", 961 | "metadata": { 962 | "id": "QzWUktaVfZ4e" 963 | }, 964 | "source": [ 965 | "def cap_word(w):\n", 966 | " return w.capitalize()\n", 967 | "\n", 968 | "participants.loc[:, 'first'].apply(cap_word)" 969 | ], 970 | "execution_count": null, 971 | "outputs": [] 972 | }, 973 | { 974 | "cell_type": "code", 975 | "metadata": { 976 | "id": "_5fCulbyprJO" 977 | }, 978 | "source": [ 979 | "def say_hello(row):\n", 980 | " return f'{row[\"first\"]} is {row[\"Age\"]} years old.'\n", 981 | "\n", 982 | "participants.apply(say_hello, axis=1)" 983 | ], 984 | "execution_count": null, 985 | "outputs": [] 986 | }, 987 | { 988 | "cell_type": "markdown", 989 | "metadata": { 990 | "id": "3mnBIVoio-pG" 991 | }, 992 | "source": [ 993 | "### Interactive Display" 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "metadata": { 999 | "id": "eKsnOn2DkHnp" 1000 | }, 1001 | "source": [ 1002 | "#@ Colab DataFrame interactive display\n", 1003 | "%load_ext google.colab.data_table" 1004 | ], 1005 | "execution_count": null, 1006 | "outputs": [] 1007 | }, 1008 | { 1009 | "cell_type": "code", 1010 | "metadata": { 1011 | "id": "nav-qa3Tkx7v" 1012 | }, 1013 | "source": [ 1014 | "college_majors" 1015 | ], 1016 | "execution_count": null, 1017 | "outputs": [] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "metadata": { 1022 | "id": "IdEFPkqcC40O" 1023 | }, 1024 | "source": [ 1025 | "" 1026 | ], 1027 | "execution_count": null, 1028 | "outputs": [] 1029 | } 1030 | ] 1031 | } -------------------------------------------------------------------------------- /Chapter-11:Machine-Learning-Libraries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Chpt12-Machine.Learning.Libraries.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyPw9W1CofjV3luCQ1MQo3me", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "view-in-github", 21 | "colab_type": "text" 22 | }, 23 | "source": [ 24 | "\"Open" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "id": "gMO7TTN-Cxhm" 31 | }, 32 | "source": [ 33 | "## Notes\n", 34 | "* Tensorflow\n", 35 | " - Developed for internal use at Google\n", 36 | " - Based on Nueral networks\n", 37 | "* Keras\n", 38 | " - opened source\n", 39 | " - can be run on top of Tensorflow\n", 40 | " - specialized for neural networks?\n", 41 | " - designed by google engineer?\n", 42 | " - [Getting Started with Keras](https://youtu.be/J6Ok8p463C4)\n", 43 | "* Pytorch\n", 44 | " - ease of use\n", 45 | " - Developed at Facebook\n", 46 | " - computer vision, natural language processing\n", 47 | " - based on torch C library\n", 48 | "* Scikit-learn\n", 49 | " - Built on numpy and scipy\n", 50 | " - popular for classical algorithms\n", 51 | " - [Getting started](https://youtu.be/rvVkVsG49uU)\n", 52 | "\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "id": "Gaj5ZbwGcGpg" 59 | }, 60 | "source": [ 61 | "## Introduction\n", 62 | "Machine learning consists of letting a computer find a way to solve a problem using data. This contrasts with traditional programming where the programmer defines the means of solution in code. In this chapter, we will take a overview of some of the more popular libraries used for machine learning. These libraries implement the algorithms used to create and train machine learning model. These models have variuos uses, dependeding on the type of problem. Some models are useful for predicting future values, others for classifying data into groups or categories. \n", 63 | "\n" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "id": "GeSQnKuwd8mK" 70 | }, 71 | "source": [ 72 | "## Popular Libraries\n", 73 | "\n", 74 | "Four of the most popular libraries are TensorFlow, Keras, Pytorch, and Scikit-learn. TensorFlow was developed by Google for internal use. It is a powerful library used to solve problems using deep learning. This involves defining layers that transform the data and which are tuned as the solution is fit to data. Keras is an opened source library designed to work with TensorFlow, and it is now included in the TensorFlow library. \n", 75 | "\n", 76 | "PyTorch is Facebook's contribution to production worthy machone learning libraries. It is based on the Torch library, which makes use of GPUs in solving parralell problems.\n", 77 | "\n", 78 | "Scikit-learn is a popular library for starting machine learning. It is built on top of NumPy and SciPy. It has classes for most of the traditional algorithms. We will take a closer look at Scikit-learn, but first lets talk about a general approach to solving a problem using machine learning." 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": { 84 | "id": "waxOj9jYhpA9" 85 | }, 86 | "source": [ 87 | "## High Level Process\n", 88 | "\n", 89 | "Machine learning algorithmns can be divided into two types, unsupervised and supervised learning. Unsupervised learning involes discovering insights about data without pre-existing results to test against. In supervised learning, you use known data to train and test a model. Generally the steps to trainig a supervised model are:\n", 90 | "\n", 91 | "1. Transform data\n", 92 | "2. Separate out test data\n", 93 | "3. Train the model\n", 94 | "4. Test accuracy\n", 95 | "\n", 96 | "Scikit-learn has tools to simplify each of these steps.\n", 97 | "\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": { 103 | "id": "e2sRiVjRiIFo" 104 | }, 105 | "source": [ 106 | "# Transformations\n", 107 | "\n", 108 | "For some algorithms it is advantagous to transform the data before training a model. For example you might want to take a continuous variable, such as age, and turn it into discreat catagories, such as age ranges. Scikit-learn includes many types of transformers, including ones for cleaning, feature extraction, reduction and expansion. These are represented as classes which generally use a .fit() method determine the transformation and a .transform() method to modify data. In figure ... we use a MinMaxScaler. This transformer scales values to fit in a defined range, between 0 and 1 by default." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "metadata": { 114 | "colab": { 115 | "base_uri": "https://localhost:8080/" 116 | }, 117 | "id": "XzJpXtnrl9JO", 118 | "outputId": "1a7eeae1-fd4b-48e5-cf95-e342b56661bd" 119 | }, 120 | "source": [ 121 | "import numpy as np\n", 122 | "\n", 123 | "data = np.array([[100, 34, 4],\n", 124 | " [90, 2, 0],\n", 125 | " [78, -12, 16],\n", 126 | " [23, 45, 4]])\n", 127 | "\n", 128 | "data" 129 | ], 130 | "execution_count": null, 131 | "outputs": [ 132 | { 133 | "output_type": "execute_result", 134 | "data": { 135 | "text/plain": [ 136 | "array([[100, 34, 4],\n", 137 | " [ 90, 2, 0],\n", 138 | " [ 78, -12, 16],\n", 139 | " [ 23, 45, 4]])" 140 | ] 141 | }, 142 | "metadata": { 143 | "tags": [] 144 | }, 145 | "execution_count": 1 146 | } 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "metadata": { 152 | "colab": { 153 | "base_uri": "https://localhost:8080/" 154 | }, 155 | "id": "DKzFAL3ZkYTN", 156 | "outputId": "32cb7c93-64b8-4cdc-80bd-59b44b4105c9" 157 | }, 158 | "source": [ 159 | "from sklearn.preprocessing import MinMaxScaler\n", 160 | "\n", 161 | "minMax = MinMaxScaler()\n", 162 | "scaler = minMax.fit(data)\n", 163 | "\n", 164 | "scaler.transform(data)" 165 | ], 166 | "execution_count": null, 167 | "outputs": [ 168 | { 169 | "output_type": "execute_result", 170 | "data": { 171 | "text/plain": [ 172 | "array([[1. , 0.80701754, 0.25 ],\n", 173 | " [0.87012987, 0.24561404, 0. ],\n", 174 | " [0.71428571, 0. , 1. ],\n", 175 | " [0. , 1. , 0.25 ]])" 176 | ] 177 | }, 178 | "metadata": { 179 | "tags": [] 180 | }, 181 | "execution_count": 2 182 | } 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": { 188 | "id": "GI1EMzrDxCqa" 189 | }, 190 | "source": [ 191 | "There may be times you wish to seperate your data before fitting the tranformer. In this way the tranformer settings will not be effected by the test data. Since the fitting and transforming are seperate methods, it is easy to fit to the train data and use that to transform the test data." 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": { 197 | "id": "nSLZ6RELyJGk" 198 | }, 199 | "source": [ 200 | "## Splitting test and training data\n", 201 | "\n", 202 | "One important pitfall to avoid when training a model is over-fitting. This is when a model perfectly predicts the data used to train it, but has little predictive power with new data. In the simplest sense, we avoid overfitting by not testing the model with the data that it was trained upon. Scikit-learn offers helper methods to make splitting data easy. In figure 12.2 we use the Scikit-learn function train_test_split() to split the iris data set provided with the library, into train and test data sets. " 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": { 208 | "id": "ETi1ILPgC_Ff" 209 | }, 210 | "source": [ 211 | "Loading a sample dataset" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "metadata": { 217 | "id": "SVcKxfeLCp-8" 218 | }, 219 | "source": [ 220 | "from sklearn import datasets" 221 | ], 222 | "execution_count": null, 223 | "outputs": [] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "metadata": { 228 | "id": "A2uedwGxK5ii" 229 | }, 230 | "source": [ 231 | "data, target = datasets.load_iris(return_X_y=True)" 232 | ], 233 | "execution_count": null, 234 | "outputs": [] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "metadata": { 239 | "colab": { 240 | "base_uri": "https://localhost:8080/" 241 | }, 242 | "id": "veA82zfHLyFT", 243 | "outputId": "0e6e1bbe-f24d-4848-8cff-a08a7f5b93c6" 244 | }, 245 | "source": [ 246 | "print(type(data))\n", 247 | "print(data.shape)" 248 | ], 249 | "execution_count": null, 250 | "outputs": [ 251 | { 252 | "output_type": "stream", 253 | "text": [ 254 | "\n", 255 | "(150, 4)\n" 256 | ], 257 | "name": "stdout" 258 | } 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "metadata": { 264 | "colab": { 265 | "base_uri": "https://localhost:8080/" 266 | }, 267 | "id": "-LcZhSj_LzT0", 268 | "outputId": "5a05649f-74d9-4503-bcc3-c86b60728e3b" 269 | }, 270 | "source": [ 271 | "print(type(target))\n", 272 | "print(target.shape)" 273 | ], 274 | "execution_count": null, 275 | "outputs": [ 276 | { 277 | "output_type": "stream", 278 | "text": [ 279 | "\n", 280 | "(150,)\n" 281 | ], 282 | "name": "stdout" 283 | } 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": { 289 | "id": "5olkLFTtDMc9" 290 | }, 291 | "source": [ 292 | "Splitting data into training and test" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "metadata": { 298 | "id": "W1j3Bfo3L6gT", 299 | "colab": { 300 | "base_uri": "https://localhost:8080/" 301 | }, 302 | "outputId": "616efdc7-780c-4c18-8735-ebaf08dfeecc" 303 | }, 304 | "source": [ 305 | "from sklearn.model_selection import train_test_split\n", 306 | "\n", 307 | "train_data, test_data, train_target, test_target = train_test_split(data, target)\n", 308 | "print(train_data.shape)\n", 309 | "print(train_target.shape)\n", 310 | "print(test_data.shape)\n", 311 | "print(test_target.shape)" 312 | ], 313 | "execution_count": null, 314 | "outputs": [ 315 | { 316 | "output_type": "stream", 317 | "text": [ 318 | "(112, 4)\n", 319 | "(112,)\n", 320 | "(38, 4)\n", 321 | "(38,)\n" 322 | ], 323 | "name": "stdout" 324 | } 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": { 330 | "id": "B40MlhyJdf0F" 331 | }, 332 | "source": [ 333 | "## Training a model\n", 334 | "\n", 335 | "Scikit-learn offers many classes representing various machine learning algorithms. These are refered to as estimators. Many estimators can be tuned using parameters during instantiation. Each estimator has a .fit() method which trains the model. Most of the .fit() methods take two arguments. The first is some sort of training data, refered to as samples.The second is the results, or targets, for those samples. Both arguments should be an array like objects, such as a NumPy arrays. Once the training is done, the model can predict results using it's .predict() method. The accuracy of this prediction can be checked using functions from the methods module. Figure 12. shows a simple example using the KNeighborsClassifier estimator. " 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "metadata": { 341 | "id": "ENT6ay4o91ha", 342 | "colab": { 343 | "base_uri": "https://localhost:8080/" 344 | }, 345 | "outputId": "7aebbcc5-ab84-4651-eb27-06396a5c4a13" 346 | }, 347 | "source": [ 348 | "from sklearn import metrics\n", 349 | "from sklearn.neighbors import KNeighborsClassifier\n", 350 | "knn = KNeighborsClassifier(n_neighbors=3)\n", 351 | "\n", 352 | "knn.fit(train_data, train_target)\n", 353 | "test_prediction = knn.predict(test_data)\n", 354 | "metrics.accuracy_score(test_target, test_prediction)" 355 | ], 356 | "execution_count": null, 357 | "outputs": [ 358 | { 359 | "output_type": "execute_result", 360 | "data": { 361 | "text/plain": [ 362 | "1.0" 363 | ] 364 | }, 365 | "metadata": { 366 | "tags": [] 367 | }, 368 | "execution_count": 9 369 | } 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": { 375 | "id": "mnih6Fx1Jf4Y" 376 | }, 377 | "source": [ 378 | "## Next Steps\n", 379 | "\n", 380 | "We have only touched the surface of Scikit-learns capability. Other important features are tools for cross-validation, where a data set is split multiple times to avoid over-fitting on test data, and Pipelines which wrap up transformers, estimators and cross-validation together. If you want to learn more about Scikit-learn, there are great tutorials on the official webiste, [scikit-learn tutorials](https://scikit-learn.org/stable/tutorial/index.html#tutorial-menu)." 381 | ] 382 | }, 383 | { 384 | "cell_type": "markdown", 385 | "metadata": { 386 | "id": "6P6xFNBL_5Sw" 387 | }, 388 | "source": [ 389 | "## Summary\n", 390 | "\n", 391 | "Many of the algorthims used to create Machine Learning models are represented in the major Python Machine Learning libraries. TensorFlow is a deep learning library created by Google. PyTorch is a library built on Torch by Facebook. Scikit-learn is popular library for getting started with Machine Learning. It has modules and functions to perform the steps of creating and analysing a model. " 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": { 397 | "id": "IYLqzHmABG6X" 398 | }, 399 | "source": [ 400 | "## Questions\n", 401 | "\n", 402 | "1. In which step of training a supervised estimator would a Scikit-learn Transformer be useful?\n", 403 | "\n", 404 | "2. Why is it important to separate training and test data?\n", 405 | "\n", 406 | "3. Once you have transformed your data and trained your model, what should you do next?" 407 | ] 408 | } 409 | ] 410 | } 411 | -------------------------------------------------------------------------------- /Data-Conversion-Recipes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Data-Conversion-Recipes.ipynb", 7 | "provenance": [], 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "markdown", 18 | "metadata": { 19 | "id": "view-in-github", 20 | "colab_type": "text" 21 | }, 22 | "source": [ 23 | "\"Open" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "id": "m1oI49wGAJj2" 30 | }, 31 | "source": [ 32 | "## Convert lists to dicts, and dicts to lists " 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": { 38 | "id": "ODNn01QhGy4q" 39 | }, 40 | "source": [ 41 | "### Converting Lists to Dictionaries" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": { 47 | "id": "kC0DL7jccwTz" 48 | }, 49 | "source": [ 50 | "#### Create basic dictionary" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "metadata": { 56 | "id": "XKmoizujJ3BM", 57 | "outputId": "c1c7088a-dc7f-4e57-defc-75692becd25c", 58 | "colab": { 59 | "base_uri": "https://localhost:8080/", 60 | "height": 34 61 | } 62 | }, 63 | "source": [ 64 | "key_values = [('one', 1), ('two', 2), ('three', 3)]\n", 65 | "d = dict( key_values )\n", 66 | "d" 67 | ], 68 | "execution_count": null, 69 | "outputs": [ 70 | { 71 | "output_type": "execute_result", 72 | "data": { 73 | "text/plain": [ 74 | "{'one': 1, 'two': 2, 'three': 3}" 75 | ] 76 | }, 77 | "metadata": { 78 | "tags": [] 79 | }, 80 | "execution_count": 28 81 | } 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": { 87 | "id": "db_EeMOXcsMl" 88 | }, 89 | "source": [ 90 | "#### Zip two lists" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "metadata": { 96 | "id": "xLT9Vq-ScC3u", 97 | "outputId": "50d167d9-9e12-4d10-ba28-99007f0e6b77", 98 | "colab": { 99 | "base_uri": "https://localhost:8080/", 100 | "height": 85 101 | } 102 | }, 103 | "source": [ 104 | "instruments = [ 'violin', 'lute', 'banjo', 'accordian']\n", 105 | "players = [ 'Anne-Sophie Mutter', \n", 106 | " 'Julian Bream', \n", 107 | " 'Noam Pikelny', \n", 108 | " 'Astor Pantaleón Piazzolla' ]\n", 109 | "\n", 110 | "d = dict(zip(instruments, players))\n", 111 | "d" 112 | ], 113 | "execution_count": null, 114 | "outputs": [ 115 | { 116 | "output_type": "execute_result", 117 | "data": { 118 | "text/plain": [ 119 | "{'violin': 'Anne-Sophie Mutter',\n", 120 | " 'lute': 'Julian Bream',\n", 121 | " 'banjo': 'Noam Pikelny',\n", 122 | " 'accordian': 'Astor Pantaleón Piazzolla'}" 123 | ] 124 | }, 125 | "metadata": { 126 | "tags": [] 127 | }, 128 | "execution_count": 29 129 | } 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": { 135 | "id": "hnrbuW1OeoaP" 136 | }, 137 | "source": [ 138 | "#### From keys" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "metadata": { 144 | "id": "oOHL-nQBcEiM", 145 | "outputId": "178b795b-73ce-4a6a-ca51-894788af1993", 146 | "colab": { 147 | "base_uri": "https://localhost:8080/", 148 | "height": 34 149 | } 150 | }, 151 | "source": [ 152 | "racers = ['Tom', 'Bill', 'Will', 'Jill']\n", 153 | "start_distance = 0\n", 154 | "d = dict.fromkeys(racers, start_distance)\n", 155 | "d" 156 | ], 157 | "execution_count": null, 158 | "outputs": [ 159 | { 160 | "output_type": "execute_result", 161 | "data": { 162 | "text/plain": [ 163 | "{'Tom': 0, 'Bill': 0, 'Will': 0, 'Jill': 0}" 164 | ] 165 | }, 166 | "metadata": { 167 | "tags": [] 168 | }, 169 | "execution_count": 30 170 | } 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": { 176 | "id": "zDeFh7DeHV_k" 177 | }, 178 | "source": [ 179 | "### Converting Dictionaries to Lists" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "metadata": { 185 | "id": "s13Km8SmH0da" 186 | }, 187 | "source": [ 188 | "d = {'name': 'toby', 'id' : 14}" 189 | ], 190 | "execution_count": null, 191 | "outputs": [] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": { 196 | "id": "ZcA63r-bKUGZ" 197 | }, 198 | "source": [ 199 | "#### Get a list of keys" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "metadata": { 205 | "id": "HdldSNWZKaKQ", 206 | "outputId": "7dac2b40-48b6-4a40-a941-a63179e8e401", 207 | "colab": { 208 | "base_uri": "https://localhost:8080/", 209 | "height": 34 210 | } 211 | }, 212 | "source": [ 213 | "list(d)" 214 | ], 215 | "execution_count": null, 216 | "outputs": [ 217 | { 218 | "output_type": "execute_result", 219 | "data": { 220 | "text/plain": [ 221 | "['name', 'id']" 222 | ] 223 | }, 224 | "metadata": { 225 | "tags": [] 226 | }, 227 | "execution_count": 32 228 | } 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "id": "TSDFCcDjKhun" 235 | }, 236 | "source": [ 237 | "#### Get keys in sorted order" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "metadata": { 243 | "id": "Z-HCbayYKkDi", 244 | "outputId": "928f7c6a-4519-4705-e06f-13123191f8c0", 245 | "colab": { 246 | "base_uri": "https://localhost:8080/", 247 | "height": 34 248 | } 249 | }, 250 | "source": [ 251 | "sorted(d)" 252 | ], 253 | "execution_count": null, 254 | "outputs": [ 255 | { 256 | "output_type": "execute_result", 257 | "data": { 258 | "text/plain": [ 259 | "['id', 'name']" 260 | ] 261 | }, 262 | "metadata": { 263 | "tags": [] 264 | }, 265 | "execution_count": 33 266 | } 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": { 272 | "id": "DvTN0dDKKux-" 273 | }, 274 | "source": [ 275 | "#### Get list of values" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "metadata": { 281 | "id": "Hd2aiFbTKxE2", 282 | "outputId": "4af85c54-741a-474c-ca75-5c92f8fb4226", 283 | "colab": { 284 | "base_uri": "https://localhost:8080/", 285 | "height": 34 286 | } 287 | }, 288 | "source": [ 289 | "list(d.values())" 290 | ], 291 | "execution_count": null, 292 | "outputs": [ 293 | { 294 | "output_type": "execute_result", 295 | "data": { 296 | "text/plain": [ 297 | "['toby', 14]" 298 | ] 299 | }, 300 | "metadata": { 301 | "tags": [] 302 | }, 303 | "execution_count": 34 304 | } 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": { 310 | "id": "cKAgtsidi8_3" 311 | }, 312 | "source": [ 313 | "### Get list of items\n" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "metadata": { 319 | "id": "8lKJ9RRLi4tz", 320 | "outputId": "681a01d1-9ce6-489f-8b52-98b80e1220ec", 321 | "colab": { 322 | "base_uri": "https://localhost:8080/", 323 | "height": 34 324 | } 325 | }, 326 | "source": [ 327 | "list(d.items())" 328 | ], 329 | "execution_count": null, 330 | "outputs": [ 331 | { 332 | "output_type": "execute_result", 333 | "data": { 334 | "text/plain": [ 335 | "[('name', 'toby'), ('id', 14)]" 336 | ] 337 | }, 338 | "metadata": { 339 | "tags": [] 340 | }, 341 | "execution_count": 35 342 | } 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": { 348 | "id": "iA3xuV1YOo_M" 349 | }, 350 | "source": [ 351 | "## Convert dicts to pandas Dataframe" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": { 357 | "id": "xBThaF6Lf6-l" 358 | }, 359 | "source": [ 360 | "### Create DataFrame using data parameter" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "metadata": { 366 | "id": "1k7y_fGvca73", 367 | "outputId": "809d90f6-13bd-4b6b-9531-fac73e348a8f", 368 | "colab": { 369 | "base_uri": "https://localhost:8080/", 370 | "height": 142 371 | } 372 | }, 373 | "source": [ 374 | "from pandas import DataFrame\n", 375 | "\n", 376 | "d = {'first': ['Jill', 'Solma', 'Elizabeth'], \n", 377 | " 'last': ['Stein', 'Smith', 'Tudor']}\n", 378 | "DataFrame(data=d)" 379 | ], 380 | "execution_count": null, 381 | "outputs": [ 382 | { 383 | "output_type": "execute_result", 384 | "data": { 385 | "text/plain": [ 386 | " first last\n", 387 | "0 Jill Stein\n", 388 | "1 Solma Smith\n", 389 | "2 Elizabeth Tudor" 390 | ], 391 | "text/html": [ 392 | "
\n", 393 | "\n", 406 | "\n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | "
firstlast
0JillStein
1SolmaSmith
2ElizabethTudor
\n", 432 | "
" 433 | ] 434 | }, 435 | "metadata": { 436 | "tags": [] 437 | }, 438 | "execution_count": 36 439 | } 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": { 445 | "id": "R14wQwcOLMwI" 446 | }, 447 | "source": [ 448 | "### Use class from_dict method" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "metadata": { 454 | "id": "jkWlDXTncVM-", 455 | "outputId": "97fdfdff-53b4-45f8-e10a-e0c47de79f07", 456 | "colab": { 457 | "base_uri": "https://localhost:8080/", 458 | "height": 142 459 | } 460 | }, 461 | "source": [ 462 | "DataFrame.from_dict(d)" 463 | ], 464 | "execution_count": null, 465 | "outputs": [ 466 | { 467 | "output_type": "execute_result", 468 | "data": { 469 | "text/plain": [ 470 | " first last\n", 471 | "0 Jill Stein\n", 472 | "1 Solma Smith\n", 473 | "2 Elizabeth Tudor" 474 | ], 475 | "text/html": [ 476 | "
\n", 477 | "\n", 490 | "\n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | "
firstlast
0JillStein
1SolmaSmith
2ElizabethTudor
\n", 516 | "
" 517 | ] 518 | }, 519 | "metadata": { 520 | "tags": [] 521 | }, 522 | "execution_count": 37 523 | } 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": { 529 | "id": "ygpgBXkifzj5" 530 | }, 531 | "source": [ 532 | "### Create DataFrame with index orientation" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "metadata": { 538 | "id": "jKTTCaxafbqp", 539 | "outputId": "94809a6b-9d57-4235-f0a5-289c759bace0", 540 | "colab": { 541 | "base_uri": "https://localhost:8080/", 542 | "height": 142 543 | } 544 | }, 545 | "source": [ 546 | "d = {0: ['Edward', 'Tudor'], 1: ['Robert', 'Redford'], 3: ['Earl', 'Scruggs']}\n", 547 | "df = DataFrame.from_dict(d, orient='index')\n", 548 | "df.columns=['first', 'last']\n", 549 | "df" 550 | ], 551 | "execution_count": null, 552 | "outputs": [ 553 | { 554 | "output_type": "execute_result", 555 | "data": { 556 | "text/plain": [ 557 | " first last\n", 558 | "0 Edward Tudor\n", 559 | "1 Robert Redford\n", 560 | "3 Earl Scruggs" 561 | ], 562 | "text/html": [ 563 | "
\n", 564 | "\n", 577 | "\n", 578 | " \n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | "
firstlast
0EdwardTudor
1RobertRedford
3EarlScruggs
\n", 603 | "
" 604 | ] 605 | }, 606 | "metadata": { 607 | "tags": [] 608 | }, 609 | "execution_count": 38 610 | } 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": { 616 | "id": "vO5pY89WLz7k" 617 | }, 618 | "source": [ 619 | "### Assign column names" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "metadata": { 625 | "id": "A6eFFjrifVmb", 626 | "outputId": "6e1fa941-9a23-4b99-aaa8-2dd0dfc21a1f", 627 | "colab": { 628 | "base_uri": "https://localhost:8080/", 629 | "height": 142 630 | } 631 | }, 632 | "source": [ 633 | "d = {'a': 'A', 'b': 'B', 'c': 'C'}\n", 634 | "df = DataFrame(list(d.items()), columns=['lower', 'upper'])\n", 635 | "df" 636 | ], 637 | "execution_count": null, 638 | "outputs": [ 639 | { 640 | "output_type": "execute_result", 641 | "data": { 642 | "text/plain": [ 643 | " lower upper\n", 644 | "0 a A\n", 645 | "1 b B\n", 646 | "2 c C" 647 | ], 648 | "text/html": [ 649 | "
\n", 650 | "\n", 663 | "\n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | "
lowerupper
0aA
1bB
2cC
\n", 689 | "
" 690 | ] 691 | }, 692 | "metadata": { 693 | "tags": [] 694 | }, 695 | "execution_count": 39 696 | } 697 | ] 698 | }, 699 | { 700 | "cell_type": "markdown", 701 | "metadata": { 702 | "id": "tPHx60a2O-A_" 703 | }, 704 | "source": [ 705 | "## Convert characters to integers and back " 706 | ] 707 | }, 708 | { 709 | "cell_type": "markdown", 710 | "metadata": { 711 | "id": "Nv2oPnYcMlQ2" 712 | }, 713 | "source": [ 714 | "### Cast str to int" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": { 720 | "id": "IK3H12LAMnlh" 721 | }, 722 | "source": [ 723 | "#### Base 10" 724 | ] 725 | }, 726 | { 727 | "cell_type": "code", 728 | "metadata": { 729 | "id": "Cz744KkLMpbR", 730 | "outputId": "2bd1d427-2556-47d6-db21-f336cfe4ff9a", 731 | "colab": { 732 | "base_uri": "https://localhost:8080/", 733 | "height": 34 734 | } 735 | }, 736 | "source": [ 737 | "int('011')" 738 | ], 739 | "execution_count": null, 740 | "outputs": [ 741 | { 742 | "output_type": "execute_result", 743 | "data": { 744 | "text/plain": [ 745 | "11" 746 | ] 747 | }, 748 | "metadata": { 749 | "tags": [] 750 | }, 751 | "execution_count": 40 752 | } 753 | ] 754 | }, 755 | { 756 | "cell_type": "markdown", 757 | "metadata": { 758 | "id": "c-oF4oJoMsdD" 759 | }, 760 | "source": [ 761 | "#### Base 2" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "metadata": { 767 | "id": "VQ-rlW-UMuKd", 768 | "outputId": "44430f72-cf45-44dc-9103-f3db94d706af", 769 | "colab": { 770 | "base_uri": "https://localhost:8080/", 771 | "height": 34 772 | } 773 | }, 774 | "source": [ 775 | "int('011', 2)" 776 | ], 777 | "execution_count": null, 778 | "outputs": [ 779 | { 780 | "output_type": "execute_result", 781 | "data": { 782 | "text/plain": [ 783 | "3" 784 | ] 785 | }, 786 | "metadata": { 787 | "tags": [] 788 | }, 789 | "execution_count": 41 790 | } 791 | ] 792 | }, 793 | { 794 | "cell_type": "markdown", 795 | "metadata": { 796 | "id": "qTxUMxokM0VD" 797 | }, 798 | "source": [ 799 | "#### Base 6" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "metadata": { 805 | "id": "f4RrKcKaM231", 806 | "outputId": "f3d6bbfc-d866-49fa-ec8b-123a886e1acf", 807 | "colab": { 808 | "base_uri": "https://localhost:8080/", 809 | "height": 34 810 | } 811 | }, 812 | "source": [ 813 | "int('011', 6)" 814 | ], 815 | "execution_count": null, 816 | "outputs": [ 817 | { 818 | "output_type": "execute_result", 819 | "data": { 820 | "text/plain": [ 821 | "7" 822 | ] 823 | }, 824 | "metadata": { 825 | "tags": [] 826 | }, 827 | "execution_count": 42 828 | } 829 | ] 830 | }, 831 | { 832 | "cell_type": "markdown", 833 | "metadata": { 834 | "id": "3KfoL3upM_So" 835 | }, 836 | "source": [ 837 | "#### Base 8" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "metadata": { 843 | "id": "r2XsGuB2NBda", 844 | "outputId": "3c72b131-2146-4b13-c202-511c58426887", 845 | "colab": { 846 | "base_uri": "https://localhost:8080/", 847 | "height": 34 848 | } 849 | }, 850 | "source": [ 851 | "int('011', 8)" 852 | ], 853 | "execution_count": null, 854 | "outputs": [ 855 | { 856 | "output_type": "execute_result", 857 | "data": { 858 | "text/plain": [ 859 | "9" 860 | ] 861 | }, 862 | "metadata": { 863 | "tags": [] 864 | }, 865 | "execution_count": 43 866 | } 867 | ] 868 | }, 869 | { 870 | "cell_type": "markdown", 871 | "metadata": { 872 | "id": "SY90ktC3NDcg" 873 | }, 874 | "source": [ 875 | "#### Base 16" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "metadata": { 881 | "id": "oR-XdBNXNFIK", 882 | "outputId": "5c81b57e-78de-41a3-d619-155c72a67777", 883 | "colab": { 884 | "base_uri": "https://localhost:8080/", 885 | "height": 34 886 | } 887 | }, 888 | "source": [ 889 | "int('011', 16)" 890 | ], 891 | "execution_count": null, 892 | "outputs": [ 893 | { 894 | "output_type": "execute_result", 895 | "data": { 896 | "text/plain": [ 897 | "17" 898 | ] 899 | }, 900 | "metadata": { 901 | "tags": [] 902 | }, 903 | "execution_count": 44 904 | } 905 | ] 906 | }, 907 | { 908 | "cell_type": "markdown", 909 | "metadata": { 910 | "id": "3lbw66gCNPTz" 911 | }, 912 | "source": [ 913 | "### Cast int to string" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "metadata": { 919 | "id": "VihfMVYSk1GB", 920 | "outputId": "84200235-6421-4b83-a296-c7df92f46e5c", 921 | "colab": { 922 | "base_uri": "https://localhost:8080/", 923 | "height": 34 924 | } 925 | }, 926 | "source": [ 927 | "one = str(1)\n", 928 | "type(one)\n" 929 | ], 930 | "execution_count": null, 931 | "outputs": [ 932 | { 933 | "output_type": "execute_result", 934 | "data": { 935 | "text/plain": [ 936 | "str" 937 | ] 938 | }, 939 | "metadata": { 940 | "tags": [] 941 | }, 942 | "execution_count": 45 943 | } 944 | ] 945 | }, 946 | { 947 | "cell_type": "markdown", 948 | "metadata": { 949 | "id": "qLNbYaeLPCMG" 950 | }, 951 | "source": [ 952 | "### Convert between hexadecimal, binary, and floats" 953 | ] 954 | }, 955 | { 956 | "cell_type": "markdown", 957 | "metadata": { 958 | "id": "p7AjiUsq7Nov" 959 | }, 960 | "source": [ 961 | "### Cast to str from float\n" 962 | ] 963 | }, 964 | { 965 | "cell_type": "code", 966 | "metadata": { 967 | "id": "vx2E_TYy9Nt-", 968 | "outputId": "028c6b99-5591-4913-fbd8-dda644a16a5f", 969 | "colab": { 970 | "base_uri": "https://localhost:8080/", 971 | "height": 34 972 | } 973 | }, 974 | "source": [ 975 | "a_str = str(12.4)\n", 976 | "f\" {a_str!r} is a {type(a_str)}\"" 977 | ], 978 | "execution_count": null, 979 | "outputs": [ 980 | { 981 | "output_type": "execute_result", 982 | "data": { 983 | "text/plain": [ 984 | "\" '12.4' is a \"" 985 | ] 986 | }, 987 | "metadata": { 988 | "tags": [] 989 | }, 990 | "execution_count": 46 991 | } 992 | ] 993 | }, 994 | { 995 | "cell_type": "markdown", 996 | "metadata": { 997 | "id": "ip3NuTGqNwAv" 998 | }, 999 | "source": [ 1000 | "### Cast to float from str" 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "code", 1005 | "metadata": { 1006 | "id": "EMpCWo-tN2pg", 1007 | "outputId": "dd7d1f9d-6c16-4f70-fdd3-19d14aa362a4", 1008 | "colab": { 1009 | "base_uri": "https://localhost:8080/", 1010 | "height": 34 1011 | } 1012 | }, 1013 | "source": [ 1014 | "a_str = \"12.3\"\n", 1015 | "a_float = float(a_str)\n", 1016 | "f\" {a_float!r} is a {type(a_float)}\"" 1017 | ], 1018 | "execution_count": null, 1019 | "outputs": [ 1020 | { 1021 | "output_type": "execute_result", 1022 | "data": { 1023 | "text/plain": [ 1024 | "\" 12.3 is a \"" 1025 | ] 1026 | }, 1027 | "metadata": { 1028 | "tags": [] 1029 | }, 1030 | "execution_count": 47 1031 | } 1032 | ] 1033 | }, 1034 | { 1035 | "cell_type": "markdown", 1036 | "metadata": { 1037 | "id": "fjwdh1uO7U58" 1038 | }, 1039 | "source": [ 1040 | "### Hexadecimal" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "markdown", 1045 | "metadata": { 1046 | "id": "JRLzqsSMOkQ1" 1047 | }, 1048 | "source": [ 1049 | "#### Int to hex str" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "metadata": { 1055 | "id": "35D8N6zR-aAA", 1056 | "outputId": "3070d593-979a-4588-cb3a-4626506c90ea", 1057 | "colab": { 1058 | "base_uri": "https://localhost:8080/", 1059 | "height": 34 1060 | } 1061 | }, 1062 | "source": [ 1063 | "int_hex = hex(18)\n", 1064 | "f\" hex(18) returns the {type(int_hex)}: {int_hex!r}\"\n" 1065 | ], 1066 | "execution_count": null, 1067 | "outputs": [ 1068 | { 1069 | "output_type": "execute_result", 1070 | "data": { 1071 | "text/plain": [ 1072 | "\" hex(18) returns the : '0x12'\"" 1073 | ] 1074 | }, 1075 | "metadata": { 1076 | "tags": [] 1077 | }, 1078 | "execution_count": 48 1079 | } 1080 | ] 1081 | }, 1082 | { 1083 | "cell_type": "markdown", 1084 | "metadata": { 1085 | "id": "J9yOmbk7OrWj" 1086 | }, 1087 | "source": [ 1088 | "#### Float to hex str" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "code", 1093 | "metadata": { 1094 | "id": "EdXjN9MSOtbv", 1095 | "outputId": "d79ac032-af61-4334-b52a-40c8c0e1ad8d", 1096 | "colab": { 1097 | "base_uri": "https://localhost:8080/", 1098 | "height": 34 1099 | } 1100 | }, 1101 | "source": [ 1102 | "float_hex = 12.0.hex()\n", 1103 | "f\" 12.4.hex() returns the {type(float_hex)}: {float_hex!r}\"" 1104 | ], 1105 | "execution_count": null, 1106 | "outputs": [ 1107 | { 1108 | "output_type": "execute_result", 1109 | "data": { 1110 | "text/plain": [ 1111 | "\" 12.4.hex() returns the : '0x1.8000000000000p+3'\"" 1112 | ] 1113 | }, 1114 | "metadata": { 1115 | "tags": [] 1116 | }, 1117 | "execution_count": 49 1118 | } 1119 | ] 1120 | }, 1121 | { 1122 | "cell_type": "markdown", 1123 | "metadata": { 1124 | "id": "UaLcFYpTom-I" 1125 | }, 1126 | "source": [ 1127 | "[hex function](https://docs.python.org/3/library/functions.html)" 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "markdown", 1132 | "metadata": { 1133 | "id": "fMGfHTiR7YQg" 1134 | }, 1135 | "source": [ 1136 | "### Conversion to and from binary" 1137 | ] 1138 | }, 1139 | { 1140 | "cell_type": "markdown", 1141 | "metadata": { 1142 | "id": "Gl0_nausl0kh" 1143 | }, 1144 | "source": [ 1145 | "#### Bytes literal\n", 1146 | "Similar to strings, but limited to ASCII characters." 1147 | ] 1148 | }, 1149 | { 1150 | "cell_type": "code", 1151 | "metadata": { 1152 | "id": "ENwjzQW2mDmY", 1153 | "outputId": "5f24319c-95e2-4ace-830b-eff1e589b4ee", 1154 | "colab": { 1155 | "base_uri": "https://localhost:8080/", 1156 | "height": 34 1157 | } 1158 | }, 1159 | "source": [ 1160 | "bytes_str = b\"some bytes literal\"\n", 1161 | "type(bytes_str)" 1162 | ], 1163 | "execution_count": null, 1164 | "outputs": [ 1165 | { 1166 | "output_type": "execute_result", 1167 | "data": { 1168 | "text/plain": [ 1169 | "bytes" 1170 | ] 1171 | }, 1172 | "metadata": { 1173 | "tags": [] 1174 | }, 1175 | "execution_count": 50 1176 | } 1177 | ] 1178 | }, 1179 | { 1180 | "cell_type": "markdown", 1181 | "metadata": { 1182 | "id": "kYGgEuxnPFBI" 1183 | }, 1184 | "source": [ 1185 | "#### Encode to bytes" 1186 | ] 1187 | }, 1188 | { 1189 | "cell_type": "code", 1190 | "metadata": { 1191 | "id": "-t-2J5Da7tDB", 1192 | "outputId": "e30217ba-5dff-4874-8ee9-a23dda7fc9c3", 1193 | "colab": { 1194 | "base_uri": "https://localhost:8080/", 1195 | "height": 34 1196 | } 1197 | }, 1198 | "source": [ 1199 | "import base64\n", 1200 | "bytes_str = b\"Encode this string\"\n", 1201 | "encoded_str = base64.b64encode(bytes_str)\n", 1202 | "f\"The encoded string {encoded_str!r} is of type {type(encoded_str)}\"\n", 1203 | "\n" 1204 | ], 1205 | "execution_count": null, 1206 | "outputs": [ 1207 | { 1208 | "output_type": "execute_result", 1209 | "data": { 1210 | "text/plain": [ 1211 | "\"The encoded string b'RW5jb2RlIHRoaXMgc3RyaW5n' is of type \"" 1212 | ] 1213 | }, 1214 | "metadata": { 1215 | "tags": [] 1216 | }, 1217 | "execution_count": 51 1218 | } 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "markdown", 1223 | "metadata": { 1224 | "id": "CHhv72ZlPKNp" 1225 | }, 1226 | "source": [ 1227 | "#### Decode from bytes" 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "code", 1232 | "metadata": { 1233 | "id": "haBNDHcIPOZH", 1234 | "outputId": "4ec66cbc-e212-42d6-d422-fb4102059bba", 1235 | "colab": { 1236 | "base_uri": "https://localhost:8080/", 1237 | "height": 34 1238 | } 1239 | }, 1240 | "source": [ 1241 | "base64.b64decode(encoded_str)" 1242 | ], 1243 | "execution_count": null, 1244 | "outputs": [ 1245 | { 1246 | "output_type": "execute_result", 1247 | "data": { 1248 | "text/plain": [ 1249 | "b'Encode this string'" 1250 | ] 1251 | }, 1252 | "metadata": { 1253 | "tags": [] 1254 | }, 1255 | "execution_count": 52 1256 | } 1257 | ] 1258 | } 1259 | ] 1260 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # foundational-python-for-data-science 2 | --------------------------------------------------------------------------------