Skip to content

Control Flow

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; ...).

TypeScript
// TypeScript
const 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
Python
# Python
score: 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: B

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
// TypeScript
// Indexed loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// for...of over array
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
// Index + value
fruits.forEach((fruit, i) => console.log(i, fruit));
Python
# Python
# range() replaces the C-style for loop
for i in range(5): # 0, 1, 2, 3, 4
print(i)
# Iterate over any iterable directly
fruits = ["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:

TypeScriptPython
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.

TypeScript
// TypeScript
let n: number = 1;
while (n <= 5) {
console.log(n);
n++;
}
Python
# Python
n: int = 1
while n <= 5:
print(n)
n += 1 # no ++ operator in Python

Note: Python has no ++ or -- operators. Use += 1 and -= 1.

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
// TypeScript switch
const 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
# 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.

if/elif/else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Grade: {grade}")
# for + range
print("Counting:")
for i in range(1, 6):
print(f" {i}")
# for + enumerate
fruits = ["apple", "banana", "cherry"]
print("Fruits:")
for i, fruit in enumerate(fruits):
print(f" {i}: {fruit}")
# match/case
for cmd in ["start", "quit", "help"]:
match cmd:
case "start":
result = "Starting..."
case "stop" | "quit":
result = "Stopping"
case _:
result = f"Unknown: {cmd}"
print(result)
What is the Python equivalent of TypeScript's `else if`?
Which of the following is the correct Python equivalent of `for (let i = 1; i <= 5; i++)`?
In a Python `match`/`case`, what happens after a matching case executes?
How do you get both the index and value while iterating a list in Python?