If you’re working with physics equations or scientific simulations in Python, you don’t need to manually define constants like the speed of light or Avogadro’s number. The scipy.constants module gives you immediate access to hundreds of predefined physical and mathematical constants—all with correct units and precision. You’ll also find unit conversion factors, allowing you to convert between metric, imperial, and other systems cleanly in code.
SciPy Beginner’s Learning Path
Why Use scipy.constants?
- Avoid hardcoding physical constants.
- Get correct values with units (e.g., ‘c’ for speed of light in m/s).
- Built-in unit conversions (e.g., Celsius to Kelvin, joules to calories).
- Enables readable, reliable scientific code.
How to Acess SciPy Constants?
Start by importing the module:
from scipy import constants
Now you can directly use constants like:
energy = 2 * constants.c ** 2 # E = 2 * c²
print("Energy:", energy, "m²/s²")
Some of the most used constants:
constants.c # speed of light in vacuum [m/s]
constants.h # Planck’s constant [J⋅s]
constants.k # Boltzmann constant [J/K]
constants.G # gravitational constant [m³/kg/s²]
constants.R # gas constant [J/mol/K]
constants.N_A # Avogadro’s number [1/mol]
constants.e # elementary charge [C]
Handy SciPy Submodules for Categories
You can also access groups of constants:
from scipy.constants import physical_constants
This gives you a dictionary. Each entry includes the value, unit, and uncertainty:
value, unit, uncertainty = physical_constants['Planck constant']
print(f"Planck constant: {value} {unit} ± {uncertainty}")
Other useful dictionaries:
- codata – constants from Committee on Data
- precision – floating point precision values
Converting Units
The scipy.constants module supports many unit conversions via named variables:
# Convert 10 calories to joules
cal_to_joule = 10 * constants.calorie
print("10 cal =", cal_to_joule, "J")
# Convert 1 inch to meters
print("1 inch =", constants.inch, "meters")
Also available:
- constants.hour (seconds per hour)
- constants.atm (atmospheric pressure in Pa)
- constants.degree (radian equivalent)
Using SciPy Constants to Compute Photon Energy
Use Planck’s relation: E = hν
frequency = 5e14 # in Hz (visible light)
energy = constants.h * frequency
print("Photon energy:", energy, "J")
To convert to electron volts:
eV = energy / constants.e
print("Photon energy:", eV, "eV")
Don’t Recalculate What SciPy Already Has
Instead of defining your own π, just use:
print(constants.pi)
Instead of 9.8 for gravity (which is approximate), use:
print(constants.g) # Standard gravity in m/s²
This helps ensure that your code is scientifically consistent and precise.
Summary
Using scipy.constants isn’t just convenient—it’s essential for scientific integrity. It removes guesswork, ensures unit consistency, and makes your code easier to read and verify. Anytime you’re working with a known constant or unit conversion, check if SciPy already has it.
For more examples, continue with scipy.constants.find(), which lets you search constants by keyword.