|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python Libraries\n", |
| 8 | + "\n", |
| 9 | + "**Learning Objectives**:\n", |
| 10 | + "- Understand the purpose of libraries, and where to find them.\n", |
| 11 | + "- How to install a new library.\n", |
| 12 | + "- Import and use functions from libraries in Python.\n", |
| 13 | + "****\n", |
| 14 | + "\n", |
| 15 | + "A **library** is a collection of functions bundled neatly into a module that can be used by other users. Libraries usually contains classes, functions, and sometimes variables. Libraries are typically developed to serve a unified purpose.\n", |
| 16 | + "\n", |
| 17 | + "We have already been using Python's [standard library](https://docs.python.org/3/library/) - it comes ready and loaded with Python. We've also used `pandas` to work with data frames.\n", |
| 18 | + "\n", |
| 19 | + "Many additional libraries are available from [Anaconda](https://www.anaconda.com) or [PyPI](https://pypi.python.org/pypi) (the Python Package Index)." |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "## Installing New Libraries\n", |
| 27 | + "\n", |
| 28 | + "### Option 1: Anaconda Navigator\n", |
| 29 | + "\n", |
| 30 | + "Many popular (and some not-so-popular) libraries are available for installation through the Anaconda Navigator. Let's use the Anaconda Navigator to install a library called `fuzzywuzzy`. \n", |
| 31 | + "\n", |
| 32 | + "To do so, follow these steps:\n", |
| 33 | + "\n", |
| 34 | + "1. Open the Anaconda Navigator application.\n", |
| 35 | + "2. Click the \"Environments\" tab in the left-hand menu.\n", |
| 36 | + "3. Click the drop-down box that, by default, says \"Installed\". Change it to say \"All\".\n", |
| 37 | + "4. Click the \"Channels\" button, which will open up a dialog box. In that dialog box press the \"Add\" button in the top right corner. Type \"conda-forge\" in the new line that appears in the dialog box, then press enter. Finally, press the green \"Update channels\" button in the bottom right-hand corner.\n", |
| 38 | + "5. Use the \"Search Package\" text box to enter `fuzzywuzzy` and press \"Enter\".\n", |
| 39 | + "6. Select the checkbox next to the `fuzzywuzzy` package (library) name that appears in the list below.\n", |
| 40 | + "7. Click the green \"Apply\" button in the bottom right-hand corner. This will open up a dialog box that says \"Install Packages\". It will say \"Solving package specifications\" with a blue progress bar. This may take a few minutes.\n", |
| 41 | + "8. Eventually, it will show a list of packages that would need to be installed. Press the green \"Apply\" button in the \"Install Packages dialog box\" which will install the packages. This may take a few minutes.\n", |
| 42 | + "\n", |
| 43 | + "### Option 2: Installing Using the Command Line\n", |
| 44 | + "\n", |
| 45 | + "Another option is to install a package directly using the command line. On Macs, you would open up the Terminal application. On Windows, open up the Anaconda Prompt application.\n", |
| 46 | + "\n", |
| 47 | + "Once you have this application open, you can use `pip`, a Python package installer, to install new packages from PyPI. Simply run `pip install [PACKAGE_NAME]`, and the package will be installed.\n", |
| 48 | + "\n", |
| 49 | + "### Option 3: Installing within a Jupyter Notebook\n", |
| 50 | + "\n", |
| 51 | + "You can also install packages within a Jupyter Notebook. Create a new cell, and run the command `!pip install [PACKAGE_NAME]`. This should then run the `pip` install in the background." |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "## Importing Packages\n", |
| 59 | + "\n", |
| 60 | + "An installed package is not yet available for us to use while running Python. We still have to **import** the package into the current session.\n", |
| 61 | + "\n", |
| 62 | + "Importing is done via the `import` keyword. We simply run `import [PACKAGE_NAME]`, and everything inside the package becomes available to use.\n", |
| 63 | + "\n", |
| 64 | + "Packages are typically organized into modules and submodules. Within these modules and submodules are functions, classes, and variables. All of these components can be accessed with **dot notation**: e.g., `[LIBRARY_NAME].[MODULE_NAME]`. Python uses `.` to mean \"part of\".\n", |
| 65 | + "\n", |
| 66 | + "Let's import the `math` module, which is provided by default as part of the Python distribution. Let's access a variable and function from this module using dot notation:" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "import math\n", |
| 76 | + "\n", |
| 77 | + "print('pi is', math.pi)\n", |
| 78 | + "print('cos(pi) is', math.cos(math.pi))" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "## Finding More About a Package's Contents\n", |
| 86 | + "\n", |
| 87 | + "How did we know that `pi` and `cos()` were components of `math`? Usually, packages provide documentation which explain these components. We can access this documentation with the `help` function:" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "help(math)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "You can also view documentation [online](https://docs.python.org/3/library/math.html). \n", |
| 104 | + "\n", |
| 105 | + "Being comfortable sifting through documentation is a **very** important skill!" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "## Challenge 1: When Is Help Available?\n", |
| 113 | + "\n", |
| 114 | + "When a colleague of yours types `help(math)`,\n", |
| 115 | + "Python reports an error:\n", |
| 116 | + "\n", |
| 117 | + "> ~~~\n", |
| 118 | + "> NameError: name 'math' is not defined\n", |
| 119 | + "> ~~~\n", |
| 120 | + "\n", |
| 121 | + "What has your colleague forgotten to do?" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "## Importing Everything\n", |
| 129 | + "\n", |
| 130 | + "We can uses the `*` character to import everything from a library, and then refer to each item by name without needing the prefix." |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "from math import *\n", |
| 140 | + "print(pi)" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "metadata": { |
| 146 | + "tags": [] |
| 147 | + }, |
| 148 | + "source": [ |
| 149 | + "In general, this is bad practice. Packages can be large, and different packages might have functions sharing the same names. If you imports everything from multiple packages, you might have variable names overwritten, leading to problems down the line!" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "## Importing Specific Components of a Package\n", |
| 157 | + "\n", |
| 158 | + "We generally want to import only what we need from a package. To import a specific component of a package, we can use the `from` keyword. This allows us to import a specific module, function, or variable, and then refer to them directly without the library name as prefix.\n", |
| 159 | + "\n", |
| 160 | + "Specifically, we use the syntax `from [PACKAGE_NAME] import [COMPONENT]`.\n", |
| 161 | + "\n", |
| 162 | + "Let's do this with the `math` module." |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [ |
| 171 | + "from math import cos, pi\n", |
| 172 | + "\n", |
| 173 | + "print('cos(pi) is', cos(pi))" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "## Importing with an Alias\n", |
| 181 | + "\n", |
| 182 | + "Python allows you to import packages or modules with an alias, using the `as` keyword. Specifically, use the syntax `import [PACKAGE] as [ALIAS]` to give a library a new name. You would then refer to items in the library using that shortened name. You've seen this already with `import pandas as pd`." |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "import math as m\n", |
| 192 | + "\n", |
| 193 | + "print('cos(pi) is', m.cos(m.pi))" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "markdown", |
| 198 | + "metadata": {}, |
| 199 | + "source": [ |
| 200 | + "There are very common abbreviations used for some of the more popular libraries, including:\n", |
| 201 | + "\n", |
| 202 | + "* `pandas` -> `pd`\n", |
| 203 | + "* `numpy` -> `np`\n", |
| 204 | + "* `matplotlib` -> `mpl`.\n", |
| 205 | + "\n", |
| 206 | + "But sometimes aliases can make programs harder to understand, since readers must learn your program's aliases. Be very intentional about using aliases!" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "markdown", |
| 211 | + "metadata": {}, |
| 212 | + "source": [ |
| 213 | + "## Challenge 2: Locating the Right Library\n", |
| 214 | + "\n", |
| 215 | + "You want to select a random value from your data.\n", |
| 216 | + "\n", |
| 217 | + "1. What [standard library](https://docs.python.org/3/library/) would you most expect to help?\n", |
| 218 | + "2. Which function would you select from that library? Are there alternatives?\n", |
| 219 | + "3. Import the library, and apply the function to the following list." |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "code", |
| 224 | + "execution_count": null, |
| 225 | + "metadata": {}, |
| 226 | + "outputs": [], |
| 227 | + "source": [ |
| 228 | + " ids = [1, 2, 3, 4, 5, 6]" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": null, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [], |
| 236 | + "source": [ |
| 237 | + "# YOUR CODE HERE\n" |
| 238 | + ] |
| 239 | + } |
| 240 | + ], |
| 241 | + "metadata": { |
| 242 | + "kernelspec": { |
| 243 | + "display_name": "Python 3 (ipykernel)", |
| 244 | + "language": "python", |
| 245 | + "name": "python3" |
| 246 | + }, |
| 247 | + "language_info": { |
| 248 | + "codemirror_mode": { |
| 249 | + "name": "ipython", |
| 250 | + "version": 3 |
| 251 | + }, |
| 252 | + "file_extension": ".py", |
| 253 | + "mimetype": "text/x-python", |
| 254 | + "name": "python", |
| 255 | + "nbconvert_exporter": "python", |
| 256 | + "pygments_lexer": "ipython3", |
| 257 | + "version": "3.8.12" |
| 258 | + }, |
| 259 | + "toc": { |
| 260 | + "base_numbering": 1, |
| 261 | + "nav_menu": {}, |
| 262 | + "number_sections": false, |
| 263 | + "sideBar": true, |
| 264 | + "skip_h1_title": false, |
| 265 | + "title_cell": "Table of Contents", |
| 266 | + "title_sidebar": "Contents", |
| 267 | + "toc_cell": false, |
| 268 | + "toc_position": {}, |
| 269 | + "toc_section_display": true, |
| 270 | + "toc_window_display": false |
| 271 | + }, |
| 272 | + "varInspector": { |
| 273 | + "cols": { |
| 274 | + "lenName": 16, |
| 275 | + "lenType": 16, |
| 276 | + "lenVar": 40 |
| 277 | + }, |
| 278 | + "kernels_config": { |
| 279 | + "python": { |
| 280 | + "delete_cmd_postfix": "", |
| 281 | + "delete_cmd_prefix": "del ", |
| 282 | + "library": "var_list.py", |
| 283 | + "varRefreshCmd": "print(var_dic_list())" |
| 284 | + }, |
| 285 | + "r": { |
| 286 | + "delete_cmd_postfix": ") ", |
| 287 | + "delete_cmd_prefix": "rm(", |
| 288 | + "library": "var_list.r", |
| 289 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 290 | + } |
| 291 | + }, |
| 292 | + "types_to_exclude": [ |
| 293 | + "module", |
| 294 | + "function", |
| 295 | + "builtin_function_or_method", |
| 296 | + "instance", |
| 297 | + "_Feature" |
| 298 | + ], |
| 299 | + "window_display": false |
| 300 | + } |
| 301 | + }, |
| 302 | + "nbformat": 4, |
| 303 | + "nbformat_minor": 4 |
| 304 | +} |
0 commit comments