$ !" ! % &' ! ! # ( ( % ' ! )* ' + , ( ! )&. , 0 ( - + * !/ &! - ! 1)( , &! 0 ' ! - * ! 2 ! ! 2! 3 4 5 4! + 2 ! !2 !+ ' % ! ! + 6 * 7 + 8+ !9 +2 % !2 !4 3 9 ! : + ! ! ;+ ! /6 ! 4 3 2 )= 2 8< 2< 2&! ! 26 >? * ' 6 n = 10 # n wird integer while n <= 13: if n % 2 == 0: print n, "gerade" else: print n, "ungerade" n = n + 1 ! 5 ! + @ !! 6. , + @! * ' + 2 ++ (! ! - +' > 4 > & +! ! ! Python 2.1.1 (#20, Jul 20, 2001) >>> print 3*4 12 * ! 4 #!/usr/bin/python print 'Hello world!' !+ <* !+ >4 + ! /home/dirk/$ python test.py &. < *2 ! ? 2@ 26! #include "Python.h" Py_initialize; PyRun_SimpleString("print 4"); 5 5 ! + @ ++ ! +,-,*,/,and, or, not, ==, !=, <, >, **, in, is ... 6 ) 6 & @ ) 1 < a < 5 ! / + + )a = b = 4 ++ if, while, for, break, continue 6 ) ' A+ / for a in [1,2,3,4]: print a / ! 4 = ! 2 for + while * !+ < ! < 42, 0x26, 052 + 3.1415926535987931 > 2323299094L C (-1+4j) $ 'Python' u"Hello\u0020world!" B+ (3, 4.0) ['spam', 4, 1.0] ! {'egg':23, 'ham':40} !26 ! ) + ! ! 2> ! ' & + > ! ! + % s = 'eggs' len(s) s[0] s[-1] s[1]='a' s = 'sliceofspam' s[2:5] s[:5] s[-4:] A+ / # # # # = 4 (eingebaute Funktion) 'e' 's' TypeError! # 'ice' # 'slice' # 'spam' 'a' * 99999 # 'aaaaaaaaaaaaaaaaa...' "eggs".capitalize() # 'EGGS' "The %s who say %s' % ('knights', 'Ni!') 'Neue Zeile\n' # \n ist Newline B+ B+ A+ / t = (0, "python", 1.5) t[1] t[1:3] # heterogen # 'python' # ('python', 1.5) v = (3, (1,2,3), 7) v[1] # verschachtelbar # (1,2,3) * 4+ + ! B+ a, b, c = 4, "Python", -1.0 x, y = y, x B+ + % # tauscht Werte % A+ / L = ['spam', 3.14, 42] L[1] = 'eggs' L[0:2] = [4,3,2,1] # ['spam', 'eggs', 42] # [4,3,2,1,42] L.sort() L.append('a') L.pop() # [1,2,3,4,42] # [1,2,3,4,42,'a'] # 'a' (Liste als Stack) 42 in L range(4) # 1 (true) # [0,1,2,3] [x*x for x in [1,2,3,4] if x * x > 6] # [9, 16] ! ! 0! 4 B! / ! /! ! dict = {'schmitz': 5325, 'meier': 2345, 'huber': 'kein Telefon'} dict['schmitz'] # 5325 dict.keys() # ['meier','schmitz','huber'] dict.values() # [2345, 5325, 'kein Telefon'] dict.has_key('meier') # 1 (true) dict['mueller'] = 9876 # neu einfügen del dict['mueller'] # wieder entfernen for key in dict.keys(): print key, "--", dict[key] =+ ' + 2+ ' 2 % ! myfile = open('text.txt', 'r') s = myfile.readlines() print "Anzahl Zeilen:", len(s) myfile.close() & s s s s = = = = !+ ! myfile.read() myfile.read(n) myfile.readline() myfile.seek(pos) ! ! ! D 4 !+ ! myfile = sys.stdin myfile = sys.stdout # zum Lesen öffnen # Liste von Zeilen # Schließen (optional) 2/6) # # # # in einen String einlesen n Bytes einlesen nächste Zeile Zugriff über Position *C ,! !+/ try: myfile = open('text.txt','r') except IOError, data: print 'Oeffnen nicht moeglich: ', data else: print 'Alles ok.' * *C myException = 'Fehler' ... if notOK: raise myException ... # String als Exception *C ,! ) ! try: doSomething() finally: # fängt Exception nicht ab print "wird immer ausgeführt" @! ! *C *C ) ! 6! )assert test, data if __debug__: if not test: raise AssertionError, data ! E *C ) E ;+ def factorial(n): "Fakultaet von n" # optionaler Doc-String result = 1L # Long Integer while n > 1: result = result * n n = n - 1 return result # optional, sonst 'None' !! !+ 4!! + + ' / % + . def factorial(n=10) def factorial(n,*others) E!4 4 ! ;+ E, F !4 4 . 5 + 2! ;+ 2apply 53 " * ! =! ;+ 2 2 2@! 53 < % B < ! * 3 # ) 2B+ 2 2& + ! 2! 2 / id(obj) type(obj) + + ? 2 53 id("Python") type("Python") id(5+4) type(5+4) # # # # 135181080 <type 'string'> 125140036 <type 'int'> ! =+ ,+ % ! / ! 53 a = 42 id(a) type(a) ! 2 # 135139460 # <type 'int'> ( !+ 5 3 b = a id(b) b is a ! . ! 6 + # 135139460 # 1 (true) + 53 ! a = [1,4,5] a = a[2] def a(n):return n type(a) / + # # # # 2 a weist auf Liste a weist auf Element der Liste a weist auf Funktion <type 'function'> ! $ + ,/ ( % 6 53 ! ! a = [1, 2] b = a b[0] = [5] print b print a # erstes Element der Liste ändern # [[5], 2] # [[5], 2] a = [1,2] b = a b = [5] print b print a # neues Objekt [5] an b binden # [5] # [1, 2] ! %+ =+ ! + %+ ! %+ ; ,! ! ! ! 53 / ;+ & + @! 2< ! / ! A+! / ! - ) objekt.name = wert wert = objekt.name / +5 3 ! ! ! %+ 8 + , E ? + A+! / ! ! + ) ;+ ! !+ & + , %/ ! ! D & D @! ! - !+ ! + global var1, [var2] ... ! * + G E !+ !+ ! 8 + + ! ! ! 1 +4 + - ! ! !+ + H 8 + + !+ ! ! 8 + . ! 8 + # g a b 46 globaler Geltungsbereich = 1 = 3 = 15 def func(x): # lokaler Geltungsbereich global g g = b + 1 a = x * 2 return a print print print print print print func(15) a g b x func # # # # # # 30 3 16 15 NameError: name 'x' not defined <function 'func' at 009970C> ( , ! 8 + 8 + ! ! x = 2.0 def outer(): x = 2.2 def inner() print x inner() outer() < # 2.0 (in v2.0) # 2.2 (in v2.2) !/ + ! ! ! ! %I ! ! & + & + 4! , J - * + > import math print math.pi # importiert den Namen 'math' # 3.1415926535926535 from math import exp exp(1) # import math; exp = math.exp # 2.7182818284590451 from math import * # alle Namen 6 !+ ' ! & +! < , ! ! & + . - > & + reload() @! ,- class Counter: def __init__(self): # Konstruktor self.value = 0 # Attribut der Instanz def inc(self, n): # Methode self.value = self.value + n c = Counter() print c.value c.inc(3) Counter.inc(c, 3) print c.value c.value = 7 6 9 & # # # # # # ++ & ' /!+ < ! /! ' 6 ++ C /! & < !/ + ' + Instanz erzeugen 0 Methode aufrufen Alternativschreibweise 6 Attribute sind public @! ' / ! + < !/ @! ,- class Derived(Counter): def inc(self, n = 1): Counter.inc(self, n) def dec(self, n = 1): self.value -= n def __repr__(self): print 'Zaehlerstand =', # Operator overloading self.value d = Derived() d.inc() print d # 'Zaehlerstand = 1' # Methode überschreiben # kein 'super' # Klasse erweitern < !/ ! + ) self.value @! + ) Klasse.value ! ! (! ! & )value @! , 1- & ! * + !+ ? . ! @! ! + ) 0' ! < , , ! + @ + ) ' + E , ! - UserList, UserDict, @! ! E! class MyClass(A, B) ! - + ! __x @! MyClass _MyClass__x + / ! !+ ! ) / + = !/ !+ @ ++ ! + 5 ! !C + % ! !+ 3 . + ' < + ' ! ! 3 0 + 9! !+ / < % ( ? ! ), 0 ! ! 4 ;+ &! ! ! K !!4 < - ! ! ! 8$< 4 ( ( ! 1), &! !+ ' ! 0 ;! E ! +! ! ! ! G ! ! E, * ! 9! - L+ +/ 2& )" * ' + +/ 2& )" !9 N +/+ D D+ D D+ D D+ + 7 !# ! # DD 9 &! +!# D D 26)" * ' + ) D D 2 D+D+ + 28O ! 2;)" ) D D @ B+ + 28O ! 2;)" ) D D !9 +# 25M 9 + 28O ! 2;)" ) D D !9 # 25M 9 +4 ! + # D ! D ! D D