Bioinformatics Programming Using Python - Brian D. Chapman

adventurescoldSoftware and s/w Development

Nov 7, 2013 (4 years and 1 month ago)

564 views

Bioinformatics Programming Using Python
Bioinformatics Programming
Using Python
0LWFKHOO/0RGHO
Beijing

Cambridge

Farnham

Köln

Sebastopol

Taipei

Tokyo
Bioinformatics Programming Using Python
E\0LWFKHOO/0RGHO
&RS\ULJKW0LWFKHOO/0RGHO$OOULJKWVUHVHUYHG
3ULQWHGLQWKH8QLWHG6WDWHVRI$PHULFD
3XEOLVKHGE\2¦5HLOO\0HGLD,QF*UDYHQVWHLQ+LJKZD\1RUWK6HEDVWRSRO&$
2¦5HLOO\
ERRNVPD\EHSXUFKDVHGIRUHGXFDWLRQDOEXVLQHVVRUVDOHVSURPRWLRQDOXVH2QOLQHHGLWLRQV
DUHDOVRDYDLODEOHIRUPRVWWLWOHV
KWWSP\VDIDULERRNVRQOLQHFRP
)RUPRUHLQIRUPDWLRQFRQWDFWRXU
FRUSRUDWHLQVWLWXWLRQDOVDOHVGHSDUWPHQWRU
FRUSRUDWH#RUHLOO\FRP

