Bearbeiten von „Python“
Zur Navigation springen
Zur Suche springen
Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden.
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 1: | Zeile 1: | ||
[[Kategorie:Sprache]] | [[Kategorie:Sprache]] | ||
= Datentypen = | = Datentypen = | ||
* List: ['a', 1]; x[1] = 5 | |||
* | * Tupel: ('a', 1), ro | ||
== | == Spezielle Methoden/Attribute == | ||
< | * Statische Methoden: | ||
<pre> | |||
class X: | |||
@staticmethod | |||
def doIt(param): | |||
pass | |||
X.doIt(True) | |||
</pre> | |||
* Feststellen, ob Attribut existiert: hasattr(instance, nameOfAttribute) | |||
* | |||
== Dictionary== | == Dictionary== | ||
< | <pre> | ||
x = { 'key' : 'val', 'xyz': 3 } | x = { 'key' : 'val', 'xyz': 3 } | ||
x['key'] = value | x['key'] = value | ||
Zeile 87: | Zeile 23: | ||
contains = 'key' in x and 'key2' not in x | contains = 'key' in x and 'key2' not in x | ||
size = len(x) | size = len(x) | ||
for | for pair in x.iteritems(): | ||
key = pair[0] | |||
for key in x: | for key in x.iterkeys(): | ||
print key | print key | ||
</ | </pre> | ||
* x.itervalues() | * x.itervalues() | ||
* x.setdefault(key[, value]): setzt Wert nur, wenn noch nicht gesetzt | * x.setdefault(key[, value]): setzt Wert nur, wenn noch nicht gesetzt | ||
Zeile 99: | Zeile 35: | ||
== Mengen == | == Mengen == | ||
< | <pre> | ||
s = | s = set(['y', 3]) ; f = frozenset(['y', 3]) | ||
f = frozenset( | |||
for elem in s: | for elem in s: | ||
print elem | print elem | ||
Zeile 116: | Zeile 45: | ||
isPartOf = s <= f | isPartOf = s <= f | ||
diff = s - f | diff = s - f | ||
</ | </pre> | ||
== | == Dateien == | ||
<pre> | |||
with open(self._filename, "r") as fp: | |||
for line in fp: | |||
print line | |||
fp.close() | |||
</pre> | |||
< | |||
for | |||
</ | |||
== Typcheck == | == Typcheck == | ||
< | <pre> | ||
isStringOrSubclass = isinstance(aVariable, str) | isStringOrSubclass = isinstance(aVariable, str) | ||
isString = type(aVariable) is str | isString = type(aVariable) is str | ||
</pre> | |||
</ | |||
== | == Formatierung == | ||
<pre> | |||
< | "{:3.7f} {:s} {:07d}".format(3.14, "hello", 100) | ||
"{0:d} / {0:x}".format(714) | |||
</pre> | |||
</ |