Add materials for Operators and Expressions in Python - #847
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
Create the companion-code folder for the tutorial, which never had one. Each section of the article gets a self-contained script that runs the section's REPL examples in order and prints the 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.
New folder for a tutorial that never had one
This is a brand-new companion-code folder, not a sync of an existing one.
python-operators-expressions/did not exist in this repo before (verified:GET /repos/realpython/materials/contents/python-operators-expressionsreturned 404). Because it's new, it needs an editorial eye as well as a code review — someone should sanity-check the section-to-file mapping and the two judgement calls flagged below, not just the linting.What's in the folder
23 files: 22 standalone scripts plus a
README.md. Norequirements.txt— the draft'sdependenciesfield ispython==3.14with no third-party packages, and stdlib-only sibling folders (python-for-loop,python-f-string,python-variables,python-basic-data-types,python-repl) ship without one.The tutorial has no named scripts at all — there is not a single
$ python foo.pyin it. Every example is apyconREPL block. So, per the team's instruction, the REPL examples were turned into runnable scripts that useprint(), since seeing the output is the whole point of these examples. Each script groups one section's REPL sequence into a single coherent file, keeps the tutorial's order and variable names, and adds nothing the tutorial doesn't contain.getting_started.pyassignment.pyarithmetic.pycomparison.pycomparison_integers.pycomparison_floats.pycomparison_strings.pycomparison_sequences.pyboolean_operands.pyboolean_context.pyboolean_other_operands.pyshort_circuit.pyshort_circuit_idioms.pychained_comparisons.pyconditional_expressions.pyidentity.pymembership.pyconcatenation_repetition.pywalrus.pybitwise.pyprecedence.pyaugmented_assignment.pyTwo judgement calls worth a reviewer's attention
1.
identity.pygenuinely prints different results than the tutorial. The tutorial showsx = 1001; y = 1001; x is yreturningFalse. That holds in the REPL, where each statement is compiled separately, but in a script Python compiles the whole module at once and reuses a single1001object, sox is yisTrue(andx is not yisFalse). The code is a faithful mirror of the tutorial — I deliberately did not "fix" it by constructing the integers at runtime — but the file opens with a comment explaining the discrepancy. An editor should decide whether that's acceptable, or whether the tutorial itself should mention the script-vs-REPL difference.2. Snippets that cannot be scripts. Three kinds, all noted in comments where they belong rather than silently dropped:
-,==, andoroperators in the first section are aSyntaxErrorby design, so they can't sit in a.pyfile at all.x1 or x2 or ... or xn,variable = expression_1 if condition else expression_2,(variable := expression),variable $= expression,data_is_clean or clean_data(data),(a < 10) and (b > 30)) aren't runnable as written.TypeErroron5 < "7"and on[2, 3] > (2, 3),ZeroDivisionError,NameError), the script catches the exception and prints its message, using this repo's existingexcept X as error: print(error)idiom (seepython-exceptions/). The printed message matches the tutorial's traceback line.How this was verified
python==3.14, resolved to CPython 3.14.6),MPLBACKEND=Aggset, all 22 files run: 22/22 exit 0, no errors, nothing skipped. Nothing in this folder needs a server, a GUI, or a paid API key.identity.pycase described above, plus two expected-and-documented differences:id()values are machine-specific (the tutorial says so itself), andprint()renders strings without the REPL's surrounding quotes (Hello, World!vs'Hello, World!').requirements.txtpins (ruff==0.14.1):uvx ruff@0.14.1 format --check python-operators-expressions→22 files already formatted;uvx ruff@0.14.1 check python-operators-expressions→All checks passed!python-for-loop,python-f-string,python-variables,python-basic-data-types, andpython-repl: snake_case topic-per-file scripts, plain module-level code with noif __name__ == "__main__"boilerplate, and the standard one-sentence README opener. The README adds a short run section and the mapping table above, which felt warranted for a 22-file folder.git clean -ndandgit status --porcelainare both empty; no venvs, caches, or generated output are committed. Onlypython-operators-expressions/is touched.🤖 Generated with Claude Code