Function
Gives the mass needed of specific basic (pH above 7) substance in order to neutralise the pH of a specific acidic substance.
from decimal import Decimal as d
# m = mass
# n = moles
# M = molar mass
MkHCO3 = 100.11
MnaKHCO3 = 84.007
MphenHCl = 215.67
moles = lambda m,M: m/M
nPhenHCl = moles(1.54, MphenHCl)
mSubstance = lambda x,y: x*y
mKHCO3 = mSubstance(MkHCO3, nPhenHCl)
mNaHCO3 = mSubstance(MnaKHCO3, nPhenHCl)
nl = '\n'
print(f'{nl}KHCO3:')
print(f'{d(mKHCO3):.2f}g{nl}')
print(f'NaHCO3:')
print(f'{d(mNaHCO3):.2f}g{nl}')
Output
KHCO3:
0.71g
NaHCO3:
0.60g
Background
I know using a named lambda defeats the purpose but the function was so small I couldn't resist.
This script is just for me which is why I hardcoded the value of the nootropic. I use single variables because I'm a chemist and that's how these equations are. If it was for others I wouldn't do these things.
I usually write bash scripts to do things on the filesystem, I could do that with python but there's too much overhead, bash is better suited for that task.
In doing so I write scripts (like the above) with a single use in mind.
Which is why classes are hard for me to grasp.
I've not yet come up with an idea big enough to use a class I think that's my problem, also because I don't practice much and online example are silly and don't apply to the real world.
The above code is probably useless to use as a class, is that right?
I need ideas on when a class would be required or how you'd even turn the above code into a class or if it's just a waste of time.
Class Idea, too much?
If I compiled a bunch of acid bases and made a class for that? if you have a weak acid (vinegar) plus a strong base (sodium hydroxide) then you have to use logs and more complex equations, for a weak acid + weak base, (or strong acid + weak base and vice versa) you also have rate equations which will tell you how long these reactions will take.
I could also add reaction feasibility stuff which will tell you if a reaction is possible. Other things like deriving the pressure and temp for a reaction to occur. Could I have a class for that or is it too much?
For the curious because of the shambles that went on in the comments (now unavailable to see)
All this info is really not needed that's why I said 'for the curious'.
Neutralisation
NaHCO3(aq) + phen-HCl(aq) -> phen(aq) + NaCl(aq) + H2O(l) + CO2(g)
aq means aqueous (something dissolved in water)
Aqueous bicarbonate salt + aqueous medicine salt -> aqueous medicine + water + gaseous carbon dioxide
From the equation you can see that the reactants are 1:1.
Simpler Neutralisation
HCl(aq) + NaOH(aq) -> NaCl(aq) + H2O(l)
You might notice most medicines are salts; med-hcl, med-phosphate etc, this is for long shelf life or because med might smell really bad besides other reasons.
Moles
(n) is just a ratio, The moles
function, gives a decimal which you can multiply against the other compounds molar mass
in order to find out how much equal mass it needs to react completely to make the products.
Why you have to multiply n by molar mass
Molar mass
differs for each atoms/compounds (or say particles to group them all) this is because their weight varies; they have less or more protons, neutrons and electrons.
Molar mass
has the same number of particles for any atom/molecule/ compound per mol
.
molarMass = mass which has 6.023*10^23
particles.
This is Avogadro's number
or NA and is what 1 mol
means, mol
is different from moles
.
1 mol = mass which contains NA particles.
1 moles = mass/molarMass = ratio
So you need to multiply n by NA to ensure equal numbers of particles react.
Demystifying moles (n)
Edited, I was sleepy and had nonsense before.
x = £20 per 100g
y = £12 per 50g
z = what the price of y should be.
Where x and y are the same products.
You want to know the price per 1g. So you can know z.
x[ratio] = 1 / 100 = 0.01
x[price per 1g] = x[ratio] * x[price] = £0.20 per 1g
y[price per 1g] = x[ratio] * y[price] = £0.12 per 1g
z[price per 50g] = y[price] * 50 = £6.00 per 50g
That's all moles
is and you probably do it to compare small vs large products prices to see which is cheaper/how much you save.
Ionic and covalent bonding
KHCO3 is an inorganic compound. K+ binds to bicarbonate or HCO3- creating a salt; potassium bicarbonate, this is an ionic bond (two opposite charges attracting). Salts have ionic bonds and break in water (excluding some crystals) giving ions; atoms/molecules with charges, this is why salt water is more conductive than water.
More on ions
Water exists in an equilibrium:
H2O <-> H3O+
~1 ~0
When electricity is passed through water, it ionises much more greatly, causing the position of equilibrium to greatly shift to the right.
The bonding in KHCO3: K(+)(-O3-C-H)
C is bonded covalently (the dashes -) to each of the oxygen atoms and hydrogen atom, this bond is strong, electrons are shared, and they are bound by the strong nuclear force and require high energy to ionise, they remain unchanged in water.
This is the way salts work, and why they ionise in water. Take NaCl it's Na(+)Cl(-). You add water and it ionises to Na(+) + Cl(-). The free Na(+) is what allows you to taste salt.
H2O2 represents Hydrogen Peroxide. When used as a variable name it doesn't have the same meaning as in chemistry. It is just a string and the underlying object is an integer. It doesn't have chemical properties.
So the variables contain the molar mass of the compound. The only physical property I'm interested in is its molar mass which is an integer.
You can calculate the mass for a reaction very easily, chemical properties have nothing to do with the calculation, only physical properties do like mass in this case.
Example of 2:1 reaction
H2O2(aq) -> 2H2O(l) + O2(g)
If this is reversed, take the molar mass
of both of the reactants then say for O2 you have has a mass of 10g:
O2[n] = O2[mass] / O2[molarMass]
H2O[mass required to completely react with O2] = O2[n] * (H2O[molarMass] * 2)
That was all done without chemical properties, only physical properties. Might be obvious but to the fervent few well I hope this calms you, for the curious I hope you learned something.
Conclusion
I used to add about half a teaspoon of baking soda or potassium bicarbonate, this gave a salty or bitter taste respectively and was irritating to the throat.
Using the calculated masses for the bases there is hardly a taste and no irritation.