Python for Biologists

Lesson 14 of 14 · 11 min

Wrap-Up and Where to Go Next

When you started this course, print() was probably the only Python you knew. Now you can store data in variables, repeat work with loops, wrap logic in functions, read files, and chain those pieces into a real analysis pipeline. This final lesson recaps that journey and points you toward what to learn next.

The Journey So Far

python
# A dictionary maps each sequence name to its DNA string.
sequences = {
    "gene_A": "ATGCGCGC",
    "gene_B": "ATATATAT",
    "gene_C": "GCGCGCGC",
}

# A function that returns the GC content of one DNA string as a percent.
def gc_content(dna):
    gc = dna.count("G") + dna.count("C")   # count the G and C bases
    return round(gc / len(dna) * 100, 1)   # turn the count into a percent

# Loop over every name and sequence and print each GC content.
for name, dna in sequences.items():
    print(name, gc_content(dna), "%")
gene_A 75.0 %
gene_B 0.0 %
gene_C 100.0 %

That short program uses almost everything you learned: a dictionary, a function, a loop, and arithmetic. In real work the sequences would be read from a FASTA file, but the logic stays the same.

Reading Error Messages

python
# An empty sequence has length 0, and dividing by zero is impossible.
print(gc_content(""))
Traceback (most recent call last):
  File "pipeline.py", line 17, in <module>
    print(gc_content(""))
  File "pipeline.py", line 11, in gc_content
    return round(gc / len(dna) * 100, 1)   # turn the count into a percent
ZeroDivisionError: division by zero

Tip: Read a traceback from the bottom up. The last line names the error type and message, here ZeroDivisionError: division by zero, and the lines above point to the exact file and line number where it happened. Pasting that final line into a search engine, or checking it against the official docs, almost always leads you to the fix.

Where To Go Next

When your datasets grow beyond a handful of sequences, two libraries do the heavy lifting: pandas organizes tables of data like a spreadsheet you control with code, and matplotlib turns those numbers into charts. For biology-specific tasks, the Biopython Tutorial and Cookbook at biopython.org shows how to read FASTA files, query NCBI, and run alignments without writing everything from scratch. Whenever a function surprises you, the official Python documentation at docs.python.org/3 is the authoritative reference, and its beginner tutorial is worth a second read now that the basics click.

Try it yourself: Install the tools with pip install biopython pandas matplotlib, then practice on real problems. Rosalind at rosalind.info offers bite-sized bioinformatics puzzles that build on what you learned, and NCBI at ncbi.nlm.nih.gov lets you download free FASTA sequences. A great first project is to read a FASTA file, compute the GC content of every sequence with pandas, and plot the results as a bar chart with matplotlib.