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-11Unit 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.72Searching 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
Exercise: SciPy Constants
What does the scipy.constants module provide?