Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Modules

So far we have been using Python's inbuilt functions like print() and len(). Python also comes with lots of other software called modules. There are modules for many biological applications such as bioinformatics, molecular signalling, machine learning, plotting graphs, statistics and maths, to name but a few.

To be able to use a module we have to import it. For example, to import the math module, which contains standard maths functions like sine, log, etc., we place the following code at the start of our program:

import math

Now we can use all of the maths functions contained in this module.

Run the following code to calculate the square root of a number.

import math math.sqrt(25)
5.0

To get a list of all the mathematical functions type help(math). There's also a lot of information available online. Most likely you will never need most of these, but it's worth knowing they are there in case you do need one of them.

The math module also contains the value of π\pi so that you don't have to remember it.

# The value of pi math.pi
3.141592653589793

Remember the code you wrote to calculate a mean and median of a list of numbers? There is a Python module called statistics that can do that for you.

A full list of functions in the statistics module is here.

import statistics diameters = [56, 65, 45, 65, 57, 59, 57, 60, 58, 60, 54] print(statistics.mean(diameters)) print(statistics.median(diameters))
57.81818181818182 58

We will be making a lot of use of modules in Exploratory Data Analysis next week.

Exercise Notebook