#
# Title: l02-examples.py
# Author: Nicholas Fitzkee (nickfitzkee at jhu dot edu)
# Summary: Python examples from lecture 2
# Date: February 3, 2006
#
# Description:
#
# This file contains some samples of code we wrote in class today. While
# it's good to take notes, sometimes with programming it isn't always
# done easily. Use this to supplement what you wrote down. You can
# print this file in Microsoft Word (use the Courier font).
#
#
# As you can probably tell, hash marks (#) offset comments. The Python
# interpreter ignores everything on a line following a #. Use comments
# liberally, both to keep track of what you must do as well as to
# help others (and the instrutor in particular) understand how you're
# thinking. Think of what this file would look like without comments!
#
# First, let's initialize some variables.
i = 15 # An integer
my_list = [0, 1, 2, 3] # A list of integers
pi = 3.14159 # A real number
seq1 = 'GAATTC' # A string (storing a DNA sequence)
seq1 = "GAATTC" # You can also use double quotes--just
# make sure they match!
# The following is a list of characters, containing the same sequence
# as seq1.
seq2 = ['G', 'A', 'A', 'T', 'T', 'C']
# Let's print some of our variables: separate things to print on the
# same line with a comma. Below, we've mixed strings and variables.
print 'Variables initialized so far...'
print 'i =', i
print 'my_list =', my_list
print 'the value of pi is', pi
print 'one sequence is', seq1
print 'the other is', seq2
print '' # Print a blank line
# Now let's do some simple mathematic operations
# What does this statement mean? The right side of the equals sign
# is evaluated before assigning that value to the left side
i = i + 15
print 'new value of i is', i
# We can order operations using parenthesis. Braces and brackets don't
# work here, so next parenthesis for complex calculations. Python will
# evaluate the most deeply nested things first.
print 'expression:', ((i+2)* ( (i-3) * 4))
print ''
# Floating point numbers are similar. Make sure to use a decimal place!
r = 10.0
circumf = 2. * pi * r
volume = (4.0 / 3.0) * pi * r*r*r
print 'For an object of radius', r, '...'
print '2-d circumference =', circumf
print '3-d volume = ', volume
print ''
# Functions evaluate the arguments first.
from math import sin, cos # ignore this line for now
# Let's look at some trig functions. Python uses radians for
# degree measurement.
angle = pi / 3.0
print 'The sine of 60 degrees is', sin(angle)
print 'The cosine of 60 degrees is', cos(pi / 3.0)
print 'Their tangent is', ( sin(angle)/cos(angle) )
print ''
# We haven't done much with lists. Recall that python indexes lists
# from zero, not one.
# First, let's test the length function to get the length of a list (len)
print 'The length of seq2 is', len(seq2)
# len() also works on strings!
print 'The length of seq1 is', len(seq2)
# We can also build up empty lists
empty = []
empty.append('G')
empty.append('T')
empty.append('A')
empty.append('C')
print 'empty is\'t empty anymore!', empty
print 'the length of empty is', len(empty)
print 'the first element is', empty[0]
print 'the third is', empty[2]
# We can also delete items from a list
del(empty[2])
print 'now empty is ', empty
print ''
# Finally, let's look at some loops. Below is the basic loop structure
# we learned about in class, here used to print seq1 and seq2 side
# by side
print 'Side by side comparison of seq1 and seq2'
counter = 0
while counter < len(seq1):
print seq1[counter], seq2[counter]
counter = counter + 1
print ''
# We can also use loops to calculate other mathematical expressions
# The following code calculates the result of raising a value to a positive
# whole number exponent.
value = 3.0
exponent = 5
counter = 1
result = 1.0 # Why can't this be zero?
while counter <= exponent:
result = result * value
counter = counter + 1
print 'exponential value is', result
# When we combine all this with if statements, we have a pretty
# powerful language!
if result > 0.0 and result < 5.0:
print 'result was between zero and five'
# On the other hand, we can make our comparisons more sophisticated
if result > 0.0 and result <= 5.0:
print 'result was between zero and five'
elif result > 5.0 and result <= 10.0:
print 'result was between five and ten'
else:
print 'result was greater than ten (or less than zero, why?)'