GGistDev

Hello, World in Python

A minimal start with Python’s printing and execution. Python executes top‑to‑bottom; no main() is required (though it’s often used by convention).

First program

Create a file and run it with the interpreter. print writes a textual representation and appends a newline by default.

# hello.py
print("Hello, world!")

Run it:

  • python hello.py or python3 hello.py depending on your system
  • The interpreter searched is the one on your PATH (which python on Unix)

Shebang (Unix) and execution

A shebang selects the interpreter when you execute the file directly. Using /usr/bin/env respects virtual environments.

#!/usr/bin/env python3
print("Hello, world!")
chmod +x hello.py
./hello.py

REPL and one‑liners

The interactive shell (CPython REPL) is great for experiments; help(obj) shows docs, exit() or Ctrl‑D quits.

$ python
>>> print("Hello, world!")
Hello, world!

One‑liner:

python -c "print('hi')"

Printing basics

print(*args, sep=' ', end='\n', file=sys.stdout) joins arguments with sep and writes end after.

print("a", 1, True)            # a 1 True
print("no newline", end="")   # no newline
print("csv", "row", sep=",")  # csv,row

f‑strings (formatting)

f‑strings evaluate expressions inside {} and support format specifiers like :.2f.

name = "Python"; version = 3.12
print(f"Hello, {name} {version}")
pi = 3.14159
print(f"pi ≈ {pi:.2f}")   # pi ≈ 3.14

The main guard (convention)

Use this pattern so a module can be imported without running script code.

def main():
    print("Hello from main")

if __name__ == "__main__":
    main()

Summary

  • Run with python file.py, or use the REPL for quick tests
  • print is flexible; f‑strings are the preferred formatter
  • Use a shebang for direct execution and a main guard for import‑safe scripts