Control Flow
Control flow: same ideas, cleaner syntax
Section titled “Control flow: same ideas, cleaner syntax”TypeScript’s control flow translates almost word-for-word into Python. The biggest differences are cosmetic: no curly braces (indentation is the structure), elif instead of else if, and match/case instead of switch. Python’s for loop is always a for ... in — there is no C-style for (let i = 0; ...).
if / elif / else
Section titled “if / elif / else”// TypeScriptconst score: number = 85;let grade: string;
if (score >= 90) { grade = "A";} else if (score >= 80) { grade = "B";} else if (score >= 70) { grade = "C";} else { grade = "F";}
console.log(`Grade: ${grade}`); // Grade: B# Pythonscore: int = 85
if score >= 90: grade = "A"elif score >= 80: grade = "B"elif score >= 70: grade = "C"else: grade = "F"
print(f"Grade: {grade}") # Grade: Bfor … in and range()
Section titled “for … in and range()”Python’s for always iterates over an iterable — a list, string, dict, or a range. There is no C-style indexed loop. For index-based access, use enumerate().
// TypeScript// Indexed loopfor (let i = 0; i < 5; i++) { console.log(i);}
// for...of over arrayconst fruits = ["apple", "banana", "cherry"];for (const fruit of fruits) { console.log(fruit);}
// Index + valuefruits.forEach((fruit, i) => console.log(i, fruit));# Python# range() replaces the C-style for loopfor i in range(5): # 0, 1, 2, 3, 4 print(i)
# Iterate over any iterable directlyfruits = ["apple", "banana", "cherry"]for fruit in fruits: print(fruit)
# Index + value with enumerate()for i, fruit in enumerate(fruits): print(i, fruit)range(start, stop, step) mirrors TypeScript patterns:
| TypeScript | Python |
|---|---|
for (let i = 0; i < 10; i++) | for i in range(10) |
for (let i = 1; i <= 10; i++) | for i in range(1, 11) |
for (let i = 0; i < 10; i += 2) | for i in range(0, 10, 2) |
for (let i = 10; i > 0; i--) | for i in range(10, 0, -1) |
while behaves identically — loop until the condition is false.
// TypeScriptlet n: number = 1;while (n <= 5) { console.log(n); n++;}# Pythonn: int = 1while n <= 5: print(n) n += 1 # no ++ operator in PythonNote: Python has no ++ or -- operators. Use += 1 and -= 1.
match / case — Python’s switch
Section titled “match / case — Python’s switch”Python 3.10 introduced match/case, which is more powerful than TypeScript’s switch. It supports OR patterns with |, wildcard _, and structural pattern matching (matching shapes of objects).
// TypeScript switchconst command: string = "quit";
switch (command) { case "start": console.log("Starting..."); break; case "stop": case "quit": console.log("Stopping"); break; default: console.log(`Unknown: ${command}`);}# Python match/case (Python 3.10+)command: str = "quit"
match command: case "start": print("Starting...") case "stop" | "quit": # OR pattern — no fallthrough needed print("Stopping") case _: # wildcard — like default print(f"Unknown: {command}")Unlike switch, match does NOT fall through. You never need break. The | character combines cases in a single case clause.
Try it
Section titled “Try it”score = 85if score >= 90: grade = "A"elif score >= 80: grade = "B"elif score >= 70: grade = "C"else: grade = "F"print(f"Grade: {grade}")
# for + rangeprint("Counting:")for i in range(1, 6): print(f" {i}")
# for + enumeratefruits = ["apple", "banana", "cherry"]print("Fruits:")for i, fruit in enumerate(fruits): print(f" {i}: {fruit}")
# match/casefor cmd in ["start", "quit", "help"]: match cmd: case "start": result = "Starting..." case "stop" | "quit": result = "Stopping" case _: result = f"Unknown: {cmd}" print(result)Loading Python runtime (first run only)…