Skip to content

SciPy - Constants

The scipy.constants module in SciPy provides a comprehensive collection of physical and mathematical constants. These constants are invaluable in scientific computing and engineering applications, ensuring accuracy and consistency.

Constants from scipy.constants can be directly imported and used. SciPy aims to provide higher precision for these constants compared to standard libraries like math where applicable, though for fundamental constants like pi, they are often equivalent due to standardization.

Let’s compare the value of ‘pi’ from scipy.constants and Python’s built-in math module:

import scipy.constants
import math
# Accessing pi from scipy.constants
scipy_pi = scipy.constants.pi
# Accessing pi from math module
math_pi = math.pi
print(f"scipy.constants.pi: {scipy_pi:.16f}")
print(f"math.pi: {math_pi:.16f}")

The above program will typically produce the following output, showing identical precision for pi:

scipy.constants.pi: 3.1415926535897931
math.pi: 3.1415926535897931

SciPy offers a wide array of constants. Below are some common categories and examples.

These include fundamental mathematical numbers.

Constant NameDescription
piPi (π)
golden or golden_ratioThe golden ratio, (1 + sqrt(5))/2

A vast collection of precisely measured physical constants. Each physical constant in scipy.constants.physical_constants is a tuple: (value, unit, uncertainty).

Key in physical_constants (Examples)Description
'speed of light in vacuum'Speed of light (c)
'Planck constant'Planck constant (h)
'Newtonian constant of gravitation'Gravitational constant (G)
'elementary charge'Elementary charge (e)
'molar gas constant'Molar gas constant (R)
'Avogadro constant'Avogadro constant (N_A)
'Boltzmann constant'Boltzmann constant (k)
'electron mass'Electron mass (m_e)
'proton mass'Proton mass (m_p)
'neutron mass'Neutron mass (m_n)

Example of accessing a physical constant:

import scipy.constants
# Get the data for the speed of light
c_data = scipy.constants.physical_constants['speed of light in vacuum']
value, unit, uncertainty = c_data
print(f"Speed of light: {value} {unit}")
print(f"Uncertainty: {uncertainty}")

Output:

Speed of light: 299792458.0 m s^-1
Uncertainty: 0.0

scipy.constants also provides SI prefixes and conversion factors for common units.

Prefix/Unit NameValue (SI Factor or definition)
milli1e-3
micro1e-6
kilo1e3
gram1e-3 (mass in kg)
degreepi/180 (angle in radians)
minute60.0 (time in seconds)
hour3600.0 (time in seconds)
day24 * 3600.0 (time in seconds)
inch0.0254 (length in meters)
light_year9.4607304725808e+15 (length in meters)
atm or atmosphere101325.0 (pressure in Pascals)
eV or electron_voltValue of elementary charge (energy in Joules)
hp or horsepower745.6998715822701 (power in watts)

These unit values can be used for conversions. For example, to convert 1500 grams to kilograms:

import scipy.constants
mass_grams = 1500
mass_kg = mass_grams * scipy.constants.gram # gram is 0.001 kg
print(f"{mass_grams} grams is {mass_kg} kilograms")

Output:

1500 grams is 1.5 kilograms

With so many constants available, it can be hard to remember the exact key or name. The scipy.constants.find() function helps you search for constants based on keywords. It returns a list of keys from scipy.constants.physical_constants that match the search term.

Let’s find constants related to ‘alpha particle’:

import scipy.constants
# Find constants containing 'alpha particle'
keys_found = scipy.constants.find(key='alpha particle')
print("Found keys related to 'alpha particle':")
for k in keys_found:
print(f"- {k}")
# Example: Accessing one of the found constants
if 'alpha particle mass' in keys_found:
alpha_mass_val, alpha_mass_unit, alpha_mass_unc = scipy.constants.physical_constants['alpha particle mass']
print(f"\nAlpha particle mass: {alpha_mass_val} {alpha_mass_unit}, uncertainty: {alpha_mass_unc}")

The above program will generate output similar to this (exact list might vary slightly with SciPy versions):

Found keys related to 'alpha particle':
- alpha particle mass
- alpha particle mass energy equivalent
- alpha particle mass energy equivalent in MeV
- alpha particle mass in u
- electron to alpha particle mass ratio
Alpha particle mass: 6.6446573357e-27 kg, uncertainty: 1.2e-35

scipy.constants.find() is a powerful tool for exploring the available physical constants. For a full list, you can also iterate through scipy.constants.physical_constants.keys().

Further resources: Refer to the official SciPy documentation for a complete list and more details.