Extensible Periodic Table of Elements
The `periodictable` library for Python (current version 2.1.0) provides an extensible periodic table of elements, pre-populated with data crucial for neutron and X-ray scattering experiments. It offers access to element properties such as mass, density, and scattering information. The library is actively maintained with regular updates to data sources and improvements. Its release cadence is driven by updates to fundamental physical constants and scattering data.
Common errors
-
ModuleNotFoundError: No module named 'periodictable'
cause The `periodictable` package was not installed correctly or is not accessible in the current Python environment. This can happen if `python setup.py install` was run directly instead of using pip, or if multiple Python environments are present.fixEnsure the package is installed using `pip install periodictable`. If using virtual environments, activate the correct environment before installation and running your script. -
AttributeError: 'module' object has no attribute 'plancks_constant' (or similar for other constants)
cause Attempting to access an old constant name or a constant that has been renamed or removed.fixRefer to the `periodictable.constants` module documentation. For Planck's constant, use `periodictable.constants.planck_constant`. -
IndexError: list index out of range (when accessing element by number)
cause This error can occur if you attempt to access an element using `periodictable.elements[0]` for the bare neutron after version 2.0.0, or an invalid atomic number.fixAvoid using atomic number 0 for the bare neutron after version 2.0.0. Ensure the atomic number used is valid and corresponds to an existing element in the periodic table (1-118).
Warnings
- breaking The constant `constants.plancks_constant` (eV s) has been renamed to `constants.planck_constant` (J/Hz) and its units changed to align with standard physical constants.
- breaking The bare neutron (Z=0) has been removed from the `PeriodicTable` iterator and is no longer directly accessible as `periodictable.elements[0]` or when iterating `periodictable.elements`.
- breaking The `neutron_sld` function (for neutron scattering length density) now returns real and imaginary coherent scattering lengths, and incoherent scattering length, instead of coherent, absorption, and incoherent. This changes the order and type of returned values.
- breaking The `periodictable.formula()` constructor's `value` keyword argument for compounds has been renamed to `compound`.
- gotcha Tabulated values for element properties (e.g., density, scattering factors) may differ from other sources (e.g., International Tables for Crystallography, Volume C) due to the use of specific data compilations (NIST, IAEA, IUPAC, LBL Center for X-ray Optics).
Install
-
pip install periodictable
Imports
- periodictable
import periodictable
- Element (e.g., H, C, Fe)
from periodictable import elements.H
from periodictable import H, C, Fe
- elements
from periodictable import elements
Quickstart
import periodictable
# Access an element by its symbol
hydrogen = periodictable.H
print(f"Element: {hydrogen.name}, Symbol: {hydrogen.symbol}, Atomic Number: {hydrogen.number}, Mass: {hydrogen.mass:.3f} {hydrogen.mass_units}")
# Access an element by its full name
iron = periodictable.Fe
print(f"Element: {iron.name}, Density: {iron.density:.2f} {iron.density_units}")
# Iterate through all elements
from periodictable import elements
print("\nFirst 5 elements:")
for i, el in enumerate(elements):
if i >= 5: break
print(f" {el.symbol}: {el.name}")
# Calculate properties for a chemical formula
water = periodictable.formula('H2O')
print(f"\nFormula: {water}, Molar Mass: {water.mass:.3f}")
# Access an isotope
nickel_58 = periodictable.Ni[58]
print(f"\nIsotope: {nickel_58.name}-{nickel_58.isotope}, Neutron Coherent Scattering Length: {nickel_58.neutron.coherent:.3f}")