"hello, world"
5
5.
This is really cool
$\pi$
a = 5
a
a = a + 3
a
a += 3
a
2*3
2-3
6/4
Integer division, useful when we don't want to count the remainder (e.g., if we need list indices for making a histogram)
6//4
Modular division, which we used for the reading frame problem
6%4
A = .03
L = 70
C = (.02*A)/(330*L)
C
.02*A/330/L
L=25
NGC=13
MM=2
81.5+(41*NGC-100*MM-675)/L
def Tm(L,NGC,MM):
"""Calculate the Tm of a QuickChange mutagenesis primer
with length L, NGC GC bases, and MM mismatches."""
return 81.5+(41*NGC-100*MM-675)/L
Tm(25,13,2)
Tm(25,13,1)
L
print(L)
print(L,MM,Tm(L,NGC,MM))
Tm
b = Tm
b(L,NGC,MM)
sum([1,2,3,4,5])
dna = "ATGCATAGATATTAA"
dna
Python indexing counts from 0
dna[0]
dna[1]
or from -1 when indexing from the end of the string
dna[-1]
Taking multiple elements is called slicing. The slice includes the first index and stops right before the second index
dna[2:4]
dna[:4]
dna[2:]
dna[:]
Like strings, but they can hold arbitrary data
numbers = [1,2,3,4,5]
numbers
numbers[2:4]
numbers[3]
Unlike strings, we can overwrite individual elements of a list
numbers[3] = 7
numbers
numbers[3] = "seven"
numbers
numbers[3] = 7
a = numbers
a
numbers
numbers[2] = "hi"
numbers
a
a = numbers[:]
a
numbers[2] = 3
numbers
a
x = [1,2,3,4,5]
for i in x:
print(i**2)
print(i/2)
def f(x):
s = 0
for i in x:
s += i
return s
f(x)
f([2,3,4])
for i in [sum, f]:
print(i(x))
dna = "GCGATAATGCCGATAGACTGGATATGCA"
dna
dna.find("ATG")
See slides for more expressions like these. The important detail is that they evaluate to True or False and can be used as the condition for a while loop
3 != 2
3 == 2
assert(3 != 2)
assert(3 == 2)
dna.find("ATG")
dna.find("ATG",6)
dna.find("ATG",7)
dna.find("ATG",24)
offset = dna.find("ATG")
print(offset)
while(offset > -1):
offset = dna.find("ATG", offset + 1)
print(offset)
6%3
23%3
Sets are like lists but do not have a predictable order and contain only unique elements.
set([0,2,2,0])
s = set([2,4])
s
We can add elements to a set with add
s.add("hello")
s
Likewise, we can add elements to a list with append
L = [1,2,3]
L
L.append(4)
L
L = []
def reading_frames(dna):
offset = dna.find("ATG")
L = []
while(offset > -1):
L.append(offset)
offset = dna.find("ATG", offset + 1)
frames = []
for offset in L:
frames.append(offset%3)
return set(frames)
reading_frames(dna)
A more succinct implementation
def reading_frames(dna):
offset = dna.find("ATG")
L = []
while(offset > -1):
L.append(offset)
offset = dna.find("ATG", offset + 1)
frames = set()
for offset in L:
frames.add(offset%3)
return frames
for i in range(10):
print(i)
dna
dna[0]
for i in range(len(dna)):
print(dna[i])