import math
type(math)module2025-03-31
Justin Post
Now that we know the basics of how python works and a little bit about how our programming environment functions, we can look at python ‘modules’. If you know R, these are smilar to R packages.
Note: These types of webpages are built from Jupyter notebooks (.ipynb files). You can access your own versions of them by clicking here. It is highly recommended that you go through and run the notebooks yourself, modifying and rerunning things where you’d like!
A collection of (related) definitions and statements that are grouped together in a single file (a .py file)
Some come standard, others must be installed (i.e. downloaded)
Modules are then imported into your session
import module_namefrom module_name import thing1 thing2These modules are already downloaded but not loaded in when starting python or a Jupyterlab notebook.
math
pi, e, etc.)exp(), sin(), sqrt(), etc.)random
statistics
scipy and pandas have a lot more)datetime
import module_namehelp() on the module but it is usually better to find the documentation on the web!import module_name:math module contains the sqrt() function--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-840f67a85afc> in <cell line: 1>() ----> 1 sqrt(9) NameError: name 'sqrt' is not defined
math module we could do a common statistical computation such as evaulating the Normal distribution PDF\[ f(1;\mu = 3, \sigma = 1) = \frac{1}{\sqrt{2\pi}}\exp^{-\frac{1}{2}(1 - 3)^2} \]
Modules can contain more than functions!
math module also defines variables like e and pi2.718281828459045
3.141592653589793
The area is 78.53981633974483random Modulerandom module gives functionality to do so (although we’ll use other modules when we do this later)0.5811521325045647
0.1947544955341367
0.9652511070611112#notice we can get the same 'random value if we set the seed back to the same starting point
random.seed(101)
print(random.random())0.5811521325045647range() function to return an iterator that is able to produce valuesrandom.sample() to obtain four values from that rangeasas when we use import0.5811521325045647
0.1947544955341367
0.9652511070611112We’ll see that most of the commonly used modules have common aliases. For instance, numpy as np, pandas as pd, and pyspark as ps.
from module import object1, object2 to do thispip is a package manager for python
Used through the command line usually
We’ll use it through a code cell with ! first
pip list
pip install module_name to install new modulespip install scipy
Colab has most everything we need for now but we’ll need to do some installing later in the course!
Modules are just collections of functions, objects, etc.
pip to install them (Colab has most that we need!)
import to bring them in
If you are on the course website, use the table of contents on the left or the arrows at the bottom of this page to navigate to the next learning material!
If you are on Google Colab, head back to our course website for our next lesson!