A brief note on Moles and Reactions by Computer . Read it before a test. Try each question at the end before you open its answer.
Keep atomic masses in a dictionary: atomic = {"H": 1.008, "C": 12.01, "O": 16.00}. Read a formula with re.findall(r"([A-Z][a-z]?)(\d*)", formula): one pair for each symbol, and an empty count means one. Open brackets first: the number after a bracket multiplies every atom inside. Molar mass: sum(atomic[el] * n for el, n in counts.items()). Mass percents add up to 100 100 100 . Reactions as functions Keep an equation as two dictionaries of coefficients. Rounds = = = moles ÷ \div ÷ coefficient. min(rounds, key=rounds.get) gives the limiting reagent. Products: extent × \times × coefficient; excess left: moles − - − extent × \times × coefficient. Percent yield = actual theoretical × 100 = \dfrac{\text{actual}}{\text{theoretical}} \times 100 = theoretical actual × 100 ; at STP a mole of gas fills 22.7 L 22.7\ \text{L} 22.7 L . Solutions Moles = = = molarity × V in mL 1000 \times \dfrac{V \text{ in mL}}{1000} × 1000 V in mL . Dilution: M 1 V 1 = M 2 V 2 M_1V_1 = M_2V_2 M 1 V 1 = M 2 V 2 . Mixing: add the moles and the volumes. End point: moles of base = = = moles of acid × \times × the ratio of the equation; a default argument ratio=1 keeps the common case short. A worked example What does this print?
Rounds. Phosphorus 0.10 1 = 0.10 \dfrac{0.10}{1} = 0.10 1 0.10 = 0.10 ; oxygen 0.60 5 = 0.12 \dfrac{0.60}{5} = 0.12 5 0.60 = 0.12 . Limiting. Phosphorus has fewer rounds, so min returns 'P4'. Product. In P 4 + 5 O 2 → P 4 O 10 \text{P}_4 + 5\text{O}_2 \rightarrow \text{P}_4\text{O}_{10} P 4 + 5 O 2 → P 4 O 10 the product's coefficient is 1 1 1 , so 0.1 0.1 0.1 mol of P 4 O 10 \text{P}_4\text{O}_{10} P 4 O 10 forms. It prints P4 0.1.
Practice with new numbers
A program to try The program reads a formula, finds its molar mass and the molarity of a solution made from it.
Run it. Check the molar mass of potassium nitrate by hand.
1. Change the mass to 20.2 g. What happens to the molarity?
Mistakes to avoid A missing element. KeyError: 'K' means the dictionary has no potassium. int(''). An empty count raises ValueError; write int(n or 1). Comparing moles instead of rounds. Divide by the coefficient first. Millilitres as litres. Divide by 1000 1000 1000 . Adding molarities when mixing. Add the moles, then divide by the total volume. Check yourself How many atoms of each element does a correct reader count in Zn(NO3)2? Stuck? Show a hint Show the answerFor N 2 + 3 H 2 → 2 NH 3 \text{N}_2 + 3\text{H}_2 \rightarrow 2\text{NH}_3 N 2 + 3 H 2 → 2 NH 3 with 1 1 1 mol of each gas, which gas does the function name as limiting? Stuck? Show a hint Show the answerA function returns 6000.0 mol for 0.3 mol/L 0.3\ \text{mol/L} 0.3 mol/L in 20 L 20\ \text{L} 20 L . What is wrong, and what is the right answer? Stuck? Show a hint Show the answerWhy should you test a molar mass function on a formula with brackets? Stuck? Show a hint Show the answer