Solutions

Solutions#

Okay folks, time to get loopy.

1. Write a program to play “Fizz Buzz”. Your code should loop over numbers between \(1\) and \(100\), printing 'Fizz' for those that are divisible by \(3\), 'Buzz' for those divisible by \(5\) and 'Fizz Buzz' for those divisible by both \(3\) and \(5\).

for number in range(1, 101):
    if number % 3 == 0 and number % 5 == 0:
        print('Fizz Buzz')

    elif number % 3 == 0:
        print('Fizz')

    elif number % 5 == 0:
        print('Buzz')

    else:
        print(number)
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz Buzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
Fizz Buzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
Fizz Buzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
Fizz Buzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
Fizz Buzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

2. You have been provided with the melting and boiling points of various metals:

metals = ['Fe', 'Al', 'Hg', 'Ti', 'Pb']
melting_points = [1811, 933, 234, 1941, 601]
boiling_points = [3134, 2743, 630, 3560, 2022]

Using a for loop, determine whether each metal is a solid, liquid or gas at \(500\,\)K.

metals = ['Fe', 'Al', 'Hg', 'Ti', 'Pb']
melting_points = [1811, 933, 234, 1941, 601]
boiling_points = [3134, 2743, 630, 3560, 2022]

for idx, metal in enumerate(metals):
    if boiling_points[idx] < 500:
        print(f'{metal} is a gas at 500 K.')

    elif melting_points[idx] < 500:
        print(f'{metal} is a liquid at 500 K.')

    else:
        print(f'{metal} is a solid at 500 K.')
Fe is a solid at 500 K.
Al is a solid at 500 K.
Hg is a liquid at 500 K.
Ti is a solid at 500 K.
Pb is a solid at 500 K.

3. Consider \(5.00 \times 10^{-2}\,\)moles of an ideal gas in a \(1.00 \times 10^{-3}\,\)m\(^{3}\) container.

Calculate the pressure at each temperature between \(100\) and \(1000\,\)K in steps of \(50\,\)K and append these values to a list.

import scipy

pressures = []

n = 5.00e-02
V = 1.00e-03

for temperature in range(100, 1050, 50):
    pressure = (n * scipy.constants.R * temperature) / V

    pressures.append(pressure)

print(f'The pressure (in Pa) at each temperature: {pressures}')
The pressure (in Pa) at each temperature: [41572.3130907662, 62358.4696361493, 83144.6261815324, 103930.7827269155, 124716.9392722986, 145503.0958176817, 166289.2523630648, 187075.40890844792, 207861.565453831, 228647.7219992141, 249433.8785445972, 270220.0350899803, 291006.1916353634, 311792.3481807465, 332578.5047261296, 353364.6612715127, 374150.81781689584, 394936.9743622789, 415723.130907662]

4. Recall the previous exercise in lab 1 in which we calculated various lines in the Hydrogen emission spectrum using the following equation:

\[\frac{1}{\lambda} = R_{\mathrm{H}}\left[\frac{1}{n_{1}^{2}} - \frac{1}{n_{2}^{2}}\right],\]

where \(\lambda\) is the wavelength, \(R_{\mathrm{H}}\) is the Rydberg constant, \(n_{1}\) is the principal quantum number of the emission state and \(n_{2}\) is the principal quantum number of the excited state.

a) Write a for loop to print the wavelengths (in nanometres) of the first \(10\) lines of the Balmer series (\(n_{1} = 2\)).

n_1 = 2

for n_2 in range(3, 13):
    wavelength = 1 / (scipy.constants.Rydberg * (1 / n_1 ** 2 - 1 / n_2 ** 2))
    wavelength *= 10 ** 9 # Convert to nanometres

    print(f'{wavelength} nm')
656.1122764194981 nm
486.0090936440728 nm
433.9366907536364 nm
410.0701727621865 nm
396.90742647599274 nm
388.8072749152582 nm
383.4422394659406 nm
379.6946044094319 nm
376.96859186495385 nm
374.92130081114186 nm

b) Add one additional loop to your code so that it can print the wavelengths of the first \(10\) lines in the Lyman, Balmer and Paschen series.

for n_1 in range(1, 4):
    print(f'n_1 = {n_1}')
    for n_2 in range(n_1 + 1, n_1 + 11):
        wavelength = 1 / (scipy.constants.Rydberg * (1 / n_1 ** 2 - 1 / n_2 ** 2))
        wavelength *= 10 ** 9 # Convert to nanometres

        print(f'{wavelength} nm')
n_1 = 1
121.5022734110182 nm
102.51754319054662 nm
97.20181872881454 nm
94.92365110235798 nm
93.73032520278547 nm
93.02517808031081 nm
92.57316069410909 nm
92.26578887149195 nm
92.04717682652894 nm
91.8860942670825 nm
n_1 = 2
656.1122764194981 nm
486.0090936440728 nm
433.9366907536364 nm
410.0701727621865 nm
396.90742647599274 nm
388.8072749152582 nm
383.4422394659406 nm
379.6946044094319 nm
376.96859186495385 nm
374.92130081114186 nm
n_1 = 3
1874.6065040557094 nm
1281.4692898818328 nm
1093.5204606991638 nm
1004.6719232673566 nm
954.345129337452 nm
922.6578887149195 nm
901.2531269498604 nm
886.0444804325814 nm
874.8163685593312 nm
866.2732399601189 nm

c) Create three empty lists called lyman, balmer and paschen.

Add some logic to your code from b) so that, rather than printing each wavelength to the screen, each value is stored in the appropriate list.

lyman = []
balmer = []
paschen = []

for n_1 in range(1, 4):
    for n_2 in range(n_1 + 1, n_1 + 11):
        wavelength = 1 / (scipy.constants.Rydberg * (1 / n_1 ** 2 - 1 / n_2 ** 2))
        wavelength *= 10 ** 9 # Convert to nanometres

        if n_1 == 1:
            lyman.append(wavelength)

        elif n_1 == 2:
            balmer.append(wavelength)

        else:
            paschen.append(wavelength)

print(lyman)
print(balmer)
print(paschen)
[121.5022734110182, 102.51754319054662, 97.20181872881454, 94.92365110235798, 93.73032520278547, 93.02517808031081, 92.57316069410909, 92.26578887149195, 92.04717682652894, 91.8860942670825]
[656.1122764194981, 486.0090936440728, 433.9366907536364, 410.0701727621865, 396.90742647599274, 388.8072749152582, 383.4422394659406, 379.6946044094319, 376.96859186495385, 374.92130081114186]
[1874.6065040557094, 1281.4692898818328, 1093.5204606991638, 1004.6719232673566, 954.345129337452, 922.6578887149195, 901.2531269498604, 886.0444804325814, 874.8163685593312, 866.2732399601189]