Add materials for Python Program Lexical Structure - #848
Open
realpython-bot wants to merge 1 commit into
Open
realpython-bot wants to merge 1 commit into
realpython-bot wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.mdand 8 scripts, one per section of the tutorial, in the order the tutorial presents them:statements.pylong_statements.pyimplicit_line_continuation.pyexplicit_line_continuation.pymultiple_statements.pycomments.pyfoo.pywhitespace.pyNo
requirements.txt. The draft'sdependenciesfield ispython==3.14and the tutorial is stdlib-only, so it ships none — the same as siblings likepython-scope-legb-rule/andhow-to-indent-in-python/.REPL examples became runnable scripts
Nearly every example in this tutorial is a
pyconblock. 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:print(). String values useprint(repr(...))so the console output matches the quoted form the article shows ('oba', notoba).print()too, so that running the script shows you the result. The one exception is the set definition inimplicit_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: offThis 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: onand 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.RUF100confirms both suppressions are load-bearing; without themruff checkreports 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 inpep8-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 orstatements, 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 anIndentationError.How this was verified
uvvenv on the draft's pin (CPython 3.14.6), with-W error::SyntaxWarningandMPLBACKEND=Agg. All 8 exited 0.[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.requirements.txtpins:uvx ruff@0.14.1 format --check python-program-structure→8 files already formatteduvx ruff@0.14.1 check python-program-structure→All checks passed!Nothing in this folder needs a server, a GUI, or a paid API key, so nothing was skipped.
🤖 Generated with Claude Code