Wir lernen Python Teil 1


$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 == None
False
>>> int(None)
Traceback (most recent call last):
File "", line 1, in
TypeError: int() argument must be a string or a number, not 'NoneType'
>>> bool(None)
False
>>> bool(0)
False
>>> bool(1)
True
>>> if None:
... print "is nicht"
...
>>>
>>> if 1:
... print "is nicht"
...
is nicht
>>> if "test" == "test2":
... print "nö"
...
>>> if "test" == "test":
... print "nö"
...

>>> if "test" != "test":
... print "nö"
...
>>> for i in range(0,101)
File "", line 1
for i in range(0,101)
^
SyntaxError: invalid syntax

>>> for i in range(0,101):
... print i,
...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> g = [7, 90, 23]
>>> for i in g:
... print i,
...
7 90 23
>>> for i in g:
... print i
...
7
90
23
>>> j = {"red": "#f00", "blue": "#00f"}
>>> for i in j:
... print i
...
blue
red
>>> for i in j.items():
... print i
...
('blue', '#00f')
('red', '#f00')
>>> for k,v in j.items():
... print "%s=>%s" % (k,v)
...
blue=>#00f
red=>#f00
>>> for k,v in j.items():
... print k,v
...
blue #00f
red #f00
>>> for k,v in j.items():
... print k + v
...
blue#00f
red#f00
>>> r, d = (9, 90)
>>> r
9
>>>
>>> d
90
>>> r+d
99
>>> for i in range(0,101)
File "", line 1
for i in range(0,101)
^
SyntaxError: invalid syntax
>>> for i in range(0,101):
... print i, i+i
...
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
11 22
12 24
13 26
14 28
15 30
16 32
17 34
18 36
19 38
20 40
21 42
22 44
23 46
24 48
25 50
26 52
27 54
28 56
29 58
30 60
31 62
32 64
33 66
34 68
35 70
36 72
37 74
38 76
39 78
40 80
41 82
42 84
43 86
44 88
45 90
46 92
47 94
48 96
49 98
50 100
51 102
52 104
53 106
54 108
55 110
56 112
57 114
58 116
59 118
60 120
61 122
62 124
63 126
64 128
65 130
66 132
67 134
68 136
69 138
70 140
71 142
72 144
73 146
74 148
75 150
76 152
77 154
78 156
79 158
80 160
81 162
82 164
83 166
84 168
85 170
86 172
87 174
88 176
89 178
90 180
91 182
92 184
93 186
94 188
95 190
96 192
97 194
98 196
99 198
100 200
>>>
>>>
>>> "abc".len()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'len'
>>> dir("abc")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> "abc".replace("a","b")
'bbc'
>>> dir(0)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> 0.__type__
File "", line 1
0.__type__
^
SyntaxError: invalid syntax
>>> 0.__class__
File "", line 1
0.__class__
^
SyntaxError: invalid syntax
>>> (0).__class__

>>> (0).__sub__(10)
-10
>>> class Foo(object):
... def __sub__(self, other):
... print "minus", other, "und so"
...
>>> foo = Foo()
>>> foo - 10
minus 10 und so
>>> foo.__sub__(10)
minus 10 und so
>>> class Foo(object):
... def __sub__(self, other):
... print "minus", other, "und so"
... def test(self, blub):
... print "arrghhh", blub
...
>>> foo = Foo()
>>> foo.test(56)
arrghhh 56
>>> foo.test(foo)
arrghhh <__main__.Foo object at 0x7f9bec5e8390>
>>> foo.test
>
>>> u =foo.test
>>> u(78)
arrghhh 78
>>> u.__type__
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'function' object has no attribute '__type__'
>>> u.__class__

>>> Foo.test.__class__

>>>
>>>
>>>
>>> import datetime
>>> datetime

>>> datetime.date

>>> datetime.date.day

>>> d =datetime.date()
Traceback (most recent call last):
File "", line 1, in
TypeError: Required argument 'year' (pos 1) not found
>>> datetime.date.today

>>> datetime.date.today.()
File "", line 1
datetime.date.today.()
^
SyntaxError: invalid syntax
>>> datetime.date.today()
datetime.date(2011, 7, 29)
>>> from datetime import *
>>> date.today()
datetime.date(2011, 7, 29)
>>> from sys import stdout
>>> stdout
', mode 'w' at 0x7f9bec67b150>
>>> stdout.write("dooh")
dooh>>>
>>>
>>> from sys import stdout as ooo
>>> ooo.write("dooh")
doohimport datetime as d
>>> d.date.today()
datetime.date(2011, 7, 29)
>>> d = d.date.today()
>>> dir(d)
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'ctime', 'day', 'fromordinal', 'fromtimestamp', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'year']
>>> d.dsay
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'datetime.date' object has no attribute 'dsay'
>>> d.day
29
>>> repr

>>> repr(d)
'datetime.date(2011, 7, 29)'
>>> str(d)
'2011-07-29'
>>> d.__str__()
'2011-07-29'
>>> d.__repr__()
'datetime.date(2011, 7, 29)'
>>> d1 = d+1
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'
>>> d1 = d+ datetime.timedelta(1)
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
>>> datetime

>>> import datetime
>>> datetime

