In scientific computing, constants such as the speed of light, Planck’s constant, Avogadro’s number, and many others play a vital role in calculations. The SciPy library provides a submodule called scipy.constants
that gives access to a vast list of these physical and mathematical constants.
In this article, we will explore:
-
✅ What
scipy.constants
is -
A list of commonly used constants
-
How to use constants in calculations
-
Unit conversion with SciPy
-
Tips and Common pitfalls
-
✅ Full code example
What is scipy.constants
?
The scipy.constants
module provides standard physical constants and units that are commonly used in scientific and engineering computations. These constants are stored with high precision and updated regularly to match international standards.
To use it, first install SciPy (if not already installed):
pip install scipy
Then import the constants module:
from scipy import constants
Commonly Used Constants
Here are some widely used constants available in scipy.constants
:
Constant Name | Description | Value |
---|---|---|
constants.pi |
Pi (π) | 3.1415926535... |
constants.c |
Speed of light (m/s) | 299792458 |
constants.h |
Planck constant (J·s) | 6.62607015e-34 |
constants.k |
Boltzmann constant (J/K) | 1.380649e-23 |
constants.G |
Gravitational constant | 6.67430e-11 |
constants.N_A |
Avogadro's number | 6.02214076e+23 |
constants.e |
Elementary charge (C) | 1.602176634e-19 |
constants.R |
Gas constant (J/mol·K) | 8.314462618 |
constants.g |
Standard gravity (m/s²) | 9.80665 |
To list all available constants, use:
import scipy.constants
print(dir(scipy.constants))
Using Constants in Calculations
You can directly use constants for any computation. Here’s a simple example:
Convert energy (in joules) to frequency using Planck’s relation:
from scipy.constants import h
energy = 3.2e-19 # in joules
frequency = energy / h
print("Frequency:", frequency, "Hz")
Unit Conversion
Besides constants, scipy.constants
also includes unit conversion factors. For example:
from scipy.constants import inch, mile, pound
# Convert 5 inches to meters
meters = 5 * inch
print("5 inches in meters:", meters)
# Convert 3 miles to meters
print("3 miles in meters:", 3 * mile)
# Convert 10 pounds to kilograms
print("10 pounds in kilograms:", 10 * pound)
Available unit conversion constants include:
-
inch
,foot
,yard
,mile
-
minute
,hour
,day
-
gram
,pound
,ton
-
calorie
,electron_volt
-
degree
,radian
Full Working Example
Here’s a complete example that uses SciPy constants in a basic physics calculation:
from scipy.constants import c, h, k, R, N_A
# 1. Calculate the energy of a photon with a wavelength of 500 nm
wavelength = 500e-9 # 500 nm in meters
photon_energy = h * c / wavelength
print("Photon Energy (500nm):", photon_energy, "J")
# 2. Convert 1 calorie to joules
from scipy.constants import calorie
print("1 calorie =", calorie, "J")
# 3. Convert 100°F to Kelvin
fahrenheit = 100
celsius = (fahrenheit - 32) * 5/9
kelvin = celsius + 273.15
print("100°F in Kelvin:", kelvin)
# 4. Calculate moles from particles using Avogadro's number
particles = 1.204e24
moles = particles / N_A
print("Moles:", moles)
Tips
-
✅ Use aliases: You can import only needed constants with aliasing for cleaner code.
from scipy.constants import c as speed_of_light
-
✅ Convert properly: Always convert units to SI when performing scientific computations.
-
✅ Check availability: If unsure of a constant name, use
dir(constants)
or check the official SciPy constants docs.
Common Pitfalls
Issue | Explanation |
---|---|
❌ Forgetting to convert units | Always check the input unit matches what SciPy expects (e.g., meters, not nm). |
❌ Mixing different units | Avoid mixing imperial and metric units in the same calculation without proper conversion. |
❌ Using outdated constants | SciPy regularly updates constants to align with NIST. Ensure you're using an updated version. |
❌ Using from scipy.constants import * |
This can pollute your namespace and lead to unexpected bugs. Prefer specific imports. |
Summary
The scipy.constants
module is a treasure trove for scientists, engineers, and researchers who rely on accurate, standardized constants and units for their calculations.
With just a few lines of code, you can:
-
Use physical and mathematical constants
-
Perform unit conversions
-
Make your code cleaner, more readable, and scientifically accurate
Next Steps
-
Learn more about
scipy.optimize
for solving equations -
Explore
scipy.integrate
for numerical integration -
Combine with
matplotlib
for data visualization