Administrators-Computer:~ loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = { "a": 10, "b":20} >>> x {'a': 10, 'b': 20} >>> x['a'] 10 >>> x['a'] = 109 >>> x {'a': 109, 'b': 20} >>> x['c'] Traceback (most recent call last): File "", line 1, in KeyError: 'c' >>> len(x) 2 >>> k.keys() Traceback (most recent call last): File "", line 1, in NameError: name 'k' is not defined >>> x.keys() ['a', 'b'] >>> x.values() [109, 20] >>> x['c']=109 >>> x {'a': 109, 'c': 109, 'b': 20} >>> x.values() [109, 109, 20] >>> x.items() [('a', 109), ('c', 109), ('b', 20)] >>> x.has_key('c') True >>> 'c' in x True >>> m = [2,3,5,7,11,13] >>> 7 in m True >>> 8 in m False >>> m [2, 3, 5, 7, 11, 13] >>> m[2] 5 >>> m[-2] 11 >>> m[2] 5 >>> del m[2] >>> m [2, 3, 7, 11, 13] >>> del x['c'] >>> x {'a': 109, 'b': 20} >>> del m >>> m Traceback (most recent call last): File "", line 1, in NameError: name 'm' is not defined >>> set([2,3,7,11,2]) set([11, 2, 3, 7]) >>> set() set([]) >>> set([2,3,7,11,2]) set([11, 2, 3, 7]) >>> s = set([2,3,7,11,2]) >>> 2 in x False >>> 2 in s True >>> 4 in s False >>> s.add(89) >>> s set([11, 89, 2, 3, 7]) >>> s2 = set((4,5,6)) >>> s set([11, 89, 2, 3, 7]) >>> s2 set([4, 5, 6]) >>> s.union(s2) set([2, 3, 4, 5, 6, 7, 11, 89]) >>> Administrators-Computer:~ loewis$ mkdir h Administrators-Computer:~ loewis$ cd h Administrators-Computer:h loewis$ ls Administrators-Computer:h loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f=open("foo","w") >>> f >>> f.write("Hallo") >>> f.close() >>> Administrators-Computer:h loewis$ ls foo Administrators-Computer:h loewis$ mkdir k Administrators-Computer:h loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f=open("k/bar","w") >>> f.write("Welt") >>> f.close() >>> Administrators-Computer:h loewis$ ls foo k Administrators-Computer:h loewis$ ls k/ bar Administrators-Computer:h loewis$ cat k/bar WeltAdministrators-Computer:h loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f=open("k/bar") >>> f.read() 'Welt' >>> f.close() >>> f=open("foo") >>> f.read() 'Hallo' >>> f.close() >>> f=open("/etc/passwd") >>> t = f.read() >>> f.close() >>> type(t) >>> len(t) 2888 >>> t[100:] 'in single-user mode. At other times this information is provided by\n# Open Directory.\n#\n#[truncated for log]' >>> Administrators-Computer:h loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> f=open("/etc/passwd") >>> f >>> f=open("foo","w") >>> f >>> f.write("Hallo") >>> f.close() >>> f=open("foo","w") >>> f.write("Hallo, Welt") >>> f.flush() >>> f.write("Hallo, Welt") >>> f.write("Hallo, Welt") >>> f.write("Hallo, Welt") >>> f.close() >>> f=open("/etc/passwd") >>> s=f.read() >>> type(s) >>> len(s) 2888 >>> f.close() >>> f=open("/etc/passwd") >>> s=f.readlines() >>> f.close() >>> type(s) >>> len(s) 53 >>> s[:3] ['##\n', '# User Database\n', '# \n'] >>> f=open("/etc/passwd") >>> f.readline() '##\n' >>> f.readline() '# User Database\n' >>> f.readline() '# \n' >>> f.readline() '# Note that this file is consulted directly only when the system is running\n' >>> x = [2,3,5,7,9] >>> for a in x: ... print a,a*a ... 2 4 3 9 5 25 7 49 9 81 >>> for i in range(len(x)): ... print i, x[i] ... 0 2 1 3 2 5 3 7 4 9 >>> d = { "a":1, "b":2} >>> >>> for k in d.keys(): ... print k ... a b >>> for k in d: ... print k ... a b >>> for k in d: ... print k, d[k] ... a 1 b 2 >>> d.items() [('a', 1), ('b', 2)] >>> for i in d.items(): ... print i ... ('a', 1) ('b', 2) >>> for key,value in d.items(): ... print key,value ... a 1 b 2 >>> x [2, 3, 5, 7, 9] >>> list(enumerate(x)) [(0, 2), (1, 3), (2, 5), (3, 7), (4, 9)] >>> for indx, value in enumerate(x): ... print indx, value ... 0 2 1 3 2 5 3 7 4 9 >>> f = open("/etc/passwd") >>> c = 0 >>> for line in f.readlines(): ... c += len(line) ... >>> c 2888 >>> for line in f.readlines() KeyboardInterrupt >>> f.close() >>> f = open("/etc/passwd") >>> for line in f: ... c += len(line) ... >>> c 5776 >>> Administrators-Computer:h loewis$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> None >>> type(None) >>> x = None >>> y = None >>> x is y True >>> class VBaumListe: ... def __init__(self, baum, restliste): ... self.baum=baum ... self.restliste=restliste ... >>> liste = None >>> liste = VBaumListe("foo.txt", liste) >>> liste = VBaumListe("bar.txt", liste) >>> liste.baum 'bar.txt' >>> liste.restliste <__main__.VBaumListe instance at 0x5e1c0> >>> liste.restliste.baum 'foo.txt' >>> print liste.restliste.restliste None >>> liste.restliste.restliste >>> def name(): ... return "Mustermann" ... >>> name() 'Mustermann' >>> s=name() >>> s 'Mustermann' >>> name=name2 Traceback (most recent call last): File "", line 1, in NameError: name 'name2' is not defined >>> name2=name >>> name2 >>> name2() 'Mustermann' >>> import math >>> math.sin(3) 0.14112000805986721 >>> functions = [math.sin, math.cos] >>> functions[0](3.14) 0.0015926529164868282 >>> functions[1](3.14) -0.9999987317275395 >>> def mkinc(n): ... def inc(m): ... return m+n ... return inc ... >>> mkinc(1) >>> mehr= mkinc(1) >>> mehr(5) 6 >>> mkinc(2)(8) 10 >>>