# string
s = '40'
r = '20'
print r + s
2040
# int
s = 40
r = 20
print r + s
60
# float
s = 40.0
r = 20.0
print r + s
60.0
# boolean
s = True
r = False
print s or r
print s and r
print 42 == 42
print 4 > 5
c = True
print (42 >= 41) and ('A' == 'a' or c)
True False True False True
# list
l = ['A', 'C', 'G', 'T']
print l[0]
l[0] = 'a'
A
l.append('U')
print l
['a', 'C', 'G', 'T', 'U']
print l[10]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-27-acf326dacfd3> in <module>() ----> 1 print l[10] IndexError: list index out of range
# dict
d = {'A':'T','T':'A','G':'C','C':'G'}
print d['A']
print d['T']
T A
d['A'] = 'U'
d['U'] = 'A'
del d['T']
print d['N']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-30-341d183ad361> in <module>() ----> 1 print d['N'] KeyError: 'N'
# tuple
t = (1,2,3)
print t[0]
1
t[1] = 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-e9323da3946c> in <module>() ----> 1 t[1] = 5 TypeError: 'tuple' object does not support item assignment
for
loops are much more commonly used in python then while
loops (although this isn’t necessarily true in other languages). for
loops allow you to iterate over several steps of code to perform the same operation multiple times, usually with a different value for one or a few variables. For example, what does this code do:
for c in 'ACCGATTGACC':
print c.lower(),
a c c g a t t g a c c
c
is a variable name that gets set to each entry in the string in turn. All code in the indented block will be executed before c gets assigned to the next value.
Sometimes you need to do the same thing some specified number of times with no different variable settings (which differs from the previous example where we’re doing the same thing multiple times with a different value of c
each time). To do this in python, you would typically use the range function, which when called as:
n = 20
range(n)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
returns a list of numbers from 0
to n-1
(i.e., [0, 1, 2, ..., n-1]
). For example, to randomly reorder the positions in a given DNA sequence 25 times and print the resulting sequences, you could do the following:
sequence = list('ACCGAGGACCATACATTA')
for i in range(25):
shuffle(sequence)
print ''.join(sequence)
CTTGAAGCACACGACAAT GATTAGAACCCATAGCCA ATAGAGACATCCCGCAAT CGACGCATTAGAAAACTC ACCAACATTAGGTAACCG CATCCCAGGATATAACAG ACGCCACCGAGATTATAA CGTCCTGCAAAAAGCTAA CACAATATACGATGCGAC CAAAAGAGCACTTCGTAC AACATGACCAGCTTACAG AGATCATACAAGACGTCC TCCGAAAATAAGACGCTC CCATGGCATTCAAAACGA GGTTTACAAGCCCAACAA ACGTACATACACAAGTGC CAAAGAATGTAGTCCACC ACAACGTCGAATCGCAAT CAATAAATCCTCGGCGAA ACCAGACGTATATGAACC ACGGAATTCACAGACATC CGGACTCCATCAATAAGA TAACAAGCGTGAACCCAT CTCAAGGACTGCAACATA CACGACAGTCAACAAGTT
Note that we don’t actually use the value assigned to the variable i
ever: we just use that in combination with range to iterate 25 times.
Conditional statements allow you to apply some steps under certain conditions, such as when two values are equal, and apply some other steps in cases where those certain conditions are not met. The if
/elif
/else
statements are used for conditionally executing some code in python. For example, describe line-by-line what this code does:
sequence = 'ACCTAGGCAT'
result = []
for base in sequence:
if base == 'T':
result.append('U')
elif base == 'A' or base == 'C' or base == 'G':
result.append(base)
else:
print "Unknown base (%s). Cannot complete." % base
exit()
print ''.join(result)
ACCUAGGCAU
Functions are used to define blocks of re-usable code that you may want to work with multiple times.
def complement_sequence(seq,complements={'A':'T','C':'G','T':'A','G':'C'}):
result = []
for base in seq:
result.append(complements[base])
return ''.join(result)
# Define a sequence
my_seq = 'ACCGATTAGCCA'
# Print its complement
print complement_sequence(my_seq)
# Save its complement to a new variable
my_seq_comp = complement_sequence(my_seq)
print my_seq_comp
# Change a default value in the function
my_seq_comp_non_default = complement_sequence(my_seq,{'A':'t','C':'g','T':'a','G':'c'})
print my_seq_comp_non_default
TGGCTAATCGGT TGGCTAATCGGT tggctaatcggt