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]f-strings and t-strings
Section titled “f-strings and t-strings”An f-string interpolates immediately and hands you a finished str. Python 3.14 adds the t-string (PEP 750): same syntax with a t prefix, but it evaluates to a Template object that keeps the literal parts and the interpolated values apart. TypeScript has had exactly this split for years — a plain template literal versus a tagged template.
// TypeScriptconst name = "O'Brien";
// Template literal — one finished stringconst msg = `Hello, ${name}`;
// Tagged template — the tag sees the piecesfunction sql(strings: TemplateStringsArray, ...values: unknown[]) { return { text: strings.join("?"), values };}const query = sql`SELECT * FROM users WHERE name = ${name}`;# Python 3.14name = "O'Brien"
# f-string — one finished stringmsg = f"Hello, {name}"
# t-string — a Template, not a strquery = t"SELECT * FROM users WHERE name = {name}"print(type(query).__name__) # Templateprint(query.strings) # ('SELECT * FROM users WHERE name = ', '')print([i.value for i in query.interpolations]) # ["O'Brien"]Why it matters is the same reason tagged templates matter in TypeScript: an f-string glues the value into the text, so a quote inside name becomes SQL injection. A t-string hands the library the static text and the values separately, so it can bind them as parameters. Rule of thumb: f-string for messages you print, t-string for anything a parser downstream will read — SQL, HTML, shell.
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)…