Variables#
What are 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.
Variables are Python objects which contain data. That may sound exceedingly vague, so here is a minimal working example:
example_variable = 5
example_variable
5
The first line of code above assigns the value of \(5\) to the variable example_variable
. We are essentially telling Python:
“Hello friend, I would like you to store the value \(5\) under the name example_variable
, cheers!”
The second line of code is simply the name of our new variable: example_variable
. Running this line of code returns the value \(5\), confirming that we have indeed stored this value under a new name.
Note
For the most part, you can choose any name you would like for a given variable. Above we used example_variable
, but we could just as easily have used variable_1
, v1
or arbitrary_name
- it doesn’t (functionally) matter. That being said, there are rules governing possible variable names:
They 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 asV1
.If they consist of multiple words, these must be separated by underscores not hyphens.
They cannot be the same as certain reserved keywords. We will meet some of these keywords later.
How can we use variables?#
Okay great so we can store a number under a new name - so what? Well let’s take the exercise from the Using Python as a calculator section. We were given the equilibrium partial pressures of the reactants and products:
Let’s assign all of 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 by plugging in the numbers, we can now use our new variables:
p_NH3 ** 2 / (p_N2 * p_H2 ** 3)
5.404592592592592e-05
Or, even better, why don’t we store the result in another new variable:
K = p_NH3 ** 2 / (p_N2 * p_H2 ** 3)
Okay great, we’ve calculated the equilibrium constant! But wait, why has Python not 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.
Tip
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 (example shown below) or use the print
function.
K = p_NH3 ** 2 / (p_N2 * p_H2 ** 3)
K
5.404592592592592e-05
Aha, we have recovered our equilibrium constant! But this method still only allows us to display one value for each block of code, what if there are multiple values we want to see? To explicitly control what is printed to the screen, we can use the in-built print
function:
print(K)
5.404592592592592e-05
This code may not seem much different to our previous solution, but notice that if we have multiple lines of code:
print(K)
5 * 5
5.404592592592592e-05
25
The print
function allows us to see the value stored in K
even if it is not the last line of code. The syntax (the way in which we write our code) of using the print
function is just the same as the math
library functions we used before: we type the name of the function (print
) followed by a pair of brackets containing the relevant inputs (here the variable K
).
Right, back to the problem at hand. We have a value for the equilibrium constant, let’s use this to calculate \(\Delta G\) at \(298\,\mathrm{K}\). Just like before, we can assign the temperature to a variable:
temperature = 298
Now we can calculate \(\Delta G\):
delta_G = -scipy.constants.R * temperature * math.log(K)
print(delta_G)
24345.175281530013
Here we have \(\Delta G\) in \(\mathrm{J \, mol}^{-1}\), but perhaps we want to convert to \(\mathrm{kJ \, mol}^{-1}\). Rather than recalculating from scratch, we can just modify the variable we already have:
delta_G = delta_G / 1000
print(delta_G)
24.345175281530015
This codes highlights the fact that most objects in Python are mutable: they can be changed. It can be slightly confusing to read a line like:
delta_G = delta_G / 1000
It is slightly easier to understand this by looking at the right side of the =
operator first. We are calculating the result of dividing the value stored in delta_G
by \(1000\). All the left hand side is doing is assigning that new value to the same variable name we were using before. This can also be written in the more concise form:
delta_G /= 1000
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:
temperature = 500
Now we can just re-run the code we already wrote:
delta_G = -scipy.constants.R * temperature * math.log(K)
print(delta_G)
40847.60953276849
And we now have \(\Delta G\) calculated at \(500\mathrm{\,K}\) (under the admittedly faulty approximation 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:
Using the table below, calculate (for the reaction above at \(298\,\mathrm{K}\)):
The enthalpy change
The entropy change
The Gibbs free energy change
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}\) / kJ mol\(^{-1}\) |
\(S^{\circ}\) / J mol\(^{-1}\) K\(^{-1}\) |
---|---|---|
Graphite |
0 |
5.74 |
Diamond |
1.87 |
2.38 |
You will need the relation below.
Answers