LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 35 lessons

Lesson 1 of 8

Module 1 · Python basics

Setting up Python and Jupyter

Setting up Python and Jupyter

Setting up Python and Jupyter

Python is the most widely used language for data science and machine learning, and a major one in bioinformatics. This lesson gets it running and has you write your first lines.

⏱ 12 min readModule 1 · Python basicsFree
Lesson at a glance4 steps · about 12 min
  1. Two ways to start
  2. Notebooks and cells
  3. Your first lines
  4. Naming values

Two ways to start

Before you can write Python you need two things: the Python interpreter, the program that reads your code and carries it out line by line, and a place to type and run that code. There are two common routes, and both lead to the same language. Code you write in one runs unchanged in the other.

This module builds the working basics you will lean on for the rest of the course: running code, storing values, reading tables, cleaning them, drawing a first figure and handling sequences. It ends with a small project that ties these together into one report. Nothing here assumes you have programmed before; each lesson adds one layer to the last.

Google Colab
  • Runs in your browser; nothing to install
  • Needs a Google account
  • Most libraries are already there
  • Best for getting started today
On your own computer
  • Install Python from python.org, or the Anaconda distribution
  • Install the libraries with pip (below)
  • Work in JupyterLab or VS Code
  • Best for long-term work and private data

Choosing between them

  • Colab runs your code on a computer owned by Google, not on your own machine. That is why there is nothing to install, and also why you should not upload data you are not allowed to share, such as identifiable patient records.
  • Your own computer keeps the data where it is. The Anaconda distribution is a single installer that bundles Python with many scientific libraries already included, which saves a few steps; installing from python.org gives you Python alone, and you add libraries yourself.
  • JupyterLab and VS Code are the programs you type into. JupyterLab is built around notebooks; VS Code is a general code editor that can also open notebooks.

A library (Python also calls it a package) is a collection of ready-made code that someone else has written and tested. Python itself knows how to add numbers; libraries add tables, figures and sequence tools on top.

One pitfall catches many people on their own computer. If more than one copy of Python is installed, which is common on a Mac or after installing Anaconda alongside another Python, pip can put a library into one copy while your notebook runs another. The notebook then reports that the library does not exist, even though the install seemed to work. Inside a notebook, writing %pip install biopython in a cell avoids this: the %pip form installs into the same Python the notebook is using.

Terminal, or a Colab cell starting with !
pip install jupyterlab numpy pandas matplotlib seaborn biopython

In Colab, put an exclamation mark first (!pip install biopython) to run it from a notebook cell. The other libraries are already installed there.

What this does

pip is Python's package installer. The word install tells it what to do, and each word after that is the name of one library to download and set up. pip fetches them from the Python Package Index, the public online collection of Python libraries. In order, the six names are:

  • jupyterlab: the notebook program described below.
  • numpy: fast arrays of numbers (NumPy, pandas and files).
  • pandas: tables with named columns (NumPy, pandas and files and Cleaning and combining data).
  • matplotlib and seaborn: figures (Figures with matplotlib and seaborn and Module 2).
  • biopython: reading and handling biological sequences (Sequences with Biopython).

The exclamation mark in Colab tells the notebook to pass the line to the computer's command line instead of treating it as Python: pip is a separate program, not a Python command. In a terminal on your own computer you type the line without the mark. You only need to install a library once per computer; in Colab, which starts each session on a fresh machine, you repeat the install whenever you open the notebook again.

You can also ask pip for an exact version, by writing two equals signs and a version number after the name, such as pip install pandas==2.2.2. You rarely need this when learning. It matters when you repeat someone else's analysis, because libraries change between versions and a function can behave differently or disappear. Writing down the versions you used, as the research box below suggests, is what lets someone install the same ones later.

Notebooks and cells

Colab and JupyterLab use notebooks made of cells. A code cell runs when you press Shift+Enter, and its output appears underneath. Text cells hold your notes, so a notebook is a record of your analysis.

