Solutions, dilution and titration in code · Python Programming Class 11–12 | StemPy Lab
Course 4.1·Lesson 3 of 3
Solutions, dilution and titration in code
Molarity is moles of solute per litre of solution. Small functions find the mass to weigh for a solution, the volume of stock for a dilution, the concentration after mixing and the end point of a titration. A loop then prints the acid left as the base runs in.
about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
Molarity in code
Molarity is the number of moles of solute in one litre of solution, in mol/L (often written M). In the laboratory, volumes are in millilitres, so a function must divide by 1000:
moles = molarity ×1000volume in mL
Making a solution
To make a solution of a given molarity, weigh the solute, dissolve it, and make it up to the mark in a volumetric flask. The mass to weigh is the moles times the molar mass:
mass = molarity ×1000V×M
Dilution and mixing
Adding water does not change the moles of solute, only the volume. So:
M1V1=M2V2, and the volume of stock to take is V1=M1M2V2.
When two solutions of the same solute are mixed, the moles add up and the volumes add up:
M=V1+V2M1V1+M2V2
The end point of a titration
In a titration, a base runs from a burette into an acid until the acid is just used up. At the end point, the moles match the ratio in the equation. For hydrochloric acid and sodium hydroxide the ratio is 1:1. For sulphuric acid, H2SO4, each mole needs two moles of base.
volume of base at the end point =MbaseMacid×Vacid×ratio
A function with a default argument, ratio=1, covers both cases: leave it out for 1:1, or give ratio=2.
Practise with new numbers
Try it in Python
The program has three small functions, then a loop that follows the acid left in the flask as the base runs in.
Common mistakes
Forgetting to divide by 1000. A volume in millilitres gives moles a thousand times too large.
Solving M1V1=M2V2 the wrong way round. The stock is more concentrated, so its volume is the smaller one.
Adding molarities when mixing. Add the moles, then divide by the total volume.
Leaving out the ratio. Sulphuric acid needs twice the base of hydrochloric acid at the same molarity.
Calling with the wrong number of values.end_point(0.1, 20) stops with TypeError: end_point() missing 1 required positional argument: 'base_M'.
Remember these
Moles = molarity ×1000V in mL; mass to weigh = moles ×M.
Dilution keeps the moles: M1V1=M2V2.
Mixing: add the moles and add the volumes.
End point: the moles of acid and base match the ratio in the equation; a default argument such as ratio=1 keeps the common case short.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
What does moles_in(0.25, 200) return?
You dilute 10mL of a 2mol/L solution to 100mL. What is the new molarity?
You mix 100mL of 0.3mol/L salt water with 100mL of 0.1mol/L salt water. What is the molarity of the mixture?
Why does the function end_point have ratio=1 in its first line?
In the loop of the program, why is the acid left written as max(left, 0)?
Enroll free in Moles and Reactions by Computer
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.