Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Nächste Überarbeitung
Vorherige Überarbeitung
gf_informatik:python_extras [2024-01-16 10:18] – angelegt hofgf_informatik:python_extras [2026-02-02 06:49] (aktuell) – [Kompakte Zuweisungen] hof
Zeile 1: Zeile 1:
 ## Python Extras ## Python Extras
- 
 ### Kompakte Zuweisungen ### Kompakte Zuweisungen
  
Zeile 15: Zeile 14:
 </code> </code>
  
-Dasselbe funktioniert für alle binären Operatoren (`+`, `-`, `*`, `/`, ...).+Dasselbe funktioniert für alle binären Operatoren (`+`, `-`, `*`, `/`, `//`, `%`, ...). 
 + 
 +### Negative Indices 
 +Der Index bei einem Listenzugriff kann auch negativ sein und nummeriert die Elemente dann vom letzten her. Der Index `-1` zeigt also auf das letzte Element: 
 + 
 +``` 
 + +---+---+---+---+---+---+ 
 + | P | y | t | h | o | n | 
 + +---+---+---+---+---+---+ 
 +             6 
 +-6  -5  -4  -3  -2  -1 
 +``` 
  
 ### List Slicing ### 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!+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: Kurz:
Zeile 25: Zeile 36:
 <code python> <code python>
 a[start:stop]  # items start through stop-1 a[start:stop]  # items start through stop-1
-a[start:     # items start through the rest of the array+a[start:     # items start through the rest of the list
 a[:stop]       # items from the beginning through stop-1 a[:stop]       # items from the beginning through stop-1
-a[:]           # a copy of the whole array+a[:]           # a copy of the whole list
  
 a[start:stop:step] # start through not past stop, by step a[start:stop:step] # start through not past stop, by step
 </code> </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: Dasselbe funktioniert für alle _Sequences_, also neben Listen auch für Strings:
Zeile 38: Zeile 51:
 print(s[7, 11]) print(s[7, 11])
 </code> </code>
 +
 +Was gibt das folgende Snippet aus? Weshalb?
 +
 +<code python>
 +s = '!reverof gnicils tsiL'
 +print(s[::-1])
 +</code>
 +
  • gf_informatik/python_extras.1705400302.txt.gz
  • Zuletzt geändert: 2024-01-16 10:18
  • von hof