RGGS 670: Algorithmic Approaches to Biological Data

Spring 2016

Topics: Python-as-Calculator: using the built-in math library & loops to compute values and make simple tables; Debugging: common parse errors,
also use functions in a table...

Lab write-up a bit different for these-- instead of discussing results in terms of hypothesis, discuss in term of methods used.

A common task is to convert data from one format to another. Let's start with a simple example of converting distances in kilometers to distance in meters. This task follows a standard pattern: *Input, Process, Output (IPO)*.

In a text window in IDLE, write out what the input is, what the process is, and what the output is.

Here is one possible design:

Input the distance in kilometers (call it km) Calculate the distance in miles as ? Output milesAbove, we are missing an important part-- the formula to calculate miles from kilometers. By typing

Input the distance in kilometers (call it km) Calculate the distance in miles as 0.621371*km Output milesIf you have not done so already, now would be a good time to save your work as the file,

Now, we are ready to convert our outline of the algorithm to Python. Begin by adding a comment to the beginning of the file:

# Lab 2: Converter Program # YourNameHere, January 2016 Input the distance in kilometers (call it km) Calculate the distance in miles as 0.621371*km Output milesNext, let's add in the function definition, function call, and comment our outline so that we can test as we go:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print "Welcome to milage calculator!" #Input the distance in kilometers (call it km) #Calculate the distance in miles as 0.621371*km #Output miles main()Make sure that there are no typos by saving and running your program (F5 or run in the Run menu).

Next, fill in the program line by line. First, ask the user for their input:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print "Welcome to milage calculator!" #Input the distance in kilometers (call it km) km = eval(input('Enter distance in kilometers: ')) #Calculate the distance in miles as 0.621371*km #Output miles main()Test to make sure that the program runs. Note that it will ask for the input but will do nothing else since there is only that one Python statement in the

Python cares about spacing and distinguishes between spaces and tabs. If you get an error above, check that each of the lines beneath the definition is indented using a TAB.

Now, process the input (ie do the conversion):

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print "Welcome to milage calculator!" #Input the distance in kilometers (call it km) km = input('Enter distance in kilometers: ') #Calculate the distance in miles as 0.621371*km miles = 0.621371*km #Output miles main()Save and run your program again. It will ask for input but does not yet produce output.

Finally, add the print statement that shows the output:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print "Welcome to milage calculator!" #Input the distance in kilometers (call it km) km = input('Enter distance in kilometers: ')) #Calculate the distance in miles as 0.621371*km miles = 0.621371*km #Output miles print "The miles are", miles main()Save and run your program again. This time, it will produce output. Try it with some simple values to make sure that it gets the right answer. For example, if you input 0 km, the output should also be 0. If you input 1 km, you should output 0.621371 miles.

Our second program for today builds on the program above to make a table that gives kilometers and their corresponding kilometers:

km miles 0 0.0 1 0.621371 2 1.242742 3 1.864113 4 2.485484 5 3.106855 6 3.728226 7 4.349597 8 4.970968 9 5.592339Currently, our function asks us for input, but that's not needed here since we will always print out the first kilometers from 0 to 10 and their corresponding miles. For each conversion from kilometers to miles, we use the same formula. We could rewrite the formula 10 times, but it would be faster to use a loop:

#Lab 2, Chart of km-miles #YourNameHere def main(): # <-- Nothing inside the parenthesis, since no input is used for km in range(10): miles = 0.621371*kmSave this program as

For information to be displayed to the screen, we need to have a print statement:

#Lab 2, Chart of km-miles #YourNameHere def main(): for km in range(10): miles = 0.621371*km print milesAdd the print statement above, and run again. What else is needed to produce the nice chart at the beginning of this section? Try adding to the print statement to duplicate the chart above.

How did you do? Here is the final version of the program that produced the initial chart:

#Lab 2, Chart of km-miles #YourNameHere def main(): print("km\tmiles") for km in range(10): miles = 0.621371*km print km, "\t", miles main()Note that the

We can use the same general template for computing wind chill. Since this takes multiple inputs, we'll use a function. An outline of the program is:

import random import math ### Fill in missing functions here! ### def main(): welcome() #Print a message that explains what your program does #Print table headings: for temp in range(20, -25, -5): print "\t", temp, print #Print table: for wind in range(5,55,5): #For wind speeds between 5 and 50 mph print wind, "\t" #Print row label for temp in range(20, -25, -5): #For temperatures between 20 and -20 degrees wchill = computeWindChill(temp, wind) print wchill, "\t" #Print the wind chill, separated by tabs print #Start a new line for each temp main()

Fill in the missing functions to yield a program that prints out the chart of wind chill for temperatures between 20 and -20 (by 5 degree increments) and wind speeds between 5 and 50 mph (by 5 mph increments). The formula for computing wind chill (in degrees Fahrenheit) is:

wind chill = 35.74 + 0.6215T - 35.75(Wwhere T is the temperature and W is the wind speed.^{0.16}) + 0.4275T(W^{0.16})

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: 8 February 2016

Title: Lab 2:

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: If applicable, 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 two challenges.