{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pre-requisites\n", "\n", "* functions\n", "* types\n", "* collections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the key concepts in programming is the creation of simple and easy to maintain code, this is where Python packages come in. A Python package contains code (often written by somebody else) with specific functionalities that allow it to be reused without the need for duplicating code.\n", "\n", "For example, if we wanted to write a short Python script that calculates the cosine of an angle we can use the `NumPy` package. Before we use the package we need to install it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installing Packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installing Python packages is very simple and one of the most popular package managers to achieve this is `pip`. Here, `pip` accesses the [Python Package Index](https://pypi.org/) (`PyPI`) that contains a large number of Python packages.\n", "\n", "For our example we need the `NumPy` package and this can be installed via the following command at the terminal/anaconda prompt (see [here](https://pythoninchemistry.org/import-anything) for more information about different operating systems):\n", "\n", "```bash\n", "pip install numpy\n", "```\n", "\n", "Here, `pip` will install `numpy` and all of the dependencies that it needs to work. We now have our package, but how can we go about using it?\n", "\n", "***\n", "\n", "An alternative and popular package maneger is Conda. The benefit of using Conda is that it can install packages written in different langauges (e.g. C, C++ etc) and can also create independent Python environments. You can find out more about Conda [here](https://docs.conda.io/en/latest/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing Packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to use an installed package in our Python script we can make use of the `import` statement. It is customary (and recommended) to `import` any packages at the start of a Python script. For our example we will need to do this:\n", "\n", "```python\n", "import numpy\n", "```\n", "\n", "We can then use the `numpy` package and the functions it contains to construct our script:\n", "\n", "```python\n", "## Import the entire numpy package\n", "import numpy\n", "\n", "angle = numpy.radians(90)\n", "\n", "cosine_angle = numpy.cos(angle)\n", "```\n", "\n", "Great! We've now successfully used `import` to use our newly installed `numpy` package. If we don't want to `import` the entire `numpy` package (as we have above) we can instead only `import` the specific functions we need:\n", "\n", "```python\n", "## Import specific functions from numpy\n", "from numpy import cos, radians\n", "\n", "angle = radians(90)\n", "\n", "cosine_angle = cos(angle)\n", "```\n", "\n", "However, this can sometimes have unintended consequences if these functions share the same name with variables in a longer script. This problem can also arise using `from numpy import *`, which imports all functions without the need to use the `numpy.` syntax. To get around this it is common to `import` packages and rename them:\n", "\n", "```python\n", "## Import the specific functions from numpy\n", "import numpy as np\n", "\n", "angle = np.radians(90)\n", "\n", "cosine_angle = np.cos(angle)\n", "```\n", "\n", "Although our example has used the `numpy` package, there are a large number of packages to choose from and each have a customary name that is often used. For example:\n", "\n", "```python\n", "import matplotlib as mpl \n", "import matplotlib.pyplot as plt \n", "import numpy as np\n", "import pandas as pd\n", "``` " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.3 64-bit", "language": "python", "name": "python37364bitd38a54fb656a4d2291427feb2f9b7184" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3-final" } }, "nbformat": 4, "nbformat_minor": 2 }