Before you start
- Skim The Computing a Biologist Needs first. You need no setup: Python runs free in your browser or in Google Colab, including right on this page.
- Any term new? The Glossary has it.
Learning objectives
By the end of this lesson you will be able to: store and display a DNA sequence in Python, measure its length, count each base, and compute its GC content with just ten lines of code.
No installation needed. You can use Google Colab (a free coding notebook in your browser; it needs a Google account), or skip straight to the โถ Run box further down this page, which needs nothing at all. Let's go.
โถ Open your notebook
Click below to open the companion notebook in Colab. You'll type code into the box, then press the โถ button (or Shift+Enter) to run it.
How a notebook works
A Colab notebook is made of cells - boxes you type code into. Run a cell and its output appears right underneath. Type each block below into a cell and run it before moving on. Making mistakes is free here, so experiment.
Lines 1-2: store and show a DNA sequence
First, we put a DNA sequence into a variable - a named container - then print it to the screen.
dna = "ATGCGTACGTTAGCCGTA" print(dna)
Run it. The computer shows you back the sequence:
That's it - you just ran code. dna is now a labelled box holding your sequence, and print() displays whatever you give it.
Line 3: how long is it?
print(len(dna))
len() counts the characters - here, 18 bases. On a real genome this same command would count millions or even billions of bases in a blink.
Lines 4-7: count each base
Now something genuinely useful - count how many of each base the sequence contains.
print("A:", dna.count("A")) print("C:", dna.count("C")) print("G:", dna.count("G")) print("T:", dna.count("T"))
.count("G") asks the sequence "how many G's do you contain?" You're now extracting information from biological data with code.
Lines 8-10: compute GC content
Finally, a real metric biologists actually use - the percentage of bases that are G or C.
gc = (dna.count("G") + dna.count("C")) / len(dna) * 100 print("GC content:", round(gc, 1), "%") print("AT content:", round(100 - gc, 1), "%")
You just did real bioinformatics
GC content is a genuine measure scientists use - GC-rich DNA generally needs more heat to separate its two strands, and GC content helps characterize genes and genomes. You computed it yourself, from scratch, in your first coding session. That's the entire field in miniature: take biological data, write a few lines, get a real answer.
Make it yours - run it right here
No Colab needed for this one. Here is the whole program in one editable box. Press โถ Run to execute it in your browser, then change the dna sequence on the first line - make it longer, or all A's and T's - and run it again. Watch the counts and GC content change. Tinkering like this is exactly how you learn.
dna = "AAATTTAAA" and run it to see for yourself.len() function counts the characters in the sequence, and this DNA string has 18 bases..count() method returns how many times a given letter appears, so dna.count("G") counts the G bases.print() simply shows its output on the screen, like when it displayed the DNA sequence back to you.๐ You finished the Introduction!
You understand what bioinformatics is, you've got the vocabulary, and you've written and run real code that analyzes DNA. You are no longer a complete beginner - you're someone who has done this.
Sources & further reading
- Ekmekci B, McAnany CE, Mura C. An introduction to programming for bioscientists: a Python-based primer. PLoS Comput Biol 12: e1004867, 2016. doi:10.1371/journal.pcbi.1004867
- Yakovchuk P, Protozanova E, Frank-Kamenetskii MD. Base-stacking and base-pairing contributions into thermal stability of the DNA double helix. Nucleic Acids Res 34: 564-574, 2006. doi:10.1093/nar/gkj454
- Python Software Foundation. "An informal introduction to Python" (Python tutorial, chapter 3). docs.python.org
- Google Research. Colaboratory frequently asked questions. research.google.com
Last reviewed: October 2026.
Where to go next
You've got two great paths from here. Set up the tools properly on your own machine, or keep building data skills. Either works - pick what excites you.
