14. Lektion: Variablen vielfalt.extend(): Unterschied zwischen den VersionenAus Attraktor Wiki
Version vom 3. Oktober 2023, 10:54 UhrInhaltsverzeichnisZuweisungsvielfaltPython ermöglicht es mehreren Variablen gleichzeitig Werte zuzuweisen.
>>> a = b = c = 42 >>> print(a, b, c) 42 42 42 >>>
>>> a, b, c = 1, 2, 3 >>> print(a, b, c) 1 2 3 >>>
>>> a, b, c = [10, 20, 30] >>> print(a, b, c) 10 20 30 >>> z = [1, 2, 3] >>> a, b, c = z >>> print(a, b, c) 1 2 3 >>>
>>> z = [1, 2, 3, 4, 5] >>> a, b, c = z Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack (expected 3) >>> a, b, *c = z >>> print(a, b, c) 1 2 [3, 4, 5] >>> >>> z = [1, 2, 3] >>> a, b, *c = z >>> print(a, b, c) 1 2 [3] >>> Inhalte tauschen>>> x, y = 1, 2 >>> print(x, y) 1 2 >>> y, x = x, y >>> print(x, y) 2 1 >>> Mysterien>>> x = [9,8,7] y = x >>> >>> y.append(6) print(y) print(x) [9, 8, 7, 6] [9, 8, 7, 6] Hier der Grund, es ist das selbe Objekt: >>> id(x) 537041808 >>> id(y) 537041808 Aber so funktioniert es wie erwartet: >>> x = [9,8,7] y = x.copy() y.append(6) print(y) print(x) [9, 8, 7, 6] [9, 8, 7] >>> Jetzt sind es verschiedene Objekte. >>> id(x) 537062496 >>> id(y) 537063216 >>>
Übungen:https://medium.com/@hannanmentor/pythons-best-code-snippets-c34ed1f07239 Zurück zu Micropython Kurs 2023 - Teil 1 |