2.2 Python Modules#

Previously, you have learned how to:

  1. initialize variables in Python;

  2. perform simple actions with them (eg.: adding numbers together, displaying variables content, etc);

  3. work with functions (have your own code in a function to reuse it many times or use a function, which was written by another person).

However, the scope of the last Notebook was limited by your Python knowledge and available functions in standard/vanilla Python (so-called ’built-in’ Python functions).

There are not many Python built-in functions that can be useful for you, such as functions for math, plotting, signal processing, etc. Luckily, there are countless modules/packages written by other people.

Python built-in modules#

By installing any version of Python, you also automatically install its built-in modules.

One may wonder — why do they provide some functions within built-in modules, but not directly as a built-in function, such as the abs() or print()?

The answers may vary, but, generally, it is to keep your code clean; compact; and, working. It keeps your code clean and compact as you only load functions that you need. It keeps your code working as it allows you to define your own functions with (almost) any kind of name, and use them easily, without worrying that you might break something if there is a function with the same name that does something completely different.

math#

The math module is one of the most popular modules since it contains all implementations of basic math functions (\(\sin\), \(\cos\), \(\log_{10}\) (as log10), factorial, etc — the full list can be found here).

In order to access it, you just have to import it into your code with an import statement.

# importing all contents of the module math
import math

print(math) # showing that math is actually a built-in Python module

You can now use its functions like this:

print(f'Square root of 16 is equal to {math.sqrt(16)}')

You can also use the constants defined within the module, such as math.pi:

print(f'π is equal to {math.pi}')

print(f"and Euler's number e is {math.e}")

math.pi#

As you can see, both constants and functions of a module are accessed by using: the module’s name (in this case math) and a . followed by the name of the constant/function (in this case pi).

We are able to do this since we have loaded all contents of the module by using the import keyword. If we try to use these functions somehow differently — we will get an error:

print('Square root of 16 is equal to')
print(sqrt(16))

You could, however, directly specify the functionality of the module you want to access. Then, the above cell would work.

This is done by typing: from module_name import necessary_functionality, as shown below:

from math import sqrt

print(f'Square root of 16 is equal to {sqrt(16)}.')
from math import pi

print(f'π is equal to {pi}.')

Listing all functions of a module#

Sometimes, when you use a module for the first time, you may have no clue about the functions inside of it. In order to unveil all the potential a module has to offer, you can either access the documentation on the corresponding web resource or you can use some Python code, as shown below:

import math

# listing all contents of a module
print('contents of math:', dir(math))

# trying to learn something about, let's say, the hypot thingy
# note that \n here is used to add a new line
print('\n\nmath.hypot is a', math.hypot) 
# you can also use ? or ?? to read the documentation about it in Python
math??

Python third-party modules#

Besides built-in modules, there are also modules developed by other people and companies, which can be also used in your code. These modules are not installed by default in Python, they are usually installed by using the ’pip’ or ’conda’ package managers. Once they have been installed, they are accessed like any other Python module.

However, for the purpose of this course, you will not need to use these since Anaconda has all the needed modules already installed.

Additional study material