Editor:
0LNH/RXNLGHV
Production Editor:
6DUDK6FKQHLGHU
Copyeditor:
5DFKHO+HDG
Proofreader:
6DGD3UHLVFK
Indexer:
/XFLH+DVNLQV
Cover Designer:
.DUHQ0RQWJRPHU\
Interior Designer:
'DYLG)XWDWR
Illustrator:
5REHUW5RPDQR
Printing History:
'HFHPEHU
)LUVW(GLWLRQ
2¦5HLOO\ DQG WKH 2¦5HLOO\ ORJR DUH UHJLVWHUHG WUDGHPDUNV RI 2¦5HLOO\ 0HGLD,QF
%LRLQIRUPDWLFV
3UR
JUDPPLQJ8VLQJ3\WKRQ
WKHLPDJHRIDEURZQUDWDQGUHODWHGWUDGHGUHVVDUHWUDGHPDUNVRI2¦5HLOO\
0HGLD,QF
0DQ\RIWKHGHVLJQDWLRQVXVHGE\PDQXIDFWXUHUVDQGVHOOHUVWRGLVWLQJXLVKWKHLUSURGXFWVDUHFODLPHGDV
WUDGHPDUNV:KHUHWKRVHGHVLJQDWLRQVDSSHDULQWKLVERRNDQG2¦5HLOO\0HGLD,QFZDVDZDUHRID
WUDGHPDUNFODLPWKHGHVLJQDWLRQVKDYHEHHQSULQWHGLQFDSVRULQLWLDOFDSV
:KLOHHYHU\SUHFDXWLRQKDVEHHQWDNHQLQWKHSUHSDUDWLRQRIWKLVERRNWKHSXEOLVKHUDQGDXWKRUDVVXPH
QRUHVSRQVLELOLW\IRUHUURUVRURPLVVLRQVRUIRUGDPDJHVUHVXOWLQJIURPWKHXVHRIWKHLQIRUPDWLRQFRQ
WDLQHGKHUHLQ
TM
7KLVERRNXVHV5HS.RYHUDGXUDEOHDQGIOH[LEOHOD\IODWELQGLQJ
,6%1
>0@

Table of Contents
Preface .
.................................................................... xi
1.Primitives .
............................................................ 1
6LPSOH9DOXHV

%RROHDQV

,QWHJHUV

)ORDWV

6WULQJV

([SUHVVLRQV

1XPHULF2SHUDWRUV

/RJLFDO2SHUDWLRQV

6WULQJ2SHUDWLRQV

&DOOV

&RPSRXQG([SUHVVLRQV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

2.Names, Functions, and Modules .
......................................... 21
$VVLJQLQJ1DPHV

'HILQLQJ)XQFWLRQV

)XQFWLRQ3DUDPHWHUV

&RPPHQWVDQG'RFXPHQWDWLRQ

$VVHUWLRQV

'HIDXOW3DUDPHWHU9DOXHV

8VLQJ0RGXOHV

,PSRUWLQJ

3\WKRQ)LOHV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

v
7UDSV

7UDFHEDFNV

3.Collections .
........................................................... 47
6HWV

6HTXHQFHV

6WULQJV%\WHVDQG%\WHDUUD\V

5DQJHV

7XSOHV

/LVWV

0DSSLQJV

'LFWLRQDULHV

6WUHDPV

)LOHV

*HQHUDWRUV

&ROOHFWLRQ5HODWHG([SUHVVLRQ)HDWXUHV

&RPSUHKHQVLRQV

)XQFWLRQDO3DUDPHWHUV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

4.Control Statements .
................................................... 99
&RQGLWLRQDOV

/RRSV

6LPSOH/RRS([DPSOHV

,QLWLDOL]DWLRQRI/RRS9DOXHV

/RRSLQJ)RUHYHU

/RRSVZLWK*XDUG&RQGLWLRQV

,WHUDWLRQV

,WHUDWLRQ6WDWHPHQWV

.LQGVRI,WHUDWLRQV

([FHSWLRQ+DQGOHUV

3\WKRQ(UURUV

([FHSWLRQ+DQGOLQJ6WDWHPHQWV

5DLVLQJ([FHSWLRQV

([WHQGHG([DPSOHV

([WUDFWLQJ,QIRUPDWLRQIURPDQ+70/)LOH

7KH*UDQG8QLILHG%LRLQIRUPDWLFV)LOH3DUVHU

3DUVLQJ*HQ%DQN)LOHV

7UDQVODWLQJ51$6HTXHQFHV

&RQVWUXFWLQJD7DEOHIURPD7H[W)LOH

vi | Table of Contents
7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

5.Classes .
............................................................. 165
'HILQLQJ&ODVVHV

,QVWDQFH$WWULEXWHV

&ODVV$WWULEXWHV

&ODVVDQG0HWKRG5HODWLRQVKLSV

'HFRPSRVLWLRQ

,QKHULWDQFH

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

6.Utilities .
............................................................ 209
6\VWHP(QYLURQPHQW

'DWHVDQG7LPHVGDWHWLPH

6\VWHP,QIRUPDWLRQ

&RPPDQG/LQH8WLOLWLHV

&RPPXQLFDWLRQV

7KH)LOHV\VWHP

2SHUDWLQJ6\VWHP,QWHUIDFHRV

0DQLSXODWLQJ3DWKVRVSDWK

)LOHQDPH([SDQVLRQIQPDWFKDQGJORE

6KHOO8WLOLWLHVVKXWLO

&RPSDULQJ)LOHVDQG'LUHFWRULHV

:RUNLQJZLWK7H[W

)RUPDWWLQJ%ORFNVRI7H[WWH[WZUDS

6WULQJ8WLOLWLHVVWULQJ

&RPPDDQG7DE6HSDUDWHG)RUPDWVFVY

6WULQJ%DVHG5HDGLQJDQG:ULWLQJLR

3HUVLVWHQW6WRUDJH

3HUVLVWHQW7H[WGEP

3HUVLVWHQW2EMHFWVSLFNOH

.H\HG3HUVLVWHQW2EMHFW6WRUDJHVKHOYH

'HEXJJLQJ7RROV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

Table of Contents | vii
7.Pattern Matching .
.................................................... 257
)XQGDPHQWDO6\QWD[

)L[HG/HQJWK0DWFKLQJ

9DULDEOH/HQJWK0DWFKLQJ

*UHHG\9HUVXV1RQJUHHG\0DWFKLQJ

*URXSLQJDQG'LVMXQFWLRQ

7KH$FWLRQVRIWKHUH0RGXOH

)XQFWLRQV

)ODJV

0HWKRGV

5HVXOWVRIUH)XQFWLRQVDQG0HWKRGV

0DWFK2EMHFW)LHOGV

0DWFK2EMHFW0HWKRGV

3XWWLQJ,W$OO7RJHWKHU([DPSOHV

6RPH4XLFN([DPSOHV

([WUDFWLQJ'HVFULSWLRQVIURP6HTXHQFH)LOHV

([WUDFWLQJ(QWULHV)URP6HTXHQFH)LOHV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

8.Structured Text .
...................................................... 287
+70/

6LPSOH+70/3URFHVVLQJ

6WUXFWXUHG+70/3URFHVVLQJ

;0/

7KH1DWXUHRI;0/

$Q;0/)LOHIRUD&RPSOHWH*HQRPH

7KH(OHPHQW7UHH0RGXOH

(YHQW%DVHG3URFHVVLQJ

H[SDW

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

9.Web Programming .
................................................... 325
0DQLSXODWLQJ85/VXUOOLESDUVH

'LVDVVHPEOLQJ85/V

$VVHPEOLQJ85/V

2SHQLQJ:HE3DJHVZHEEURZVHU

0RGXOH)XQFWLRQV

viii | Table of Contents
&RQVWUXFWLQJDQG6XEPLWWLQJ4XHULHV

&RQVWUXFWLQJDQG9LHZLQJDQ+70/3DJH

:HE&OLHQWV

0DNLQJWKH85/VLQD5HVSRQVH$EVROXWH

&RQVWUXFWLQJDQ+70/3DJHRI([WUDFWHG/LQNV

'RZQORDGLQJD:HE3DJH¦V/LQNHG)LOHV

:HE6HUYHUV

6RFNHWVDQG6HUYHUV

&*,

6LPSOH:HE$SSOLFDWLRQV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

10.Relational Databases .
................................................. 359
5HSUHVHQWDWLRQLQ5HODWLRQDO'DWDEDVHV

'DWDEDVH7DEOHV

$5HVWULFWLRQ(Q]\PH'DWDEDVH

8VLQJ5HODWLRQDO'DWD

64/%DVLFV

64/4XHULHV

4XHU\LQJWKH'DWDEDVHIURPD:HE3DJH

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

11.Structured Graphics .
.................................................. 399
,QWURGXFWLRQWR*UDSKLFV3URJUDPPLQJ

&RQFHSWV

*8,7RRONLWV

6WUXFWXUHG*UDSKLFVZLWKWNLQWHU

WNLQWHU)XQGDPHQWDOV

([DPSOHV

6WUXFWXUHG*UDSKLFVZLWK69*

69*)LOH&RQWHQWV

([DPSOHV

7LSV7UDSVDQG7UDFHEDFNV

7LSV

7UDSV

7UDFHEDFNV

Table of Contents | ix
A.Python Language Summary .
........................................... 449
B.Collection Type Summary .
............................................. 459
Index ..................................................................... 473
x | Table of Contents
Preface
7KLVSUHIDFHSURYLGHVLQIRUPDWLRQ,H[SHFWZLOOEHLPSRUWDQWIRUVRPHRQHUHDGLQJDQG
XVLQJ
WKLV ERRN 7KH ILUVW SDUW LQWURGXFHV WKH ERRN LWVHOI 7KH VHFRQG WDONV DERXW
3\WKRQ7KHWKLUGSDUWFRQWDLQVRWKHUQRWHVRIYDULRXVNLQGV
Introduction
,ZRXOGOLNHWREHJLQZLWKVRPHFRPPHQWVDERXWWKLVERRNWKHILHOGRIELRLQIRUPDWLFV
DQGWKHNLQGVRISHRSOH,WKLQNZLOOILQGLWXVHIXO
About This Book
7KHSXUSRVHRIWKLVERRN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IUDPHVLQDIUDPHDQGPDQ\IUDPHVZLWKRSHQUHDGLQJIUDPHV
$QG VRPHWLPHV WKHUH DUH WKUHH IUDPHV WR FRQVLGHU DQG VRPHWLPHV DOVR WKH UHYHUVH
FRPSOHPHQW IUDPHV PDNLQJ VL[ 2SHQ UHDGLQJ IUDPHV FDQ DSSHDU LQ DQ\ RI WKH VL[
UHDGLQJIUDPHV
7KHYRFDEXODU\RYHUODSLVVRRPQLSUHVHQWWKDWLWLVDOPRVWKXPRURXV7KHQDJDLQWKH
ZRUGVLQYROYHGDUHILQHZRUGVWKDWKDYHPHDQLQJVLQDJUHDWPDQ\RWKHUGRPDLQVWRR
VRZHVKRXOGQRWEHVXUSULVHGWRHQFRXQWHUWKHPLQRXUWKUHH(YHQWKRXJK\RXKDYH
QRW\HW EHHQ SURSHUO\ LQWURGXFHG WR WKHP
7DEOH 3
 OLVWV VRPH RI WKH PRVW YH[LQJ
H[DPSOHV6WD\SDUWLFXODUO\DOHUWZKHQ\RXHQFRXQWHUWKHVHHVSHFLDOO\ZKHQ\RXVHH
WKHZRUGVLQFRGHH[DPSOHV
7DEOH3'RPDLQDPELJXRXVWHUPV
Term Biology
Programming
Python
Sequence Part of a DNA or RNA mole-
cule; more often refers to the
abstraction thereof, as rep-
resented with letters
(Usually) one of a number of data struc-
tures that arrange their elements linearly
A
linear, and therefore numer-
ically indexable, collection of
values
Base
A single nucleotide in a DNA
or RNA molecule
Base 10, 16, 2, etc.
Base 10, 16, 2, etc., as used in
input and output operations
String
A series of letters represent-
ing a DNA, RNA, or amino
acid sequence
A sequence of characters, often a “primi-
tive type” of a language
An immutable sequence type
named
str
Expression The production of proteins
under the control of cellular
machinery influenced by life
stage, the organ containing
the cell, internal states (dis-
ease, hunger), and external
conditions (dryness, heat)
(
1) (Generally) a combination of primitive
values, operators, and function calls, with
specifics differing significantly among
languages
(1) A combination of primitive
values, operators, and function
calls

(
2) Regular expression: a pattern describ-
ing a set of strings with notations for types
of characters, grouping, repetition, and so
on, the details of which differ among lan-
guages and editors
(
2a) A regular expression string
(
2b) A regular expression string
compiled into a regular expres-
sion object
Type The specimen of an organ-
ism first used to describe and
name it
A theoretical construct defined differently
in different contexts and implemented
differently by different
programming
lan-
Synonymous with “class,” but
often used in the context of Py-
thon’s built-in types, as op-
xxii | Preface
Term Biology
Programming
Python
guages; corresponds roughly to “the kind
of thing” something is and “the kind of
operations” it supports
posed to classes defined in a
Python or externally obtained
library or in user code
Translate,
translate
Convert DNA codons (base
triples) to amino acids ac-
cording to the genetic code
of the organism
Convert computer code in one language
into computer code in another, typically
lower-level, language
A method of
str
that uses a
table to produce a new
str
with all the characters of the
original replaced by the corre-
sponding entries in the table
Class One of the levels in the
standard taxonomic classi-
fication of organisms
In languages that support object-
oriented
programming, the encapsulated
definition of data and related code
As in programming; more spe-
cifically, the type of an object,
which itself is an object that
defines the methods for its
instances
Loop
A property of RNA secondary
structures (among other
meanings)
An action performed repeatedly until
some condition is no longer true
An action performed repeat-
edly until some condition is no
longer true
Library A collection of related se-
quences, most commonly
used in the context of a li-
brary of expressed RNA in
cDNA form
Like a program, but meant to be used by
other programs rather than as a free-
standing application; most languages use
a core set of libraries and provide a large
selection of optional ones
A collection of modules, each
containing a collection of rela-
ted definitions, as in “Python
comes with an extensive
library of optional tools and
facilities”
Complement The nucleotide with which
another always pairs
“Two’s complement” is the standard rep-
resentation of negative integers

R.E.
Restriction enzyme

Regular expression
)RUWXQDWHO\ZKLOHWKHWHUP£VHTXHQFH¤KDVDFRQFHSWXDOPHDQLQJLQ3\WKRQWKHUHLV
QRWKLQJ
GHILQHGLQWKHODQJXDJHE\WKDWQDPHVRZHFDQXVHLWLQRXUGHVFULSWLRQVDQG
FRGHH[DPSOHV/LNHZLVHWKHQDPHRIWKHVWULQJW\SHLV
str
VRZHFDQXVHWKHWHUP
£VWULQJ¤LQGHVFULSWLRQVDQGH[DPSOHV7KHODFNRIRYHUODSLQWKHVHLQVWDQFHVVDYHVD
IDLUDPRXQWRIDZNZD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W)RUH[DPSOH
>>>
Non
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
Non
NameError: name 'Non' is not defined
>>>
:KHQD

#
V\PERODSSHDUVRQDOLQHRIFRGH3\WKRQLJQRUHVLWDQGWKHUHVWRIWKHOLQH
7H[WIROORZLQJWKH
#
LVFDOOHGD
FRPPHQW
7\SLFDOO\FRPPHQWVRIIHULQIRUPDWLRQDERXW
WKHFRGHWRDLGWKHUHDGHUEXWWKH\FDQLQFOXGHPDQ\RWKHUNLQGVRIWH[WDSURJUDP
PHU¦VQRWHVWRIL[RULQYHVWLJDWHVRPHWKLQJDUHIHUHQFHGRFXPHQWDWLRQHQWU\ERRN
WLWOH85/HWFDQGVRRQ7KH\FDQHYHQEHXVHGWR£FRPPHQWRXW¤FRGHOLQHVWKDW
DUH QRW ZRUNLQJ RU DUH REVROHWH EXW VWLOO RI LQWHUHVW 7KH FRGH H[DPSOHV WKDW IROORZ
LQFOXGHRFFDVLRQDOFRPPHQWVWKDWSRLQWRXWLPSRUWDQWGHWDLOV
Booleans
7KHUHDUHRQO\WZR%RROHDQYDOXHV
True
DQG
False
7KHLUW\SHLV
bool
3\WKRQQDPHV
DUH£FDVHVHQVLWLYH¤VR
true
LVQRWWKHVDPHDV
True

>>>
True
True
>>>
False
False
Integers
7KHUH¦VQRWPXFKWRVD\DERXW3\WKRQLQWHJHUV7KHLUW\SHLV
int
DQGWKH\FDQKDYHDV
PDQ\GLJLWVDV\RXZDQW7KH\PD\EHSUHFHGHGE\DSOXVRUPLQXVVLJQ6HSDUDWRUV
VXFKDVFRPPDVRUSHULRGVDUHQRWXVHG
>>>
14
14
>>>
!1
!1
>>>
1112223334445556667778889990000000000000

DYHU\ODUJHLQWHJHU
1112223334445556667778889990000000000000
3\WKRQ

$GLVWLQFWLRQLVPDGHEHWZHHQLQWHJHUVWKDWILWZLWKLQDFHUWDLQ
ODUJHUDQJHDQGWKRVHWKDWDUHODUJHUWKHODWWHUDUHDVHSDUDWHW\SHFDOO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RWDWLRQZLWKWKH
OHWWHU £H¤ VHSDUDWLQJ WKH EDVH IURP WKH H[SRQHQW:KHQ 3\WKRQ SULQWV D QXPEHU LQ
VFLHQWLILF QRWDWLRQ LW ZLOO DOZD\V KDYH D VLQJOH GLJLW EHIRUH WKH GHFLPDO SRLQW VRPH
QXPEHURIGLJLWVIROORZLQJWKHGHFLPDOSRLQWD
+
RU
-
IROORZLQJWKH
e
DQGILQDOO\DQ
LQWHJHU2QLQSXWWKHUHFDQEHPRUHWKDQRQHGLJLWEHIRUHWKHGHFLPDOSRLQW5HJDUGOHVV
RIWKHIRUPXVHGZKHQHQWHULQJDIORDW3\WKRQZLOORXWSXWYHU\VPDOODQGYHU\ODUJH
QXPEHUV XVLQJ VFLHQWLILF QRWDWLRQ 7KH H[DFW FXWRIIV DUH GHSHQGHQW RQ WKH 3\WKRQ
LPSOHPHQWDWLRQ+HUHDUHVRPHH[DPSOHV
>>>
2e4

6FLHQWLILFQRWDWLRQEXW
20000.0
ZLWKLQWKHUDQJHRIRUGLQDU\IORDWV
>>>
2e-2
0.02
>>>
.0001

:LWKLQWKHUDQJHRIRUGLQDU\IORDWV
0.0001
VRSULQWHGDVDQRUGLQDU\IORDW
>>>
.00001

$QLQQRFHQWORRNLQJIORDWWKDWLV
1e-05
VPDOOHUWKDQWKHORZHUOLPLWVRH
>>>
1002003004005000.

$IORDWZLWKPDQ\GLJLWVWKDWLV
1002003004005000.0
VPDOOHUWKDQWKHXSSHUOLPLWVRQRH
>>>
100200300400500060.

)LQDOO\DIORDWWKDWLVODUJHUWKDQWKH
1.0020030040050006e+17
XSSHUOLPLWVRSULQWHGZLWKDQH
Simple Values | 3
Strings
6WULQJV
DUH VHULHV RI 8QLFRGH

 FKDUDFWHUV 7KHLU W\SH LV
str
 0DQ\ ODQJXDJHV KDYH D
VHSDUDWH£FKDUDFWHU¤W\SHEXW3\WKRQGRHVQRWDORQHFKDUDFWHULVVLPSO\DVWULQJRI
OHQJWKRQH$VWULQJLVHQF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HPDWLFDOODZVRI
%RROHDQORJLF
7KHFODVVLF%RROHDQRSHUDWRUV
DUH
not

and
DQG
or
,Q3\WKRQWKRVHDUHZULWWHQMXVWWKDWZD\UDWKHUWKDQXVLQJVSHFLDO
V\PEROV
>>>
not True
False
>>>
not False
True
>>>
True and True
True
>>>
True and False
False
>>>
True or True
True
® $
FRPSXWHUVFLHQFHILHOGFDOOHG£QXPHULFDODQDO\VLV¤SURYLGHVWHFKQLTXHVIRUPDQDJLQJWKHDFFXPXODWLRQRI
VXFKHUURUVLQFRPSOH[RUUHSHWLWLYHFRPSXWDWLRQV
Expressions | 7
>>>
True or False
True
>>>
False and False
False
>>>
False or True
7KHUHVXOWVRI
and

DQG
or
RSHUDWLRQVDUHQRWFRQYHUWHGWR%RROHDQV)RU
and
H[SUHVVLRQVWKHILUVWRSHUDQGLVUHWXUQHGLILWLVIDOVHRWKHUZLVHWKH
VHFRQGRSHUDQGLVUHWXUQHG)RU
or
H[SUHVVLRQVWKHILUVWRSHUDQGLVUH
WXUQHG LI LW LV WUXH RWKHUZLVH WKH VHFRQG RSHUDQG LV UHWXUQHG )RU
H[DPSOH
>>>
'' and 'A'
''
1RW)DOVH

LVDIDOVHYDOXH
>>>
0 and 1 or 2

5HDGDVDQGRU
2
1RW7UXHLVDIDOVHYDOXH
:KLOHFRQIXVLQJWKLVFDQEHXVHIXOZH¦OOVHHVRPHH[DPSOHVODWHU
7KHRSHUDQGVRI
and
DQG
or
FDQDFWXDOO\EHDQ\WKLQJ
None

0

0.0
DQGWKHHPSW\VWULQJ
DVZHOODVWKHRWKHUNLQGVRI£HPSW\¤YDOXHVH[SODLQHGLQ
&KDSWHU
DUHFRQVLGHUHG
False
(YHU\WKLQJHOVHLVWUHDWHGDV
True

7RDYRLGUHSHWLWLRQDQGDZNZDUGSKUDVHVWKLVERRNZLOOXVH£WUXH¤DQG
£IDOVH¤
LQUHJXODUW\SHIDFHWRLQGLFDWHYDOXHVFRQVLGHUHGWREH
True
DQG
False
 UHVSHFWLYHO\,W ZLOO RQO\ XVH WKH FDSLWDOL]HG ZRUGV
True
 DQG
False
 LQ WKH FRGH W\SHIDFH ZKHQ UHIHUULQJ WR WKRVH VSHFLILF %RROHDQ
YDOXHV
7KHUH LV RQH PRUH ORJLFDO RSHUDWLRQ LQ 3\WKRQ WKDW IRUPV D
FRQGLWLRQDO H[SUHVVLRQ

:ULWWHQXVLQJWKHNH\ZRUGV
if
DQG
else
LWUHWXUQVWKHYDOXHIROORZLQJWKH
if
ZKHQWKH
FRQGLWLRQLVWUXHDQGWKHYDOXHIROORZLQJWKH
else
ZKHQLWLVIDOVH:H¦OOORRNDWVRPH
PRUHPHDQLQJIXOH[DPSOHVDELWODWHUEXWKHUHDUHDIHZWULYLDOH[DPSOHVWKDWVKRZ
ZKDWFRQGLWLRQDOH[SUHVVLRQVORRNOLNH
>>>
'yes' if 2 - 1 else 'no'
'yes'
>>>
'no' if 1 % 2 else 'no'
'no'
,Q DGGLWLRQ WR WKH %RROHDQ RSHUDWRUV WKHUH DUH VL[
FRPSDULVRQ RSHUDWRUV
 WKDW UHWXUQ
%RROHDQ YDOXHV
==

!=

<

<=

>
 DQG
>=
 7KHVH ZRUN ZLWK PDQ\ GLIIHUHQW NLQGV RI
RSHUDQGV
>>>
2 == 5 // 2
True
>>>
3 > 13 % 5
False
>>>
'one' < 'two'
True
8 | Chapter 1:

Primitives
>>>
'one' != 'one'
False
<RXPD\DOUHDG\EHIDPLOLDUZLWKORJLFDODQGFRPSDULVRQRSHUDWLRQVIURPRWKHUFRP
SXWHU
ZRUN\RX¦YHGRQHLIRQO\HQWHULQJVSUHDGVKHHWIRUPXODV,IWKHVHDUHQHZWR\RX
VSHQGVRPHWLPHH[SHULPHQWLQJZLWKWKHPLQWKH3\WKRQLQWHUSUHWHUXQWLO\RXEHFRPH
FRPIRUWDEOHZLWKWKHP<RXZLOOXVHWKHPIUHTXHQWO\LQFRGH\RXZULWH
String Operations
7KHUHDUHIRXUELQDU\RSHUDWRUVWKDWDFWRQVWULQJV
in

not in

+
DQG
*
7KHILUVWWKUHH
H[SHFWERWKRSHUDQGVWREHVWULQJV7KHODVWUHTXLUHVWKHRWKHURSHUDWRUWREHDQLQWHJHU
$ RQHFKDUDFWHU VXEVWULQJ FDQ EH H[WUDFWHG ZLWK
VXEVFULSWLRQ
 DQG D ORQJHU VXEVWULQJ
E\
VOLFLQJ
%RWKXVHVTXDUHEUDFNHWVDVZH¦OOVHHVKRUWO\
String operators
7KH
in
DQG
not in
RSHUDWRUVWHVWZKHWKHUWKHILUVWVWULQJLVDVXEVWULQJRIWKHVHFRQG
RQHVWDUWLQJDWDQ\SRVLWLRQ7KHUHVXOWLV
True
RU
False

>>>
'TATA' in 'TATATATATATATATATATATATA'
True
>>>
'AA' in 'TATATATATATATATATATATATA'
False
>>>
'AA' not in 'TATATATATATATATATATATATA'
True
$ QHZ VWULQJ FDQ EH SURGXFHG E\
FRQFDWHQDWLQJ
 WZR H[LVWLQJ VWULQJV 7KH UHVXOW LV D
VWULQJFRQVLVWLQJRIDOOWKHFKDUDFWHUVRIWKHILUVWRSHUDQGIROORZHGE\DOOWKHFKDUDFWHUV
RIWKHVHFRQG&RQFDWHQDWLRQLVH[SUHVVHGZLWKWKHSOXVRSHUDWRU
>>>
'AC' + 'TG'
'ACTG'
>>>
'aaa' + 'ccc' + 'ttt' + 'ggg'
'aaaccctttggg'
$VWULQJFDQEHUHSHDWHGDFHUWDLQQXPEHURIWLPHVE\PXOWLSO\LQJLWE\DQLQWHJHU
>>>
'TA' * 12
'TATATATATATATATATATATATA'
>>>
6 * 'TA'
'TATATATATATA'
Subscription
6XEVFULSWLRQ
H[WUDFWVDRQHFKDUDFWHUVXEVWULQJRIDVWULQJ6XEVFULSWLRQLVH[SUHVVHG
ZLWKDSDLURIVTXDUHEUDFNHWVHQFORVLQJDQLQWHJHUYDOXHGH[SUHVVLRQFDOOHGDQ
LQGH[

7KHILUVWFKDUDFWHULVDWSRVLWLRQQRW
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[0]
'M'
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[1]
'N'
Expressions | 9
7KHLQGH[FDQDOVREHQHJDWLYHLQZKLFKFDVHWKHLQGH[LVFRXQWHGIURPWKHHQGRIWKH
VWULQJ7KHODVWFKDUDFWHULVDWLQGH[
!1

>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[!1]
'A'
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[!5]
'D'
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[7 // 2]
'K'
$V
)LJXUH

VKRZVVWDUWLQJDW
0
IURPWKHEHJLQQLQJRUHQGRIDVWULQJDQLQGH[FDQ
EHWKRXJKWRIDVDODEHOIRUWKHFKDUDFWHUWRLWVULJKW7KHHQGRIDVWULQJLVWKHSRVLWLRQ
RQH DIWHU WKH ODVW HOHPHQW,I\RX DUH XQIDPLOLDU ZLWK LQGH[LQJ LQ SURJUDPPLQJ ODQ
JXDJHVWKLVLVSUREDEO\DQHDVLHUZD\WRYLVXDOL]HLWWKDQLI\RXSLFWXUHWKHLQGH[HVDV
DOLJQHGZLWKWKHFKDUDFWHUV
)LJXUH,QGH[SRVLWLRQVLQVWULQJV
$WWHPSWLQJ
WRH[WUDFWDFKDUDFWHUEHIRUHWKHILUVWRUDIWHUWKHODVWFDXVHVDQHUURUDV
VKRZQKHUH
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[50]
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'[50]
IndexError: string index out of range
7KHODVWOLQHUHSRUWVWKHQDWXUHRIWKHHUURUZKLOHWKHQH[WWRODVWOLQHVKRZVWKHLQSXW
WKDWFDXVHGWKHHUURU
Slicing
6OLFLQJ
H[WUDFWVDVHULHVRIFKDUDFWHUVIURPDVWULQJ<RX¦OOXVHLWRIWHQWRFOHDUO\DQG
FRQFLVHO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len('TATA')
4
>>>
print('AAT', 'AAC', 'AAG', 'AAA')
AAT AAC AAG AAA
>>>
input('Enter a codon: ')
Enter a codon:
CGC
'CGC'
>>>
Expressions | 13
+HUHDUHVRPHFRPPRQQXPHULFIXQFWLRQVLQ3\WKRQ
abs(
value
)
5HWXUQVWKHDEVROXWHYDOXHRILWVDUJXPHQW
max(
args
...)
5HWXUQVWKHPD[LPXPYDOXHRILWVDUJXPHQWV
min(
args
...)
5HWXUQVWKHPLQLPXPYDOXHRILWVDUJXPHQWV
7\SHVFDQEHFDOOHGDV
IXQFWLRQVWRR7KH\WDNHDQDUJXPHQWDQGUHWXUQDYDOXHRIWKH
W\SHFDOOHG)RUH[DPSOH
str(
arg
)
5HWXUQVDVWULQJUHSUHVHQWDWLRQRILWVDUJXPHQW
int(
arg
)
5HWXUQVDQLQWHJHUGHULYHGIURPLWVDUJXPHQW
float(
arg
)
5HWXUQVDIORDWGHULYHGIURPLWVDUJXPHQW
bool(
arg
)
5HWXUQV
False
IRU
None
]HURVHPSW\VWULQJVHWFDQG
True
RWKHUZLVHUDUHO\XVHG
EHFDXVH RWKHU W\SHV RI YDOXHV DUH DXWRPDWLFDOO\ FRQYHUWHG WR %RROHDQ YDOXHV
ZKHUHYHU%RROHDQYDOXHVDUHH[SHFWHG
+HUHDUHVRPHH[DPSOHVRIWKHVHIXQFWLRQVLQDFWLRQ
>>>
str(len('TATA'))
'4'
>>>
int(2.1)
2
>>>
int('44')
44
>>>
bool('')
False
>>>
bool(' ')
True
>>>
float(3)
3.0
8VLQJ
int

LVWKHRQO\ZD\WRJXDUDQWHHWKDWWKHUHVXOWRIDGLYLVLRQLVDQ
LQWHJHU$VQRWHGHDUOLHU
//
LVWKHIORRURSHUDWRUDQGUHVXOWVLQDIORDWLI
HLWKHURSHUDQGLVDIORDW
7KHUHLVDEXLOWLQKHOSIDFLOLW\IRUXVHLQWKH3\WKRQLQWHUSUHWHU8QWLOZH¦YHH[SORUHG
PRUHRI3\WKRQPXFKRI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WDSSHDUVLQWKHVWULQJWKDWSUH
FHGHVLWLQWKHFDOO7KHIROORZLQJH[DPSOHUHWXUQV
2
EHFDXVHWKHVWULQJ
'DL'
DSSHDUV
WZLFHLQWKHORQJHUVWULQJ
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.count('DL')
2
([FHSWIRUKDYLQJWKHLUILUVWDUJXPHQWEHIRUHWKHIXQFWLRQQDPHFDOOVWRPHWKRGVKDYH
WKHVDPHIHDWXUHVDVFDOOVWRRUGLQDU\IXQFWLRQVRSWLRQDODUJXPHQWVLQGHILQLWHQXPEHU
RIDUJXPHQWVHWF+HUHDUHVRPHFRPPRQO\XVHGPHWKRGVRIWKH
str
W\SH
string1
.count(
string2
[,

start
[,

end
]])
5HWXUQVWKHQXPEHURIWLPHV
string2
DSSHDUVLQ
string1
,I
start
LVVSHFLILHGVWDUWV
FRXQWLQJDWWKDWSRVLWLRQLQ
string1
LI
end
LVDOVRVSHFLILHGVWRSVFRXQWLQJEHIRUH
WKDWSRVLWLRQLQ
string1

string1
.find(
string2
[,

start
[,

end
]])
5HWXUQV WKH SRVLWLRQ RI WKH ODVW RFFXUUHQFH RI
string2
 LQ
string1

!1
 PHDQV
string2
 ZDV QRW IRXQG LQ
string1
,I
start
 LV VSHFLILHG VWDUWV VHDUFKLQJ DW WKDW
SRVLWLRQLQ
string1
LI
end
LVDOVRVSHFLILHGVWRSVVHDUFKLQJEHIRUHWKDWSRVLWLRQLQ
string1

string1
.startswith(
string2
[,

start
[,

end
]])
5HWXUQV
True
RU
False
DFFRUGLQJWRZKHWKHU
string2
VWDUWVZLWK
string1
,I
start
LV
VSHFLILHGXVHVWKDWDVWKHSRVLWLRQDWZKLFKWRVWDUWWKHFRPSDULVRQLI
end
LVDOVR
VSHFLILHGVWRSVVHDUFKLQJEHIRUHWKDWSRVLWLRQLQ
string1

Expressions | 15
string1
.strip([
string2
])
5HWXUQV
DVWULQJZLWKDOOFKDUDFWHUVLQ
string2
UHPRYHGIURPLWVEHJLQQLQJDQGHQG
LI
string2
LVQRWVSHFLILHGDOOZKLWHVSDFHLVUHPRYHG
string1
.lstrip([
string2
])
5HWXUQV D VWULQJ ZLWK DOO FKDUDFWHUV LQ
string2
 UHPRYHG IURP LWV EHJLQQLQJ LI
string2
LVQRWVSHFLILHGDOOZKLWHVSDFHLVUHPRYHG
string1
.rstrip([
string2
])
5HWXUQVDVWULQJZLWKDOOFKDUDFWHUVLQ
string2
UHPRYHGIURPLWVHQGLI
string2
LV
QRWVSHFLILHGDOOZKLWHVSDFHLVUHPRYHG
+HUHDUHVRPHH[DPSOHVRIPHWKRGFDOOVLQDFWLRQ
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.find('DL')
4
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.find('DL', 5)
14
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.find('DL', 5, 12)
-1
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.startswith('DL')
False
>>>
'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'.startswith('DL', 4)
True
7KHUHVWULFWLRQHQ]\PHZLWKWKHDPLQRDFLGVHTXHQFHLQWKHVHH[DPSOHVUHFRJQL]HVWKH
VLWHZLWKWKHEDVHVHTXHQFH7&&**$,W¦VHDV\HQRXJKWRILQGWKHILUVWORFDWLRQLQD
'1$EDVHVHTXHQFHZKHUHWKLVRFFXUV
>>>
'AAAAATCCCGAGGCGGCTATATAGGGCTCCGGAGGCGTAATATAAAA'.find('TCCGGA')
27
>>>
,IWKHUHFRJQLWLRQVLWHGLGQRWRFFXULQWKHVHTXHQFH
find
ZRXOGKDYHUHWXUQHG
!1

Compound Expressions
7KHH[DPSOHVRIRSHUDWRUH[SUHVVLRQVWKDWZH¦YHORRNHGDWWKXVIDUKDYHKDGRQO\D
VLQJOHRSHUDWRU+RZHYHUMXVWDVLQWUDGLWLRQDODOJHEUDRSHUDWRUVFDQEHFRPSRXQGHG
LQDVHULH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