Python Lab
From one thermometer reading to judging a measurement
Learn data analysis with Python in eight small steps, right here in the browser. A cup of tea is cooling. Nothing to install.
A cup of tea is cooling on your desk, and you are the one measuring it. In each step you guess first, then try, and the picture grows with you. About 45 minutes. Skip what you already know.
1One reading
I know this → skipThe thermometer shows a number. Let's give it a name.
Why does this matter?
Every measurement is a number with a unit: 78.3 °C. Giving it a name (a variable) is the first step of every analysis, from a school lab to a space probe.
The tea reads 78.3 °C and the room 21 °C. Which number tells you how fast the tea is cooling right now?
The hotter the tea is above the room, the faster it cools: that gap is what Newton's law is about. You just computed it: 57.3 °C.
Change the number. Press Run Code.
Your turn
Fill in the blank (___). Press Run Code.
Stuck? Show the answer
gap = T - roomNow you can store a measurement in a variable and calculate with it.
One reading says nothing about cooling. We need many.
2 · Many readings2Many readings
I know this → skipEvery 15 seconds, one more reading. A row of numbers with one name is an array.
Why does this matter?
A data set is a list of readings tied to time. A NumPy array does arithmetic on the whole list at once, which is why scientists use it instead of plain lists.
The tea starts at 78.2 °C and the last of 6 readings, 75 s later, is 62.9 °C. About how much did it cool in total?
15.3 °C in 75 s, and you got it with T[0] - T[-1]. Keep the number: in the next chapters you will see that the cooling slows down.
Change a reading. Press Run Code.
Your turn
Fill in the blank. Press Run Code.
Stuck? Show the answer
drop = T[0] - T[-1]Now you can keep many readings in an array and ask it questions.
Numbers in a row are hard to read. Let's look at them.
3 · See it3See it
I know this → skipOur eyes read a picture faster than a column of numbers.
Why does this matter?
Look at your data before you compute anything. A graph shows in a second what a column of numbers hides: trends, outliers, mistakes.
Plot temperature against time. What shape do you expect?
A curve that flattens: fast while the tea is hot, slow when it is close to room temperature. A straight line would mean a constant cooling speed.
Change the marker. Press Run Code.
Your turn
Fill in both blanks. Press Run Code.
Stuck? Show the answer
plt.plot(t[:10], T[:10], "o")Now you can turn numbers into a graph with labelled axes, and cut out the part you want.
The points are not exactly on a curve. Each reading wobbles. By how much?
4 · Wobble4Wobble
I know this → skipRead the same temperature twice and you get two numbers. The typical gap between them is σ (sigma).
Why does this matter?
Every instrument wobbles. σ, the standard deviation of repeated readings, is how a physicist puts a number on that wobble. It is the error bar in your lab report.
Drag the wobble σ to the right. What do you expect to see?
The bars are ± σ: more wobble, longer bars, more scatter. The curve behind the points does not change. And repeated readings give you σ: your 30 readings of the bath came out close to the true 0.4 °C.
Drag the slider. The bars on the points are ±σ.
How much one reading can be off, in °C. Here every point has a bar of ± σ. More wobble: messier points.
Your turn
Fill in the blank. Press Run Code, then again: every run is a new set of readings.
Stuck? Show the answer
sigma = T.std()Now you can measure the wobble of a reading: σ is the standard deviation of repeated readings.
Now we know how much the points wobble. Which curve passes through them?
5 · Fit5Fit
I know this → skipNewton: the gap between tea and room shrinks by the same fraction every second. Python finds the one number that matters: τ.
Why does this matter?
Newton's law of cooling says the gap to the room shrinks by the same fraction in equal times. The same exponential describes a discharging capacitor, radioactive decay, and caffeine leaving your blood.
Set the cooling time to 400 s: the tea cools more slowly. What will the fit find?
The fit finds the τ the tea really had, within its ±. That is what fitting is for. Now use it: how long until the tea is drinkable?
Drag the slider: the fit follows. Then read the code.
How slowly the tea cools: after τ seconds the gap to room temperature has shrunk to about 37%. Larger τ means slower cooling. This is the number the fit has to find.
The same thing in code
Change a number. Press Run Code.
Your turn
Use the fit to predict. Fill in the blank (use np.log). Press Run Code.
Stuck? Show the answer
t60 = -tau * np.log((60 - Ta) / (T0 - Ta))Now you can fit a law to data, read τ with its uncertainty, and use it to predict.
A fit always returns numbers, even for a wrong law. How do you know it is good?
6 · Judge6Judge
I know this → skipThe residuals (data − fit) and χ² tell you whether to believe a fit.
Why does this matter?
A fit with a number is not yet a fit you can trust. χ² compares the scatter of the data with the scatter the error bars promise. Around 1 means they agree.
You will fit three laws to the same readings. Which one gets the red light?
newton gets green (χ² ≈ 0.7). almost gets amber: to the eye the curve looks fine, but the residuals show a pattern. wrong gets red. χ² sees what the eye misses.
Change the word in quotes. Press Run Code. Watch the light.
Your turn
Compute the number behind the light. Fill in the blank. Press Run Code.
Stuck? Show the answer
chi2 = np.sum(residuals ** 2)Now you can tell a good fit from a bad one: the residuals look like noise and the reduced χ² is about 1 (green).
The fit looks good. But is the ± it reports honest? Repeat the experiment and see.
7 · Repeat7Repeat
I know this → skipDo the experiment again, and again. If the fit's ± is honest, τ scatters by about that much. Real noise often has memory: each reading remembers the one before.
Why does this matter?
Repeating a virtual experiment many times, to see how the result scatters, is called a Monte Carlo. When you cannot repeat the real experiment a hundred times, the computer can.
The fit says τ = 240 ± 3 s. Repeat the experiment 100 times when the noise has memory 0.9. How much does τ really scatter?
About twice as much. With memory the fit's ± is too optimistic, even though τ is still right on average.
Same experiment, again and again. Change a number in the code. Press Run Code.
Challenge. Which memory makes the fit overconfident by a factor 2? The fit gets τ right on average. What does it get wrong?
Now you can check whether a fit's uncertainty is honest by repeating the experiment on the computer (a Monte Carlo).
Memory fools the fit. How does it look inside real data?
8 · Noise8Noise
I know this → skipNoise is not only how much. It can have memory. Look at the residuals, then at your own data.
Why does this matter?
Real instruments have inertia and drift, so one reading remembers the last. When that happens, averaging N readings helps less than the usual σ/√N promises.
In the residuals, the chip shows r₁, how much each value resembles the one before. If the noise were pure chance, what would r₁ be?
About 0, within ± 2/√N. The sample data show r₁ = +0.75: the wobble has memory, so the ± of a plain fit is too optimistic.
Drag the sliders. The chip is the memory of the noise.
How much each noise value remembers the one before. 0: every reading is independent (white noise). Near 1: the noise wanders slowly. The spread of the values stays the same; only the order changes.
A digital thermometer shows steps, not every value. With a big step the histogram turns into teeth; the rounding alone adds a wobble of about step/√12.
Your turn
Your data. A cooling measurement is below. Press Run Code; then paste your own (time, temperature) lines in place of it.
If the chip is amber or red, the wobble of your data has memory: the ± of the fit is too optimistic.
Now you can read the noise in real data: how much it wobbles, whether it has memory, and what that does to the uncertainty.
That is the whole path.
The endWhere you are now
- Store a measurement in a variable and calculate with it.
- Keep many readings in an array and ask it questions.
- Turn numbers into a graph with labelled axes.
- Measure the wobble σ of a reading.
- Fit a law, read τ ± its uncertainty, and predict with it.
- Judge a fit with the residuals and the reduced χ².
- Repeat an experiment on the computer to test the uncertainty.
- Read the noise in real data: wobble and memory.
Want to go deeper? Play with the noise itself: its memory, the thermometer step, and how much averaging really helps.
Noise Lab →
NoteWords
- variable: a name for a value, like
T = 78.3. - array: many values in a row, with one name.
- σ (sigma), noise, wobble: how much one reading can be off. The bars on the points.
- fit: finding the numbers that bring a curve closest to the data.
- cooling time (τ, tau): how fast the tea cools. Larger means slower.
- residual: data minus fit, point by point.
- χ² (chi-squared): around 1 (green) the fit agrees with the bars; higher (amber, red) something is off.
- memory (autocorrelation): how much a noise value resembles the one before.
- repeat (Monte Carlo): do the experiment many times on the computer and look at the scatter of the result.
- function, lambda, dictionary: a recipe with inputs; a tiny unnamed recipe; a labelled collection like
laws["newton"].
NoteAbout this page
Python runs inside your browser (Pyodide, WebAssembly): nothing you type is sent to a server. The first visit downloads tens of MB of Python files and page libraries from a public content delivery network (jsDelivr), which can see your IP address; later visits use the browser cache. The data are simulated.