Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lehrkraefte:blc:informatik:glf25:python:zusammenfassung [2025/09/12 14:58] – [Logische Operatoren] Michael Stambach | lehrkraefte:blc:informatik:glf25:python:zusammenfassung [2025/10/20 15:36] (current) – [Arrays (Listen)] Ivo Blöchliger | ||
|---|---|---|---|
| Line 144: | Line 144: | ||
| **Achtung**: | **Achtung**: | ||
| - | Zutreffen lässt sich ein Python-Programm mit < | + | Zutreffen lässt sich ein Python-Programm mit < |
| <code python> | <code python> | ||
| Line 176: | Line 176: | ||
| + | |||
| + | ===== Arrays (Listen) ===== | ||
| + | Eine Liste '' | ||
| + | |||
| + | <code python> | ||
| + | # Array mit 3 Einträgen | ||
| + | a = [23,42,123] | ||
| + | print(f" | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | Einträge können verändert werden: | ||
| + | <code python> | ||
| + | a = [23,42,123] | ||
| + | a[1] = 4321 | ||
| + | # a ist jetzt [23, | ||
| + | </ | ||
| + | |||
| + | Loop über die Einträge eines Arrays: | ||
| + | <code python> | ||
| + | a = [23,42,123] | ||
| + | for e in a: | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | Elemente hinten anfügen: | ||
| + | <code python> | ||
| + | quadrate = [] | ||
| + | for i in range(10): | ||
| + | quadrate.append(i*i) | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | Initialisierung in einer Zeile (ergibt gleiches Array mit Quadratzahlen wie der obige Code): | ||
| + | <code python> | ||
| + | q = [n*n for n in range(10)] | ||
| + | </ | ||
| + | |||
| + | ===== Dictionaries ===== | ||
| + | Dictionaries sind wie Listen, wobei die Elemente aber keine Zahlen als Indizies haben, sondern beliebige Zeichenketten (Schlüssel). Man spricht auch von **Schlüssel-Wert-Paaren**. | ||
| + | |||
| + | Initialisierung und Zugriff | ||
| + | <code python> | ||
| + | person = {" | ||
| + | print(person[" | ||
| + | person[" | ||
| + | |||
| + | schluessel = person.keys() | ||
| + | |||
| + | leer = {} # Leerer Dictionary | ||
| + | </ | ||
| + | |||
| + | Wiederholungen | ||
| + | <code python> | ||
| + | for schluessel in person: | ||
| + | | ||
| + | |||
| + | |||
| + | for schluessel, wert in person.items(): | ||
| + | | ||
| + | |||
| + | </ | ||
| + | |||
| + | Existenz von Schlüsseln überprüfen | ||
| + | <code python> | ||
| + | if " | ||
| + | # tu was | ||
| + | </ | ||