"hello, world"
This is cool
$\sqrt{3}$
5
5.0
3 - 2
3 + 5
3 * 2
3 ** 2
To get floating point division, make sure that at least one value in the division is a float
3/2
3./2.
float(4)/3
a = 3./2.
a
a = 0
a = a + 1
a
a = a + 1
a
a += 1
a
x = 1
y = 2
z = 3
V = x*float(y)/z
V
Using string.find to find start codons
dna = "GGCCGTATGAGGTCATGCACACACACACCGAGAGTATGA"
dna
dna.find("ATG")
dna.find("ATG",6)
dna.find("ATG",7)
dna.find("M")
dna.find("M") == -1
2 == 3
2 != 3
2 > 3
Using a while loop to find all start codons
offset = dna.find("ATG")
print offset
while(offset != -1):
offset = dna.find("ATG",offset+1)
print offset
[6,14,35,-1]
a = [5,2,1,"hi"]
a
Concatenating and appending to lists
a += ["3",13]
a
a.append(5)
a
starts = []
offset = dna.find("ATG")
while(offset != -1):
starts.append(offset)
offset = dna.find("ATG",offset+1)
starts
def find_starts(dna):
starts = []
offset = dna.find("ATG")
while(offset != -1):
starts.append(offset)
offset = dna.find("ATG",offset+1)
return starts
find_starts(dna)
other_seq = "XXXXATGXXXX"
find_starts(other_seq)
other_seq = "XXXXaTGXXXX"
find_starts(other_seq)
find_starts(dna)
Using the moduls operator to find reading frame
6 % 3
for i in find_starts(dna):
print i
Two tricks that I didn't mention explicitly in class:
(5 == 3) or (5 == 5)
len(dna)
At this point I crashed my notebook demonstrating an infinite while loop.
As a reminder, you can usually break out of an infinite loop via Kernel->Interupt from inside of Jupyter.
If that doesn't work, find IPython's process ID via:
Linux:
ps -ealf | grep -i ipython
OS X:
ps -awx | grep -i ipython
And use kill to kill it.
A simple linear congruent generator based on modular division
a = 6
m = 13
z = 2
numbers = []
while(len(numbers) < 20):
z = (a*z) % m
numbers.append(z)
numbers
Checking for unique elements with a set
set(numbers)
a = 6
m = 12
z = 1
numbers = []
while(len(numbers) < 20):
z = (a*z) % m
numbers.append(z)
numbers
A very good choice of a and m (determined empirically) from Comm. ACM 31:1192
a = 16807
m = 2**31-1
z = 1
numbers = []
while(len(numbers) < 20):
z = (a*z) % m
numbers.append(z)
numbers