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 setup, no installation, no excuses. We'll use Google Colab - a free coding notebook that runs in your browser. You just need a Google account. Let's go.
โถ Open your notebook
Click below to open a blank 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.
Line 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, instantly.
Line 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.
Line 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), "%")
You just did real bioinformatics
GC content is a genuine measure scientists use - it relates to how stable a stretch of DNA is and 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.
๐ 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.
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.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.
