Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet Jena sascha.winter@uni-jena.de 11.08.2014
Python Erschien 1991, Guido van Rossum Nach Monty Python benannt Leicht erlernbare Syntax Gut lesbar Multi-Paradigmen Sprache Objektorientiert, Imperativ, Funktional Python 2 vs. Python 3
Python Interpretiert Automatische Speicherverwaltung Dynamisches Typensystem
Python i f 2==2: p r i n t TEST. s t r i p ( T ) # Kommentar! e l s e : p r i n t TEST. s s t t r r i i p p ( T )
Python python # i n t e r a k t i v e python c command # wegwerf Code python FILE. py # f u e h r t FILE aus python i FILE. py # FILE, dann i n t e r a k t i v e
Blöcke Blocksyntax! i f 1 + 1 == 2 : p r i n t foo p r i n t bar i f 1 + 1 == 2 : p r i n t foo ; p r i n t bar Tab XOR Leerzeichen! Styleguide: Einrückung == 4 Leerzeichen
Zahlen, Strings
Zahlen Unendlich große ints a, b = 5, 2 a + b, a b, a b, a / b, a % b i = 0 i += 1 a, b = 5. 5, 2.25 a + b, a b, a b, a / b
Strings S i n g l e or Double q u o t es p r i n t A v e r y l o n g s t r i n g with \ a l i n e break A v e r y l o n g s t r i n g with a l i n e break p r i n t Other v e r y l o n g s t r i n g with l i n e break Other v e r y l o n g s t r i n g with l i n e break
Strings Con + c a t Concat Test 3 TestTestTest
Strings word = Simple Test word [ 3 ] m word [ : 6 ] Simple word [ 7 : ] Test word [ : i ] + word [ i : ] == word
Strings word = Simple Test word [ 1 : 1 0 0 ] Simple Test word [ 1] t word [ 3:] e s t
Strings s. e n d s w i t h ( s u f f i x ) # Also with s t a r t, end s. s t a r t s w i t h ( p r e f i x ) # Also with s t a r t, end s. f i n d ( s u b s t r i n g ) # Also with s t a r t, end
Strings s. s t r i p ( [ c h a r s ] ) # Also l s t r i p ( ), r s t r i p ( ) t e s t t e s t. s t r i p ( s t ) # e s t t e s. s t r i p ( ) # Remove w i t h e s p a c e
Strings s. s p l i t ( s t r i n g ) # Also l s p l i t ( ), r s p l i t ( ) # m a x s p l i t s 1, 2,, 3. s p l i t (, ) # [ 1, 2,, 3 ] 1 2 3. s p l i t ( ) # [ 1, 2, 3 ]
Strings,. j o i n ( [ 1, 2, 3 ] ) # 1,2,3,. j o i n ( 1,2,3. s p l i t (, ) ) # 1,2,3 # j o i n i s the r e v e r s e o p e r a t i o n o f s p l i t
C-style Format Strings %3d, %3d, %3.3 f % (127, 8, 13.0236131) 127, 8, 13.024 H e l l o %s % World #=> H e l l o World %d + %d < 10 % ( 5, 4) #=> 5 + 4 < 10
Logik
Logik <, >, ==, <=, >=,!= a < b == c and, or, not FALSE : F a l s e, None, 0, 0. 0,, [ ],...
Logik p r i n t 0 0 and B p r i n t A and F a l s e F a l s e p r i n t B A and B
Logik a = s o m e S t r i n g F u n c t i o n ( ) # Might be p r i n t a or D e f a u l t Value
Kontrollstrukturen
IF x = 4 i f x < 2 : p r i n t x i s t k l e i n e r 2 e l i f x == 2 : p r i n t x i s t g l e i c h 2 e l s e : p r i n t x i s t g r o e s s e r 2 p r i n t Ende des IF
WHILE a, b = 0, 1 while b < 1 0 : p r i n t b a, b = b, a+b 1 1 2 3 5 8
FOR words = [ Mary, had, a, l i t t l e, lamb ] f o r word i n words : p r i n t word Mary had a l i t t l e lamb
FOR f o r i i n range ( 1 0 ) : p r i n t i range ( 5, 10) #[ 5, 6, 7, 8, 9 ] range ( 0, 10, 2) #[ 0, 2, 4, 6, 8 ]
FOR words = [ Mary, had, a, l i t t l e, lamb ] f o r i i n range ( len ( words ) ) : p r i n t i, words [ i ] 0 Mary 1 had 2 a 3 l i t t l e 4 lamb
Continue f o r i i n range ( 1 0 ) : i f i % 2 == 0 : continue p r i n t i 1 3 5 7 9
Break, Else f o r n i n range ( 3, 1 0 ) : f o r x i n range ( 2, n ) : i f n % x == 0 : break e l s e : p r i n t n 3 5 7
Pass f o r i i n range ( 1 0 ) : i f i % 2 == 0 : pass # TODO: Implement t h i s l a t e r e l s e : p r i n t i 0 2 4 6 8
keyword def introduces a function definition first statement may be a docstring Defining and calling a function def gcd ( a, b ) : E u c l i d e a n a l g o r i t h m while b!= 0 : t, b, a = b, a%t, t return a gcd ( 4 2, 2 3 )