Math with Python

Type a few lines of Python on your phone, press Run, and the answer appears underneath. That is the whole of it — and it goes a great deal further than a scientific calculator.

Four lessons follow. Every example in them was run against the exact Python that runs on your device, and every output shown is the output it produced.

What people use it for

  • Arithmetic that would beat a calculator. Exact fractions, a thousand digits of π, factorising a number, solving an equation for x and getting an answer in symbols rather than a decimal.
  • Algebra and calculus, worked symbolically. Differentiate, integrate, solve a differential equation, invert a matrix — and see the expression, not a rounded number. Ask for the integral of the square root of one over x and the answer comes back exact:
1xdx=2x+C\int \sqrt{\frac{1}{x}}\, dx = 2\sqrt{x} + C
  • Measurements turned into answers. Fit a curve to readings, find where something crosses zero, run a statistical test.
  • Charts. A line, a scatter, bars, a histogram, several on one grid.
  • Testing a piece of Python. Paste it in, run it, look at what came back.
  • Learning Python at all. Small pieces, run one at a time, each result visible before the next.
  • Writing something up. Text and code in one document, in order — the explanation beside the working that produced it, with the charts in place.

Start in a CodeBook

A CodeBook is a document made of cells — small blocks you fill in and run one at a time, working down the page. Some hold Python, some hold writing, some show a picture. (If you have met the idea elsewhere, it is a notebook.)

  1. Make one from the + menu — CodeBook. It opens empty.
  2. Press the + and choose Code. You get a cell, already set to Python.
  3. Type 2 ** 100 and press Run. The answer appears under the cell.
  4. Press + again for the next one. Code runs; Text is prose for explaining what you are doing. Those two are all you need for a long time.
  5. Image displays a picture a code cell produced — this is how charts appear. The + also offers Agent, Sheet, Chart and Figure cells, which the CodeBook Tour covers.

Variables stay alive from one cell to the next. Define x in the first cell and every cell below it can use x. That is what makes this good for learning and for writing something up: the document is the sequence of steps, and you can go back, change one, and run it again.

If you have used Jupyter or Colab

It is the same idea and mostly the same keys — a cell runs with the Run button or ⇧↵, and the interpreter keeps its state between cells. Four differences worth knowing:

  • Python runs on the device you are holding, not on a machine somewhere else. Nothing to start, nothing to reconnect to, no session that expires while you read — and once the libraries are there it works with no network.
  • Your own machines do the heavy lifting when you want it, per cell, rather than whatever hardware you are allocated. See the cluster above.
  • Each cell picks its own language. A notebook can be Python throughout, or mix in JavaScript or another language where that is easier.
  • The same document opens as a workflow on a canvas, where the cells are connected steps, and can be put on a schedule. See One document, two views.

Bringing notebooks with you: File → Import… turns a notebook file into a CodeBook. There is more detail in Coming from Jupyter.

You can also write Python in a plain code document — one file, one Run button, each run starting fresh. Better for a script you intend to keep than for a lesson.

The four lessons

Take them in order if the subject is new to you; NumPy makes the other three easier.

  1. NumPy — Array Math — treating a whole list of numbers as one thing instead of looping over it. The foundation the rest stand on.
  2. Symbolic Math with SymPy — algebra, calculus and exact answers. The one that makes a phone better than a calculator.
  3. Matplotlib — numbers into pictures, from one line to a grid of charts.
  4. SciPy — fitting, solving and statistics on real measurements.

Pictures need an image cell

A code cell shows text. To see a chart, put an image cell directly beneath the code cell that made it, and the picture appears there. The Matplotlib lesson does this in its first example.

In a plain code document a chart comes back as data rather than as a picture, so use a CodeBook when you want to see something.

The first run of a library

Each library is fetched the first time you use it, so that first run needs a connection and takes a moment. Every run after that on the same device starts straight away.

NumPy is the exception: it ships with Circuitry, so it works with no connection at all, first run included.

One CodeBook, a compute cluster behind it

Everything on these pages runs on the thing you are holding — phone, tablet or computer. No machine to rent, no queue, no session that expires while you are reading.

Connect your machines with Circuit and the same CodeBook becomes a compute cluster:

  • Per-cell placement — each cell runs where you send it: this device, this computer, or a named machine of yours.
  • Parallel execution — with Fork and Join, independent branches run on different machines at once.
  • Mix of hardware — the GPU box trains the model, general-purpose machines handle everything else, and the phone in your hand drives all of it.
  • Big data science — spread a large dataset across machines instead of waiting on one CPU.

Serious hardware, then, without renting any: the tower in the other room does the work while you sit with the CodeBook.

One thing worth knowing if you put a CodeBook on a schedule: a workflow running in the cloud has plain Python and its standard library, so a step that uses these libraries belongs on your device or on one of your machines.