Reproducible Python experiments
A reproducible experiment records the inputs, code, environment and expected result so another person can repeat it. A notebook is convenient for exploration, but its kernel can retain earlier values. A script launched in a new Python process starts with fresh program state. Neither format alone guarantees reproducibility; dependencies, data and execution order matter.
Calculate a mean with a clear input contract
The arithmetic mean is the sum of numbers divided by their count. Use the three invented processing times from the previous lesson: 4, 8 and 6 minutes. Their sum is 18 minutes; dividing by three gives 6 minutes. We define the input as a list of integers and reject an empty list because there is no defined arithmetic mean for zero observations.
def mean(values: list[int]) -> float: if len(values) == 0: raise ValueError("mean requires at least one value") return sum(values) / len(values)
print(mean([4, 8, 6]))print(mean([2]))try: mean([])except ValueError as error: print(error)sum adds the values, len counts them, and / performs true division. The annotations list[int] and float describe the intended input and output types; Python does not automatically validate values against these annotations. raise ValueError signals a violation of the empty-input contract. try runs code that might fail and except ValueError handles that particular exception. The output is 6.0, 2.0, then mean requires at least one value. An exception is different from returning a numeric answer; zero would incorrectly suggest an observed average.
Run a clean script
In a local folder you control, save the preceding example as mean_demo.py. With an installed Python 3 interpreter, check python --version and run python mean_demo.py twice. On systems where the command is python3, substitute it in both commands. The authoring checks used Python 3.13.5; no third-party packages were needed. Each run should display the same three lines. These are local terminal instructions, not ByteMind runner commands. A fresh process does not eliminate external state if a program reads changing data, a clock or other services.
Why notebook cell order matters
Consider two illustrative cells in a notebook. Cell A contains values = [4, 8, 6]; Cell B contains print(sum(values) / len(values)). Run A then B: the result is 6.0. Now edit A to values = [2, 4] but do not execute it. Executing only B still prints 6.0, because the kernel retains the old value. Run A and B in order to get 3.0. Restart the kernel and execute all required cells from top to bottom; if B runs first in a fresh kernel, values has not been defined and a NameError occurs. Notebook menu wording depends on the installed interface. This is a manual walkthrough, not an executed notebook test.
Record the environment and dependencies
A module is a unit of Python code that can be imported, such as the standard-library statistics module. An import makes a module or selected names available; it does not install a package. Third-party libraries have versions and may depend on other packages. A virtual environment separates a project’s Python package installation from other environments; it does not provide a security sandbox or guarantee identical versions. For this no-dependency experiment, record the Python version, the exact function and input list, units, and expected output. For later library experiments, record dependency versions and relevant hardware, random seeds and numerical tolerance as well. A seed does not guarantee identical results across all versions or devices.
Practice: reproduce a small summary
Create a fresh script with the mean function and the fixed values [3, 9, 12]. Predict the output before running: the sum is 24 and the mean is 8.0. Repeat with [0] and an empty list; distinguish an observed zero from invalid input. Record the interpreter version, inputs and expected values, and check that both independent script runs agree. For the notebook scenario, explain why changing text without executing the cell is not enough. Next, learn to distinguish samples, features, targets and missing values.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reproducible Python experiments — understanding check
Apply Python data and reproducibility concepts to small synthetic examples.
What is the arithmetic mean of [3, 9, 12]?
Why does the example raise ValueError for an empty list?
Cell A was executed, then edited without rerunning it. When Cell B reads values, which value does it use?
What does the annotation list[int] accomplish by itself?
Which record best supports repeating the mean experiment?