Orthologs: the same gene across species
The same gene shows up in a human, a mouse, a fish — slightly different in each. These matched-up genes are orthologs, and comparing them in code reveals how related two species are.
The big idea
Orthologs are versions of the same gene in different species; comparing them base by base measures how similar they are.
See it in code
Comparing two orthologs comes down to comparing bases. == tells you whether a whole strand is identical — or whether the bases at one position match:
human = "ATGGCG"
mouse = "ATGGCA"
print("Identical strands?", human == mouse)
print("Position 0:", human[0], "vs", mouse[0], "->", human[0] == mouse[0])
print("Position 5:", human[5], "vs", mouse[5], "->", human[5] == mouse[5])Identical strands? False Position 0: A vs A -> True Position 5: G vs A -> False
The strands aren't identical, but most positions match — position 0 does, position 5 doesn't. To measure how similar, we need to count the matches.
Loop over every position, compare the two bases there, and add one to a running tally each time they match:
human = "ATGGCG"
mouse = "ATGGCA"
matches = 0
for i in range(len(human)):
if human[i] == mouse[i]:
matches += 1
print("Matching bases:", matches, "out of", len(human))Matching bases: 5 out of 6
Five of six positions match. Turn that count into a fraction of the length and you have a similarity score.
Now on real gene versions: line up a human and mouse strand, walk them position by position counting matches, and turn that count into a similarity score:
human = "ATGGCGTTA"
mouse = "ATGGCATTA"
matches = 0
for i in range(len(human)):
if human[i] == mouse[i]:
matches += 1
similarity = matches / len(human) * 100
print("Human:", human)
print("Mouse:", mouse)
print(f"Matching bases: {matches}/{len(human)} ({similarity:.0f}% similar)")Human: ATGGCGTTA Mouse: ATGGCATTA Matching bases: 8/9 (89% similar)
Eight of nine bases match — the one difference is a mutation that accumulated since these species shared an ancestor. Counting matches like this, scaled up, is how scientists build family trees of life from raw sequence.
Comparing two sequences position by position is a core algorithm far beyond biology: diffing two files, checking a password against a hash character by character, measuring how similar two strings are. Real tools add gap handling (alignment), but the counting idea is the seed.
Try it yourself
Add a third species and compare it to both. Then handle sequences of different lengths by looping only up to the shorter one with min(len(a), len(b)).
The common mistake
Comparing sequences of different lengths without care. Indexing past the end of the shorter strand raises an IndexError. Real comparisons also align first — inserting gaps so similar regions line up — before counting matches.
What it unlocks
Comparison builds on list indexing, conditionals, and sequence analysis, and hints at how evolution is read from DNA.