Skip to content

Add materials for Operators and Expressions in Python - #847

Open
realpython-bot wants to merge 1 commit into
masterfrom
materials/python-operators-expressions-20260916
Open

realpython-bot wants to merge 1 commit into
masterfrom
materials/python-operators-expressions-20260916

Conversation

@realpython-bot

Copy link
Copy Markdown
Collaborator

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-expressions returned 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. No requirements.txt — the draft's dependencies field is python==3.14 with 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.py in it. Every example is a pycon REPL block. So, per the team's instruction, the REPL examples were turned into runnable scripts that use print(), 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.

File Tutorial section
getting_started.py Getting Started With Operators and Expressions
assignment.py The Assignment Operator and Statements
arithmetic.py Arithmetic Operators and Expressions in Python
comparison.py Comparison Operators and Expressions in Python
comparison_integers.py Comparison of Integer Values
comparison_floats.py Comparison of Floating-Point Values
comparison_strings.py Comparison of Strings
comparison_sequences.py Comparison of Lists and Tuples
boolean_operands.py Boolean Expressions Involving Boolean Operands
boolean_context.py Evaluation of Regular Objects in a Boolean Context
boolean_other_operands.py Boolean Expressions Involving Other Types of Operands
short_circuit.py Compound Logical Expressions and Short-Circuit Evaluation
short_circuit_idioms.py Idioms That Exploit Short-Circuit Evaluation
chained_comparisons.py Compound vs Chained Expressions
conditional_expressions.py Conditional Expressions or the Ternary Operator
identity.py Identity Operators and Expressions in Python
membership.py Membership Operators and Expressions in Python
concatenation_repetition.py Concatenation and Repetition Operators and Expressions
walrus.py The Walrus Operator and Assignment Expressions
bitwise.py Bitwise Operators and Expressions in Python
precedence.py Operator Precedence in Python
augmented_assignment.py Augmented Assignment Operators and Expressions

Two judgement calls worth a reviewer's attention

1. identity.py genuinely prints different results than the tutorial. The tutorial shows x = 1001; y = 1001; x is y returning False. 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 single 1001 object, so x is y is True (and x is not y is False). 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:

  • The bare -, ==, and or operators in the first section are a SyntaxError by design, so they can't sit in a .py file at all.
  • Syntax sketches with placeholder names (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.
  • Where the tutorial shows a traceback (TypeError on 5 < "7" and on [2, 3] > (2, 3), ZeroDivisionError, NameError), the script catches the exception and prints its message, using this repo's existing except X as error: print(error) idiom (see python-exceptions/). The printed message matches the tutorial's traceback line.

How this was verified

  • Every script was executed, not just linted. A fresh venv on the draft's pin (python==3.14, resolved to CPython 3.14.6), MPLBACKEND=Agg set, 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.
  • Output compared against the tutorial line by line. Everything matches except the identity.py case described above, plus two expected-and-documented differences: id() values are machine-specific (the tutorial says so itself), and print() renders strings without the REPL's surrounding quotes (Hello, World! vs 'Hello, World!').
  • Repo gates at the version requirements.txt pins (ruff==0.14.1): uvx ruff@0.14.1 format --check python-operators-expressions22 files already formatted; uvx ruff@0.14.1 check python-operators-expressionsAll checks passed!
  • Conventions were taken from python-for-loop, python-f-string, python-variables, python-basic-data-types, and python-repl: snake_case topic-per-file scripts, plain module-level code with no if __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 -nd and git status --porcelain are both empty; no venvs, caches, or generated output are committed. Only python-operators-expressions/ is touched.

🤖 Generated with Claude Code

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>
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