Astropy

Official documentation: http://docs.astropy.org/en/stable/

Units and Quantities

Quantities (astropy.units.quantity) are the combination of a value (integer, float, ...) and a unit (astropy.units).

In [1]:
from astropy import units as u

Attach units to scalar values

In [2]:
1.85 * u.meter
Out[2]:
$1.85 \; \mathrm{m}$
In [3]:
500. * u.gram
Out[3]:
$500 \; \mathrm{g}$
In [4]:
400. * u.hertz
Out[4]:
$400 \; \mathrm{Hz}$
In [5]:
q = 2000. * u.meter
type(q)
Out[5]:
astropy.units.quantity.Quantity

Attach units to lists

In [6]:
[1., 2., 3.] * u.meter
Out[6]:
$[1,~2,~3] \; \mathrm{m}$

Attach units to Numpy array

In [7]:
import numpy as np
np.array([1., 2., 3.]) * u.meter
Out[7]:
$[1,~2,~3] \; \mathrm{m}$