Behind every notebook sits a kernel: the running Python program that carries out your cells and keeps everything they create in memory. When a cell runs, a number appears beside it in square brackets, such as [3]. That number records the order in which cells were run, which is not always the order in which they sit on the page. Notebook files end in .ipynb and store the code, the text and the last outputs together, so you can send one to a colleague and they see exactly what you saw.

A good habit from the first day: put a short text cell above each step saying what it does and why. Six months later, those notes are what let you, or a reviewer, follow the analysis.

Text cells are written in Markdown, a simple way of marking up plain text: a line starting with # becomes a heading, and words between single stars become italic. You do not need to learn much of it; headings and short sentences are enough. Remember too that the output shown under a cell is from the last time that cell ran. If you change the code and do not run it again, the output on the page no longer matches the code above it.

The notebook format is described in Kluyver et al. (2016), "Jupyter Notebooks: a publishing format for reproducible computational workflows", in the conference book Positioning and Power in Academic Publishing (IOS Press). The language itself is documented at docs.python.org. A notebook alone does not make work reproducible. A large study of public notebooks on GitHub (Pimentel et al., 2019, International Conference on Mining Software Repositories) found that only about a quarter ran from top to bottom without errors, and far fewer gave the same results again; cells run out of order and missing lists of libraries were common causes. When you report work, give the Python version (import sys; print(sys.version)) and the version of each library.

Your first lines

Python
print(2 + 3)
print(10 / 4)
print(2 ** 10)        # ** means "to the power of"

import math
print(math.log2(8))
5 2.5 1024 3.0

What this does

  • print(2 + 3): print() is a function, a named piece of code that does one job. Whatever sits inside its round brackets is worked out first, here 2 + 3, and the result is shown below the cell.
  • print(10 / 4): a single slash is ordinary division. It always gives a number with a decimal point, even when the answer is whole.
  • print(2 ** 10): two stars raise a number to a power, so this is 2 multiplied by itself ten times. Everything after the # is a comment: a note for people, which Python ignores.
  • import math: loads math, a module that comes with Python and holds mathematical functions. After the import, its functions are reached with a dot: math.log2.
  • print(math.log2(8)): the logarithm to base 2 of 8, which asks "2 to what power gives 8?".

Reading the output

Each print() gives one line, in the same order as the code. 5 is a whole number, which Python calls an integer. 2.5 is a float, Python's name for a number with a decimal point. 1024 is 210. 3.0 is the answer 3, because 2 × 2 × 2 = 8; it is printed with .0 because math.log2() always returns a float.

Base-2 logarithms are worth meeting early. In expression analysis, a log2 fold change of 1 means a gene's level doubled, 2 means it quadrupled, and −1 means it halved. You will see that scale throughout Module 2.

Two more operators are worth knowing now. A double slash, //, divides and drops the remainder, so 7 // 2 gives 3, an integer. The percent sign, %, gives only the remainder, so 7 % 2 gives 1. You will meet them when splitting things into equal groups, for example working out how many full codons of three bases fit in a sequence and how many bases are left over.

A common first error is to use a module before importing it. If you run print(math.log2(8)) in a fresh notebook without the import math line, Python stops with a NameError: it does not know the name math. The fix is to run the import first, and the habit that prevents it is to put all imports in one cell at the top of the notebook.

Go deeper: two more kinds of division, and a rounding surprise

Besides /, Python has two more division operators. 7 // 2 is floor division: it divides and rounds down to a whole number, giving 3. 7 % 2 is the remainder, also called modulo, giving 1. The remainder is handy in sequence work: position i is the first letter of a codon when i % 3 == 0. Floor division always rounds down, to the next lower whole number, so -7 // 2 gives -4, not −3.

Floats can surprise you too. print(0.1 + 0.2) shows 0.30000000000000004, because most decimal fractions cannot be stored exactly in binary, just as 1/3 cannot be written exactly in decimal. This is not a bug in Python; every language that uses standard floating-point numbers does the same. So never compare floats with ==. Use math.isclose(0.1 + 0.2, 0.3) instead, which gives True.

