{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Math Module\n", "\n", "In addition to the functions built-in to the base\n", "portion of python, there are also a standard set\n", "of \"modules\" included with python. A module\n", "is a collection of functions and variables (and\n", "sometimes more advanced items we haven't yet talked\n", "about). These modules allow access to a much larger\n", "set of functions that are commonly necessary.\n", "\n", "One frequently used module is the `math` module.\n", "The math module contains many familiar mathematical\n", "functions and constants. The [documentation for\n", "the contents of the math module](https://docs.python.org/3/library/math.html)\n", "contain a information on all of the function and constants.\n", "\n", "## Importing\n", "\n", "To access the functions in the math module, we first\n", "must let python know we want to use it by *importing*\n", "it. As we expand our python knowledge, we'll end up\n", "needing more modules. Convention says to put\n", "all of the `import` statements at the top of your program\n", "or notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Math Functions\n", "We'll highlight a few of them commonly used math functions\n", "here:\n", "\n", "* `floor(x)` - return the largest integer less than or equal to x\n", "* `ceil(x)` - return the smallest integer greater than or equal to x\n", "* `factorial(x)` - return $x!$\n", "* `exp(x)` - return $e^x$\n", "* `log(x)` - returns $\\ln(x)$\n", "* `log10(x)` - returns $\\log_{10}(x)$\n", "* `log2(x)` - returns $\\log_2(x)$\n", "* `cos(x)` - returns cosine of x in radians\n", "* `sin(x)` - returns sine of x in radians\n", "* `tan(x)` - returns tangent of x in radians\n", "\n", "Since these functions belong to the math module, they\n", "live in what's called the math *namespace*. This means\n", "that in order to access them, we need to use `math.function()`\n", "where `function()` is replaced by one of the functions.\n", "Although this is more to type, it is done to prevent\n", "multiple functions with the same name from clashing\n", "with one another. This way you always know exactly\n", "which function you are calling." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 2.89\n", "y = 4\n", "print(math.factorial(y))\n", "print(math.floor(x))\n", "print(math.ceil(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Beyond these, there's a good chance that if you want \n", "a standard mathematical operation it's available in\n", "the math module. In addition, there is a similar module,\n", "`cmath` that implements many of the same mathematical\n", "functions for complex numbers.\n", "\n", "## Math Constants\n", "In addition to the functions discussed above (and the\n", "many other functions contained in the math module),\n", "it also contains a few constants. The most commonly used\n", "ones are:\n", "\n", "* `pi` - mathematical constant $3.14159\\ldots$\n", "* `e` - mathematical constant $e = 2.7182\\ldots$\n", "\n", "Like with the functions, these constants also live in\n", "the `math` namespace, so we access them by preceeding them\n", "with `math.`. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(math.pi)\n", "\n", "r = 2\n", "area = math.pi*r**2\n", "print(area)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }