Python is commonly classified as an interpreted language, but behind the scenes, modern CPython (the standard reference implementation written in C) employs a hybrid compilation-interpretation pipeline:
- Lexing & Parsing: When a Python program (`script.py`) is executed, the interpreter's tokenizer converts source text into a stream of lexical tokens. The parser then validates grammatical syntax against Python's formal grammar, generating an Abstract Syntax Tree (AST).
- Bytecode Compilation: If syntax is valid, the compiler translates the AST into an intermediate, platform-independent representation called Python Bytecode. Bytecode instructions are low-level numeric opcodes optimized for virtual execution. Bytecode is cached on disk inside the `pycache` directory as `.pyc` files to accelerate future execution startup.
- Python Virtual Machine (PVM): The PVM is the runtime software engine of Python. It contains an execution loop that reads bytecode instructions one by one, maps them to native machine code instructions, and directs the underlying CPU to execute them.
Execution Modes
- Interactive Mode (REPL): Launched by typing `python` in a terminal. Provides an immediate Read-Eval-Print Loop denoted by the primary prompt `>>>`. Excellent for rapid prototyping, syntax experimentation, and one-line evaluations. Code is executed immediately and not saved to disk.
- Script Mode: Source code is authored in a plain text file saved with a `.py` extension (e.g., `main.py`). The entire script is executed from top to bottom via `python main.py`. Required for multi-line modular programming, functions, and persistent production software.