Introduction to Python for Biologists
Katerina Taˇ skova1 Jean-Fred Fontaine1,2
1Faculty of Biology, Johannes Gutenberg-Universit¨
at Mainz, Mainz, Germany
2Genomics and Computational Biology, Kernel Press, Mainz, Germany
Introduction to Python for Biologists skova 1 Jean-Fred Fontaine 1 , - - PowerPoint PPT Presentation
Introduction to Python for Biologists skova 1 Jean-Fred Fontaine 1 , 2 K aterina Ta 1 Faculty of Biology, Johannes Gutenberg-Universit at Mainz, Mainz, Germany 2 Genomics and Computational Biology, Kernel Press, Mainz, Germany
1Faculty of Biology, Johannes Gutenberg-Universit¨
2Genomics and Computational Biology, Kernel Press, Mainz, Germany
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
created by Guido van Rossum (1991) high-level (abstraction from the details of the computer) interpreted (needs an interpreter software)
code readability syntax brevity
rich built-in features powerful scientific extensions plotting capabilities March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 i n s t r u c t i o n 1
2
3 repeat 20 times
4
5
6
7
8
9
10
11
12 # backslashs
13 i n s t r u c t i o n 3 \
14 i n s t r u c t i o n 3
15
16 # Expressions
17 i n s t r u c t i o n 4 (1 ,
18
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
e.g. a=2 assigns value 2 to variable a
built-in functions are available (see list on slide 100) e.g. print(a) will display ’2’ on the screen
users can define new names of variables and functions in their
imported modules can add names of variables and functions
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
a class is a template defining what objects can store and do an object is an instance of a class
has a name (an attribute ”name”) has an age (an attribute ”age”) can introduce itself (a method ”who”) example with 1 existing Human object P1: 1 P1 .name = ” Mary ” # assigns
2 P1 . age
3 P1 . who ( )
4 who ( )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
e.g. classes and functions
NumPy for scientific computing Matplotlib for plotting BioPython for Biology
1 # import
2 import
3 datetime . date . today ( ) # 2017−03−16 4 today ( )
5 6 # import
7 # in
8 from math import
9 log10 (1) # equal 0 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 $ python3
2 a=2
3 b=3
4 e x i t ( )
e.g. let say myscript.py is a script file (simple text file) and it contains: print(”hello world!”) 1 $ python3 myscript . py
2 hello
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
web-based graphical interface manage cells of code or text see execution results on the same notebook save/open notebooks March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
e.g. help(print) to display help for function print()
1 # Forgetting
2
3 # F i l e ”<stdin >”,
4 #
5 #
6 # SyntaxError :
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Spelling
2 prin ( ” Hello
3 # Traceback
4 #
5 # NameError : name
1 # Wrong l i n e
2
3 World ” ) 4 # F i l e ”<stdin >”,
5 #
6 #
7 # SyntaxError : EOL while
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Numeric
2 12 3 −123 4 1.6E3
5 6 # Strings
7
8
9 ”A
10
11
12
13 r ’A \ ’ s t r i n g \ ’ ’
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1
2
3 4
5 6
7
8
9
10 11
12
13 14
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
avoid accents, non-alphanumeric, non English underscores may be used (e.g. abc 123)
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Numeric types 2 a=2
3
4 b=a
5
6 a=5
7
8
9 10 # Strings 11 c1= ’a ’ 12
13 myName125 =
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 type (7)
2 type (8.25)
3 type (4.52e−3) # <class
4 5 # Operators
6 1 + 3
7 4 − 1
8 3 ∗ 2
9 9 / 2
10 9
11 9 % 2
12 2∗∗3
13 14 # Lowest to
15 +,−
16 ∗ ,
17 +x , −x
18 ∗∗
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # B u i l t −in
2 abs( −2.58) # 2.58
3 round ( 2 . 5 ) # 2
4 5 # With
6 a = 1
7 b = 1 + 1
8 c = a + b
9 d = a+c∗b
10 d = ( a+c ) ∗b # 8 ( use parentheses
11 12 # Short
13 a += 1 # a = a + 1 14 a ∗= 5 # a = a ∗ 5 15 16 # Special
17
18
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Quotes 2
3
4 ”A
5
6
7 8 # Escape sequences ( see annexes ) 9 ”A single
10
11 ”A t a b u l a t i o n
12 ”A newline
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Operators 2
3
4
5
6 7 # B u i l t −in
8 len ( ’A s t r i n g
9 type ( ’a ’ ) # <class
10 11 # Slices
12 ”ABCDEFG” [ 2 : 5 ]
13 ”ABCDEFG” [ : 5 ]
14 ”ABCDEFG” [ 5 : ]
15 ”ABCDEFG” [ −2:]
16 ”ABCDEFG” [ 0 : 5 : 2 ]
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 seq = ”ACGtCCAgTnAGaaGT” 2 3 # Case 4 seq . c a p i t a l i z e ( )
5 seq . casefold ( )
6 seq . lower ( )
7 seq . swapcase ( )
8 seq . upper ( )
9 10 # Search and replace 11 seq . count ( ’a ’ )
12 seq . count ( ’G ’ ,0 ,
13 seq . endswith ( ’GT ’ )
14 seq . endswith ( ’G ’ ,0 ,
15 seq . f i n d ( ’GtC ’ )
16 seq . replace ( ” aa ” ,
17 seq . replace ( ”A” ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 seq = ”ACGtCCAgTnAGaaGT” 2 3 # Is
4 seq . isalnum ( )
5 seq . isalpha ( )
6 seq . islower ( )
7 seq . isnumeric ( ) # False
8 seq . isspace ( )
9 seq . isupper ( )
10 11 # Join and
12 ”−” . j o i n ( [ ”A” , ”B” ] )
13 ”−” . j o i n ( seq )
14 seq . p a r t i t i o n ( ” aa ” ) # ( ’ACGtCCAgTnAG ’ ,
15 seq . s p l i t ( ” aa ” )
16
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 seq = ”ACGtCCAgTnAGaaGT” 2 3 # Deleting 4 seq . l s t r i p ( )
5 seq . r s t r i p ( )
6 seq . s t r i p ( )
7 8 seq . l s t r i p ( ”AC” ) #
9 seq . l s t r i p ( ”CA” ) #
10 seq . l s t r i p ( ”C” )
11 # same f o r
12 13 # Simple parsing
14 l i n e . s t r i p ( ) . s p l i t ( ’ , ’ ) # remove newline and
15 16 # t r a n s l a t e
17 table = seq . maketrans ( ’ atcg ’ ,
18 seq . lower ( ) . t r a n s l a t e ( table )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
python notebooks data
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 List1 =
2 3 List1 = [ ’b ’ ,
4 List1 [ 0 ]
5 List1 [ 1 ]
6 List1 [ −1]
7 List1 [ −2]
8 9 # Slices
10 List1 [ 2 : 5 ]
11 List1 [ : 5 ]
12 List1 [ 5 : ]
13 List1 [ −2:]
14 List1 [ 0 : 5 : 2 ] # [ ’ b ’ ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # B u i l t −in
2 List2 = [1 ,
3 len ( List1 ) # 5
4 max( List2 ) # 5 5 min ( List2 ) # 1 6 sum( List2 ) # 15 7 8 # L i s t
9 List2 =
10 List2 . append (1)
11 List2 . append ( ’A ’ )
12 List2 . extend ( [ ’B ’ , 2 ] ) # [1 ,
13 List2 . pop (2)
14 List2 . i n s e r t (3 ,
15 List2 . index ( ’A ’ )
16 List2 . count ( ’A ’ )
17 List2 . reverse ( )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # so rt in g 2 List3 = [5 ,
3 sorted ( List3 ) # [1 ,
4 List3
5 List3 . sort ( )
6 List3
7 8 # nested
9 myList = [
10
11 myList [ 0 ]
12 myList [ 0 ] [ 0 ]
13 myList [ 0 ] [ 1 ]
14 myList [ 1 ]
15 myList [1][0]=10 #
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 myList = [
2
3 4 f o r
5
6
7 # b 8 # a 9 # 10 10 # cat 11 12 f o r
13
14
15 # b
16 # 10
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 Tuple1 = ( )
2 Tuple1 = ( ’b ’ ,
3 4 Tuple1 [ 0 ]
5 Tuple1 [ 1 : 3 ]
1 # Range( st a r t ,
2 range (10)
3
4
5
6
7
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 S0 = set ( )
2 S0 = { ’a ’ , 1}
3 S1 = { ’a ’ , 1 ,
4 S2 = { ’a ’ , 1 ,
5 len (S0)
6 7 # Operators 8
9
10 S1 − S2
11 S1 | S2
12 S1 & S2
13 S1 ˆ S2
14 S0 <= S1
15 S1 >= S2
16 S1 >= S0
17 S0 . i s d i s j o i n t (S1) # False March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Methods 2 S0 . copy ( )
3 S0 . add ( item )
4 S0 . remove ( item )
5 S0 . discard ( item ) # remove element item from the
6 S0 . pop ( )
7 S0 . clear ( )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 d = {}
2 d = { ’A ’ : ”ALA” ,
3 d [ ’A ’ ]
4 d [ ’C ’ ]
5 d [ ’H ’ ]= ” HIS ” # add new item 6 d
7 del d [ ’A ’ ]
8 9
10
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 sign =
2 sign =
3 sign #
4 5 # convert
6
7
8 9 # convert
10
11
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # convert
2 s t r (3.141592)
3 s t r ( [ 1 , 2 , 3 , 4 ] ) #
4 5 # convert a sequence type
6 # ( str ,
7 new set
8 new tuple = tuple ( o l d l i s t ) #
9 new set
10 n e w l i s t
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Numeric types
2 a = 1
3 b = a
4 b = b + 1 # b binds a new object
5 a
6 b
7 8 # Strings
9 a = ” Hello ”
10 b = a
11 a = a . replace ( ’o ’ ,
12 a
13 b
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Dictionary
2 d1 = { ’A ’ : ”ALA” ,
3 d2 = d1
4 d2 [ ’H ’ ]= ” HIS ” # add item
5 d1
6 d2
7 8 d2 = d1 . copy ( )
9 d2 [ ’P ’ ]= ”PRO” # add item
10 d1
11 d2
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # L i s t
2 l1 = [ ’A ’ ,
3 l2 = l1 4 l2 . append ( ’P ’ ) 5 l1 # [ ’A ’ ,
6 l2 # [ ’A ’ ,
7 8 l2 = l1 [ : ]
9 l2 . append ( ’V ’ ) 10 l1 # [ ’A ’ ,
11 l2 # [ ’A ’ ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 n e w l i s t
2 new dict
3 new set
4 new tuple = tuple ( o l d l i s t ) # copy
1 import copy 2 x . copy ( )
3 x . deepcopy ( ) # deep copy of x ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # For items
2 f o r
3
4 # Hi
5 # Hi Kate 6 # Hi Michael 7 8 # For items
9 seq =
10 d = { ’A ’ : ”ALA” ,
11 f o r
12
13
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # For items
2 f o r
3
4 # a 5 # b 6 # c 7 8 # For items
9 f o r n in
10
11 # 0 12 # 1 13 # 2 14 15 # For items from any
16 f o r n in
17
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # loop
2 RNAs = [ ’miRNA ’ ,
3 f o r
4
5 # 0 miRNA 6 # 1 tRNA 7 # 2 mRNA 8 9 # loop over 2
10 RNAtypes = [ ’ micro ’ ,
11 f o r
12
13
14 # 0 micro miRNA 15 # 1 t r a n s f e r tRNA 16 # 2 messenger mRNA March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1
2 value=1 3 while
4
5
6
7 # 1 1 8 # 2 2 9 # 3 6 10 # 4 24 11 # 5 120 12 # 6 720 March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 from random import
2 3 # Simple
4 def kmerFixed ( ) :
5
6 7 kmerFixed ( )
8 9 # Returning a value 10 def kmer10 ( ) :
11
12
13
14
15 16 newKmer = kmer10 ( )
17
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # One parameter 2 def kmer ( k ) :
3
4
5
6
7 8
9
10
11
12
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # Parameters with more parameters and d ef a u lt
2 def
3
4
5
6
7 8 generic kmer ( ”AB12” , 15) # e . g .
9 generic kmer ( ”AB12” )
10 generic kmer ( k=20)
11 generic kmer ( )
12 generic kmer ( k=8 , alphabet=” #$%&” ) # e . g .
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 a = 10
2 3 def
4
5 6 my function ( )
7
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 a = 10
2 3 def
4
5
6
7 8 my function ( )
9
10
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 a = 10
2 3 def
4
5
6
7
8
9
10 11 c = my function ( a )
12
13
14 15 a = my function ( a )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 #
2 a = True 3 b = False 4 c = True 5 6 7 # simple
8 a == True 9 b == False 10 c == True March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 #
2 a = True 3 b = False 4 c = True 5 6 # order
7 ( a or b ) == ( b or a ) 8 ( a and b ) == ( b and a ) 9 10 # neutral
11 ( a or False ) == a 12 ( a and True ) == a 13 14 # always the same ( whatever value
15 ( a and False ) == False 16 ( a or True )
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 #
2 a = True 3 b = False 4 c = True 5 6 # precedence ”==” > ” not ” > ” and ” > ” or ” 7 ( a and b or c ) == ( ( a and b )
8 ( not a == b ) == ( not
9 10 # equivalent
11
12 ( a or a or a ) == a 13 ( b and b and b ) == b 14 15 b and b and b == b # False and False and True => False ! ! 16 17 a and ( b or c ) == ( a and b )
18 a or
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 Operations 2 <
3 <=
4 >
5 >=
6 ==
7 math . isclose (a , b ) # equal
8 !=
9 i s
10 i s
11 x < y <= z
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 seq =
2
3
4
5
6 else : 7
8 9 # 10 # sequence contains
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 match = re . search ( expression ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 import
2 s t r =
3 match = re . search ( r ’ word :\w\w\w ’ ,
4
5
6 else : 7
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 match = re . search ( r ’ i i i ’ ,
2 match . group ( ) == ” i i i ”
3 4 match = re . search ( r ’ igs ’ ,
5 match == None
6 7 match = re . search ( r ’ . . g ’ ,
8 match . group ( ) == ” i i g ”
9 10 match = re . search ( r ’ \d\d\d ’ ,
11 match . group ( ) == ” 123 ”
12 13 match = re . search ( r ’ \w\w\w ’ ,
14 match . group ( ) == ” abc ”
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
e.g. i+ = one or more i’s
e.g. A{5} for 5 A letters A{6,10} for 6 to 10 A letters
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 # simple
2 re . search ( r ’ pi+ ’ ,
3 re . search ( r ’ pi ? ’ ,
4 re . search ( r ’ pi ? ’ ,
5 re . search ( r ’ pi ∗ ’ ,
6 re . search ( r ’ pi ∗ ’ ,
7 re . search ( r ’ pi {3} ’ ,
8 re . search ( r ’ i + ’ ,
9 10 # 3
11 re . search ( r ’ \d\s∗\d\s∗\d ’ ,
12 re . search ( r ’ \d\s∗\d\s∗\d ’ ,
13 re . search ( r ’ \d\s∗\d\s∗\d ’ ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
[ABC] matches ’A’ or ’B’ or ’C’.
[a-z] for lowercase alphabetic characters [a-zA-Z] for alphabetic characters [AB-] for A, B or dash
[ˆAB] for any character except A or B. March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 s t r =
2 match = re . search ( r ’ \w+
3
4
5 6 match = re . search ( r ’ [\w.−]+@[\w.−]+ ’ ,
7
8
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 import
2 seq = ”RPAPPDRAPDQX” # A sequence 3 expr =
4 5 match = re . search ( expr ,
6
7
8
9
10
11
12
13
14
15 # 2 − 6 − (2 ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 import
2 seq = ”RPAPPDRAPDQX” # A sequence 3 expr =
4 5 match = re . match ( expr ,
6
7 # None 8 9 matches = re . f i n d a l l ( expr ,
10
11 # [ ’APPD ’ ,
12 13 matches = re . f i n d i t e r ( expr ,
14 f o r m in matches :
15
16 # (2 ,
17 # (7 ,
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
match.group(): the whole match text match.group(1): match text of 1st left parenthesis match.group(2): match text of 2nd left parenthesis ... 1 import
2 s t r =
3 match = re . search ( ’ ( [ \w. −]+)@( [ \w. −]+) ’ ,
4
5
6
7
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 s t r =
2 tuples = re . f i n d a l l ( r ’ ( [ \w\. −]+)@( [ \w\. −]+) ’ ,
3
4 #
5 6 f o r
7
8 # a l i c e
9 # bob | abc .com March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
Note that \s (whitespace) includes newlines
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 s t r i n g =
2 3 matches = re . f i n d a l l ( r ’<.∗> ’ ,
4
5 6 matches = re . f i n d a l l ( r ’<.∗?> ’ ,
7
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
1 text1 =
2 text2 = re . sub ( r ’ \.\w+ ’ ,
3
4 # alice@google . de and bob@abc. de
1 text1 =
2 text2 = re . sub ( 3
4
5
6
7 ## google . com@alice and abc .com@bob March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
https://docs.python.org
Google’s Python Class ProgrammingForBiologists.org March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
Operation Result s[i] = x item i of s is replaced by x s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t del s[i:j] same as s[i:j] = [] s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t del s[i:j:k] removes the elements of s[i:j:k] from the list s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) s.clear() removes all items from s (same as del s[:]) s.copy() creates a shallow copy of s (same as s[:]) s.extend(t) or s += t extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t) s *= n updates s with its contents repeated n times s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x]) s.pop([i]) retrieves the item at i and also removes it from s s.remove(x) remove the first item from s where s[i] == x s.reverse() reverses the items of s in place
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine
abs() Return the absolute value of a number. all() Return True if all elements of the iterable are true (or if the iterable is empty). any() Return True if any element of the iterable is true. If the iterable is empty, return False. ascii() Return a string containing a printable representation of an object (escape non-ASCII characters). bin() Convert an integer number to a binary string. bool() Convert a value to a Boolean. chr() Return the string representing a character. dict() Create a new dictionary. dir() Return the list of names in the current local scope. float() Convert a string or a number to floating point. format() Convert a value to a ”formatted” representation. help() Invoke the built-in help system. hex() Convert an integer number to a hexadecimal string.
March 21, 2017 Johannes Gutenberg-Universit¨ at Mainz Taˇ skova & Fontaine