Skip to main content

chemhelper

PyPI CI Docs License: MIT

General chemistry answers in one line of Python, and the worked solution in one more.

chemhelper solves the problems that fill a first-year chemistry course: molar mass, percent composition, converting between grams, moles and molarity, and the ideal gas law. Every solver has a twin ending in _steps that prints the solution the way you would write it on a whiteboard.

It was written by a chemistry tutor, for tutoring: check a student's answer in a second, show where it came from, and spend the session on understanding.

Status: actively developed. Version 1.0 is a full overhaul of the original 2017 package, and more is planned. See the roadmap.

📖 Full documentation: https://drosenman.github.io/chemhelper/


Contents

Install

pip install chemhelper

Version 1.0 requires Python 3.10 or newer and installs pandas with it. Until 1.0 is on PyPI, install it from GitHub:

pip install git+https://github.com/drosenman/chemhelper.git

A two-minute tour

>>> from chemhelper import elements, compounds, conversions, ideal_gas

Get an answer

>>> compounds.mass('Ca(OH)2')
74.1
>>> conversions.grams_to_moles(90.1, 'H2O')
5.0
>>> ideal_gas.ideal_gas('P', V=5, n=4, T=300)
19.7

Show the work

Add _steps to any solver's name.

>>> compounds.mass_steps('Al2(SO4)3')
Al2(SO4)3
Al: 2 x 26.98 = 53.96
S: 3 x 32.07 = 96.21
O: 12 x 16.0 = 192.0

Mass Al2(SO4)3 = 53.96 + 96.21 + 192.0 = 342.17

Look up an element

>>> print(elements.Fe)
Element: Iron, Fe
Atomic Number: 26
Mass: 55.85
>>> elements.element_table.head()
Atomic Number Symbol       Name   Mass
1                  H   Hydrogen   1.01
2                 He     Helium   4.00
3                 Li    Lithium   6.94
4                 Be  Beryllium   9.01
5                  B      Boron  10.81

Percent composition

>>> compounds.percent_composition('C6H12O6')
C          39.999
H           6.714
O          53.287
Total:    100.000
Name: C6H12O6, dtype: float64
>>> compounds.percent_composition_steps('H2O')
H2O
H: 2 x 1.008 = 2.016
O: 16.0

Mass H2O = 2.016 + 16.0 = 18.02

H          2.016
O         16.000
Total:    18.020
Name: H2O, dtype: float64
H: 100 x 2.016g H/18.02g H2O = 11.19%
O: 100 x 16.0g O/18.02g H2O = 88.81%

Solutions

How many grams of NaCl are needed for 2 L of a 0.5 M solution?

>>> conversions.molarity_to_grams(0.5, 2, 'NaCl')
58.44
>>> conversions.molarity_to_grams_steps(0.5, 2, 'NaCl')
0.5 moles/L x 2 L = 1 moles
NaCl
Na: 22.99
Cl: 35.45

Mass NaCl = 22.99 + 35.45 = 58.44
1 moles x 58.44 grams/mole = 58.44 grams

What is the molarity of 10 g of NaOH in 0.25 L of solution?

>>> conversions.molarity_steps(0.25, mass=10, chemical='NaOH')
10g NaOH x 1 mole NaOH/40.0g NaOH = 0.25 moles NaOH
0.25 moles/0.25 L = 1 M

The ideal gas law

Solve for any variable. Give a mass in place of moles, and use the units the problem gives you.

>>> ideal_gas.ideal_gas_steps('P', substance='O2', m=32, V=500, Vunits='mL', T=25, Tunits='C')
n = 32g x 1 mole O2/32.0g O2 = 1 moles O2
V = 500 mL x 1 L/1000 mL = 0.5 L
T = (25 + 273.15) K = 298.15 K
PV = nRT
P = nRT/V
P = 1 moles x 0.0821 L*atm/(mol*K) x 298.15 K / 0.5 L = 48.96 atm

Helpful errors

>>> compounds.mass('NACL')
Traceback (most recent call last):
  ...
ValueError: Unknown element 'A' in formula 'NACL'
>>> ideal_gas.ideal_gas('P', V=5, T=300)
Traceback (most recent call last):
  ...
ValueError: Missing value(s) for: n

Everything it can do

Every function below, except parse_formula, has a _steps twin that takes the same arguments and prints the worked solution.

chemhelper.elements

