Worked Examples: Lists#
These worked solutions correspond to the exercises on the Lists page.
How to use this notebook:
Try each exercise yourself first before looking at the solution
The code cells show both the code and its output
Download this notebook if you want to run and experiment with the code yourself
Your solution might look different - that’s fine as long as it gives the correct answer!
Setup#
We’ll import the math
module here for use throughout the exercises.
import math
Exercise 1: List Slicing with Halogens#
Problem: Given the list of halogens, use list slicing to:
a) Extract the first three elements
b) Extract the last three elements
c) Extract every second element
d) Determine what using a negative number following the second colon (e.g.,
[::-1]
) gives
First, let’s define the list:
halogens = ['fluorine', 'chlorine', 'bromine', 'iodine', 'astatine']
print(halogens)
['fluorine', 'chlorine', 'bromine', 'iodine', 'astatine']
Part (a): First three elements#
Goal: Extract ['fluorine', 'chlorine', 'bromine']
print(halogens[:3])
['fluorine', 'chlorine', 'bromine']
Explanation:
The slice [:3]
means “from the start up to (but not including) index 3”.
Index 0:
'fluorine'
Index 1:
'chlorine'
Index 2:
'bromine'
Index 3:
'iodine'
← stop here (not included)
General pattern: [:n]
gives you the first n
elements.
Part (b): Last three elements#
Goal: Extract ['bromine', 'iodine', 'astatine']
print(halogens[-3:])
['bromine', 'iodine', 'astatine']
Explanation:
The slice [-3:]
means “from the 3rd element from the end to the end”.
Negative indices count backwards from the end:
Index -1:
'astatine'
(last element)Index -2:
'iodine'
(second-to-last)Index -3:
'bromine'
(third-to-last) ← start hereIndex -4:
'chlorine'
Index -5:
'fluorine'
Alternative solution: You could also use [2:]
(from index 2 onwards), but [-3:]
is more flexible - it works regardless of list length.
Part (c): Every second element#
Goal: Extract ['fluorine', 'bromine', 'astatine']
print(halogens[::2])
['fluorine', 'bromine', 'astatine']
Explanation:
The slice [::2]
means “from start to end, stepping by 2”.
The full slice syntax is [start:stop:step]
:
start
: where to begin (default: beginning)stop
: where to end (default: end)step
: how many indices to jump (default: 1)
So [::2]
takes:
Index 0:
'fluorine'
Index 2:
'bromine'
Index 4:
'astatine'
Other examples:
print(halogens[1::2]) # Every second element starting from index 1
print(halogens[::3]) # Every third element
['chlorine', 'iodine']
['fluorine', 'iodine']
Part (d): Using a negative step#
Goal: Discover what [::-1]
does
print(halogens[::-1])
['astatine', 'iodine', 'bromine', 'chlorine', 'fluorine']
Explanation:
Using -1
as the step value reverses the list!
[::-1]
means:
Start: default (end of list when step is negative)
Stop: default (beginning of list when step is negative)
Step: -1 (move backwards by 1)
This is a very common Python idiom for reversing sequences.
Other negative steps:
print(halogens[::-2]) # Every second element, reversed
['astatine', 'bromine', 'fluorine']
Exercise 2: Calculating Interatomic Distances#
Problem: For each molecule, calculate the intramolecular distances between all three atoms using the formula:
Then comment on the shape of each molecule.
Molecule 1#
Given coordinates:
Atom 1: (0.1, 0.5, 3.2)
Atom 2: (0.4, 0.5, 2.3)
Atom 3: (-0.3, 0.3, 1.7)
First, define the atomic positions:
atom_1 = [0.1, 0.5, 3.2]
atom_2 = [0.4, 0.5, 2.3]
atom_3 = [-0.3, 0.3, 1.7]
Now calculate all three pairwise distances:
# Distance between atoms 1 and 2
r_12 = sqrt((atom_1[0] - atom_2[0])**2 + (atom_1[1] - atom_2[1])**2 + (atom_1[2] - atom_2[2])**2)
# Distance between atoms 1 and 3
r_13 = sqrt((atom_1[0] - atom_3[0])**2 + (atom_1[1] - atom_3[1])**2 + (atom_1[2] - atom_3[2])**2)
# Distance between atoms 2 and 3
r_23 = sqrt((atom_2[0] - atom_3[0])**2 + (atom_2[1] - atom_3[1])**2 + (atom_2[2] - atom_3[2])**2)
print(f"Distance between atoms 1 and 2: {r_12:.4f}")
print(f"Distance between atoms 1 and 3: {r_13:.4f}")
print(f"Distance between atoms 2 and 3: {r_23:.4f}")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[10], line 2
1 # Distance between atoms 1 and 2
----> 2 r_12 = sqrt((atom_1[0] - atom_2[0])**2 + (atom_1[1] - atom_2[1])**2 + (atom_1[2] - atom_2[2])**2)
4 # Distance between atoms 1 and 3
5 r_13 = sqrt((atom_1[0] - atom_3[0])**2 + (atom_1[1] - atom_3[1])**2 + (atom_1[2] - atom_3[2])**2)
NameError: name 'sqrt' is not defined
Explanation of the formula:
For each distance calculation:
atom_1[0]
accesses the x-coordinate of atom 1atom_1[1]
accesses the y-coordinate of atom 1atom_1[2]
accesses the z-coordinate of atom 1
We calculate:
\((x_1 - x_2)^2\) using
(atom_1[0] - atom_2[0])**2
\((y_1 - y_2)^2\) using
(atom_1[1] - atom_2[1])**2
\((z_1 - z_2)^2\) using
(atom_1[2] - atom_2[2])**2
Sum these three values
Take the square root
Analysis of Molecule 1:
The distance \(r_{13}\) (1.565) is not equal to the sum \(r_{12} + r_{23}\) (1.892).
In fact, \(r_{13} < r_{12} + r_{23}\), which means the three atoms are not collinear (not in a straight line).
Conclusion: Molecule 1 has a bent geometry. Atom 2 sits at an angle between atoms 1 and 3.
This is similar to the structure of water (H₂O) or sulfur dioxide (SO₂), where the central atom forms an angle with the two outer atoms.
Molecule 2#
Given coordinates:
Atom 1: (-0.1, 0.5, 1.5)
Atom 2: (0.2, 0.5, 2.6)
Atom 3: (0.5, 0.5, 3.7)
Define the atomic positions:
atom_1 = [-0.1, 0.5, 1.5]
atom_2 = [0.2, 0.5, 2.6]
atom_3 = [0.5, 0.5, 3.7]
Calculate the distances:
r_12 = sqrt((atom_1[0] - atom_2[0])**2 + (atom_1[1] - atom_2[1])**2 + (atom_1[2] - atom_2[2])**2)
r_13 = sqrt((atom_1[0] - atom_3[0])**2 + (atom_1[1] - atom_3[1])**2 + (atom_1[2] - atom_3[2])**2)
r_23 = sqrt((atom_2[0] - atom_3[0])**2 + (atom_2[1] - atom_3[1])**2 + (atom_2[2] - atom_3[2])**2)
print(f"Distance between atoms 1 and 2: {r_12:.4f}")
print(f"Distance between atoms 1 and 3: {r_13:.4f}")
print(f"Distance between atoms 2 and 3: {r_23:.4f}")
Distance between atoms 1 and 2: 1.1402
Distance between atoms 1 and 3: 2.2804
Distance between atoms 2 and 3: 1.1402
Analysis of Molecule 2:
Check if the atoms are collinear:
The distance \(r_{13}\) (2.280) is equal to the sum \(r_{12} + r_{23}\) (also 2.280).
Notice also that \(r_{12} = r_{23}\) = 1.140 - the two bonds are the same length!
Conclusion: Molecule 2 has a linear geometry. The three atoms are arranged in a straight line, with atom 2 positioned exactly midway between atoms 1 and 3.
This is similar to the structure of carbon dioxide (CO₂) or beryllium chloride (BeCl₂), where a central atom forms 180° bonds with two identical atoms.|
Summary#
1. List Slicing#
[:n]
- first n elements[-n:]
- last n elements[::step]
- every step-th element[::-1]
- reverse the list
2. Molecular Geometry from Coordinates#
For a triatomic molecule ABC:
Linear geometry: \(r_{AC} = r_{AB} + r_{BC}\)
Atoms arranged in a straight line
Bond angle = 180°
Example: CO₂
Bent geometry: \(r_{AC} < r_{AB} + r_{BC}\)
Atoms form an angle
Bond angle < 180°
Example: H₂O, SO₂
The distance formula in 3D:
This is the 3D extension of Pythagoras’ theorem.