|
|
Zeile 1: |
Zeile 1: |
− | == Erste Zeile== | + | == Zuweisungsvielfalt== |
− | * Namen | + | Python ermöglicht es mehreren Variablen gleichzeitig Werte zuzuweisen. |
− | ** Bedeutung des _ | + | * Mehrfach Zuweisung eines Wertes: |
− | ** __name__ == __main__ | + | <pre> |
− | * Mehrfach Zuweisung | + | >>> a = b = c = 42 |
− | ** y, x = 1, 2 | + | >>> print(a, b, c) |
| + | 42 42 42 |
| + | >>> |
| + | </pre> |
| + | * Mehrfachzuweisung verschiedener Werte: |
| + | <pre> |
| + | >>> a, b, c = 1, 2, 3 |
| + | >>> print(a, b, c) |
| + | 1 2 3 |
| + | >>> |
| + | </pre> |
| + | * Mehrfachzuweisung aus einer Liste: |
| + | <pre> |
| + | >>> 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 |
| + | >>> |
| + | </pre> |
| + | * Mehrfachzuweisung aus einer Liste mit mehr Werten: |
| + | <pre> |
| + | >>> 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] |
| + | >>> |
| + | </pre> |
| + | |
| + | |
| + | |
| ** x, y, z = [1, 2, 3] | | ** x, y, z = [1, 2, 3] |
| ** 2) Tuple Unpacking (with & without *) | | ** 2) Tuple Unpacking (with & without *) |
Version vom 27. September 2023, 16:33 Uhr
Zuweisungsvielfalt
Python ermöglicht es mehreren Variablen gleichzeitig Werte zuzuweisen.
- Mehrfach Zuweisung eines Wertes:
>>> a = b = c = 42
>>> print(a, b, c)
42 42 42
>>>
- Mehrfachzuweisung verschiedener Werte:
>>> a, b, c = 1, 2, 3
>>> print(a, b, c)
1 2 3
>>>
- Mehrfachzuweisung aus einer Liste:
>>> 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
>>>
- Mehrfachzuweisung aus einer Liste mit mehr Werten:
>>> 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]
>>>
- x, y, z = [1, 2, 3]
- 2) Tuple Unpacking (with & without *)
person = ['tom', 5, 'male']
name, age, gender = person
# name='tom', age=5, gender='male'
We can use tuple unpacking to assign multiple variables in one line.
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
first, second, *others = letters
# first='A', second='B'
# letters = ['C', 'D', 'E', 'F', 'G']
We can add a * character in front of variables to signify that it should contain everything else that hasn’t been unpacked.
https://medium.com/@sarperismetmakas/10-python-shortcuts-6f5c62c53bb1
-
- Tupel
- Als Eingabe für Funktionen
- Als Rückgabewert von Funktionen
- Listen
- Slicing
Sortieren
Mitgliedschaft
Übungen:
https://medium.com/@hannanmentor/pythons-best-code-snippets-c34ed1f07239
Navigation
Zurück zu Micropython Kurs 2023 - Teil 1
Zurück zur "Micropython Kurs 2023" Startseite
Zurück zur Programmieren Startseite
Zurück zur Wiki Startseite