{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Libraries\n",
"In order to use the functionality provided by a library in own programs it is necessary to *import* the library. Importing\n",
"a library makes the provided functionality available. This unit focuses on the different possibilities to import\n",
"libraries.\n",
"\n",
"\n",
"# Importing libraries\n",
"In Python 🐍 libraries are imported using the `import` statement. In the following examples different libraries from the\n",
"Python Standard Library are used to explain the `import` statement. In order to import the `math` library the following\n",
"import statement can be used."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [math library](https://docs.python.org/3/library/math.html) - as the name suggests - provides mathematical functions\n",
"and constants. Examples of provided mathematical functions are\n",
"[sin()](https://docs.python.org/3/library/math.html#math.sin) and\n",
"[sqrt()](https://docs.python.org/3/library/math.html#math.sqrt). An Example of a provided constant is\n",
"[π](https://docs.python.org/3/library/math.html#math.pi). \n",
"\n",
"After importing the `math` library the provided functions can be accessed using their *qualifier* as shown in the\n",
"example blow. This is necessary as importing a library creates a *namespace* into which the functions of the library\n",
"are imported. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = math.cos(math.pi)\n",
"print(\"The value of cos(π) is\", x)\n",
"\n",
"print(\"The value of sin(π/2) is\", math.sin(math.pi / 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Always writing the complete library name is quite tedious. Therefore, the `import` statement enables the renaming of the\n",
"namespace upon import. In case of the `math` library the namespace could be renamed to `m` to enable access to the\n",
"functions using a shorter prefix."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math as m\n",
"\n",
"x = float(input(\"Please enter a number:\"))\n",
"\n",
"sin_x_square = m.pow(m.sin(x), 2)\n",
"cos_x_square = m.pow(m.cos(x), 2)\n",
"\n",
"result = sin_x_square + cos_x_square\n",
"\n",
"print(\"The value of \", result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the example above, the functions [sin()](https://docs.python.org/3/library/math.html#math.sin),\n",
"[cos()](https://docs.python.org/3/library/math.html#math.cos) and\n",
"[pow()](https://docs.python.org/3/library/math.html#math.pow) are used to calculate the\n",
"[Pythagorean Identity](https://en.wikipedia.org/wiki/Pythagorean_trigonometric_identity) $(sin(x))^2 + (cos(x))^2 = 1$. Of course it would also be possible to use `**` instead of the `pow()` function.\n",
"The `pow()` function was used here to show an additional function from the `math` library.\n",
"\n",
"## Partially importing libraries\n",
"Besides importing all functions of a library it is also possible to import only individual functions of a library. This\n",
"is done using the import variant shown below. If the `from` ... `import` statement is used to import functions, these\n",
"can be accessed without providing the library name. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import sin, pi\n",
"\n",
"x = sin(3 * pi / 2)\n",
"print(\"The value of sin(3 * π /2) is\", x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Namespaces\n",
"As mentioned above importing a library creates a namespace. Why are namespaces useful? In Python 🐍 some names are\n",
"already assigned:\n",
"- Keywords: `with`, `for`, `and`\n",
"- Builtin functions: `print()`, `input()`\n",
"\n",
"By importing a library more names are assigned. By adding those to namespaces, conflicts with already assigned names are\n",
"avoided. Why this is necessary is shown by the following example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import sin\n",
"\n",
"print(\"The value of sin(0) is:\", sin(0))\n",
"\n",
"\n",
"def sin(n):\n",
" return \"I don't know how to calculate the sine of \" + str(n)\n",
"\n",
"\n",
"print(\"The value of sin(0) is:\", sin(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the example first the `sin()`function from the `math` library is imported. This adds the function `sin()` to the\n",
"default namespace. Therefore, it can be invoked using just the name `sin()` to calculate $sin(0)$. \n",
"Next a new function named `sin()` is defined. This function definition assigns the name `sin` to the new function.\n",
"Therefore, the `sin()` function from the `math` library cannot be invoked any more. Instead the new function is called.\n",
"\n",
"If the `sin()` function from the `math` library is imported into an own namespace, this problems does not occur any more\n",
"as you can see below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math as m\n",
"\n",
"print(\"The value of sin(0) is:\", m.sin(0))\n",
"\n",
"\n",
"def sin(n):\n",
" return \"I don't know how to calculate the sine of \" + str(n)\n",
"\n",
"\n",
"print(\"The value of sin(0) is:\", m.sin(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## When to use which form of `import`?\n",
"There are good arguments for all of the above mentioned ways to import libraries or certain functions of libraries. So which of those is best? It has turned out, that specific ways to import certain libraries have been established. For example:\n",
"\n",
"```python\n",
" import requests\n",
" \n",
" import matplotlib.pyplot as plt\n",
" import numpy as np\n",
" \n",
" from bs4 import BeautifulSoup\n",
" from tkinter import *\n",
"```\n",
"\n",
"These forms of import are basically used in all tutorials of the respective library. We recommend to use the established way to import a library. This makes it more easy to work with these specific libraries."
]
}
],
"metadata": {
"interpreter": {
"hash": "9fd0e282e2343d8b38b390b803aabc7fcea80a18eee8e5bd23ce64f6435b30a1"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}