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:10] Ivo Blöchliger [Selektion (if, elif, else)] |
lehrkraefte:blc:informatik:glf4-20:simulation:python-repe [2021/04/06 20:15] Ivo Blöchliger [Python Repetition / Cheat Sheet] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Python Repetition / Cheat Sheet ====== | ====== 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, Strings vs. Zahlen ===== | ||
=== Variablen mit Zahlen === | === Variablen mit Zahlen === | ||
Line 9: | Line 16: | ||
print(bar) | print(bar) | ||
print(foo-bar) # Ausgabe 20 | print(foo-bar) # Ausgabe 20 | ||
+ | print(" | ||
+ | print(5**3) | ||
+ | print(" | ||
+ | print(2**0.5) | ||
</ | </ | ||
Line 64: | Line 75: | ||
if z%15 == 0: # z%15 ist der Rest der Division durch 15. Um Gleichheit zu prüfen wird das doppelte Gleichheitszeichen verwendet | 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(" | ||
- | print(" | + | print(" |
elif z%5 == 0: # Wenn die erste Bedingung falsch ist, und z%5==0 | elif z%5 == 0: # Wenn die erste Bedingung falsch ist, und z%5==0 | ||
print(" | print(" | ||
Line 71: | Line 82: | ||
else: # Wenn keine der obigen Bedingungen wahr ist | else: # Wenn keine der obigen Bedingungen wahr ist | ||
print(" | print(" | ||
- | print(" | + | 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) | ||
+ | </ | ||
+ |