Beyond physical constants like the speed of light, scipy.constants also bundles a large set of plain unit-conversion multipliers — SI prefixes, kilo equals 1e3, mega equals 1e6, milli equals 1e-3, and so on, and named unit conversions, metric_ton equals 1000, meaning 1 metric ton equals 1000 kilograms, along with mile, inch, pound, gallon, and many others — all expressed as the multiplication factor needed to convert that unit into the corresponding SI base unit. This makes unit conversions in code both self-documenting and less error-prone than hardcoding a conversion factor you have to look up and verify yourself each time.
1Understanding constants.metric
Beyond physical constants like the speed of light, scipy.constants also bundles a large set of plain unit-conversion multipliers — SI prefixes, kilo equals 1e3, mega equals 1e6, milli equals 1e-3, and so on, and named unit conversions, metric_ton equals 1000, meaning 1 metric ton equals 1000 kilograms, along with mile, inch, pound, gallon, and many others — all expressed as the multiplication factor needed to convert that unit into the corresponding SI base unit. This makes unit conversions in code both self-documenting and less error-prone than hardcoding a conversion factor you have to look up and verify yourself each time.
Use scipy.constants.find('ton'), or similar searches, to discover the exact name of a specific unit conversion constant you need, rather than guessing at its name or hardcoding the conversion factor yourself.
from scipy import constants
weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)2Practical Example
Here is a real-world application of constants.metric showing how it is used in production SciPy code.
from scipy import constants
distance_km = 5 * constants.kilo
print(distance_km)3Best Practices
Follow these guidelines when working with constants.metric:
1. Use scipy.constants' named unit conversions instead of hardcoding conversion factors, for both readability and to avoid manual arithmetic mistakes
2. Use constants.find(substring) to search for a specific constant's exact name when you're not sure how it's spelled or named
3. Multiply by the SI-prefix constants, kilo, mega, milli, etc., instead of writing out the equivalent power-of-ten literal directly, for clarity about what the number represents
Tip: Use scipy.constants.find('ton'), or similar searches, to discover the exact name of a specific unit conversion constant you need, rather than guessing at its name or hardcoding the conversion factor yourself.
from scipy import constants
weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)