3.3. Conditionals

Sometimes the continuation of a program depends on a condition. We would either execute a part of the program if this condition is fulfilled or adapt the behavior of the program depending on the truth value of the condition.

The simplest case of a conditional statement can be expressed by the if statement.

>>> from string import *

>>> seq = 'ATGAnnATG'
>>> if 'n' in seq:
...     print "sequence contains undefined bases"                         (1)
...     nb = count(seq, 'n')
... else:
...     print "sequence does not contains any undefined bases"
...
sequence contains undefined bases
      

In the code above, the first block of statements, between the if and the else, is executed if the string contains some 'n', whereas the second block, after the else, is executed when it does not. Notice that the two blocks are indented.