Operators

- — Subtraction and set difference

For built-in numbers subtracts the right operand from the left. For sets and frozensets it selects elements present in the left but absent from the right; the result has the left set type for mixed built-in set operands. Ordinary sequences do not support subtraction. __sub__ and __rsub__ provide custom dispatch, including reflected fallback. Binary subtraction associates left to right and differs from unary negation.

Formal Syntax & Structure

python syntaxpython
left - right

Parameters & Attributes

NameTypeDescription
leftnumber, set or supporting objectNumber or set from which to subtract.
rightnumber, set or supporting objectNumber to subtract or set of elements to exclude.

Return Value

object

Result selected by the operand types and their operation implementations.

Working Example

Example Snippetpython
print(20 - 7)print(2 - 4.5)print(sorted({1, 2, 3} - {2, 4}))
Python Reference