Python for Data Science

The Data Science unit on the language and libraries that every later article runs on. How Python and Jupyter execute code and why cell order matters; the built-in types, with floating-point rounding derived from the binary64 format; functions, arguments, scope and closures; installing, versioning and pinning packages; and NumPy, pandas and matplotlib, each taken apart through behavior that gives a wrong answer without an error, checked under the library versions current in November 2026.

Python is the common language of data science because of its libraries rather than its speed: a readable general-purpose language for the glue, and compiled array libraries, with NumPy at their base, for the arithmetic (Harris et al., 2020; McKinney, 2022). This unit covers the language as analysis code uses it, the tools for installing and versioning its libraries, and the three packages that almost every later article relies on: NumPy for arrays, pandas for tables and matplotlib for plots. The examples run on the adults examined in the 2017 to 2018 National Health and Nutrition Examination Survey (NHANES), the running data of the whole collection.

The unit is organized around one kind of failure: code that runs, prints a plausible number and is wrong. Three notebook cells print 16.85, 2.25 or 126.36 depending on the order they are run in. A float makes round(2.675, 2) equal 2.67. A default argument is created once and shared by every call. Respondent numbers squared in 32-bit integers sum to a ninth of the true total. On a sorted table loc[0:2] returns 1,022 rows. The same two lines of pandas change a table under pandas 2.2 and leave it alone under 3.1, because of copy-on-write. A plot title lands on the wrong panel. Each article explains its case from the rules of the language or library, and its worked problems derive the numbers from first principles: the stored value of 0.1 from the binary64 format, the int32 sum from modular arithmetic, a display coordinate from matplotlib's defaults.

Library behavior changes between versions, so every article states the versions its script ran with. The scripts were rerun on 23 November 2026 under Python 3.13.5 with NumPy 2.2.5, pandas 2.2.3 and matplotlib 3.10.1, and again with pandas 3.1.1 and matplotlib 3.11.2, the current releases on that date, and NumPy 2.5.4. Some checks also used pandas 2.3.3 and a Python 3.11.0 interpreter.

Articles, in order

  • Programming with Python and Jupyter: where Python came from and why data science uses it; compilation to bytecode, names bound to objects and dynamic typing; the Jupyter kernel, cells and execution counts; hidden state, shown by one notebook that prints three different means; and the evidence of Pimentel et al. (2019) on how few public notebooks reproduce.
  • Python Data Types: unlimited integers and Booleans that count; IEEE 754 binary64 floats, with the stored value of 0.1, the lost 1 in 1e16 + 1 derived from the format, and the bounded error of rounding half to even proved; why sum([0.1] * 10) differs between Python 3.11 and 3.13; NaN; strings against bytes; the four containers, their costs and hashability; and aliasing.
  • Python Functions: how arguments are matched to parameters; defaults evaluated once; pass by assignment, and what a function can change in its caller's DataFrame; scope by the LEGB rule; closures and their late binding; functions as arguments; and type hints that Python does not check.
  • Data Science Packages: what import does and how a local file can replace a library; pip, virtual environments and conda; PEP 440 version specifiers and what a range does and does not guarantee; one script run under three versions of pandas; and a map of the core packages.
  • The NumPy Package: the array as memory with a dtype, shape and strides; integer overflow and float32 cancellation, each traced to its exact source; vectorization measured; broadcasting; views and copies; least squares and the condition number; and seeded random generation, with the Monte Carlo error of a bootstrap measured over 30 seeds.
  • The pandas Package: Series, DataFrame and Index; automatic alignment by label; float NaN, nullable types and the str dtype of pandas 3; loc against iloc; Boolean masks and operator precedence; copy-on-write and the end of chained assignment; method chains; and the cost of row-wise apply.
  • The matplotlib Package: the backend, Artist and scripting layers; one figure taken apart into its 144 Artists and accounted for; the explicit and implicit interfaces; transforms from data to pixels, computed by hand; backends; repeatable SVG output; and figures left open.

The order runs from the language to its libraries. The first article says how code runs and why the order of execution matters; the next two cover the values and functions that analysis code is built from; the fourth explains how libraries are installed and versioned; and the last three take the libraries one at a time, each building on the one before: pandas stores its columns in NumPy arrays, and matplotlib draws them. Each article ends with worked problems that extend it and with open questions that an experiment or a source could settle.

Where the site goes deeper

Several results these articles use are proved elsewhere on the site. The Geometry of Least Squares shows how much accuracy the normal equations lose on a badly conditioned fit, and The Singular Value Decomposition in Statistics treats the decomposition behind np.linalg.lstsq. The Bootstrap Method derives the Monte Carlo error of a bootstrap standard error that the NumPy article measures, and Summarizing Data with pandas compares the divisors that pandas and NumPy use for a standard deviation. The pandas article's operations are put to work across the Data Wrangling unit, from Introduction to Data Wrangling to Data Engineering, which also describes how this site's scripts pin their input files by hash. The matplotlib article's companion on design is Best Practices for Visualizing Data.

What lies underneath all seven articles is numerical computing, which the site does not yet treat on its own. The subjects a later collection could hold include floating-point arithmetic and the error of summation, stable algorithms for variance and least squares, numerical linear algebra, pseudorandom number generators and the seeding of independent streams, and the testing of numerical code.

References