Today's lab begins with a quick tutorial on using Python, the built-in turtle and random libraries, and the on-line bioinformatics challenges, Rosalind. The main focus is on simulating Brownian motion by
1D and 2D random walks.
Software tools needed: Python IDLE programming environment and a web browser (for accessing lab and Rosalind).
We will be using the IDLE programming environment for Python. The Python programming language and IDLE environment are freely available for many platforms from python.org. For this class, we are using Python 2, which is part of the Mac OSX operating system. Many features of the language (including the syntax of print statements) changed between the second and third version, and many popular packages are still only available for the second version.
To launch IDLE on your Macintosh:
idle(remember to hit the enter/return key, so, the computer realizes you're done typing).
print "Hello!"(remember to hit the enter/return key, so, the computer realizes you're done typing).
Once you have IDLE running, you can keep it in your dock by clicking on it and choosing options.
For our first programs, we will use a built-in library (file of useful commands) for drawing using turtles. We will begin with a simple program and modify it to add functionality. This can be done from the Python prompt ( >>> in IDLE), but it is easier to store the program in a file and modify the file. To do this, go to the File menu of IDLE and choose New File (or type ⌘N).
In the new file window, type:
import turtle teddy = turtle.Turtle() for i in range(4): teddy.forward(100) teddy.right(90) turtle.exitonclick()Indentation does matter, so, remember to indent the lines after the for, using TABs, so that they are included in the for-loop. Save the file as firstProgram.py.
Notes on saving files:
What is this program doing? Let's go through, line-by-line:
import turtletells Python that you would like to use some of the commands contained in the module, called, turtle.
teddy = turtle.Turtle()I named my turtle after President Roosevelt, but you can use any name you would like (as long as you spell it the same throughout).
for i in range(4):To repeat the actions more times, you can change the parameter to the range statement. For example, range(6) in our loop would repeat the actions 6 times.
import turtle teddy = turtle.Turtle() for i in range(40): teddy.forward(10*i) teddy.right(90) turtle.exitonclick()
Repeat 45 times: Walk forward 100 steps Turn right 92 degrees
Your output should look similar to:
Another useful library is random library that generates (pseudo)-random numbers. The following:
import random #use the random library # other code here.... random.randrange(7)generates a random whole number between 0 and 6. Similarly, the command
random.randrange(360)generates a random whole number between 0 and 359. The command also allows random numbers to be chosen from multiples. For example:
#Include the random and turtle libraries #Create a turtle #Have a loop that repeats 25 times #Inside that loop, #Move forward 10 steps #Turn a random amount #Close window on mouse clickTry to fill in each piece (if you're stuck, here is a possible solution).
We can use the programs above to simulate `random walks' where an object moves in a random direction (and a random distance forward). Random walks are well studied for modeling Brownian motion, diffusion models, paths of foraging animals, fluctuations in stock prices, and genetic drift.
For this lab, we will use the random turn programs to gather data to affirm or refute the hypothesis:
The longer the walk, on average, the farther east or west from the origin it ends.
For each lab, you should submit a lab report by the target date to: kstjohn AT amnh DOT org. The reports should be about a page for the first labs and contain the following:
Target Date: 1 February 2016
Title: Lab 1: Simulating Brownian Motion with Random Walks
Name & Email:
Purpose: Give summary of what was done in this lab.
Procedure: Describe step-by-step what you did (include programs or program outlines).
Results: Show all data collected. Including screen shots is fine (can capture via the Grab program).
Discussion: Give a short explanation and interpretation of your results here.
This course will use the on-line Rosalind system for submitting programs electronically. The password for the course has been sent to your email. Before leaving lab today, complete the first challenge.