Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen...

32
Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet Jena [email protected] 11.08.2014 Scriptsprachen

Transcript of Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen...

Page 1: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

ScriptsprachenPython Basics

Sascha Winter

Lehrstuhl fuer BioinformatikFriedrich-Schiller-Universitaet Jena

[email protected]

11.08.2014

Scriptsprachen

Page 2: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Scriptsprachen

Page 3: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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

Scriptsprachen

Page 4: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Python

Interpretiert

Automatische Speicherverwaltung

Dynamisches Typensystem

Scriptsprachen

Page 5: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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” )

Scriptsprachen

Page 6: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Python

python # i n t e r a k t i v epython −c ’command ’ # wegwerf Codepython FILE . py # fu e h r t FILE auspython − i FILE . py # FILE , dann i n t e r a k t i v e

Scriptsprachen

Page 7: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Blocke

Blocksyntax!

i f 1 + 1 == 2 :p r i n t ” f o o ”p r i n t ” bar ”

i f 1 + 1 == 2 :p r i n t ” f o o ” ; p r i n t ” bar ”

Tab XOR Leerzeichen!

Styleguide: Einruckung == 4 Leerzeichen

Scriptsprachen

Page 8: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Zahlen, Strings

Scriptsprachen

Page 9: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Zahlen

Unendlich große ints

a , b = 5 , 2a + b , a − b , a ∗ b , a / b , a % b

i = 0i += 1

a , b = 5 . 5 , 2 . 2 5a + b , a − b , a ∗ b , a / b

Scriptsprachen

Page 10: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Strings

’ S i n g l e ’ o r ” Double q u o t e s ”

p r i n t ”A v e r y l o n g s t r i n g w i t h \a l i n e b r e a k ”A v e r y l o n g s t r i n g w i t h a l i n e b r e a k

p r i n t ””” Other v e r y l o n g s t r i n gw i t h l i n e b r e a k ”””Other v e r y l o n g s t r i n gw i t h l i n e b r e a k

Scriptsprachen

Page 11: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Strings

”Con” + ” c a t ”” Concat ”

” Test ”∗3” T e s t T e s t T e s t ”

Scriptsprachen

Page 12: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Strings

word = ” Simple Test ”word [ 3 ]”m”

word [ : 6 ]” S imple ”

word [ 7 : ]” Test ”

word [ : i ] + word [ i : ] == word

Scriptsprachen

Page 13: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Strings

word = ” Simple Test ”word [ 1 : 1 0 0 ]” S imple Test ”

word [−1]” t ”

word [ −3 : ]” e s t ”

Scriptsprachen

Page 14: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Strings

s . e n d s w i t h ( s u f f i x ) # Also wi th s t a r t , end

s . s t a r t s w i t h ( p r e f i x ) # Also wi th s t a r t , end

s . f i n d ( s u b s t r i n g ) # Also wi th s t a r t , end

Scriptsprachen

Page 15: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 th e space

Scriptsprachen

Page 16: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 ( )# max 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” ]

Scriptsprachen

Page 17: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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

Scriptsprachen

Page 18: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

C-style Format Strings

”%3d , %3d , %3.3 f ” % (127 , 8 , 1 3 . 023 61 31 )” 127 , 8 , 13 .024 ”

” H e l l o %s ” % ” World ”#=> ” He l l o World”

”%d + %d < 10” % ( 5 , 4)#=> ”5 + 4 < 10”

Scriptsprachen

Page 19: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Logik

Scriptsprachen

Page 20: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Logik

<, >, ==, <=, >=, !=

a < b == c

and , or , not

FALSE : F a l s e , None , 0 , 0 . 0 , ”” , [ ] , . . .

Scriptsprachen

Page 21: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Logik

p r i n t 0 and ”B”0

p r i n t ”A” and F a l s eF a l s e

p r i n t ”A” and ”B”B

Scriptsprachen

Page 22: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 ”

Scriptsprachen

Page 23: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Kontrollstrukturen

Scriptsprachen

Page 24: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

IF

x = 4i 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 ”

Scriptsprachen

Page 25: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

WHILE

a , b = 0 , 1whi le b < 1 0 :

p r i n t ba , b = b , a+b

112358

Scriptsprachen

Page 26: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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

Maryhadal i t t l elamb

Scriptsprachen

Page 27: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 ]

Scriptsprachen

Page 28: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

FOR

words = [ ”Mary” , ” had ” , ”a” , ” l i t t l e ” , ” lamb ” ]f o r i i n range ( l en ( words ) ) :

p r i n t i , words [ i ]

0 Mary1 had2 a3 l i t t l e4 lamb

Scriptsprachen

Page 29: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

Continue

f o r i i n range ( 1 0 ) :i f i % 2 == 0 :

continuep r i n t i

13579

Scriptsprachen

Page 30: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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

357

Scriptsprachen

Page 31: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 re l s e :

p r i n t i

02468

Scriptsprachen

Page 32: Scriptsprachen - Python Basics€¦ · Python Zahlen, Strings Logik Kontrollstrukturen Scriptsprachen Python Basics Sascha Winter Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet

Python Zahlen, Strings Logik Kontrollstrukturen

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 ’ ’ ’whi le b != 0 :

t , b , a = b , a%t , treturn a

gcd ( 4 2 , 2 3 )

Scriptsprachen