
Ein Überblick
#!/bin/env python print "hello, world!"
hello, world!
i = 1 while i<10: i *= 1.5 print i
1.5 2.25 3.375 5.0625 7.59375 11.390625
def fak(x):
if x<=1:
return 1
else:
return x*fak(x-1)
>>> fak(3) 6 >>> fak(9) 362880 >>> fak(42) 1405006117752879898543142606244511569936384000000000L
import fakmod print fakmod.fak(3)
from fakmod import fak as blubber print blubber(3)
from mathe import fakmod print fakmod.fak(3)oder
from mathe.fakmod import fak print fak(3)
class Auto:
def __init__(self):
self.raeder = 4*["okay"]
def nagelfahren(self, rad):
self.raeder[rad]="platt"
def status(self):
print self.raeder
>>> a = Auto() >>> a.status() ['okay', 'okay', 'okay', 'okay'] >>> a.nagelfahren(3) >>> a.status() ['okay', 'okay', 'okay', 'platt']
from objekt import Auto >>> a=Auto() >>> a.hallo Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: Auto instance has no attribute 'hallo' >>> a.hallo="Gruss an alle" >>> a.hallo 'Gruss an alle'
>>> a <objekt.Auto instance at 0x12b970> >>> class boot: ... def rudern(self): ... print "plätscher..." ... >>> a.__class__=boot >>> a <__main__.boot instance at 0x12b970> >>> a.rudern <bound method boot.rudern of <__main__.boot instance at 0x12b970>> >>> a.rudern() plätscher... >>> a.nagelfahren() Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: boot instance has no attribute 'nagelfahren' >>> a.__class__=Auto >>> a..status() ['okay', 'okay', 'okay', 'okay']
>>>f = lambda x,y: x+y >>> f <function <lambda> at 0xe5440> >>> f(1,2) 3
>>> a = Auto() >>> a() Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: Auto instance has no __call__ method >>> a.__call__ = a.status >>> a() ['okay', 'okay', 'okay', 'okay']
import string
text = """Dies ist ein
mehrzeiliger
String"""
for zeichen in text:
code = ord(zeichen)
print zeichen, ord(zeichen), string.upper(
zeichen)
>>>a = "Ein Text" >>>b = """Ein ...Text, der über ...mehrere ...Zeilen ...geht""" >>> c = """enthält ' und ''', und """ '''sogar """ und "''' >>> print c enthält ' und ''', und sogar """ und " >>> d = u"Unicode \u0042" >>> print d Unicode B >>> e = r"string\0x123" >>> print e 'string\\0x123'
>>> count = 2 >>> language = 'Python' >>> print '%(language)s has %(count)03d quote types.' % vars() Python has 002 quote types. >>> print "%+E" % 3 +3.000000E+00 >>> print "%017i" % 3 00000000000000003
>>> "QNH".encode("rot13")
'DAU'
>>> "Chaos Seminar".encode("uu")
'begin 666 <data>\n-0VAA;W,@4V5M:6YA<@ \n \nend\n'
>>> "Chaos Seminar".encode("base64")
'Q2hhb3MgU2VtaW5hcg==\n'
>>> "Chaos Seminar".encode("Quoted-Printable")
'Chaos=20Seminar'
>>> u"abcäöü".encode("utf-8")
'abc\xc3\xa4\xc3\xb6\xc3\xbc'
""" Datei docstring.py Demonstriert, wie man Docstrings erstellt und anwendet""" def funktion(parameter): """demo-Funktion, die einfach den Parameter ausgibt""" print parameter
>>> import docstring
>>> docstring.__doc__
' Datei docstring.py\nDemonstriert, wie man Docstrings erstellt und anwendet'
>>> docstring.funktion.__doc__
'demo-Funktion, die einfach den Parameter ausgibt'
>>> help(docstring.funktion)
Help on function funktion in module docstring:
funktion(parameter)
demo-Funktion, die einfach den Parameter ausgibt
>>> help(docstring)
Help on module docstring:
NAME
docstring
FILE
/users/student1/s_mschab/public_html/pythonsem/examples/docstring.py
DESCRIPTION
Datei docstring.py
Demonstriert, wie man Docstrings erstellt und anwendet
FUNCTIONS
funktion(parameter)
demo-Funktion, die einfach den Parameter ausgibt
DATA
__file__ = 'docstring.py'
__name__ = 'docstring'
>>> tupel = (2,3,4); liste=[6,7,8] >>> erg = zip(tupel, liste) >>> print erg [(2, 6), (3, 7), (4, 8)] >>> a,b,c = erg >>> print a (2, 6) >>> a = divmod(2,3) >>> print a (0, 2) >>> wert,rest = divmod(2,3) >>> print wert 0
>>> hash = {1:2, "hallo":7j, ((1,2),3):[7,8,9]}
>>> hash[1]
2
>>> hash["hallo"]
7j
>>> hash[1]=7
>>> hash[1]
7
try:
try:
print "alles in Ordnung"
raise "Ausnahme"
print "nie erreicht"
except "Ausnahme":
print "Exception bekommen
finally:
print "fertig"
for i in range(23,42):
print i
for line in iter(open("dateiname)):
print line
for anything in meindictionary.items:
print anything
>>> def otto(start): ... wert = start ... while wert>0: ... yield start ... yield wert ... wert -= 1 ... yield wert ... yield "hallo" ... >>> for i in otto(3): print i, ... 3 3 2 hallo 3 2 1 hallo 3 1 0 hallo
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
>>> [{x: x**2} for x in vec]
[{2: 4}, {4: 16}, {6: 36}]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
>>> [vec1[i]*vec2[i] for i in range(len(vec1))]
[8, 12, -54]
>>> def funktion(a,b=1,c=2,*l,**d):
... print a,b,c,l,d
...
>>> funktion(1)
1 1 2 () {}
>>> funktion(1,2,3,4,5,6,7)
1 2 3 (4, 5, 6, 7) {}
>>> funktion(1,e=2,c=3,d=7)
1 1 3 () {'e': 2, 'd': 7}
from __future__ import generators from __future__ import division from __future__ import nested_scopes