Scientific calculations often need named physical constants with their units and uncertainty, not copied numbers from an old notebook. scipy.constants provides named constants and a CODATA-backed lookup table so Python code can retrieve those values by key.
The constants database stores each entry as a key mapped to a value, SI-style unit string, and absolute uncertainty. The helper functions find(), value(), unit(), and precision() support lookups when the exact key is not already known or when code should read one field at a time.
Use the key names returned by find() or listed in physical_constants exactly, including capitalization and punctuation. Exact constants can report zero uncertainty and zero relative precision, while measured constants such as the Newtonian gravitational constant return nonzero uncertainty fields.
Steps to look up physical constants with SciPy:
- Find matching constant keys by substring.
$ python - <<'PY' from scipy import constants print(constants.find("gravitation")) PY ['Newtonian constant of gravitation', 'Newtonian constant of gravitation over h-bar c']find() returns matching key names. Use one returned string for later lookups instead of guessing spelling or capitalization.
- Read the value, unit, and relative precision for the selected key.
$ python - <<'PY' from scipy import constants key = "Newtonian constant of gravitation" print(constants.value(key)) print(constants.unit(key)) print(constants.precision(key)) PY 6.6743e-11 m^3 kg^-1 s^-2 2.2474266964325848e-05
precision() reports relative precision. A result of 0.0 means the installed SciPy constants table marks that constant as exact, although the returned Python float still has normal floating-point representation limits.
- Read the full tuple when code needs absolute uncertainty.
$ python - <<'PY' from scipy.constants import physical_constants value, unit, uncertainty = physical_constants["Newtonian constant of gravitation"] print(value) print(unit) print(uncertainty) PY 6.6743e-11 m^3 kg^-1 s^-2 1.5e-15
physical_constants maps each key to (value, unit, uncertainty). The third field is absolute uncertainty in the same unit as the value.
- Catch missing keys when the lookup name comes from external input.
$ python - <<'PY' from scipy import constants try: print(constants.value("not a constant")) except KeyError as exc: print(f"missing key: {exc}") PY missing key: 'not a constant'Unknown keys raise KeyError. Validate names with find() or catch the exception before using user-supplied constant names in longer calculations.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.