Skip to content

Add materials for Python Program Lexical Structure - #848

Open
realpython-bot wants to merge 1 commit into
masterfrom
materials/python-program-structure-20260916
Open

realpython-bot wants to merge 1 commit into
masterfrom
materials/python-program-structure-20260916

Conversation

@realpython-bot

Copy link
Copy Markdown
Collaborator

What this is

A new companion-code folder, python-program-structure/, for Python Program Lexical Structure. This tutorial has never had one, so this isn't a sync — it's a folder built from scratch. Please give it an editorial eye as well as a code review: the mapping from article to scripts is a judgement call, not a mechanical diff.

Built against the updated draft, which is the source of truth for the article's current state, not the live page:

What's in it

9 files — a README.md and 8 scripts, one per section of the tutorial, in the order the tutorial presents them:

Script Tutorial section
statements.py Python Statements
long_statements.py Line Continuation
implicit_line_continuation.py Implicit Line Continuation
explicit_line_continuation.py Explicit Line Continuation
multiple_statements.py Multiple Statements Per Line
comments.py Comments
foo.py Comments — the script file the tutorial names and shows in full
whitespace.py Whitespace

No requirements.txt. The draft's dependencies field is python==3.14 and the tutorial is stdlib-only, so it ships none — the same as siblings like python-scope-legb-rule/ and how-to-indent-in-python/.

REPL examples became runnable scripts

Nearly every example in this tutorial is a pycon block. Each section's REPL sequence was grouped into one coherent script named after the section, keeping the article's order and variable names. Two consequences worth reviewing:

  • Where the REPL echoed a value, the script calls print(). String values use print(repr(...)) so the console output matches the quoted form the article shows ('oba', not oba).
  • Blocks that only define something and show no output get a print() too, so that running the script shows you the result. The one exception is the set definition in implicit_line_continuation.py, which is left unprinted because a set of strings displays in a different order on each run.

No examples were invented, and none of the tutorial's code was "improved."

Why every script has # fmt: off

This is the part most worth a second opinion. The tutorial's subject is lexical structure — line layout, whitespace, and quoting are the content, not incidental style. Running the repo's formatter over these files verbatim reports "8 files would be reformatted": it joins the continued lines, splits the semicolon statements onto separate lines, and pads out the deliberately cramped whitespace. That deletes the very thing each example demonstrates.

So each script wraps its body in # fmt: off / # fmt: on and the article's code is reproduced verbatim, single quotes and all. Likewise, the two semicolon examples carry # noqa: E702 — Ruff flagging them is exactly the tutorial's point. RUF100 confirms both suppressions are load-bearing; without them ruff check reports 5 errors.

There's precedent in the repo for style-exempt example code (how-to-indent-in-python/sample_code.py, the commented-out fragments in pep8-beautiful-code/), though those use different mechanisms. Happy to switch approach if you'd rather.

Deliberate errors were not shipped

The tutorial shows several blocks that raise on purpose: unterminated s = / x = 1 + 2 + / ... <= 65 or statements, a backslash followed by a stray space, a comment after a line continuation, sin [...] (NameError), y is20, 'qux' notin [...], and an unexpected indent. These can't run, so they're not reproduced as code. Each script's docstring names the ones its section skips, and the README lists them.

For the same reason there is no script for the final section, "Whitespace as Indentation" — its only Python is a single print('foo') and an IndentationError.

How this was verified

  1. Every script executed, in a fresh uv venv on the draft's pin (CPython 3.14.6), with -W error::SyntaxWarning and MPLBACKEND=Agg. All 8 exited 0.
  2. Output compared line by line against the article. Every value matches what the tutorial shows — [2], 'oba', True, 21, foo bar baz, '---abc---', 'bar', ['bar'], 2, [[['foo', 'bar'], [1, 2, 3]], {1, 3, 5}, {'a': 1, 'b': 2}], The area of a circle with radius 12.35 is 479.163565508706, 15, ('foo', 14, 21.1), 'foobarbaz', and the rest.
  3. The repo's own gates, at the version the root requirements.txt pins:
    • uvx ruff@0.14.1 format --check python-program-structure8 files already formatted
    • uvx ruff@0.14.1 check python-program-structureAll checks passed!

Nothing in this folder needs a server, a GUI, or a paid API key, so nothing was skipped.

🤖 Generated with Claude Code

New companion-code folder for a tutorial that never had one. The article
is almost entirely REPL sessions, so each section's sequence became one
runnable script that prints its results.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant