Synoptic exercises
Congratulations, your Python journey has officially begun! Here are a few synoptic exercises to test your understanding of this session’s content.
Use variables to minimise the amount of data you have to input manually. You will also find that using variables enables you to do the same calculation with different inputs in much less time than if you had to re-write the entire calculation from scratch.
You should use print statements and f-strings where possible to clearly display the results you have calculated (and to improve the readability of your code).
1. Calculate the energy of a photon (in Joules) with frequency \(2.18 \times 10^{10}\,\mathrm{GHz}\) according to:
\[E = h\nu,\]
where \(\nu\) is the frequency and \(h\) is Planck’s constant. You can use Planck’s constant from the scipy.constants module
import scipy
scipy.constants.hwhich has units of \(\mathrm{J\, s}\) as stated here.
2. Calculate the wavelengths (in nanometres) corresponding to the first line in the Lyman, Balmer and Paschen series. You will need the following relation:
\[\frac{1}{\lambda} = R_{\mathrm{H}}\left[\frac{1}{n^{2}_\mathrm{f}} - \frac{1}{n^{2}_\mathrm{i}}\right],\]
where \(\lambda\) is the wavelength, \(R_{\mathrm{H}}\) is the Rydberg constant, and \(n_\mathrm{f}\) and \(n_\mathrm{i}\) specify the principal quantum numbers of the final and initial states involved in the transition, respectively. \(n_\mathrm{f} = 1\) for the Lyman series, \(2\) for the Balmer series and \(3\) for the Paschen series.
You may access the Rydberg constant in \(\mathrm{m^{-1}}\) using scipy.constants.Rydberg.
3. Consider the following blocks of code. Each one of them will cause an error when run. Taking each block one-by-one:
- Predict which line of code will cause the error and why this line is problematic.
- Run the block of code in a Jupyter notebook and see whether or not you were correct.
- Modify the code to solve the problem and thus remove the error.
Block 1
a = 32
b = '64'
print(a + b)Block 2
string_variable = 'Python is pretty cool! '
repeat = 4.0
print(string_variable * repeat)Block 3
The code below uses the following equation for the root mean square speed of a molecule:
\[v_\mathrm{rms} = \sqrt{\frac{3RT}{M}},\]
where \(R\) is the gas constant, \(T\) is the temperature and \(M\) is the molar mass of a particle.
# You do not have to import scipy or math
# again if you've already imported it in
# a previous cell
import math
import scipy
temperature = 500
molar_mass = 31.998 / 1000 # molar mass of an oxygen molecule in kgmol-1
# Calculate the root mean square speed of an oxygen molecule at 500 K
rms_speed = math.sqrt{3 * scipy.constants.R * temperature / molar_mass}
print(f'The root mean square speed of an O2 molecule at 500 K is {rms_speed} ms-1.')Block 4
The code below is trying to solve the quadratic equation
\[5x^{2} + 12x + 7,\]
using the quadratic formula:
\[\frac{-b \pm \sqrt{b^{2} - 4ac}}{2a}.\]
This block also contains a problem which, whilst it will not cause an error, will lead to the wrong result being calculated. See if you can spot and fix this problem in addition to removing the error.
print = 'This code calculates the solutions to 5x^2 + 12x + 7=0'
a = 5
b = 12
c = 7
solution_1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / 2 * a
solution_2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / 2 * a
print(f'The solutions are {solution_1} and {solution_2}')