Python Lectures 3
Python Lectures 3
AND
LOOPS
PYTHON
FOR
GENOMIC
DATA
SCIENCE
Decision
Making
condition
false
true
code
if
true
code
if
false
if condition
block of code to execute if condition is true
else
other block of code to execute if condition is
false
The
if
Statement
Take
a
look
at
the
following
code:
>>> dna=input('Enter DNA sequence:')
Enter DNA sequence:agcgcgggtatatatatgcnccann
>>> if 'n' in dna :
nbases=dna.count('n')
print("dna sequence has %d undefined bases " % nbases)
The
if
Statement
Take
a
look
at
the
following
code:
>>> dna=input('Enter DNA sequence:')
Enter DNA sequence:agcgcgggtatatatatgcnccann
condition
>>> if 'n' in dna :
nbases=dna.count('n')
print("dna sequence has %d undefined bases " % nbases)
notice
the
indentation
that
deHines
the
block
dna sequence has 3 undefined bases
Boolean
Expressions
The
condition
in
the
if
statement
is
called
a
Boolean
expression.
Boolean
expressions
are
expressions
that
are
either
true
or
false.
>>> 0<1
True
>>> len('atgcgt')>=10
False
Comparison
Operators
Comparison
Operator
>>> 'a'=='A'
Equal
==
False
Not equal
!=
Less than
<
Greater than
>
Less than or
equal to
<=
Greater than
or equal to
>=
True
Comparison
Operators
Comparison
Operator
>>> 'a'=='A'
Equal
==
False
Not equal
!=
Less than
<
True
Greater than
>
>>> 'A'<'C'
Less than or
equal to
<=
Greater than
or equal to
>=
True
>>> 10+1==11
True
Membership
Operators
Operator
Description
in
not in
>>> motif='gtccc'
>>> dna='atatattgtcccattt'
>>> motif in dna
True
Identity
Operators
Operator
Description
is
is not
Identity
Operators
Operator
Description
is
is not
>>> alphabet=[a,c,g,t]
>>> newalphabet=alphabet[:]
>>> alphabet == newalphabet
True
>>> alphabet is newalphabet
False
10
Alternative
Execution
check_dna.py
#!/usr/bin/python
dna=input('Enter DNA sequence:')
if 'n' in dna :
nbases=dna.count('n')
print("dna sequence has %d undefined bases " % nbases)
else:
print("dna sequence has no undefined bases")
11
elif condition n:
do action n
else: # none of conditions 1,2,,n are true
do something else
12
13
Logical
Operators
and
or
not
check_dna.py
#!/usr/bin/python
dna=input('Enter DNA sequence:')
if 'n' in dna or N in dna:
nbases=dna.count('n')+dna.count(N)
print("dna sequence has %d undefined bases " % nbases)
else:
print("dna sequence has no undefined bases")
14
Loops
condition
true
code
if
true
false
while
condition
block
of
code
to
execute
while
condition
is
true
15
16
while pos>-1 :
print("Donor splice site candidate at position %d%pos)
pos=dna.find('gt',pos+1)
notice
the
indentation
that
deHines
the
while
block
17
sequence to traverse
print(m,len(m))
step
...
...
break
22
protein='SDVIHRYKUUPAKSHGWYVCJRSRFTWMVWWRFRSCRA'
corrected_protein=''
for i in range(len(protein)):
if protein[i] not in 'ABCDEFGHIKLMNPQRSTVWXYZ':
continue
corrected_protein=corrected_protein+protein[i]
print("Corrected protein sequence is:%s"%corrected_protein)
24
for i in range(n):
if not condition_1:
continue
function_1(i)
if not condition_2:
continue
funtion_2(i)
if not condition_3:
continue
function_3(i)
...
25
26
27
28
some_function_here(motif,dna)
29
some_function_here(motif,dna)
Sometimes you can use the pass statement also when you
didnt
yet
write
the
code
for
a
particular
situation
but
you
need
the
placeholder
so
that
the
syntax
of
the
rest
of
your
program
is
correct.
30