Variables

So far we have been using Python as a calculator - we supply inputs in the form of numbers and use basic mathematical operations and functions to derive a result (another number). This works perfectly well if the calculation we want to do is relatively simple, the process is very similar to how we would use a literal calculator. On the other hand, as the calculations we want to do become more complicated, it becomes somewhat laborious (and eventually impractical) to use Python in this way, and it would be nice if we could somehow store numbers in the computer and refer back to them - a bit like the Ans key on your calculator.

Using variables

Variables are Python objects which contain data. That may sound vague, so here is a basic example

example_variable = 5

we assign the integer value 5 to the variable example_variable - the computer stores this value in its memory under this name.

If we simply type the name of a variable into a code cell and execute it, we are told its value

example_variable
5
NoteSyntax

You can choose almost any name for a variable but you must follow a set of rules.

  • Variable names must start with a letter or an underscore i.e. they cannot start with a number.
  • They are case-sensitive: v1 is not the same variable as V1.
  • If they consist of multiple words, these must be separated by underscores not hyphens and not spaces.
  • They cannot be the same as certain reserved keywords - we’ll meet some of these later.

We can combine variables with the operators you just learned about. As an example, take the exercise from the Using Python as a calculator section. We were given the equilibrium partial pressures of the reactants and products of the Haber-Bosch process

\[p_{\ce{N2}} = 1.00\,\mathrm{bar}\]

\[p_{\ce{H2}} = 3.00\,\mathrm{bar}\]

\[p_{\ce{NH3}} = 3.82 \times 10^{-2}\,\mathrm{bar}\]

Assign these values to some new variables

p_N2 = 1.00
p_H2 = 3.00
p_NH3 = 3.82e-2

Rather than calculating the equilibrium constant \(K\) by manually typing in the numbers, we can instead use the variables

p_NH3 ** 2 / (p_N2 * p_H2 ** 3)
5.404592592592592e-05

Or, even better, can assign the result to another variable which we’ll call K

K = p_NH3 ** 2 / (p_N2 * p_H2 ** 3)
Warning

You should always use variables to store data, and should never copy and paste the result of a calculation back into the code - this is error-prone and makes your code very unclear.

Note

You might be wondering why Python hasn’t displayed the result beneath our previous line of code. This example highlights something that we have hitherto ignored, which is how Jupyter decides what to print to the screen.

Jupyter only displays the value corresponding to the last line of code in the cell. If the last line is a variable assignment, like our example above, then nothing is printed to the screen.

If we want to display the value stored in a variable, we can either explicitly evaluate it (as we did for example_variable) or use the built-in print function.

print(K)
5.404592592592592e-05

The print function allows us to print as many variables as we’d like, e.g.

print(K, p_NH3)
5.404592592592592e-05, 0.0382

Unfortunately, while accurate, this doesn’t look very nice - we’ll learn about how to print variables nicely in a little while. Notice that the way in which we use print is the same as when we were using math.sin - we call the function and provide some inputs. We use the word syntax when describing the way in which we write code, so the syntax of using print and math.sin are the same - they’re both functions.

Returning to the Haber-Bosch process, the final step is to calculate the Gibbs free energy of reaction \(\Delta G\) at \(298\,\mathrm{K}\) and assign it to a new variable delta_G.

import math
import scipy

temperature = 298

delta_G = -scipy.constants.R * temperature * math.log(K)
print(delta_G)
24345.175281081323
Tip

Notice that we’ve used scipy.constants.R in our equation rather than typing out the numerical value - this is more accurate and easier to read.

Now think about what the units of this value will be. The computer is just like your calculator - it doesn’t know anything about units or ideal gases. You’ve just asked it to multiply some numbers and that’s what it’s done.

A quick dimensional analysis tells us that our answer is in \(\mathrm{J \, mol}^{-1}\), but conventionally we work with \(\mathrm{kJ \, mol}^{-1}\). Rather than recalculating this from scratch, we can just modify our existing variable using

delta_G = delta_G / 1000
print(delta_G)
24.345175281081325
Tip

If you’re struggling to convert between units, or with SI prefixes, then perhaps it’s worth revisiting workshop 1 of CH12002.

This code shows that some Python objects are mutable - they can be changed.

It can be slightly hard to read a line of code like

delta_G = delta_G / 1000

so we can instead use the following shorthand

delta_G /= 1000

Both have the same meaning - divide the value stored in the variable delta_G by 1000 and then assign that new number in the variable delta_G instead.

We’ve calculated \(\Delta G\) here at \(298\mathrm{\,K}\), but with variables we can very easy redo the calculation for a different temperature. All we need to do is change the value stored in the temperature variable and re-run the cell.

import math
import scipy

temperature = 298

delta_G = -scipy.constants.R * temperature * math.log(K)
print(delta_G)

We now have \(\Delta G\) calculated at \(500\mathrm{\,K}\) (assuming that the equilibrium constant is not temperature dependent).


Hopefully, you can now see how variables can be useful. They allow us to input our raw data once (in the example above, inputting the partial pressures and temperature) and then let Python do the rest for us programmatically.

Exercise

At high temperatures and high pressures, graphite can be converted into diamond

\[\ce{C}_{\mathrm{Graphite}} \rightarrow \ce{C}_{\mathrm{Diamond}}.\]

Using the table below, calculate (for the reaction above at \(298\,\mathrm{K}\))

  • The enthalpy of reaction.
  • The entropy of reaction.
  • The Gibbs free energy of reaction.

You should use variables to minimize the number of times you need to manually type out (or copy/paste) numbers into your code.

Allotrope \(\Delta H^{\circ}_{f}\ / \ \mathrm{kJ\, mol^{-1}}\) \(S^{\circ}\ / \ \mathrm{J\, mol^{-1}K^{-1}}\)
Graphite 0 5.74
Diamond 1.87 2.38

You will need to use

\[\Delta G = \Delta H - T\Delta S\]

\[\Delta H = 1.87 \, \mathrm{kJ\, mol^{-1}}\] \[\Delta S = -3.36 \, \mathrm{J\, mol^{-1}K^{-1}}\] \[\Delta G = 2.87 \, \mathrm{kJ\, mol^{-1}}\]