Let's assume that you need to calculate the square root of a number for one of your programs. There is no existing square root function in Python. You could write one, but there have been a lot of people before you in the exact same position. Guess what? One of them already created a function and stored it in a module!
What are modules in Python?
A module is like a code library: a file containing a set of functions, classes, and variables you want to include in your application.
For instance, if you're working on a geometry application, you may need some:
A square with its side's length as attribute.
A triangle with the length of its three sides as attributes.
A circle defined by its radius.
PI: constant, useful to calculate circle area, etc.
Phi: constant, represents the golden ratio.
Area with calculations dependent upon an object (square, triangle, etc.).
Angles which calculate angles of a triangle based on the length of its sides.
You could write/define all these in your notebook, or you could store them in a Python file, and then import them in your notebook, which is called a module!
import keyword to import the geometry module:
Now you can use your functions, classes, etc. in your notebook:
sq = geometry.square(4)tri = geometry.triangle(3, 6, 5)print(geometry.pi) # -> 3.14159265359geometry.area(sq) # -> 16
All of the functions, variables, classes, etc. included in the geometry module can be used with
moduleName.class(). If you don't want to write
geometry every time you want to use a geometry function, you have two options:
from geometry import * # -> we can use area() or access pi directly# OR :import geometry as geo # we can now use geo.are() or geo.pi
You can also import a specific function from a module and use it like any other Python native function (print, len, etc.):
from geometry import piprint(pi) # -> 3.14159265359
When one module isn't enough: the packages
A package is a collection of Python modules. While a module is a single Python file, a package is a directory containing an additional
__init__.py file. This distinguishes it from a directory that contains a number of scripts.
For instance, you could have stored your geometry in three different files:
One for the classes: classes.py
One for the variable: variables.py
One for the functions: function.py
In this case, you would have the following directory:
You have to use the
. operator to access a module after importing the whole package:
import geometry # import all the geometry packageprint(geometry.variables.pi) # -> 3.1415...sq = geometry.classes.square(4)geometry.function.area(sq) # -> 16
Or you can import a module from a package:
import geometry.variables as var # import only what is available in variables.pyprint(var.pi) # -> 3.1415...
NumPy: the first glimpse of scientific computing
Let's try this with
numpy, a well-known package containing a lot of scientific tools! To import the NumPy package, you could write
import numpy, but it's easier to write:
import numpy as np
Now that you've imported the NumPy module, what about the square root function? It's the
sqrt function of NumPy:
np.sqrt(16) # -> 4.0
But NumPy is providing a particular new object: the array. An array is similar to a list, or a mathematical matrix, and includes a lot of useful methods! Let's see an example of what is possible with arrays:
To go deeper with Python packages
These are just a few examples of what NumPy does! If you want to go deeper into packages, below are some other frequently used ones:
math: contains a lot of mathematical functions/variables. A lot of these are also included in NumPy.
seaborn: used for data visualization.
pandas: to import and process your data into Python.
sklearn: simple and efficient tools for data mining and data analysis.
scipy: used for scientific computing.
In this chapter, you learned the basics of modules that provide useful functions:
A module is a file consisting of Python code which can define functions, classes, and variables.
You can use any module in Python through the
To use a module's function, classes, etc., use the
A package is a collection of Python modules.
A Python array is a NumPy's object, similar to a list, but with far more available methods.