Getting Started with Python

This section documents the current Python release line as published at the official Python documentation, which is the reference these pages are written and verified against. No specific patch version is pinned.

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

This section’s bibliography lists the reference material consulted while preparing these pages.

Python is an interpreted, dynamically typed language. This page installs it, shows the two ways to run code — typed one line at a time or saved to a file — and traces what actually happens between saving a .py file and seeing output.

Installing Python

Download an installer for your platform from python.org. On Windows, tick Add python.exe to PATH on the first installer screen — it is not checked by default. On macOS, prefer the python.org installer or Homebrew over the system Python that ships with the OS. On Linux, most distributions package Python through their own package manager:

# Windows: installer from python.org, or
winget install Python.Python.3

# macOS: installer from python.org, or
brew install python3

# Debian / Ubuntu
sudo apt update && sudo apt install python3

# Fedora
sudo dnf install python3

Check what got installed. On Windows the launcher is usually python; on macOS and Linux, python3 (a bare python may not exist, or may point at a Python 2 left over from the OS):

$ python3 --version
Python 3.13.0

$ python3 -m pip --version
pip 24.2 from /usr/lib/python3.13/site-packages/pip (python 3.13)

If the command is not found, the installer did not add Python to your PATH — re-run the installer and enable that option, or add the install directory manually.

Script Mode vs. Interactive Mode

Running python3 with no file starts the interactive mode (the REPL — Read-Eval-Print Loop): each line is read, evaluated, and its result printed immediately, which makes it the fastest way to try an expression:

$ python3
Python 3.13.0 (main, Oct  7 2024, 15:38:29)
>>> 2 + 2
4
>>> name = "Ada"
>>> f"Hello, {name}!"
'Hello, Ada!'
>>> exit()

Script mode instead runs a whole file saved on disk. Save this as hello.py:

print("Hello, world!")

and run it by passing the filename to the interpreter:

$ python3 hello.py
Hello, world!

Full command-line usage, including -c (run a string) and -m (run a module as a script), is documented under Using the Python Interpreter.

The shebang line

On Unix-like systems (macOS, Linux), a script can be made directly executable by giving it a shebang as its first line and marking the file executable:

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

#!/usr/bin/env python3 looks up python3 on the current PATH rather than hardcoding an absolute interpreter path, so the same script works across machines with Python installed in different locations. Windows has no shebang mechanism of its own, but the py launcher installed alongside python.org’s Windows build reads the same line to pick an interpreter version.

Editors and IDEs

Any text editor can write a .py file, but three tools cover most Python work:

  • VS Code with the official Python extension — linting, debugging, and IntelliSense in a general-purpose editor.

  • PyCharm — a Python-specific IDE with deeper refactoring and debugging tools built in.

  • IDLE — the simple editor and REPL that ships with every python.org install; no separate download needed.

Pick whichever is already installed to write hello_world.py:

def main() -> None:
    print("Hello, world!")


if __name__ == "__main__":
    main()

if name == "main": guards code that should run only when the file is executed directly, not when it is imported by another module — see Lexical Structure and Style for module structure.

The Execution Model: Source to Bytecode to the PVM

python3 hello.py is not a single step. CPython (the reference implementation) first compiles the source to an intermediate form called bytecode, then a loop called the Python Virtual Machine (PVM) executes that bytecode instruction by instruction:

flowchart LR A["hello.py
(source)"] -->|compile| B["bytecode
(.pyc)"] B -->|execute| C["Python Virtual Machine
(PVM)"] C --> D["program output"] B -.->|cached under| E["__pycache__/hello.cpython-313.pyc"]

For a script run directly, this compile step happens in memory on every run. For imported modules, CPython caches the compiled bytecode on disk in a pycache directory next to the source file, named after the interpreter version (e.g. hello.cpython-313.pyc), so the next import skips recompilation as long as the source file’s modification time has not changed:

$ python3 -c "import hello"
$ ls __pycache__
hello.cpython-313.pyc

This compile-then-interpret design is why Python is described as interpreted even though a real compilation pass happens — there is no separate step producing a native executable, and the same .pyc bytecode is not portable across incompatible interpreter versions. Background on the interpreter and why Python favours this tight edit-run loop is in Whetting Your Appetite and Using the Python Interpreter; installers for every platform are at python.org/downloads.

See Also