Hello World — Your First Python Program
The simplest program
Section titled “The simplest program”In TypeScript you typically have a project setup before you write a first line. In Python you can go from zero to running in one file and one command.
// hello.tsconst name: string = "TypeScript Developer";console.log(`Hello, ${name}!`);
// Run: npx ts-node hello.ts// Output: Hello, TypeScript Developer!# hello.pyname: str = "TypeScript Developer"print(f"Hello, {name}!")
# Run: python3 hello.py# Output: Hello, TypeScript Developer!The print function
Section titled “The print function”console.log in TypeScript becomes print in Python. Both accept multiple arguments, but the details differ:
// TypeScript console.logconsole.log("a", "b", "c"); // a b c (space separated)console.log("count:", 42); // count: 42console.log(`sum = ${1 + 2}`); // sum = 3 (template literal)# Python printprint("a", "b", "c") # a b c (space separated)print("count:", 42) # count: 42print(f"sum = {1 + 2}") # sum = 3 (f-string)
# print has keyword args:print("a", "b", sep="-") # a-b (custom separator)print("loading", end="...") # loading... (no newline)F-strings — Python’s template literals
Section titled “F-strings — Python’s template literals”Python’s f-string (f"...") is the equivalent of TypeScript’s template literal (`...`). Prefix the string with f and put expressions in {}.
// TypeScript template literalconst user = { name: "Alice", age: 30 };const msg = `${user.name} is ${user.age} years old`;console.log(msg); // Alice is 30 years old
// Format numberconst pi = 3.14159;console.log(`pi ≈ ${pi.toFixed(2)}`); // pi ≈ 3.14# Python f-stringuser = {"name": "Alice", "age": 30}msg = f"{user['name']} is {user['age']} years old"print(msg) # Alice is 30 years old
# Format specifier inside {}pi = 3.14159print(f"pi ≈ {pi:.2f}") # pi ≈ 3.14
# Any expression worksprint(f"2 + 2 = {2 + 2}") # 2 + 2 = 4Running a script
Section titled “Running a script”# TypeScript# 1. Compile and runtsc hello.ts && node hello.js
# 2. Direct with ts-nodenpx ts-node hello.ts
# 3. Via npm script (package.json)npm start# Python# 1. Direct — no compilation steppython3 hello.py
# 2. Make the file executable (Unix)chmod +x hello.py # add #!/usr/bin/env python3 as line 1./hello.py
# 3. Run as a module (inside a package)python3 -m mypackage.helloThe entry-point guard: if __name__ == "__main__":
Section titled “The entry-point guard: if __name__ == "__main__":”This is a Python pattern with no direct TypeScript equivalent, so it deserves a careful explanation.
When Python runs a file directly (python3 hello.py), it sets the module’s __name__ to the string "__main__". When another file imports hello.py with import hello, __name__ is set to "hello" (the module name) instead.
The guard if __name__ == "__main__": means “only run this code if this file is the entry point, not when it is imported.”
// TypeScript: no direct equivalent// In Node, top-level code always runs when the file is imported// You work around this with explicit exports:
export function greet(name: string) { return `Hello, ${name}!`;}
// This runs unconditionally on import — often undesirable:// console.log(greet("world"));# Python: the entry-point guarddef greet(name: str) -> str: return f"Hello, {name}!"
if __name__ == "__main__": # This block ONLY runs when you do: python3 hello.py # It does NOT run when another file does: import hello print(greet("world"))A complete first script
Section titled “A complete first script”# hello.py — a complete first Python script
def greet(name: str) -> str: """Return a personalised greeting.""" return f"Hello, {name}!"
def main() -> None: names = ["Alice", "Bob", "TypeScript Developer"] for name in names: message = greet(name) print(message)
# f-string formatting count = len(names) print(f"\nGreeted {count} people.")
if __name__ == "__main__": main()Loading Python runtime (first run only)…