Variables & Types
Variables: familiar concept, different rules
Section titled “Variables: familiar concept, different rules”In TypeScript you declare a variable with let or const and optionally annotate its type. The TypeScript compiler enforces that type throughout the variable’s life. In Python you just write the name and assign — no keyword, no compiler. The variable simply exists from that line forward, and its type is whatever you assigned.
The key difference: TypeScript’s type system lives at compile time (and disappears at runtime). Python’s optional type hints are just annotations — the interpreter does not enforce them, and third-party tools like mypy do the checking separately.
Declaring variables
Section titled “Declaring variables”// TypeScriptlet age: number = 30;let name: string = "Alice";const PI: number = 3.14159;let isActive: boolean = true;
// Inferred type — TS figures it outlet score = 100; // inferred as number# Pythonage: int = 30name: str = "Alice"PI: float = 3.14159is_active: bool = True
# No annotation needed — works the samescore = 100 # Python infers nothing; it is just an intNotice the style differences:
- No
letorconstkeyword — the assignment IS the declaration. snake_casefor variable names (notcamelCase).True/Falseare capitalized (not lowercase like JS/TS).
Constants by convention: UPPER_CASE
Section titled “Constants by convention: UPPER_CASE”Python has no const keyword. The community convention is to name constants in UPPER_SNAKE_CASE. Nothing stops you from reassigning them — it is purely a signal to other developers.
// TypeScript — enforced at compile timeconst MAX_RETRIES: number = 3;const API_BASE = "https://api.example.com";
// MAX_RETRIES = 5; // TS error: cannot assign to const# Python — convention only, NOT enforcedMAX_RETRIES: int = 3API_BASE: str = "https://api.example.com"
# Technically legal (but don't do this):# MAX_RETRIES = 5 # no error, just bad styleDynamic typing in action
Section titled “Dynamic typing in action”Python variables are not locked to their initial type. A variable is just a name pointing to an object — you can rebind it to any type at any time. TypeScript will refuse this at compile time; Python will not.
// TypeScriptlet value: string | number = "hello";value = 42; // OK — union type// value = true; // Error — not in the union# Pythonvalue = "hello"print(type(value)) # <class 'str'>
value = 42print(type(value)) # <class 'int'>
value = [1, 2, 3]print(type(value)) # <class 'list'>Multiple assignment
Section titled “Multiple assignment”Python lets you unpack multiple values in a single line — a pattern TypeScript developers recognise from array destructuring.
// TypeScript destructuringconst [x, y, z] = [1, 2, 3];const { name, age } = { name: "Alice", age: 30 };# Python tuple unpackingx, y, z = 1, 2, 3print(x, y, z) # 1 2 3
# Works with any iterablefirst, *rest = [10, 20, 30, 40]print(first, rest) # 10 [20, 30, 40]Try it
Section titled “Try it”# Experiment with Python variablesage: int = 30name: str = "Alice"PI: float = 3.14159is_active: bool = True
print(f"name={name}, age={age}, PI={PI}, is_active={is_active}")
# Dynamic reassignment — Python allows thisage = "thirty"print(f"age is now: {age} (type: {type(age).__name__})")
# Multiple assignmentx, y, z = 1, 2, 3print(f"x={x}, y={y}, z={z}")
# Star unpackingfirst, *rest = [10, 20, 30, 40]print(f"first={first}, rest={rest}")Loading Python runtime (first run only)…