Skip to content

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.

TypeScript
// TypeScript
let age: number = 30;
let name: string = "Alice";
const PI: number = 3.14159;
let isActive: boolean = true;
// Inferred type — TS figures it out
let score = 100; // inferred as number
Python
# Python
age: int = 30
name: str = "Alice"
PI: float = 3.14159
is_active: bool = True
# No annotation needed — works the same
score = 100 # Python infers nothing; it is just an int

Notice the style differences:

  • No let or const keyword — the assignment IS the declaration.
  • snake_case for variable names (not camelCase).
  • True / False are capitalized (not lowercase like JS/TS).

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
// TypeScript — enforced at compile time
const MAX_RETRIES: number = 3;
const API_BASE = "https://api.example.com";
// MAX_RETRIES = 5; // TS error: cannot assign to const
Python
# Python — convention only, NOT enforced
MAX_RETRIES: int = 3
API_BASE: str = "https://api.example.com"
# Technically legal (but don't do this):
# MAX_RETRIES = 5 # no error, just bad style

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.

TypeScript
// TypeScript
let value: string | number = "hello";
value = 42; // OK — union type
// value = true; // Error — not in the union
Python
# Python
value = "hello"
print(type(value)) # <class 'str'>
value = 42
print(type(value)) # <class 'int'>
value = [1, 2, 3]
print(type(value)) # <class 'list'>

Python lets you unpack multiple values in a single line — a pattern TypeScript developers recognise from array destructuring.

TypeScript
// TypeScript destructuring
const [x, y, z] = [1, 2, 3];
const { name, age } = { name: "Alice", age: 30 };
Python
# Python tuple unpacking
x, y, z = 1, 2, 3
print(x, y, z) # 1 2 3
# Works with any iterable
first, *rest = [10, 20, 30, 40]
print(first, rest) # 10 [20, 30, 40]
# Experiment with Python variables
age: int = 30
name: str = "Alice"
PI: float = 3.14159
is_active: bool = True
print(f"name={name}, age={age}, PI={PI}, is_active={is_active}")
# Dynamic reassignment — Python allows this
age = "thirty"
print(f"age is now: {age} (type: {type(age).__name__})")
# Multiple assignment
x, y, z = 1, 2, 3
print(f"x={x}, y={y}, z={z}")
# Star unpacking
first, *rest = [10, 20, 30, 40]
print(f"first={first}, rest={rest}")
Which statement correctly declares a constant in Python by convention?
What does Python use instead of the `let` keyword to declare a variable?
What is the Python naming convention for regular variables?
After `x: int = 5`, you write `x = "hello"`. What happens at runtime?