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:pyhton-cheatsheet [2022/06/13 08:07] – Ivo Blöchliger | lehrkraefte:blc:informatik:pyhton-cheatsheet [2022/06/16 10:17] (current) – [Python Cheat-Sheet] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python Cheat-Sheet ====== | ||
| + | Eine Zusammenfassung der wichtigsten Python-Elemente. | ||
| + | Es gibt aber online jede Menge solcher Cheat-Sheets, | ||
| + | * https:// | ||
| + | |||
| + | ===== Wiederholungen ===== | ||
| + | ==== for-Schlaufe ==== | ||
| + | === mit range === | ||
| + | <code python> | ||
| + | for i in range(10): | ||
| + | print(i) | ||
| + | for i in range(10, | ||
| + | print(i) | ||
| + | for i in range(10, | ||
| + | print(i) | ||
| + | </ | ||
| + | === mit String === | ||
| + | <code python> | ||
| + | for buchstabe in " | ||
| + | print(buchstabe) | ||
| + | </ | ||
| + | === mit Array === | ||
| + | <code python> | ||
| + | for element in [2, | ||
| + | print(element) | ||
| + | for index, element in enumerate([2, | ||
| + | print(" | ||
| + | </ | ||
| + | ==== while-Schlaufe ==== | ||
| + | <code python> | ||
| + | i = 0 | ||
| + | while i< | ||
| + | i=1+i*2 | ||
| + | print(i) | ||
| + | </ | ||
| + | ==== break und continue==== | ||
| + | * '' | ||
| + | * '' | ||
| + | <code python> | ||
| + | for i in range(20): | ||
| + | if i==10: | ||
| + | break | ||
| + | print(i) | ||
| + | for i in range(10): | ||
| + | if i>4 and i<8: | ||
| + | continue | ||
| + | print(i) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Arrays und Tuples ===== | ||
| + | Arrays werden mit '' | ||
| + | |||
| + | Der Zugriff auf einzelne Elemente erfolgt immer mit '' | ||
| + | <code python> | ||
| + | a = [10, | ||
| + | b = [10*(i+1) for i in range(4)] | ||
| + | print(a[1]) # gibt 20 aus | ||
| + | a[1]=42 | ||
| + | print(a) | ||
| + | |||
| + | a.append(123) | ||
| + | print(len(a)) | ||
| + | |||
| + | c = a[2:] # Unterliste ab 3. Element | ||
| + | c = a[:2] # Unterliste bis und mit 2. Element | ||
| + | c = a[0:: | ||
| + | c = a[:: | ||
| + | </ | ||
| + | |||
| + | ===== Strings ===== | ||
| + | <code python> | ||
| + | a = "Hallo Welt!" | ||
| + | print(len(a)) | ||
| + | b = (a+" " | ||
| + | w = a[6: | ||
| + | w = a[:: | ||
| + | w = a.upper() | ||
| + | ord(" | ||
| + | chr(65) | ||
| + | </ | ||
| + | ===== Formatierte Ausgabe mit % ===== | ||
| + | |||
| + | Der generelle Syntax ist | ||
| + | String % Liste | ||
| + | und kann auch ausserhalb eines Print-Statements stehen. | ||
| + | <code python> | ||
| + | print(" | ||
| + | # Ganzzahlen: | ||
| + | for f in [" | ||
| + | print((" | ||
| + | | ||
| + | # Komma-Zahlen | ||
| + | for f in [" | ||
| + | print((" | ||
| + | |||
| + | </ | ||
| + | Produziert folgende Ausgabe: | ||
| + | <code txt> | ||
| + | 2 %-Zeichen werden als eines ausgegeben | ||
| + | %d -> | ||
| + | %6d -> | ||
| + | %06d -> | ||
| + | %x -> | ||
| + | %f -> | ||
| + | %.2f -> | ||
| + | %16.3f -> | ||
| + | %.20f -> | ||
| + | </ | ||
| + | ===== Zufallszahlen ===== | ||
| + | |||
| + | <code python> | ||
| + | import random | ||
| + | |||
| + | print(" | ||
| + | print([random.random() for i in range(10)]) | ||
| + | |||
| + | print(" | ||
| + | print([random.randrange(6) for i in range(10)]) | ||
| + | |||
| + | print(" | ||
| + | print([random.randrange(10, | ||
| + | |||
| + | print(" | ||
| + | print([random.randrange(10, | ||
| + | |||
| + | </ | ||
| + | produziert z.B. folgende Ausgabe | ||
| + | <code txt> | ||
| + | Kommazahlen zwischen 0.0 und 1.0: | ||
| + | [0.0658331907830767, | ||
| + | |||
| + | Ganze Zahlen zwischen 0 und 5 inklusive: | ||
| + | [1, 4, 0, 4, 5, 1, 0, 5, 1, 1] | ||
| + | |||
| + | Ganze Zahlen zwischen 10 und 12 inklusive: | ||
| + | [12, 11, 12, 11, 10, 10, 10, 12, 11, 11] | ||
| + | |||
| + | Gerade Zahlen zwischen 10 und 20 inklusive: | ||
| + | [10, 18, 10, 12, 12, 10, 20, 12, 12, 20] | ||
| + | </ | ||