Bio
The turtle module provides a visual and intuitive way to simulate biological structures like atoms, molecules and proteins.
Molecular Geometry
The Water Molecule (H2O)

Draw a classic Mickey Mouse-shaped water molecule.
- Create a turtle to represent the drawing pen.
- Draw a large red circle to represent the Oxygen atom.
- Lift the pen, move to the center of the Oxygen atom, and set the turtle’s heading.
- Draw a thick gray line (the chemical bond) outward, then draw a smaller white (or light gray) circle for the first Hydrogen atom.
- Return to the center of the Oxygen atom. Turn the turtle exactly 104.5 degrees (the bond angle of water).
- Draw the second bond and the second Hydrogen atom.
Possible solution, draw Water Molecule (H2O):
# Simple Mickey Mouse Water Molecule (H2O)
=
# -------------------------
# Function to draw a filled circle (atom)
# -------------------------
# -------------------------
# Function to draw a bond + hydrogen
# -------------------------
# -------------------------
# Draw Oxygen
# -------------------------
# -------------------------
# Draw Hydrogens
# -------------------------
Biological Dynamics
Brownian Motion (Random Walk)
Simulate the erratic, random movement of a microscopic particle suspended in a fluid.
- Import the
randommodule alongsideturtle. - Create a small, colored dot (the turtle) representing a protein or molecule.
- Write a
whileloop that repeats 500 times. - Inside the loop, have the turtle turn a random angle (between 0 and 360 degrees) and move forward a random small distance (e.g., between 1 and 10 pixels).
- Leave the turtle’s pen down so it draws a trail of its path.
- Add an “invisible boundary” (like a cell membrane). If the turtle’s x or y coordinates go beyond a certain limit, force it to bounce back into the center.
Cellular Viral Infection Dynamics
Simulate a simple viral infection spreading through a cluster of cells.
- Create a list of 20 turtles. Scatter them randomly across the screen and stamp them as green circles. These are healthy cells.
- Create one red turtle (the virus/infected cell) and place it in the center.
- Create an animation loop where all turtles move randomly (using a mini random walk).
- The Dynamic Rule: Check the distance between the red turtle and the green turtles. If a green turtle gets within a specific distance (collision radius) of a red turtle, change the green turtle’s color to red.
- Watch as the “infection” dynamically spreads through the moving population!
You will need to use
turtle.distance()to calculate the proximity between the agents on the screen.
Possible solution
=
# --- Create green cells ---
=
=
# --- Create red cell (the virus) on center ---
=
# --- Custom params ---
= 20
= 10
# --- Main program ---
# Move green cells randomly
=
=
# Check infection
Protein Structures
Primary Structure (The Polypeptide Chain)
Draw a “beads on a string” model of a polypeptide chain using different colors to represent different types of amino acids (e.g., hydrophobic vs. hydrophilic).
- Create a list of colors in Python (e.g.,
["red", "blue", "green", "blue", "yellow", "red"]). Each color represents a specific amino acid. - Write a
forloop that iterates through your list of colors. - For each color, change the turtle’s fill color, draw a filled circle (the amino acid bead), and then move the turtle forward.
- Keep the pen down as you move forward to draw a black line connecting the beads—this represents the peptide bond.
First solution:
=
=
# --------------------------
# Sample AA (AminoAcids)
# hydrophobic = blue, hydrophilic = red, special = green
# --------------------------
=
# --------------------------
# Draw polypeptide chain.
# --------------------------
# començar a l'esquerra
# bead size
# go to next bead
Improved version, based on Real AA sequence. B chain of Human Hemoglobin. 17 AA.
# --------------------------
# Classe per a un aminoàcid
# --------------------------
=
=
=
# --------------------------
# Real AA sequence. B chain: Human Hemoglobin. 17 beads.
# --------------------------
# Format: (name, color)
= ,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]
=
=
# --------------------------
# Draw AA chain
# --------------------------
# Draw bead
# Save text position
=
# Draw lines.
# t.penup()
# t.pendown()
# --------------------------
# Write aminoacid name.
# --------------------------
, =
You're reading a preview.
Sign in with Google to read the full page. A Google account includes 5 free pages in total; students and teachers read their course pages without limit.
Sign in