Python: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „Kategorie:Sprache = Datentypen = * List: ['a', 1]; x[1] = 5 * Tupel: ('a', 1), ro == Dictionary== <pre> x = { 'key' : 'val', 'xyz': 3 } x['key'] = value del …“)
 
Zeile 42: Zeile 42:
fp.close()
fp.close()
</pre>
</pre>
== Typcheck ==
<pre>
isStringOrSubclass = isinstance(aVariable, str)
isString = type(aVariable) is str

Version vom 1. Februar 2013, 00:33 Uhr

Datentypen

  • List: ['a', 1]; x[1] = 5
  • Tupel: ('a', 1), ro

Dictionary

x = { 'key' : 'val', 'xyz': 3 }
x['key'] = value
del x['key'];
contains = 'key' in x and 'key2' not in x
size = len(x)
for pair in x.iteritems():
  key = pair[0]
for key in x.iterkeys():
  print key
  • x.itervalues()
  • x.setdefault(key[, value]): setzt Wert nur, wenn noch nicht gesetzt
  • x.keys(), x.values()
  • x.copy(): flache Kopie
  • x.update(dict): addiert dict zu x

Mengen

s = set(['y', 3]) ; f = frozenset(['y', 3]) 
for elem in s:
  print elem
size = len(s)
contains = 3 in s and 4 not in s
intersection = s & f
union = s | f
isPartOf = s <= f
diff = s - f

Dateien

with open(self._filename, "r") as fp:
  for line in fp:
     print line
fp.close()

Typcheck

isStringOrSubclass = isinstance(aVariable, str)
isString = type(aVariable) is str