Skip to main content

12.2) NumPy Arrays


Lists (introduced earlier on in this course) behave a bit like fortran or IDL arrays, but because they can hold any type of value (numbers, strings, other lists), they are not practical for scientific computing. Operations on lists or nested lists are slow and cumbersome.

To solve this problem, Python has a package called NumPy which provides a new object type: NumPy Array. The arrays can be multi-dimensional (e.g. in space and/or time) and you can do operations with them. The NumPy array is like a list, except that all elements are the same type (e.g. int, float or str).

To use the NumPy package, an alias (i.e. shortcut) tends to be used. For example:

>>> import numpy as np

In the rest of this section, if you see np preceding a function, assume you have to import the NumPy package as ‘np’.

As for the rest of the course, try out all the examples interactively in IDLE (don’t forget to import NumPy as np!).