Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

DEMO: First Steps with Python

The page has five stations, and each one introduces exactly one library at the moment it becomes useful: physical constants, then arrays and plots, then symbolic math.

1. Python as a scientific calculator

Our first import: scipy.constants knows every physical constant, so you never mistype one (full list). One line of code, and hh, cc, ee, kBk_B, NAN_A are all at your fingertips:

A green laser pointer emits 532 nm light. What is the energy of one of its photons? Type the formula E=hc/λE = h c / \lambda almost exactly as you would write it:

Joules are inconvenient at the atomic scale, so chemists quote photon energies in electron volts (divide by the electron charge) or kJ/mol (multiply by Avogadro’s number). Unit conversion is just arithmetic:

A mole of green photons carries about 225 kJ, and a typical C-C single bond costs 347 kJ/mol. Can light break a bond? Drag the slider and find out. Making an interactive control is one line of code:

Notice the chemistry hiding in this toy: visible light cannot touch a C-C bond, but ultraviolet light below roughly 345 nm can. That is why UV radiation damages molecules and sunscreen exists.

2. The de Broglie wavelength, in three lines

The same calculator settles a chapter question: why do electrons diffract but baseballs do not? Compute λ=h/mv\lambda = h / mv for both. For the electron we use its speed in the ground state of the Bohr model Bohr, 1913, v2.19×106v \approx 2.19 \times 10^6 m/s:

3. Functions: the hydrogen spectrum

When a formula gets used more than once, wrap it in a function. The Rydberg formula predicts every line hydrogen emits when an electron falls from level nhin_{hi} to nlon_{lo}:

1λ=RH(1nlo21nhi2)\frac{1}{\lambda} = R_H \left( \frac{1}{n_{lo}^2} - \frac{1}{n_{hi}^2} \right)

Now the function powers a live energy-level diagram. Pick a series and an upper level; the diagram draws the transition and colors the emitted photon:

Gray arrows mean the photon lies outside the visible window. Only the Balmer series lands in it, which is why hydrogen discharge tubes glow red-pink: you are looking mostly at the 656 nm line you computed above.

4. Plotting: blackbody radiation

Two more imports, and they are the workhorses of all scientific Python: numpy handles arrays of numbers, and matplotlib turns them into figures.

Planck’s law Planck, 1901 started quantum mechanics. Typed as code, it is three lines:

The default 5800 K is the surface of the Sun: its peak sits right in the visible band (evolution is not a coincidence). Cool the slider to 3000 K, a light bulb filament, and watch the peak slide into the infrared, which is why incandescent bulbs waste most of their energy as heat.

5. Symbolic math: let the computer do calculus

Our final import is sympy, an entire computer algebra system:

The dashed “peak” line obeys Wien’s law, λmaxT=b\lambda_{max} T = b. In 1900 deriving it took real work; sympy differentiates Planck’s law and finds the root for you. One standard physics trick first: substitute the dimensionless variable x=hc/λkBTx = hc / \lambda k_B T, so Planck’s law becomes a clean scale-free shape x5/(ex1)x^5 / (e^x - 1) and every temperature collapses onto one curve:

That output is not a decimal: sympy solved the transcendental equation exactly, in terms of the Lambert W function. Turned into numbers, it hands us Wien’s constant:

The result matches the dashed line in the plot above and scipy’s tabulated constant to four digits. You will use the same maximize-by-differentiating trick later to locate maxima of probability densities.

6. Complex numbers: a 30-second preview

Quantum wavefunctions are complex-valued, so Python treats complex numbers as first-class citizens (1j is the imaginary unit). Take z=1+3iz = 1 + \sqrt{3}\,i:

The quantity zz=z2z z^* = |z|^2 is exactly how probability densities are built from wavefunctions in Chapter 3.

References
  1. Bohr, N. (1913). On the Constitution of Atoms and Molecules. Philosophical Magazine, 26(151), 1–25. 10.1080/14786441308634955
  2. Planck, M. (1901). Über das Gesetz der Energieverteilung im Normalspectrum. Annalen Der Physik, 309(3), 553–563. 10.1002/andp.19013090310