Arithmetic

Using variables in mathematical expressions

The ability to assign data to variables and then to use these variables as references back to the data allows the construction of infinitely more complex calculations than the simple single-line expressions we saw previously.

As a relatively simple example, assigning numerical data to variables allows us to express mathematical calculations algebraically.

Example: the quadratic formula

The quadratic formula is an expression for solving a quadratic equation of the form

\[ax^2+bx+c = 0\]

where \(x\) is the unknown value we want to find, and \(a\), \(b\), and \(c\) are fixed parameters.

The formula to find the solutions \(x\) is:

\[x = \frac{-b\pm \sqrt{b^2-4ac}}{2a}\]

Consider the example, \(a=1.0\), \(b=2.0\), \(c=-3.0\):

Using “Python as a calculator” we can find one of the solutions \(x\) using

import math
(-2.0 + math.sqrt(2.0**2 - 4*1.0*(-3.0)))/(2*1.0)
1.0

(we will explain the use of import math and math.sqrt for calculating square roots below.)

This works, but the Python is complicated, making it easy to mistype something without realising it. It is also difficult to understand what this code is doing if you (or someone else) is reading it. And if we want to solve another quadratic equation, then we need to retype the full equation with the new set of parameters (again, hoping we type everything correctly).

Using variables we can rewrite this calculation as:

import math
a = 1.0
b = 2.0
c = -3.0
x = (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
print(x)
1.0

This makes the calculation much more readable, and less prone to accidental errors when being typed. It is also clearer how we might adapt this to solve another quadratic equation with different parameters \(a\), \(b\), and \(c\)—by editing the variables a, b, and c.

Order of operations

Let us unpack this quadratic formula example a bit. We have already seen how simple mathematical operations can be expressed in Python. The full set of native mathematical operations in Python are

Operation

Mathematical notation

Pythonic notation

Addition

\(a + b\)

a + b

Subtraction

\(a - b\)

a - b

Multiplication

\(a \times b\)

a * b

Division

\(a \div b\)

a / b

Exponent

\(a^b\)

a ** b

As we saw in the quadratic equation example, a single line of code can include more than one mathematical operation.

When an expression contains more than one mathematical operation, Python performs these according to a strict hierarchy necessary to follow a hierarchy, also known as the order of operations. Python follows the same order as standard mathematical notation: you may know this as BODMAS.

  • Brackets ()

  • Order **

  • Division / or //

  • Multiplication *

  • Addition +

  • Subtraction -

The math module

Python provides access to more advanced mathematical functions using the math module. We will discuss modules in detail later. Here we will simply note that the import math command allows us to then use various functions within the math module, using dot syntax:

math.sqrt(4)
2.0
math.sin(math.pi / 2.0)
1.0
math.exp(2.0)
7.38905609893065
math.log(7.38905609893065)
2.0

Exercises:

  • Without using a computer or a calculator, and following the BODMAS order of operations, calculate the following:

    1. \(24 \div (10 + 2)\)

    2. \(5 + 16 \div 2 \times 3\)

    3. \([32 \div (6 + 2)]^2\)

  • Write Jupyter cells for each of the following chemical calculations:

    1. Calculate the energy, in Joules, of radiation with a particular wavenumber,

      \(E = hc\bar{\nu}\),

      where \(c\) is the speed of light, \(h\) is Planck’s constant, and \(\bar{\nu}\) is \(1700\) cm-1.

    2. Evaluate the \(\text{pH}\) when the concentration of hydroium ions is \(1.524\times10^{-5}\) M,

      \(\text{pH} = -\log_{10}{[\text{H}_3\text{O}^+]}\).

      You may need to check the \(\log_{10}\) function in the math module documentation.

    3. Find the potential energy, in Joules, between two atoms at a distance of \(2\) Å, modelled with the Lennard-Jones potential model,

      \(E = \frac{A}{r^{12}} - \frac{B}{r^6} \),

      where \(r\) is the interatomic distance, and \(A\) and \(B\) are interaction specific constants, for the Argon-Argon interaction these are \(1.36\times10^{-134}\) Jm12 and \(9.27\times10^{-78}\) Jm6 respectively.

  • The stardard Gibbs free energy for a reaction at a particular temperature is related to the equilibrium constant at that temperature via,

    \(\Delta G = -RT\ln{K}\)

    For the reaction:

    PCl3 (g) + Cl2 (g) → PCl5 (g)

    The standard free energies of formation, \(\Delta G^\circ_{\text{f}}\) for each reactant are given below,

    • PCl3: \(-267.8\) kJmol-1

    • PCl5: \(-305.0\) kJmol-1

    • Cl2: \(0\) kJmol-1

    Write a Jupyter Notebook cell to achieve the following:

    • Using Hess’s law, find the equilibrium constant, \(K_p\), for the above reaction.

    • Then, assuming that \(\Delta H\) and \(\Delta S\) are independent of temperature between 298 K and 350 K, calculate \(\Delta H^\circ\) (e.g. at 298 K), given that \(\Delta G_{\text{f}}\) is \(-20.2\) kJmol-1 at 350 K. The following relation may help,

      \( \Delta G = \Delta H - T \Delta S \).

Worked Example