Synoptic exercises

Synoptic exercises#

Congratulations, your Python journey has officially begun! Here are a few synoptic exercises to test your understanding, feel free to refer back to the previous material where it may be helpful.

Tip

Remember to 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.

It may also be worth using 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 import Planck’s constant from the scipy library:

import scipy
print(scipy.constants.h)
6.62607015e-34

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}_{1}} - \frac{1}{n^{2}_{2}}\right],\]

where \(\lambda\) is the wavelength, \(R_{\mathrm{H}}\) is the Rydberg constant and \(n_{2}\) is the principal quantum number of the excited state. \(n_{1} = 1\) for the Lyman series, \(2\) for the Balmer series and \(3\) for the Paschen series. You may access the Rydberg constant with:

scipy.constants.Rydberg
10973731.568157

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: This code uses the following expression for the root mean square speed of a molecule:

\[\sqrt{\frac{3RT}{M}},\]

where \(R\) is the gas constant, \(T\) is the temperature and \(M\) is the molar mass.

Note

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.

import math
import scipy

# You do not have to include the imports above if you have already imported math and scipy in your notebook.

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: This code is trying to solve the following equation,

\[5x^{2} + 12x + 7,\]

using the quadratic formula:

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

Note

This one is difficult if you are new to Python. See if you can figure out why the error occurs, then see the hidden solution below (the “Click to show” box) to learn how it can be fixed.

print = 'This code calculates the solutions to 5x^2 + 12x + 7'

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}')

How to fix the error for code block 4:

The problem here is that we have redefined the print function. The first line of block 4 assigns a string to the name print. This is perfectly valid Python, but the name print was already in use: it is the name of the print function. This means that when the final line runs, Python complains that you are treating a str (a string) like a function, as we have effectively replaced the print function with the string: ‘This code calculates the solutions to 5x^2 + 12x + 7’.

So if we have seem to have “lost” the print function, how do we get it back? We can do this by restarting the kernel. In your Jupyter notebook, navigate via the menu bar at the top to Kernel->Restart and Run All Cells. This will effectively clear Python’s memory and re-run all of your code, meaning that print will once again refer to the function we want. Be sure to fix code block 4 before you do this (i.e. actually call the print function rather than assigning a variable to that name).