| Konzept | JavaScript | Python | Anmerkung |
|---|---|---|---|
| Codeblock-Ende | ; (optional) |
Zeilenumbruch | Keine Semikolons in Python nötig |
| Codeblöcke | Geschweifte Klammern { ... } |
Einrückung (Indentation) mit : |
Pflicht! 4 Leerzeichen pro Ebene |
| Variablen | const x = 10; / let y = 20; |
x = 10 / y = 20 |
Keine Keywords zur Deklaration |
| Konsolen-Ausgabe | console.log("Hallo"); |
print("Hallo") |
print() ist eine globale Funktion |
| Kommentare | // Text oder /* Text */ |
# Text oder """ Text """ |
Triple-Quotes auch für Docstrings |
| Booleans & Null | true, false, null, undefined |
True, False, None |
Großschreibung beachten |
| Konzept | JavaScript | Python | Anmerkung |
|---|---|---|---|
| Arrays / Listen | const arr = [1, 2, 3]; |
arr = [1, 2, 3] |
In Python: List |
| Element anhängen | arr.push(4); |
arr.append(4) |
Methode heißt anders |
| Listen-Länge | arr.length |
len(arr) |
Globale Funktion len() nutzen |
| Objects / Dicts | const obj = { key: "val" }; |
obj = {"key": "val"} |
In Python: Dictionary (Keys in Quotes) |
| Unveränderlich | Nicht nativ | tpl = (1, 2, 3) |
In Python: Tuple |
| Konzept | JavaScript | Python | Anmerkung |
|---|---|---|---|
| If / Else If | if (x > 5) { ... } else if (x == 2) { ... } |
if x > 5: elif x == 2: |
elif statt else if, keine Runden Klammern |
| UND / ODER | && und || |
and und or |
Ausgeschriebene englische Wörter |
| Negation | !isReady |
not is_ready |
Wort not nutzen |
| Schleifen (Array) | for (const item of items) { ... } |
for item in items: |
Sehr elegante, klare Syntax |
| Zählschleife | for (let i = 0; i < 10; i++) { ... } |
for i in range(10): |
range(10) erzeugt Zahlen 0 bis 9 |
| Konzept | JavaScript | Python | Anmerkung |
|---|---|---|---|
| Funktion definieren | function name(p) { return x; } |
def name(p): return x |
Schlüsselwort def |
| Arrow / Lambda | (a, b) => a + b |
lambda a, b: a + b |
In Python seltener zwingend nötig |
| String Interpolation | `Wert: ${val}` |
f"Wert: {val}" |
Sogenannte f-Strings (f vor Anführungszeichen) |
const items = ["A", "B", "C"];
function processItems(list) {
for (const item of list) {
if (item === "B") {
console.log(`Gefunden: ${item}`);
} else {
console.log("Weiter...");
}
}
}
processItems(items);
items = ["A", "B", "C"]
def process_items(list_input):
for item in list_input:
if item == "B":
print(f"Gefunden: {item}")
else:
print("Weiter...")
process_items(items)