Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Project: Molar Mass and Formula Calculator

Build a small Python toolkit that parses molecular formulas, computes molar mass and percent composition, finds empirical formulas from combustion data and degrees of unsaturation.

In this project you will build a small toolkit that does the arithmetic organic chemists do all the time: reading a molecular formula, finding its molar mass, working out percentage composition, finding an empirical formula from experimental data, and counting the degrees of unsaturation (rings and multiple bonds). Along the way you will revisit ideas from the whole track, such as condensation reactions, combustion and isomers.

Everything runs in the browser, and every constant is a standard atomic mass in g/mol. The tools are calculators that apply formulas: they do not know any chemistry beyond the numbers you give them.

Step 1: parse a formula

A molecular formula such as C6H12O6 or C2H5OH is a sequence of element symbols (a capital letter, optionally followed by a lowercase one) and optional counts. Parentheses, as in Ca(OH)2, group atoms. The parser below handles both.

Python Step 1: formula parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: molar mass and percent composition

The molar mass is the sum of the atomic masses of all atoms. The percentage by mass of an element is its share of that total.

Python Step 2: molar mass and composition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can check the output against the earlier lessons: glucose is about 180.16 g/mol (see carbohydrates and lipids) and ethanoic acid is about 60.05 g/mol (see carboxylic acids).

Step 3: empirical formula from combustion data

In a combustion analysis a sample of an organic compound containing only C, H and O is burned completely. All the carbon becomes CO2 and all the hydrogen becomes H2O, which are weighed. Oxygen in the sample is found by difference. The steps:

  1. Mass of C = mass of CO2 ร— (12.011 / 44.009).
  2. Mass of H = mass of H2O ร— (2.016 / 18.015).
  3. Mass of O = sample mass โˆ’ mass of C โˆ’ mass of H.
  4. Convert each to moles and divide by the smallest to find the simplest ratio.
Python Step 3: empirical formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The empirical formula C2H6O fits both ethanol and methoxymethane (see ethers and thiols). To decide the molecular formula you need the molar mass, for example from mass spectrometry: if M = 46 g/mol, the formula is C2H6O, because the empirical mass is already 46. To tell the isomers apart you need other evidence, such as a test with sodium or the boiling point.

If a ratio is not close to a whole number, do not just round it. Ratios such as 1.5 or 1.33 mean you should multiply all values by 2 or 3 first.

Step 4: degrees of unsaturation

The degree of unsaturation (also called double-bond equivalents) counts rings plus double bonds, where a triple bond counts as two. For a compound CcHhNnXx (X = halogen) with any amount of oxygen or sulfur:

DoU = (2c + 2 + n โˆ’ h โˆ’ x) / 2

Oxygen does not change the result. For benzene, C6H6: (12 + 2 โˆ’ 6) / 2 = 4 (one ring and three C=C, as you saw in benzene).

Python Step 4: degrees of unsaturation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A result of 0 means a saturated, acyclic molecule. A value of 1 means one ring or one double bond (such as C=C or C=O). The value for C2H4O2 is 1, which fits a carbonyl: ethanoic acid or methyl methanoate.

Extending the project

Ideas to try on your own:

  • Add N, P and metals to the table of atomic masses.
  • Print the percentage composition as a small bar chart with matplotlib.
  • Use the molar mass to compute the number of moles in a given mass and the mass of product in a reaction (as in the nitration and esterification lessons).
  • Combine the four functions into one report for a formula, including the classifier from practice: identify the functional group.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the degree of unsaturation of C4H8?

    (2ร—4 + 2 โˆ’ 8) / 2 = 1: one double bond or one ring.
  2. 2. In combustion analysis, how is the mass of oxygen in a C, H, O compound found?

    The oxygen in the sample ends up mixed with the oxygen from the air, so it is found by difference.
  3. 3. A mole ratio of C : H : O comes out as 1 : 2.01 : 0.99. What is the empirical formula?

    The ratio is almost exactly 1 : 2 : 1, so the empirical formula is CH2O.
  4. 4. The empirical formula is CH2O and the molar mass is 180 g/mol. What is the molecular formula?

    The empirical mass is about 30 g/mol, and 180 รท 30 = 6, so the formula is C6H12O6.

Summary

  • A formula parser turns text such as (CH3)3COH into atom counts.
  • Molar mass is the sum of atomic masses; percent composition is each element's share.
  • Combustion data give C from CO2, H from H2O and O by difference, leading to the empirical formula.
  • Degrees of unsaturation, (2c + 2 + n โˆ’ h โˆ’ x) / 2, count rings and multiple bonds.

Next: Back to the Organic Chemistry Basics course page