Seite anzeigenÄltere VersionenLinks hierherCopy this pageFold/unfold allNach oben Diese Seite ist nicht editierbar. Du kannst den Quelltext sehen, jedoch nicht verändern. Kontaktiere den Administrator, wenn du glaubst, dass hier ein Fehler vorliegt. ## Python Extras ### Kompakte Zuweisungen Oft wollen wir eine Variable um einen Wert erhöhen oder verringern. Bisher schreiben wir meist etwas wie: <code python> index = index + 1 </code> Weil das Muster so oft auftritt, gibt es eine abgekürzte Syntax dafür: <code python> index += 1 </code> Dasselbe funktioniert für alle binären Operatoren (`+`, `-`, `*`, `/`, ...). ### List Slicing Die `for`-Schleife ist ja viel kompakter und weniger fehleranfällig, als die `while`-Schleife - aber dafür auch weniger flexibel. Ist "jedes zweite Element" mit `for` möglich? Lies auf [[https://stackoverflow.com/questions/509211/how-slicing-in-python-works|Stackoverflow]] diese Antwort über Slicing! Kurz: <code python> a[start:stop] # items start through stop-1 a[start:] # items start through the rest of the list a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole list a[start:stop:step] # start through not past stop, by step </code> Löse [[gf_informatik:programmieren_iii#aufgabe_h3|Aufgabe H3]] mit Slicing! Dasselbe funktioniert für alle _Sequences_, also neben Listen auch für Strings: <code python> s = 'Hallo, Welt!' print(s[7, 11]) </code> gf_informatik/python_extras.txt Zuletzt geändert: 2024-02-04 09:03von hof