Introduction
A .py file holds a program or module written in Python, as plain text.
The Python interpreter runs it directly, compiling it to bytecode on the way and caching
that bytecode in a .pyc file.
On Windows, a .pyw file is the same thing, run without opening a console
window, which suits programs with their own windows.
A .pyi file is a stub: it holds type hints for a module and no running
code, for type checkers and editors to read.
The pages listed below are a complete reference to the language. They follow The Python Language Reference for Python 3.15, released in October 2026, and give the section and PEP for each topic.
Structure
A Python file is plain text with no header, read as UTF-8 unless a comment in the
first two lines names another encoding. A #! line may name the
interpreter. Indentation is part of the syntax. Each file is a module, whose statements
run in order when it is first imported or run. See
File structure and
Lexical structure.
#!/usr/bin/env python3
def greet(name):
return f"Hello, {name}"
if __name__ == "__main__":
print(greet("world"))
Language reference
The reference is split into these pages, in roughly the order of the Language Reference's chapters.
- File structure –
.py,.pywand.pyifiles, the source encoding and coding declaration, the BOM and#!line, modules,__main__, and how a file becomes a.pyc - Lexical structure – lines, indentation, comments, keywords, names, and every kind of literal including f-strings and t-strings
- Data model – objects, identity, type and value, the type hierarchy and the special methods
- Types – numbers, sequences, mappings, sets,
None, truth values and conversions - Variables and scope – names, binding, the LEGB rule,
global,nonlocaland comprehension scopes - Expressions – the precedence table, comprehensions, conditional and assignment expressions,
lambdaand unpacking - Statements – simple and compound statements, assignment,
del,assert,type, loops,withandtry - Match – structural pattern matching and every kind of pattern
- Functions – parameter kinds, defaults, closures and decorators
- Classes – definitions, inheritance and the MRO, descriptors, properties, metaclasses and dataclasses
- Generators and async – iterators, generators, coroutines,
async forandasync with - Modules and packages – the import system,
__init__, namespace packages, relative and lazy imports,__all__ - Exceptions – the hierarchy,
raise from, exception groups andexcept* - Type hints – annotations,
typing, the generics syntax of Python 3.12 and stub files - Standard library – the groups of modules, briefly
- Versions – every release from 2.0 to 3.15, with what each added and removed
Not described
Known, not yet covered
- Reference: the standard library page groups the modules briefly. It does not describe the classes and functions inside any module (
os,re,asyncio,collectionsand the rest); the Python Standard Library reference does. - Reference: packaging and project files are not described:
pyproject.toml,setup.pyandsetup.cfg, build backends (setuptools, Hatch, Poetry),requirements.txt,.pthfiles and wheel building. Pip and venv are named, not explained. - Reference: the interpreter command line options (
-O,-X,-W), most of thePYTHON*environment variables, the C API and extension modules (Py_functions, Cython.pyxfiles), testing tools (unittest, pytest) and the other implementations (PyPy and the rest) are not described. - Viewer: the soft keywords
match,case,typeandlazyare coloured only where the line reads as the statement:caseat the start of a line followed by a pattern and a colon (the body may be on the same line,case 1: pass), andlazyat the start of a line beforeimportorfrom(the Python 3.15lazy import json, PEP 810). A pattern or statement that continues over several lines before its colon is not recognised. - Viewer: class and function names in a
classstatement or in a call are not coloured as types (a class name afterclassis plain; calls are coloured as functions by name only), and a{…}part of an f-string or t-string is coloured as part of the string, not as code. - Viewer: the file is not parsed, compiled or run: there is no indentation check, outline or list of problems, the keywords of every Python version are coloured whatever the version, and only the first 1 MiB is read and coloured.
Also used for
- marimo notebook – Reactive Python notebooks saved as plain .py files, one function per cell.
Links
Software
- Python – the official interpreter, which runs .py files (Windows, macOS, Linux)
- Visual Studio Code – free code editor with a Python extension (Windows, macOS, Linux)
- PyCharm – JetBrains’ development environment for Python (Windows, macOS, Linux)
- Jupyter – notebooks and an editor in the browser (any browser)
- Spyder – development environment aimed at scientific Python (Windows, macOS, Linux)
- Thonny – simple Python editor for beginners (Windows, macOS, Linux)
- Vim – text editor with Python syntax support (Windows, macOS, Linux)
Developers
- ast – Python’s own module for parsing Python source into a syntax tree (Python)
- LibCST – a syntax tree that keeps comments and formatting (Python)
- tree-sitter-python – Python grammar for the Tree-sitter parser used by editors
Reference
- The Python Language Reference – the official description of the language, which these pages follow
- PEP 263 – how a source file declares its encoding
- Python Enhancement Proposals – the proposals behind each language change
- Specification for the Python type system – the rules for type hints and stub files