Skip to content

Hello World — Your First Python 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.

TypeScript
// hello.ts
const name: string = "TypeScript Developer";
console.log(`Hello, ${name}!`);
// Run: npx ts-node hello.ts
// Output: Hello, TypeScript Developer!
Python
# hello.py
name: str = "TypeScript Developer"
print(f"Hello, {name}!")
# Run: python3 hello.py
# Output: Hello, TypeScript Developer!

console.log in TypeScript becomes print in Python. Both accept multiple arguments, but the details differ:

TypeScript
// TypeScript console.log
console.log("a", "b", "c"); // a b c (space separated)
console.log("count:", 42); // count: 42
console.log(`sum = ${1 + 2}`); // sum = 3 (template literal)
Python
# Python print
print("a", "b", "c") # a b c (space separated)
print("count:", 42) # count: 42
print(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
// TypeScript template literal
const user = { name: "Alice", age: 30 };
const msg = `${user.name} is ${user.age} years old`;
console.log(msg); // Alice is 30 years old
// Format number
const pi = 3.14159;
console.log(`pi ≈ ${pi.toFixed(2)}`); // pi ≈ 3.14
Python
# Python f-string
user = {"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.14159
print(f"pi ≈ {pi:.2f}") # pi ≈ 3.14
# Any expression works
print(f"2 + 2 = {2 + 2}") # 2 + 2 = 4
TypeScript
# TypeScript
# 1. Compile and run
tsc hello.ts && node hello.js
# 2. Direct with ts-node
npx ts-node hello.ts
# 3. Via npm script (package.json)
npm start
Python
# Python
# 1. Direct — no compilation step
python3 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.hello

The 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
// 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
# Python: the entry-point guard
def 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"))
# 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()
What is the Python equivalent of `console.log`?
What is the value of `__name__` when a file is run directly with `python3 hello.py`?
Which Python string prefix creates a template literal equivalent?
What happens to the `if __name__ == "__main__":` block when another module imports the file?