14. Lektion: Variablen vielfalt.extend(): Difference between revisions

From Attraktor Wiki
Jump to navigation Jump to search
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Erste Zeile==
== Zuweisungsvielfalt==
* Namen
Python ermöglicht es mehreren Variablen gleichzeitig Werte zuzuweisen.
** Bedeutung des _
* Mehrfach Zuweisung eines Wertes:
** __name__ == __main__
* Mehrfach Zuweisung
** y, x = 1, 2
** x, y, z = [1, 2, 3]
** 2) Tuple Unpacking (with & without *)
<pre>
<pre>
person = ['tom', 5, 'male']
>>> a = b = c = 42
>>> 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>


name, age, gender = person
== Inhalte tauschen==
# name='tom', age=5, gender='male'
<pre>
>>> x, y = 1, 2
>>> print(x, y)
1 2
>>> y, x = x, y
>>> print(x, y)
2 1
>>>
</pre>


We can use tuple unpacking to assign multiple variables in one line.
== Mysterien==
 
<pre>
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> x = [9,8,7]
 
    y = x
first, second, *others = letters
>>>
# first='A', second='B'
>>> y.append(6)
# letters = ['C', 'D', 'E', 'F', 'G']
    print(y)
 
    print(x)
We can add a * character in front of variables to signify that it should contain everything else that hasn’t been unpacked.
    [9, 8, 7, 6]
    [9, 8, 7, 6]
</pre>
Hier der Grund, es ist das selbe Objekt:
<pre>
>>> id(x)
    537041808
>>> id(y)
    537041808
</pre>
Aber so funktioniert es wie erwartet:
<pre>
>>> x = [9,8,7]
    y = x.copy()
    y.append(6)
    print(y)
    print(x)
    [9, 8, 7, 6]
    [9, 8, 7]
>>>
</pre>
Jetzt sind es verschiedene Objekte.
<pre>
>>> id(x)
    537062496
>>> id(y)
    537063216
>>>
</pre>
</pre>
** Inhalte tauschen
*** x , y = y, x
* Tupel
** Als Eingabe für Funktionen
*** join()
** Als Rückgabewert von Funktionen
*** NTP / RTC
* Listen
** Comprehention
** [[Media:Python's_list_Data_Type.pdf|Python's list Data Type ]]
** https://realpython.com/python-list/
* Slicing
** https://realphyton.com/phyton-list/
* Konstanten
* copy deep und flach
* https://www.heise.de/ratgeber/Phyton-Welche-Auflistungstypen-es-gibt-und-wie-sie-verwendet-werden-9216638.html?seite=all
* https://levelup.gitconnected.com/30-phyton-concepts-i-wish-i-knew-way-earlier-3add72af6433
* Bool
** bool(mylist) - not
===== Sortieren=====
* https://medium.com/@huzaifazahoor654/sorting-with-all-data-structures-in-python-c697a34931c0


===== Mitgliedschaft=====
===== Übung:=====
* in
* Probiere die verschiedenen Möglichkeiten der Wertezuweisung selbst aus.
* Probiere das unter Mysterien dargestellte Scenario mit einfachen Variablen (x = 1).


===== Übungen:=====
== Navigation==
https://medium.com/@hannanmentor/pythons-best-code-snippets-c34ed1f07239
[[Micropython_Kurs_2023_-_Teil_1|Zurück zu Micropython Kurs 2023 - Teil 1]]<br>
[[Micropython Kurs 2023|Zurück zur "Micropython Kurs 2023" Startseite]]<br>
[[Programmieren|Zurück zur Programmieren Startseite]]<br>
[[Attraktor_Wiki|Zurück zur Wiki Startseite]]<br>

Latest revision as of 12:28, 3 October 2023

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]
>>> 

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
>>> 
Übung:
  • Probiere die verschiedenen Möglichkeiten der Wertezuweisung selbst aus.
  • Probiere das unter Mysterien dargestellte Scenario mit einfachen Variablen (x = 1).

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