10.3. Notes on Python Coding Style (under construction)#

Restrictions on characters used in names#

TL;DR: Mostly letters and digits.

  • The names of Python variables (including the names of functions) must start with a letter and only contain letters, digits, and the underscore _ (typed as “shift-dash”). That is, no dashes (-), spaces or other punctuation.

  • The names of files containing Python modules must follow the above restrictions (apart from the period in the suffix “.py” of course) because these names are used as variable names in import statements.

  • The names of notebook files have just a little more flexibility: they can also include hyphens. That is, they should only contain letters, digits, underscores and hyphens; no spaces or other punctuation. (Again, apart from the period in the suffix “.ipynb”.)
    Aside: this is roughly the same rule as for web-site addresses.
    Although you can get away with some other characters in some situations, that will run into problems in situations like cross-referencing to a notebook from another notebook, posting on a web-site, and using a notebook as a section in a Jupyter Book.

Naming style#

In this book names are sometimes a description formed from several words, and since spaces are forbidden, this is generally done by using camelCase; for example, an error estimate might be in a variable errorEstimate. (Another popular style is to use the underscore as a proxy for a space, as with error_estimate.)

One place where underscores are used in names is where the corresponding mathematical notation would have a subscript; for example, the mathematical name \(x_0\) becomes x_0. (This mimics the LaTeX notation for subscripts.)