June 14, 2014

Python – The Standard Library

Python comes with a large number of builtin functions. However these alone only provides a limited functionality of python. The true power of python comes from the python’s standard library.


You can find all my latest posts on medium.

Python comes included with an extensive library of modules. modules extends python’s features, in the same way as installing an app on smart phone, will extend what the smart phone can do. However unlike smart phone which requires downloading followed by installing and activating, you don’t need to download python modules since they are already in the standard library which comes with python as standard. The only thing you need to do is activate the modules you want to using in your current python session, or inside your python script.

You use the “import” keyword to activate/access a module, like this:

import {module_name}

You can find a list of all modules in the Python Standard Library on the official website.

For example, if you want to do square roots of a number, then you need to use the “math” module:

=>=>=> import math
=>=>=> math.sqrt(81)
=>=>=> math.sqrt(81) * math.sqrt(25)

If you are familiar with oop languages, e.g. c#, then you can think of “math” as a class, and “sqrt” is a static-method of the class. Reminder, static-methods are method that you use without having to instantiate an object of that class. Hence the “.” notation is the standar oop

You can view help info for a module using the help() funtion:

=>=>=> help(math)
Help on built-in module math:


This module is always available. It provides access to the
mathematical functions defined by the C standard.


Return the arc cosine (measured in radians) of x.


Return the hyperbolic arc cosine (measured in radians) of x.


Return the arc sine (measured in radians) of x.


Return the hyperbolic arc sine (measured in radians) of x.


Return the arc tangent (measured in radians) of x.

atan2(y, x)

Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered.


If you want to view help for a specific method, rather than the whole module, then you can simply do:
=>=>=> help(math.sqrt)
Help on built-in function sqrt in module math:


Return the square root of x.


If you want, you can import method directly into the current workspace, which would allow you to use the method without needing to use it’s fully drilled down name:

=>=>=> sqrt(81) # this will output an error message.
Traceback (most recent call last):
File "<stdin=>", line 1, in <module=>
NameError: name ‘sqrt’ is not defined
=>=>=> from math import sqrt
=>=>=> sqrt(81)

However this approach can be risky in case you already have a sqrt function in the current namespace, in which case there could be a conflict. If there is a namespace clash but you still want to impart a method into the namespace directly, then you can give the method you want to name an alternative name (a bit like an alias) during the import process, e.g. :

=>=>=> from math import sqrt as SquareRoot
=>=>=> SquareRoot(81)

Note: everything in python is case sensitive.