Administrators-Computer:~ loewis$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> class Person: ... def __init__(self, vorname, name, alter): ... self.vorname = vorname ... self.name = name ... self.alter = alter ... print "Datensatz erzeugt" ... >>> Person >>> p = Person("Erika", "Mustermann", 42) Datensatz erzeugt >>> p <__main__.Person instance at 0x741c0> >>> p.vorname 'Erika' >>> p.nachname Traceback (most recent call last): File "", line 1, in ? AttributeError: Person instance has no attribute 'nachname' >>> p.name 'Mustermann' >>> p.alter 42 >>> p.alter = p.alter+1 >>> p.alter 43 >>> from math import sin >>> sin(2) 0.90929742682568171 >>> sin(2) = 34 SyntaxError: can't assign to function call >>> d = { "Brandenburg":"Potsdam", "Meckpom":"Schwerin"} >>> d["Meckpom"] 'Schwerin' >>> d["Brandenburg"]="Berlin" >>> d {'Brandenburg': 'Berlin', 'Meckpom': 'Schwerin'} >>> d["Bayern"] = "Muenchen" >>> d {'Bayern': 'Muenchen', 'Brandenburg': 'Berlin', 'Meckpom': 'Schwerin'} >>> "NRW" in d False >>> "Bayern" in d True >>> len(d) 3 >>> d.keys() ['Bayern', 'Brandenburg', 'Meckpom'] >>> d.values() ['Muenchen', 'Berlin', 'Schwerin'] >>> d.items() [('Bayern', 'Muenchen'), ('Brandenburg', 'Berlin'), ('Meckpom', 'Schwerin')] >>> type(d.items()) >>> x = [3,6,7,9] >>> x[0] 3 >>> x[-1] 9 >>> x[-4] 3 >>> x[-5] Traceback (most recent call last): File "", line 1, in ? IndexError: list index out of range >>> d["NRW"] Traceback (most recent call last): File "", line 1, in ? KeyError: 'NRW' >>> from sets import Set >>> Set((1,2,3)) Set([1, 2, 3]) >>> s = Set((1,2,3)) >>> s.add(127) >>> s Set([1, 2, 3, 127]) >>> 3 in s True >>> -3 in s False >>> s2 = Set((47,26,3)) >>> s.intersection(s2) Set([3]) >>> s.union(s2) Set([1, 2, 3, 26, 47, 127]) >>> s2-s Set([26, 47]) >>> "Hello\n" 'Hello\n' >>> len("Hello\n") 6 >>> len("\n") 1 >>> ord("\n") 10 >>> Administrators-Computer:~ loewis$ cat /etc/passwd ## # User Database [snip] nobody:*:-2:-2:Unprivileged User:/:/usr/bin/false root:*:0:0:System Administrator:/var/root:/bin/sh daemon:*:1:1:System Services:/var/root:/usr/bin/false [snip] Administrators-Computer:~ loewis$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f = open("/etc/passwd", "r") >>> f >>> f.read(5) '##\n# ' >>> f.read(50) 'User Database\n# \n# Note that this file is consulte' >>> f.read(80) 'd when the system is running in single-user\n# mode. At other times this informa' [snip] 'in/false\nsecurityagent:*:92:92:SecurityAgent:/var/empty:/usr/bin/false\nunknown:*' >>> f.read(80) ':99:99:Unknown User:/var/empty:/usr/bin/false\n' >>> f.read(80) '' >>> Administrators-Computer:~ loewis$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> open("/etc/passwd","w") Traceback (most recent call last): File "", line 1, in ? IOError: [Errno 13] Permission denied: '/etc/passwd' >>> f=open("/tmp/passwd","w") >>> f.write("Zeile 1\n") >>> f.write("Zeile 2\n") >>> f.close() >>> Administrators-Computer:~ loewis$ cat /tmp/passwd Zeile 1 Zeile 2 Administrators-Computer:~ loewis$ python Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout ', mode 'w' at 0x2a060> >>> sys.stdout.write("Zeile 3\n") Zeile 3 >>> open("/etc/passwd") >>> f=_ >>> f.close() >>> 3+4*5 23 >>> x=_ >>> x 23 >>> f = open("/etc/passwd") >>> f.readline() '##\n' >>> f.readline() '# User Database\n' >>> f.readline() '# \n' >>> f.readline() '# Note that this file is consulted when the system is running in single-user\n' >>> lines = f.readlines() >>> lines ['# mode. At other times this information is handled by one or more of:\n', '# lookupd DirectoryServices \n', '# By default, lookupd gets information from NetInfo, so this file will \n', "# not be consulted unless you have changed lookupd's configuration.\n", [snip] 'unknown:*:99:99:Unknown User:/var/empty:/usr/bin/false\n'] >>> lines = None >>> print lines None >>> lines == None True >>> >>> def add(a,b): ... return a+b ... >>> add(3,4) 7 >>> s = add(3,4) >>> s 7 >>> addiere = add >>> addiere(10,2) 12 >>> import math >>> d = [math.sin, math.cos] >>> d [, ] >>> d[0] >>> d[0](2) 0.90929742682568171 >>> def mkinc(n): ... def inc(x): ... return x+n ... return inc ... >>> mkinc(1) >>> f=mkinc(1) >>> f(19) 20 >>> f(100) 101 >>> f = mkinc(-1) >>> f(19) 18 >>> mkinc(8)(9) 17 >>>