Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| lehrkraefte:blc:informatik:glf24:python:cheat-sheet [2024/10/29 14:11] – [Formatierte Ausgabe] Ivo Blöchliger | lehrkraefte:blc:informatik:glf24:python:cheat-sheet [2024/11/04 21:06] (current) – [Formatierte Ausgabe] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python Cheat Sheet ====== | ||
| + | ===== Variablen und Operationen ===== | ||
| + | <code python> | ||
| + | a = 42 # Zuweisung | ||
| + | a = a + 1 # Variable um 1 erhöhen | ||
| + | b = a % 9 # Rest der Division von (hier 43 durch 9) also 7. | ||
| + | c = b*7 # Multiplikation | ||
| + | d = b/2 # Division | ||
| + | c = a**2 + b**2 # Potenzen | ||
| + | w = c**0.5 | ||
| + | </ | ||
| + | ===== Verzweigungen ===== | ||
| + | <code python> | ||
| + | a = 3 | ||
| + | if a>2: | ||
| + | | ||
| + | else: | ||
| + | | ||
| + | |||
| + | b=5 | ||
| + | if a%2==0 and b%2==0: | ||
| + | | ||
| + | |||
| + | if a%2==1 or b< | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | ===== Wiederholungen ===== | ||
| + | <code python> | ||
| + | n = 10 | ||
| + | i = 0 | ||
| + | while i<n: | ||
| + | | ||
| + | i = i+1 # Laufvariable am Schluss erhöhen nicht vergessen! | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Formatierte Ausgabe ===== | ||
| + | <code python> | ||
| + | a = 3.3453521 | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | ===== Zufallszahlen ===== | ||
| + | Ganzzahlen | ||
| + | <code python> | ||
| + | from random import randrange | ||
| + | z = randrange(10) | ||
| + | print(z) | ||
| + | </ | ||
| + | |||
| + | «Reelle» Zahlen zwischen 0 und 1: | ||
| + | <code python> | ||
| + | from random import random | ||
| + | z = random() | ||
| + | print(z) | ||
| + | </ | ||