SciPy Constants

scipy.constants is a subpackage that provides a large collection of physical and mathematical constants together with unit-conversion factors.

Mathematical and Physical Constants

scipy.constants bundles a large set of mathematical and physical constants as plain Python floats, based on the CODATA recommended values used across science and engineering. Rather than typing 3.14159... or looking up the speed of light, you import the constant by name.

A Few Built-in Constants

from scipy import constants

print(constants.pi)                      # 3.141592653589793
print(constants.speed_of_light)          # 299792458.0 (meters/second)
print(constants.Avogadro)                # 6.02214076e+23
print(constants.gravitational_constant)  # 6.6743e-11
ConstantAttributeApproximate Value
Piconstants.pi3.14159265...
Speed of lightconstants.speed_of_light299792458 m/s
Planck constantconstants.h6.62607015e-34 J*s
Boltzmann constantconstants.k1.380649e-23 J/K
Avogadro numberconstants.Avogadro6.02214076e+23 mol^-1

Unit Conversion Constants

Beyond fixed values like pi, scipy.constants also stores unit-conversion factors. Each one tells you how many SI base units make up one unit of something else - for example, constants.mile is the number of meters in a mile, a plain float you multiply by rather than a formula.

Converting Between Units

from scipy import constants

print(constants.mile)    # 1609.344 (meters in one mile)
print(constants.inch)    # 0.0254 (meters in one inch)
print(constants.minute)  # 60.0 (seconds in one minute)

# Convert 5 miles to meters
distance_in_meters = 5 * constants.mile
print(distance_in_meters)  # 8046.72
Note: These conversion constants always express how many SI base units (meters, kilograms, seconds) equal one unit of the given quantity - so you multiply to convert into SI, and divide to convert out of SI.

Searching for a Constant

from scipy import constants

# Search physical_constants for anything mentioning 'electron mass'
matches = constants.find('electron mass')
print(matches[0])  # 'electron mass'
  • Length and distance - constants.mile, constants.inch, constants.foot
  • Mass - constants.gram, constants.pound, constants.ounce
  • Time - constants.minute, constants.hour, constants.day
  • Temperature - constants.zero_Celsius, constants.degree
Note: When in doubt about an exact value or its category, scipy.constants.physical_constants is a dictionary keyed by full constant names that also stores the unit and measurement uncertainty.

Exercise: SciPy Constants

What does the scipy.constants module provide?