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:glf4-20:simulation:python-repe [2021/03/27 09:05] – [Selektion (if, elif, else)] Ivo Blöchliger | lehrkraefte:blc:informatik:glf4-20:simulation:python-repe [2021/04/06 18:15] (current) – [Python Repetition / Cheat Sheet] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python Repetition / Cheat Sheet ====== | ||
| + | |||
| + | Eine minimalistische Version gibt es [[lehrkraefte: | ||
| + | |||
| + | * Gehen Sie Beispiele von oben bis unten durch. | ||
| + | * Kopieren Sie jeweils den Code in TigerJython und führen Sie das jeweilige Programm aus. | ||
| + | * Versuchen Sie, jede Zeile der Programm zu verstehen. Wenn Sie nicht sicher sind, fügen sie '' | ||
| + | * Wenn Sie eine Zeile nicht verstehen, stellen Sie Fragen und/oder " | ||
| + | * Das Ziel ist nicht, alles auswendig zu wissen, aber zu wissen, was möglich ist und wo die Information zu finden ist. | ||
| + | ===== Variablen, Strings vs. Zahlen ===== | ||
| + | === Variablen mit Zahlen === | ||
| + | <code python> | ||
| + | foo = 42 # Zuweisung: Speichert die Zahl 42 in einen " | ||
| + | bar = foo/ | ||
| + | foo = foo + 1 # Rechts erst ausrechnen, Resultat in Variable vor dem = speichern. foo wird dann 43 | ||
| + | print(bar) | ||
| + | print(foo-bar) # Ausgabe 20 | ||
| + | print(" | ||
| + | print(5**3) | ||
| + | print(" | ||
| + | print(2**0.5) | ||
| + | </ | ||
| + | |||
| + | === Variablen mit Strings (Zeichenketten) === | ||
| + | <code python> | ||
| + | foo = " | ||
| + | bar = foo + " | ||
| + | print(bar) | ||
| + | baz = int(foo)+23 # int(String) wandelt in eine Zahl um. Resultat 65 | ||
| + | print(baz) | ||
| + | bla = str(baz)+" | ||
| + | print(bla) | ||
| + | </ | ||
| + | |||
| + | === Formatierte Ausgabe === | ||
| + | <code python> | ||
| + | foo = 42 | ||
| + | print(" | ||
| + | # Ausgabe 42 ist die Antwort und 21 ist nur die halbe Wahrheit | ||
| + | pi = 3.141529 | ||
| + | print(" | ||
| + | # Ausgabe pi als Ganzzahl: 3, als Dezimalzahl: | ||
| + | </ | ||
| + | |||
| + | ===== Wiederholungen ===== | ||
| + | === Bekannte Anzahl === | ||
| + | <code python> | ||
| + | for humpfdidumpf in range(10): | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | Oder Anzahl in Variable: | ||
| + | <code python> | ||
| + | anzahl = 5 | ||
| + | for i in range(anzahl): | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | === Unbekannte Anzahl === | ||
| + | Bis zu welcher Zahl muss man natürliche Zahlen aufsummieren, | ||
| + | <code python> | ||
| + | summe = 0 | ||
| + | zahl = 0 | ||
| + | while summe < 1000: # Wiederholen, | ||
| + | zahl += 1 # Kurz für zahl = zahl+1 | ||
| + | summe += zahl # Kurz für summe = summe + zahl | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Selektion (if, elif, else) ===== | ||
| + | <code python> | ||
| + | for z in range(40): | ||
| + | if z%15 == 0: # z%15 ist der Rest der Division durch 15. Um Gleichheit zu prüfen wird das doppelte Gleichheitszeichen verwendet | ||
| + | print(" | ||
| + | print(" | ||
| + | elif z%5 == 0: # Wenn die erste Bedingung falsch ist, und z%5==0 | ||
| + | print(" | ||
| + | elif z%3 == 0: | ||
| + | print(" | ||
| + | else: # Wenn keine der obigen Bedingungen wahr ist | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | Beachten Sie, dass '' | ||
| + | |||
| + | ===== Listen ===== | ||
| + | Variablen, die Listen enthalten, sollten Bezeichnungen im **plural** erhalten. | ||
| + | === Elemente auslesen und zuweisen === | ||
| + | <code python> | ||
| + | foos = [42, | ||
| + | print(foos) | ||
| + | print(" | ||
| + | # Ausgabe: Erstes Element foos[0] = 42 | ||
| + | foos[2] = 12345 # Drittes Element überschreiben | ||
| + | print(foos) | ||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | === Liste aufbauen mit .append === | ||
| + | <code python> | ||
| + | quadratzahlen = [] # Leere Liste (Name ist plural!) | ||
| + | for i in range(30): | ||
| + | quadratzahlen.append(i*i) | ||
| + | print(" | ||
| + | print(quadratzahlen) | ||
| + | </ | ||
| + | |||
| + | === Elemente durchgehen === | ||
| + | Nur Elemente | ||
| + | <code python> | ||
| + | zahlen = [42, | ||
| + | for zahl in zahlen: | ||
| + | print(zahl) | ||
| + | </ | ||
| + | Nur Index | ||
| + | <code python> | ||
| + | zahlen = [42, | ||
| + | for i in range(len(zahlen)): | ||
| + | print(zahlen[i]) | ||
| + | </ | ||
| + | Index und Element | ||
| + | <code python> | ||
| + | zahlen = [42, | ||
| + | for i,zahl in enumerate(zahlen): | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Funktionen ===== | ||
| + | Ohne Argumente (Parameter) und ohne Rückgabewert. | ||
| + | <code python> | ||
| + | def hallo(): | ||
| + | | ||
| + | |||
| + | print(" | ||
| + | hallo() | ||
| + | hallo() | ||
| + | </ | ||
| + | Mit Argumenten, ohne Rückgabewert | ||
| + | <code python> | ||
| + | def plappermaul(was, | ||
| + | for i in range(wieviel): | ||
| + | print(was) | ||
| + | | ||
| + | plappermaul(" | ||
| + | </ | ||
| + | Ohne Argumente, mit Rückgabewert | ||
| + | <code python> | ||
| + | def meineListe(): | ||
| + | zahlen = [1,1] | ||
| + | for i in range(20): | ||
| + | zahlen.append(zahlen[-1]+zahlen[-2]) | ||
| + | return zahlen | ||
| + | |||
| + | foos = meineListe() | ||
| + | print(foos) | ||
| + | </ | ||
| + | Mit Argumenten, mit Rückgabewert | ||
| + | <code python> | ||
| + | def quadrat(x): | ||
| + | return x*x # Quadrat von x berechnen und als Resultat zurückgeben. | ||
| + | z = 12 | ||
| + | q = quadrat(z) | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Zufallszahlen ===== | ||
| + | Uniform verteilte Ganzzahlen | ||
| + | <code python> | ||
| + | from random import randrange | ||
| + | for i in range(1, | ||
| + | print(" | ||
| + | </ | ||
| + | Uniform verteilte reelle Zahlen im Intervall $[0,1)$. | ||
| + | <code pyton> | ||
| + | from random import random | ||
| + | for i in range(10): | ||
| + | print(random()) | ||
| + | </ | ||
| + | Uniform verteilte reelle Zahlen im Intervall $[a,b)$. | ||
| + | <code pyton> | ||
| + | from random import random | ||
| + | a = 2.71828 | ||
| + | b = 3.14152 | ||
| + | for i in range(10): | ||
| + | print(random()*(b-a)+a) | ||
| + | </ | ||