liveBook · Manning

Operator

As explained in The Quick Python Book, Fourth Edition

An operator is a language construct that performs an operation on one or more operands, including path construction with pathlib operands.

An operator performs an operation on one or more operands. Python applies operators to built-in values and user-defined objects when the relevant special methods are available. section 3.1

Common operator families include:

  • Arithmetic operators such as +, -, *, /, //, and % combine or transform numeric values. Python 3 division with / produces a floating-point result; // performs integer division. section 4.12 section 4.5
  • Comparison operators include <, <=, >, >=, ==, and !=. Membership uses in and not in, while identity uses is and is not. section 8.11.2
  • Boolean operators and, or, and not combine Boolean expressions. Evaluation short-circuits, and and or return operand objects rather than necessarily Boolean values. section 8.11.2
  • Parentheses clarify grouping when precedence could make an expression difficult to read or interpret. section 8.11.2 section 4.5

Arithmetic expression

x = 3 y = 5 z = (x + y) / 2 Here, parentheses force addition before division, and z receives the average as a floating-point result. section 4.5

Path construction with /

from pathlib import Path cur_path = Path() path = cur_path / 'bin' / 'utils' / 'disktools' The slash operator combines Path operands into a pathname and adapts its representation to the operating system. section 12.2.5

if 0 < x < 10:
   ...

Compare chained comparison syntax with a longer expression for testing whether x lies within a range. section 8.11.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains arithmetic, comparison, Boolean, precedence, and pathlib uses, but defers the complete operator catalog to documentation.
Examples
Demonstrates averages, chained range comparisons, Boolean short-circuiting, identity tests, and cross-platform pathname construction.
What it assumes
Assumes familiarity with arithmetic expressions while clarifying Python division, precedence, short-circuiting, and object identity.
What you can do after
Apply arithmetic, comparison, Boolean, and pathlib operators in small Python expressions.
How it connects
Connects operators with operands, expressions, data types, special methods, comparison, Boolean logic, and pathlib Path objects.

Also explained in

Compare how 7 books explain operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.