Solutions

Video

Alternative link

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.

import scipy

nu = 2.18e10 # GHz
nu *= 1e9 # Convert to Hz

energy = scipy.constants.h * nu

print(f'The energy of the photon is {energy:.3g} J.')
The energy of the photon is 1.44e-14 J.

Advanced: you could manually write out the answer in standard form using string splitting and list indexing - you’ll encounter the latter in Session 2.

energy_string = f'{energy:.4e}'
energy_strings = energy_string.split('e')
mantissa = float(energy_strings[0])
exponent = int(energy_strings[1])
print(f'The energy of the photon is {mantissa:.3g} x 10^{exponent} J.')
The energy of the photon is 1.44 x 10^-14 J.

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 do not have to import scipy
# again if you've already imported it in
# a previous cell
import scipy

# Lyman series
n_f = 1
n_i = 2

iwavelength = scipy.constants.Rydberg * (1 / n_f**2 - 1 / n_i**2)
wavelength = 1 / iwavelength  # m

wavelength *= 1E9  # nm 

print(f'The wavelength of the first line in the Lyman series is {wavelength:.2f} nm')

# Balmer series
n_f = 2
n_i = n_f + 1

iwavelength = scipy.constants.Rydberg * (1 / n_f**2 - 1 / n_i**2)
wavelength = 1 / iwavelength  # m

wavelength *= 1E9  # nm 

print(f'The wavelength of the first line in the Balmer series is {wavelength:.2f} nm')

# Paschen series
n_f = 3
n_i = n_f + 1

iwavelength = scipy.constants.Rydberg * (1 / n_f**2 - 1 / n_i**2)
wavelength = 1 / iwavelength  # m

wavelength *= 1E9  # nm 

print(f'The wavelength of the first line in the Paschen series is {wavelength:.2f} nm')
The wavelength of the first line in the Lyman series is 121.50 nm
The wavelength of the first line in the Balmer series is 656.11 nm
The wavelength of the first line in the Paschen series is 1874.61 nm

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)

We can’t add str and int. Instead, use

a = 32
b = 64

print(a + b)
96

or, alternatively

a = '32'
b = '64'

print(a + b)
3264

Block 2


string_variable = 'Python is pretty cool! '
repeat = 4.0

print(string_variable * repeat)

We can’t multiply str by float, only int.

Instead, use

string_variable = 'Python is pretty cool! '
repeat = 4

print(string_variable * repeat)
Python is pretty cool! Python is pretty cool! Python is pretty cool! Python is pretty cool! 

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.

The problem is that braces (curly brackets) have been used instead of parenthesis (normal brackets) in the call to math.sqrt.

# 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

# 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.')

The print statement could also be improved using a format specifier.

print(f'The root mean square speed of an O2 molecule at 500 K is {rms_speed:.1f} 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}.\]

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

The problem is that the print function has been redefined as a string variable on the first line, simply change this to call print instead.

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

The second problem is that the order of operations inside the call is also wrong - look at the denominator in the definitions of solution_1 and solution_2.

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

If print has been redefined, simply running this “correct” cell will not fix the problem - we need to tell python to forget about everything it’s run before and start from scratch.

We can do this by restarting the kernel. In your Jupyter notebook, navigate via the menu bar at the top to Kernel then 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 you know and love.