Scientific computing with Python

by Conor Lawless email: conor.lawless@ncl.ac.uk

Python libraries

Before we get onto the Structure section, which is quite long and describes fundamental programming concepts and how they apply in Python specifically, I'll briefly introduce the idea of extending functionality in Python using libraries (also called packages). Importing libraries or packages allows you to tweak Python to better suit the particular problem you're currently trying to solve.

In the previous example, you might have noticed a line which began with "import". One of the best things about Python is that many people have developed packages for it which add to its capabilities, typically by providing you with access to functions (which we will learn about in the Structure section). Many packages are already included with the default Python installation (e.g. math, random, os, sys). Try importing math and typing "math.". You will get a prompt listing possible function names (e.g. cos, sin). To learn more, you could search online for the Python math package documentation, or type help(math) at the prompt in IDLE to find out more about the math package. This help structure works for any package. Try executing:

import math
math.cos(math.pi)
To avoid typing "math" all the time, we could do the following (this is called importing a library into your namespace) which loads all the functionality in the math package as if it were a core part of Python:
from math import *
cos(pi)

You should also try this out:
import antigravity

Additional, external Python packages can be found online and added to your Python installation, greatly extending Python's functionality. For example, try downloading and installing these three useful packages. Unfortunately, only numPy is currently installed on computers in the Dene cluster.

Note that if you are running 64-bit Python on Windows, it is often better to download your Python packages from this useful site, maintained by Christoph Gohlke.


OverviewInstallationFirst ScriptExecutionLibrariesStructureOther Resources


Last updated: April 2016