SciPy Constants
Scientific code is full of numbers that never change: the speed of light, Avogadro's number, the number of seconds in a day. Typing them by hand invites typos. The scipy.constants module stores hundreds of them with full precision, in SI units, plus conversion factors for common non-SI units.
from scipy import constants
print("pi :", constants.pi)
print("golden ratio :", constants.golden)
print("speed of light:", constants.c, "m/s")
print("Planck :", constants.h, "J s")
print("gravity (g) :", constants.g, "m/s^2")pi : 3.141592653589793
golden ratio : 1.618033988749895
speed of light: 299792458.0 m/s
Planck : 6.62607015e-34 J s
gravity (g) : 9.80665 m/s^2
Unit conversion factors
Many constants are really conversion factors: each value tells you how many SI base units one of that unit equals. For example constants.inch is 0.0254, because one inch is 0.0254 metres. Multiplying by the factor converts to SI; dividing converts from SI.
from scipy import constants
# Length: everything is expressed in metres
print("1 inch =", constants.inch, "m")
print("1 mile =", round(constants.mile, 3), "m")
print("1 light year =", constants.light_year, "m")
# Convert a 10 km run to miles
print("10 km =", round(10_000 / constants.mile, 3), "miles")1 inch = 0.0254 m
1 mile = 1609.344 m
1 light year = 9460730472580800.0 m
10 km = 6.214 miles
The same pattern works for every quantity. Here are the most useful groups:
| Group | Base SI unit | Examples |
|---|---|---|
| SI prefixes | (pure number) | kilo, mega, giga, milli, micro, nano |
| Binary prefixes | (pure number) | kibi (1024), mebi, gibi |
| Mass | kilogram | gram, pound, ounce, metric_ton, atomic_mass |
| Time | second | minute, hour, day, week, year |
| Length | metre | inch, foot, mile, nautical_mile, angstrom, au |
| Pressure | pascal | atm, bar, psi, mmHg |
| Area / volume | m² / m³ | hectare, acre, liter, gallon |
| Speed | m/s | kmh, mph, knot, mach |
| Energy | joule | eV, calorie, erg, kilowatt_hour (via kilo * hour) |
| Power / force | watt / newton | horsepower, pound_force, kgf |
| Angle | radian | degree, arcminute, arcsecond |
from scipy import constants
print("1 hour =", constants.hour, "s")
print("1 year =", constants.year, "s")
print("1 pound =", round(constants.pound, 6), "kg")
print("1 atm =", constants.atm, "Pa")
print("1 degree =", round(constants.degree, 6), "rad")
print("100 km/h =", round(100 * constants.kmh, 2), "m/s")
print("1 GiB =", constants.gibi, "bytes")1 hour = 3600.0 s
1 year = 31536000.0 s
1 pound = 0.453592 kg
1 atm = 101325.0 Pa
1 degree = 0.017453 rad
100 km/h = 27.78 m/s
1 GiB = 1073741824 bytes
Temperature conversion
Temperatures can't be converted with a single factor, because the scales have different zero points. SciPy provides convert_temperature(), which works on single values or whole arrays:
import numpy as np
from scipy.constants import convert_temperature
celsius = np.array([-40, 0, 37, 100])
print("Fahrenheit:", convert_temperature(celsius, "Celsius", "Fahrenheit"))
print("Kelvin :", convert_temperature(celsius, "C", "K"))
print("Body temp :", round(convert_temperature(98.6, "F", "C"), 2), "C")Fahrenheit: [-40. 32. 98.6 212. ]
Kelvin : [233.15 273.15 310.15 373.15]
Body temp : 37.0 C
The physical constants database
Besides the named attributes, SciPy includes the full CODATA table of physical constants. Use physical_constants[name] to get a (value, unit, uncertainty) tuple, or the shortcuts value(), unit() and precision(). find() searches the names:
from scipy import constants
name = "electron mass"
value, unit, uncertainty = constants.physical_constants[name]
print(name, "=", value, unit)
print("uncertainty:", uncertainty)
print("unit only :", constants.unit("Boltzmann constant"))
matches = constants.find("Avogadro")
print("search for 'Avogadro':", matches)
print("N_A =", constants.value(matches[0]))electron mass = 9.1093837015e-31 kg
uncertainty: 2.8e-40
unit only : J K^-1
search for 'Avogadro': ['Avogadro constant']
N_A = 6.02214076e+23
Tip: Use
dir(constants)to list every attribute, orconstants.find("proton")to discover names in the CODATA table. The exact CODATA values may change slightly between SciPy releases when new measurements are published.
A worked example
How long does light from the Sun take to reach Earth? The average Sun–Earth distance is one astronomical unit (constants.au):
from scipy import constants
seconds = constants.au / constants.c
minutes, rest = divmod(seconds, constants.minute)
print(f"Sunlight travels for {seconds:.1f} s")
print(f"That is {int(minutes)} min {rest:.0f} s")Sunlight travels for 499.0 s
That is 8 min 19 s
Test yourself
What does constants.inch contain?
- 1
- 2.54
- 0.0254 (one inch in metres)
- 39.37
How do you convert 300 kelvin to Celsius?
- 300 * constants.kelvin
- constants.convert_temperature(300, 'K', 'C')
- constants.celsius(300)
- 300 - constants.zero_Celsius * 2
What does constants.physical_constants['electron mass'] return?
- Just the value
- A (value, unit, uncertainty) tuple
- A string
- A dictionary of all constants