Naming values

A single = stores a value under a name, called a variable.

Read = as "store the value on the right under the name on the left", not as "equals" in the mathematical sense. Names may contain letters, digits and underscores, but may not start with a digit, and capital letters count: Reads and reads are two different names. Python style is to use lower-case words joined by underscores, such as read_length.

Python
# sequencing depth for a whole genome
reads = 900_000_000       # underscores make long numbers readable
read_length = 150
genome_size = 3.1e9       # 3.1 × 10^9

coverage = reads * read_length / genome_size
print(coverage)
print(round(coverage, 1))
43.54838709677419 43.5

What this does

  • The first line is a comment saying what the cell is for.
  • reads = 900_000_000 stores nine hundred million under the name reads. Python ignores the underscores inside the number; they are there so you can count the zeros.
  • read_length = 150 stores the length of each read, in bases.
  • genome_size = 3.1e9 uses scientific notation: e9 means "times 10 to the power 9", so this is 3,100,000,000, the approximate size of the human genome in base pairs.
  • coverage = reads * read_length / genome_size multiplies (*) and divides (/), working from left to right, and stores the answer under a new name.
  • round(coverage, 1) rounds the stored value to one decimal place for display. The variable itself keeps its full value.

Reading the output

The first line, 43.54838709677419, is the full result: 900,000,000 × 150 = 135,000,000,000 bases read in total, divided by 3,100,000,000 bases in the genome. So each base was read about 43.5 times on average, written 43.5×. Computers store decimal numbers with about 15 to 17 significant digits, which is why so many appear; most of them mean nothing for a measurement like this. The second line shows the same value rounded, which is how you would report it.

The advantage of naming values is that the calculation now reads like the formula. If the run had produced 600 million reads instead, you would change one line and run the cell again; every other line stays correct.

The same names can answer the question the other way round. To plan a run that reaches 30× coverage, rearrange the formula: 30 * genome_size / read_length gives the number of reads needed, which is 620 million here. Because the names already hold the genome size and read length, this is one new line rather than a fresh calculation by hand.

One naming pitfall: avoid names that Python already uses. If you write print = 5, the name print now holds a number, and every later print() call fails until you restart the kernel. The same goes for list, sum, max and round. Choose a descriptive name, such as total_reads, and the problem never arises.

Setting up Python and Jupyter · LSMA free course A notebook with one text cell and three code cells, numbered 1 to 3 in the order they were run. Arrows show each code cell placing names and values into the memory of the kernel: reads, read_length, genome_size and coverage. 1 · CELLS ON THE PAGE 2 · THE KERNEL Text cell · sequencing depth for a whole genome [1] reads = 900_000_000 read_length = 150 genome_size = 3.1e9 [2] coverage = reads * read_length / genome_size [3] print(round(coverage, 1)) 43.5 Names held in memory name · stored value reads 900000000 read_length 150 genome_size 3100000000.0 coverage 43.548… filled in run order
Each code cell, when run, adds names and values to the kernel's memory; the numbers in square brackets record the order of running. Cell 3 only works because cells 1 and 2 ran before it. Schematic, not to scale.

Key point

Notebook cells can be run in any order, which is convenient and dangerous. Before you share a notebook, use “Restart and run all” to check it works from top to bottom.

Here is how it goes wrong. Suppose you run the cell that stores read_length = 150, then later change that line to a different value but never run it again. The page now shows one number while the kernel still holds the other, and every result below uses the old one. Or you delete a cell that created a variable: the notebook keeps working today, because the value is still in memory, and fails for the next person who opens it. Restarting empties the kernel's memory; running all then rebuilds it strictly from the page, top to bottom.

Further reading · Module 1The Python Tutorial: the official tutorial, for anything you want explained a second way. · All reading for this course

Check your understanding

1. What does 2 ** 3 return?

2. How do you install a package from inside a Colab notebook?

3. Why use “Restart and run all” before sharing?

Up next

Lesson 2

Strings, lists and functions

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.