>>> d1 = d+ datetime.timedelta(1)
>>> d1
datetime.date(2011, 7, 30)
>>> d1 = d + datetime.timedelta(1,12)
>>> d1
datetime.date(2011, 7, 30)
>>> d1 = d + datetime.timedelta(1,12) + datetime.timedelta(1,12)
>>> d1
datetime.date(2011, 7, 31)
>>> d1 = d + datetime.timedelta(1,12) + datetime.timedelta(0,12)
>>> d1
datetime.date(2011, 7, 30)
>>> d = datetime.date.today()
>>> d
datetime.date(2011, 7, 29)
>>> d = datetime.datetime.today()
>>> d
datetime.datetime(2011, 7, 29, 10, 6, 29, 522852)
>>> d1 = d + datetime.timedelta(1,12) + datetime.timedelta(0,12)
>>> d1
datetime.datetime(2011, 7, 30, 10, 6, 53, 522852)
>>> d1 = d + datetime.timedelta(1,12)
>>> d1
datetime.datetime(2011, 7, 30, 10, 6, 41, 522852)
>>> d1 = d + datetime.timedelta(1, hours=12)
>>> d1
datetime.datetime(2011, 7, 30, 22, 6, 29, 522852)
>>> d1 = d + datetime.timedelta(days=1, hours=12)
>>> d - d1
datetime.timedelta(-2, 43200)
>>> d1 - d
datetime.timedelta(1, 43200)
>>> "test" != "test""test" != "test""test" != "test"
False
>>>
>>>
>>>
>>> if 1 or function() :
...
File "", line 2

^
IndentationError: expected an indented block
>>> 1 or 0
1
>>>
>>> 1 or stdout.write("ya")
1
>>> 0 or stdout.write("ya")
ya>>>
>>>
>>>
>>> 3 / 2
1
>>> 3. / 2
1.5
>>> 3 / 2.
1.5
>>> import division from __future__
File "", line 1
import division from __future__
^
SyntaxError: invalid syntax
>>> from __future__ import division
>>>
>>>
>>>
>>> 3 / 2
1.5
>>> 3 // 2
1
>>> 3 / 2
1.5
>>> 3 // 2
1
>>> 3.0 // 2
1.0
>>> 3. // 2
1.0
>>> 3.1 // 2
1.0
>>> 1+2
3
>>> 1+2.0
3.0
>>> i=1+2.0
>>> i
3.0
>>> int(i)
3
>>> str(i)
'3.0'
>>> "tt" + i
Traceback (most recent call last):
File "", line 1, in
TypeError: cannot concatenate 'str' and 'float' objects
>>> "tt"+"i"
'tti'
>>> str(i) + "tt"
'3.0tt'
>>> "t" * 40
'tttttttttttttttttttttttttttttttttttttttt'
>>> ", ".join(["red", "blue", "zoo"])
'red, blue, zoo'
>>> "- ".join(["red", "blue", "zoo"])
'red- blue- zoo'
>>> "".join(["red", "blue", "zoo"])
'redbluezoo'
>>> ", ".join(["red", "blue", "zoo"]).split(", ")
['red', 'blue', 'zoo']
>>> ["red", "blue", "zoo"]
['red', 'blue', 'zoo']
>>> [i[0], i in ["red", "blue", "zoo"]]
Traceback (most recent call last):
File "", line 1, in
TypeError: 'float' object is unsubscriptable
>>> [i[0] where i in ["red", "blue", "zoo"]]
File "", line 1
[i[0] where i in ["red", "blue", "zoo"]]
^
SyntaxError: invalid syntax
>>> [i[0] for i in ["red", "blue", "zoo"]]
['r', 'b', 'z']
>>> [i[0] for i in ["red", "blue", "zoo"]]
['r', 'b', 'z']
>>> "".join([i[0] for i in ["red", "blue", "zoo"]])
'rbz'
>>> [i[0] for i in ["red", "blue", "zoo"]]
['r', 'b', 'z']
>>> map( lambda i: i[0],["red", "blue", "zoo"])
['r', 'b', 'z']
>>> lambda i: i[0]
at 0x7f9bec5ea848>
>>> f = lambda i: i[0]
>>> f("abc")
'a'
>>> def f(i):
... return i[0]
...
>>> f("abc")
'a'
>>> map( f,["red", "blue", "zoo"])
['r', 'b', 'z']
>>> f

>>> del f
>>> f
Traceback (most recent call last):
File "", line 1, in
NameError: name 'f' is not defined
>>> c = [0]
>>> c[0] = 5
>>> c
[5]
>>> d = c
>>> del c
>>> d
[5]
>>> c = [0]
>>> d = c
>>> d is c
True
>>> d is [0]
False
>>> d = [0]
>>> c == d
True
>>> c is d
False
>>> c[0] = 5
>>> c == d
False
>>> c is d
False
>>> # hjhj
...
>>> """sdfsdfs
... sdfsdf
... sdfsdfsd
... """
'sdfsdfs\nsdfsdf\nsdfsdfsd\n'
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help(map)

>>> def h():
... """hier ist die hilfe"""
... return 0
...
>>> h()
0
>>> help(h)

>>> "ttt
File "", line 1
"ttt
^
SyntaxError: EOL while scanning string literal
>>> print <", line 1
print <>>