Chapter 4: SciPy Constants
What actually is scipy.constants? (plain English version)
It’s a module inside SciPy that gives you:
- A huge collection of well-known physical constants (speed of light, Planck’s constant, electron mass, etc.)
- Important mathematical constants (π, golden ratio)
- A bunch of conversion factors & units (eV to Joules, Celsius to Kelvin, atm to Pa, light-year in meters, etc.)
- All values are in SI units by default (except where it makes no sense, like angles in degrees)
- Values come from the official CODATA recommendations — currently (SciPy 1.17.x in 2026) based on CODATA 2022
Why is this helpful? Because instead of writing
|
0 1 2 3 4 5 6 7 8 |
c = 299792458 # m/s ← easy to mistype one digit h = 6.62607015e-34 # J s kB = 1.380649e-23 # J/K |
…you just do
|
0 1 2 3 4 5 6 7 8 9 10 |
from scipy import constants as const print(const.c) # 299792458.0 print(const.h) # 6.62607015e-34 print(const.k) # 1.380649e-23 |
→ No typos, no version confusion, everyone in your team/lab uses exactly the same value.
Two main ways to use it
Way 1: Direct attributes (fastest & cleanest for common constants)
Many frequently used constants are available as attributes (top-level variables):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import scipy.constants as const print(const.pi) # 3.141592653589793 print(const.c) # speed of light → 299792458.0 m/s print(const.G) # gravitational constant → 6.6743e-11 m³ kg⁻¹ s⁻² print(const.h) # Planck constant → 6.62607015e-34 J Hz⁻¹ print(const.hbar) # ħ = h/2π → 1.0545718176461565e-34 J s print(const.e) # elementary charge → 1.602176634e-19 C print(const.k) # Boltzmann constant → 1.380649e-23 J K⁻¹ print(const.N_A) # Avogadro constant → 6.02214076e+23 mol⁻¹ print(const.R) # gas constant → 8.31446261815324 J mol⁻¹ K⁻¹ print(const.sigma) # Stefan–Boltzmann constant → 5.6703744191844314e-08 W m⁻² K⁻⁴ print(const.alpha) # fine-structure constant → ≈ 0.0072973525643 (dimensionless) print(const.epsilon_0) # vacuum permittivity → 8.8541878188e-12 F m⁻¹ print(const.mu_0) # vacuum permeability → 1.25663706127e-06 N A⁻² print(const.m_e) # electron mass → 9.1093837139e-31 kg print(const.m_p) # proton mass → 1.67262192595e-27 kg |
Also handy short aliases: const.golden, const.degree, const.minute, const.hour, const.day, const.year, const.atm, const.bar, const.eV, const.calorie, etc.
Way 2: The full dictionary physical_constants (when you need uncertainty or search)
For maximum precision + traceability, use:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from scipy import constants as const info = const.physical_constants['speed of light in vacuum'] print(info) # → (299792458.0, 'm s^-1', 0.0) ← value, unit, absolute uncertainty value, unit, uncertainty = const.physical_constants['Boltzmann constant'] print(f"{value} {unit} (± {uncertainty})") # → 1.380649e-23 J K^-1 (± 0.0) |
Many exact constants (since 2019 SI redefinition) have uncertainty = 0.0.
Helper functions (very practical)
- find(substring) → search for constants by name
|
0 1 2 3 4 5 6 7 8 9 10 |
print(const.find('planck')) # → ['Planck constant', 'reduced Planck constant'] print(const.find('electron')) # → ['electron mass', 'electron volt', 'electron volt-joule relationship', ...] |
- value(key), unit(key), precision(key)
|
0 1 2 3 4 5 6 7 |
print(const.value('Rydberg constant')) # 10973731.568157... print(const.unit('Wien wavelength displacement law constant')) # 'm K' |
Real-life examples (the kind you actually write in research/lab)
Example 1: Blackbody peak wavelength (Wien’s law)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
from scipy import constants as const import numpy as np T = 5800 # Sun surface ≈ 5800 K lambda_max = const.Wien / T print(f"Peak wavelength: {lambda_max*1e9:.0f} nm") # → ~500 nm (greenish) |
Example 2: Thermal energy kT at room temperature
|
0 1 2 3 4 5 6 7 |
kT = const.k * 298.15 # room temp in K print(f"kT = {kT*1e3:.3f} meV") # → ≈ 25.7 meV (classic chemistry/physics number) |
Example 3: de Broglie wavelength of an electron accelerated by 100 V
|
0 1 2 3 4 5 6 7 8 9 |
E = 100 * const.e # kinetic energy in Joules p = np.sqrt(2 * const.m_e * E) lambda_db = const.h / p print(f"λ = {lambda_db*1e10:.2f} Å") # → ~1.23 Å |
Example 4: Convert temperature scales (built-in helper)
|
0 1 2 3 4 5 6 7 8 9 |
from scipy.constants import convert_temperature print(convert_temperature(100, 'C', 'K')) # 373.15 print(convert_temperature(32, 'F', 'C')) # 0.0 |
Example 5: Quick search when you forgot the name
|
0 1 2 3 4 5 6 7 8 9 10 |
print(const.find('avogadro')) # → ['Avogadro constant'] print(const.find('stefan')) # → ['Stefan-Boltzmann constant'] |
Quick summary table — most loved constants
| What you want | Code | Typical value (2022 CODATA) |
|---|---|---|
| Speed of light | const.c | 299792458 m/s (exact) |
| Planck constant | const.h | 6.62607015 × 10⁻³⁴ J Hz⁻¹ (exact) |
| Reduced Planck (ħ) | const.hbar | ≈ 1.0545718 × 10⁻³⁴ J s |
| Elementary charge | const.e | 1.602176634 × 10⁻¹⁹ C (exact) |
| Boltzmann constant | const.k / const.kB | 1.380649 × 10⁻²³ J/K (exact) |
| Avogadro number | const.N_A | 6.02214076 × 10²³ mol⁻¹ (exact) |
| Gas constant | const.R | 8.314462618 J mol⁻¹ K⁻¹ |
| Gravitational constant | const.G | 6.67430 × 10⁻¹¹ m³ kg⁻¹ s⁻² |
| Electron mass | const.m_e | 9.1093837 × 10⁻³¹ kg |
| Proton mass | const.m_p | 1.6726219 × 10⁻²⁷ kg |
| Fine-structure constant | const.alpha | ≈ 1/137.035999 |
| Stefan–Boltzmann constant | const.sigma | 5.670374419 × 10⁻⁸ W m⁻² K⁻⁴ |
Final teacher tips (2026 edition)
- Always import scipy.constants as const — short & conventional
- Prefer direct attributes (const.c) over dictionary for speed & readability
- Use physical_constants[…] only when you need uncertainty or traceability (publications)
- Values are frozen to CODATA 2022 in SciPy 1.14–1.17 → very unlikely to change until next CODATA (probably 2026+)
- If you see ConstantWarning → you’re using an old/outdated name — just update the key
Got a calculation in mind where you need one of these? Blackbody radiation? Bohr radius? Compton wavelength? Something astrophysical? Tell me and we’ll build a small realistic example together right now. 😊
