Course 4.1Lesson 1 of 3
A molar mass is a sum: atomic mass times the count, for every element. A dictionary holds the atomic masses, and a regular expression reads the symbols and counts out of a formula. Brackets are opened first. The same function then gives the percent composition of any compound.
Try the code and the simulations beside the lesson.
The molar mass of water is a quick sum. The molar mass of copper sulphate crystals, , takes longer, and a chemist does such sums all day. A program does them in a moment, for any formula, and never forgets an atom.
In this lesson you build a molar mass calculator in three steps:
C6H12O6 into symbols and counts.Ca(OH)2.A dictionary links a key to a value. Here each key is an element symbol, and each value is its atomic mass in grams per mole, from the NCERT table.
The line with sum reads like the rule itself: for each element and its count, multiply the atomic mass by the count, and add them all up. glucose.items() gives the pairs ("C", 6), ("H", 12) and ("O", 6).
The f before the string makes it an f-string. {M:.2f} prints M with two decimal places.
Typing {"C": 6, "H": 12, "O": 6} each time is slow. It is easier to type C6H12O6 and let the program read it. Look at the pattern of a formula:
C, Ca, Cl;A regular expression describes exactly this pattern. Python's module re finds every match:
It prints [('Na', ''), ('H', ''), ('C', ''), ('O', '3')]. Read the pattern piece by piece:
[A-Z] is one capital letter, and [a-z]? is at most one small letter;\d* is any number of digits, possibly none;An empty count means one atom, so we write int(n or 1). When the count is "", the or gives 1.
Slaked lime is Ca(OH)2. The 2 belongs to the whole bracket: two oxygen and two hydrogen atoms. The program finds each bracket with its number, counts the atoms inside, multiplies them, and writes them back without the bracket. Ca(OH)2 becomes CaO2H2, which Step 2 can read.
Once you have the counts, the mass percent of each element is its mass in one mole divided by the molar mass, times . For slaked lime the program prints about calcium, oxygen and hydrogen. The percentages always add up to , which is a good test of your code.
The whole calculator: the dictionary, the reader with brackets, the molar mass and a percent composition table.
molar_mass("KCl") stops with KeyError: 'K'. Add every element you need to atomic.int("") stops with ValueError: invalid literal for int() with base 10: ''. Write int(n or 1).Co is cobalt, but CO is carbon and oxygen. The reader is right to treat them differently, so type formulas with care.Ca(OH)2 the 2 multiplies both the oxygen and the hydrogen.sum(atomic[el] * n for el, n in counts.items()).re.findall(r"([A-Z][a-z]?)(\d*)", formula) gives pairs of a symbol and its count; an empty count means one.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
re.findall(r"([A-Z][a-z]?)(\d*)", "MgCl2") return?int(n or 1) and not int(n)?Al(OH)3?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.