Name Description
Element Class holding symbol, name, atomic_number and mass
H, He, … U One Element per element, named by symbol
elements pandas Series of Element objects, indexed by symbol
element_index Symbols in order of atomic number
element_objects Element objects in order of atomic number
element_table pandas DataFrame of symbol, name and mass

chemhelper.compounds

Function Returns
parse_formula(chemical) Atom counts for each element
mass(chemical) Molar mass in g/mol
composition(chemical) Grams of each element per mole of compound
percent_composition(chemical) Percent by mass of each element

chemhelper.conversions

Function Returns
grams_to_moles(mass, chemical) Moles
moles_to_grams(moles, chemical) Grams
molarity(volume, mass=None, chemical=None, moles=None) Molarity, from moles or from a mass
molarity_to_moles(M, volume) Moles of solute
molarity_to_grams(molarity, volume, chemical) Grams of solute

chemhelper.ideal_gas

Function Returns
ideal_gas(solve_for, substance=None, P=None, V=None, n=None, T=None, m=None, decimals=2, Tunits='K', Vunits='L', Punits='atm') P in atm, V in L, n in mol or T in K

solve_for is 'P', 'V', 'n' or 'T'. Inputs may be given in torr, mmHg, kPa, mL or °C with Punits, Vunits and Tunits.

Writing formulas

Formulas are strings, written as in a textbook.

Rule Example
Symbols are case-sensitive 'Co' is cobalt, 'CO' is carbon monoxide
A number after a symbol is its count 'C6H12O6'
Groups go in parentheses or brackets 'Ca(OH)2', 'K4[Fe(CN)6]'
Repeated elements are added together 'CH3COOH' is C₂H₄O₂
Hydrates use parentheses, not a dot 'CuSO4(H2O)5'

Elements 1 to 92 (H to U) are supported.

>>> dict(compounds.parse_formula('K4[Fe(CN)6]'))
{'K': 4, 'Fe': 1, 'C': 6, 'N': 6}

Units and rounding

Quantity Unit
Mass grams
Amount moles
Volume liters (ideal_gas also accepts mL)
Concentration mol/L
Pressure atm (ideal_gas also accepts torr, mmHg, kPa)
Temperature kelvin (ideal_gas also accepts °C)

Results are rounded for display, most to two decimals. Intermediate steps keep full precision. ideal_gas uses R = 0.0821 L·atm/(mol·K) and takes a decimals argument.

Documentation

The documentation site covers everything in depth:

Section What's there
Getting started Installation, quickstart, formula rules, units and rounding
User guide Every function with arguments, formulas, errors and examples
Tutorials A tutoring session, preparing solutions, gas law problems
Cookbook Short recipes: hydrates, limiting reagent, dilution, gas density and more
Periodic table The atomic masses chemhelper uses
API reference Generated from the docstrings
Migrating from 0.4 What changed in 1.0

Development

git clone https://github.com/drosenman/chemhelper.git
cd chemhelper
pip install -e ".[test,docs]"
Task Command
Run the tests pytest
Check every example in the docs and this README python tools/docs_examples.py
Regenerate example output python tools/docs_examples.py --write
Preview the docs mkdocs serve

Bug reports, corrections and new solvers are welcome. See Contributing and the changelog.

License

MIT © Dave Rosenman

Metadata

Release files for chemhelper 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chemhelper 1.0.0
File Size Uploaded Provenance Actions
chemhelper-1.0.0.tar.gz 17.3 kB Without provenance Details for chemhelper-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chemhelper 1.0.0
File Interpreter ABI Platform Actions
chemhelper-1.0.0-py3-none-any.whl Python 3 none any Details for chemhelper-1.0.0-py3-none-any.whl

Total release size: 30.7 kB

Release files / chemhelper-1.0.0.tar.gz

Download URL chemhelper-1.0.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a0f99de1d8da577c7f2624860db4c5f1876170e5c4ab9cf08b4de03d2eda7891
BLAKE2b-256 checksum
How to use checksums
9ab7e4d5314c12b9a06a8840e3ed1c75acc2cee898c84ac9a3cc2849008175f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / chemhelper-1.0.0-py3-none-any.whl

Download URL chemhelper-1.0.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4eaa3a7bd854d4dc8f1210f2aa1db1a602af3591a1ad334954a313eb8f6fa31
BLAKE2b-256 checksum
How to use checksums
7fba0dfae71aac24c278a467bb410bdc0cc04d6d6c7af320340ed67b31d08e8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for chemhelper 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page