Operators#
Python ships the usual operator categories plus a few of its own.
Walrus assignment, comparison chaining, and a matrix-multiply
operator that NumPy and others overload. Most operators dispatch
through a dunder method on the operand (+ calls __add__),
which makes them overloadable on custom classes.
This page lists every operator group with its precedence and the form write day-to-day. For truthiness and the short-circuit rules logical operators apply, see Types.
Overview#
Category |
Operator |
Notes |
|---|---|---|
Arithmetic |
|
Addition; string and list concat. |
Arithmetic |
|
Subtraction or unary negation. |
Arithmetic |
|
Multiplication; sequence repetition. |
Arithmetic |
|
True division (always float). |
Arithmetic |
|
Floor division. |
Arithmetic |
|
Modulo; legacy string formatting. |
Arithmetic |
|
Exponentiation. |
Arithmetic |
|
Matrix multiply via |
Comparison |
|
Value equality. |
Comparison |
|
Value inequality. |
Comparison |
|
Less than. |
Comparison |
|
Less than or equal. |
Comparison |
|
Greater than. |
Comparison |
|
Greater than or equal. |
Logical |
|
Short-circuit; returns the first falsy operand or the last. |
Logical |
|
Short-circuit; returns the first truthy operand or the last. |
Logical |
|
Logical negation; always returns |
Bitwise |
|
AND. |
Bitwise |
|
OR. |
Bitwise |
|
XOR. |
Bitwise |
|
NOT (two’s complement). |
Bitwise |
|
Left shift. |
Bitwise |
|
Arithmetic right shift. |
Identity |
|
Same object (compares identity, not value). |
Identity |
|
Different object. |
Membership |
|
Membership. |
Membership |
|
Negated membership. |
Assignment |
|
Bind a name to a value. |
Assignment |
|
Augmented add. |
Assignment |
|
Augmented subtract. |
Assignment |
|
Augmented multiply. |
Assignment |
|
Augmented true divide. |
Assignment |
|
Augmented floor divide. |
Assignment |
|
Augmented modulo. |
Assignment |
|
Augmented exponentiation. |
Assignment |
|
Augmented bitwise AND. |
Assignment |
|
Augmented bitwise OR. |
Assignment |
|
Augmented bitwise XOR. |
Assignment |
|
Augmented left shift. |
Assignment |
|
Augmented right shift. |
Walrus |
|
Bind a name inside an expression. |
Arithmetic#
Operator |
Effect |
Example |
|---|---|---|
|
Addition; string and list concat. |
|
|
Subtraction or unary negation. |
|
|
Multiplication; sequence repetition. |
|
|
True division (always float). |
|
|
Floor division. |
|
|
Modulo; legacy string formatting. |
|
|
Exponentiation. |
|
|
Matrix multiply (NumPy and others). |
|
Comparison#
Operator |
Effect |
Example |
|---|---|---|
|
Value equality (calls |
|
|
Value inequality. |
|
|
Less than (calls |
|
|
Less than or equal (calls |
|
|
Greater than (calls |
|
|
Greater than or equal (calls |
|
Comparison chaining (0 < x < 10) is real and idiomatic, not
two C-style comparisons. Python evaluates the middle expression
once.
Logical#
Operator |
Effect |
Example |
|---|---|---|
|
Returns first falsy operand or the last (short-circuits). |
|
|
Returns first truthy operand or the last (short-circuits). |
|
|
Logical negation; always returns |
|
For the underlying truthiness rules, see Types.
Bitwise#
Operator |
Effect |
Example |
|---|---|---|
|
AND |
|
|
OR |
|
|
XOR |
|
|
NOT (two’s complement) |
|
|
Left shift. |
|
|
Arithmetic right shift. |
|
The same operators also work as set algebra on set and
frozenset (union, intersection, symmetric difference).
Boolean#
Identity and membership are spelled with keywords, not symbols.
Together with the Comparison and Logical operators above, they
are the operators that return bool.
Operator |
Effect |
Example |
|---|---|---|
|
Same object (not equal value). |
|
|
Different object. |
|
|
Membership. |
|
|
Negated membership. |
|
is None is the idiomatic null check. == None works but
reads as a junior tell.
Assignment#
Operator |
Effect |
Example |
|---|---|---|
|
Bind a name to a value. |
|
|
Augmented add. |
|
|
Augmented subtract. |
|
|
Augmented multiply. |
|
|
Augmented true divide. |
|
|
Augmented floor divide. |
|
|
Augmented modulo. |
|
|
Augmented exponentiation. |
|
|
Augmented bitwise AND. |
|
|
Augmented bitwise OR. |
|
|
Augmented bitwise XOR. |
|
|
Augmented left shift. |
|
|
Augmented right shift. |
|
|
Walrus; bind inside an expression. |
|
Augmented assignment mutates in place for mutable types. xs +=
[3] extends; xs = xs + [3] rebinds to a new list.
Walrus#
The := operator binds inside an expression. Useful in
while loops, if conditions, and comprehensions to avoid
recomputation.
Read until exhaustion without calling read twice.
while (chunk := f.read(4096)):
process(chunk)
Comprehension that filters and reuses the search result.
matches = [m for line in lines if (m := pattern.search(line))]
Bind a length and gate on it.
if (n := len(payload)) > MAX_SIZE:
raise ValueError(f"payload too large: {n} bytes")
Bind a regex match and use the capture in the body.
if (m := re.match(r"GET (\S+)", line)):
path = m.group(1)
Bind a dictionary lookup and branch on truthiness.
if (user := users.get(uid)):
greet(user)
else:
deny(uid)
Precedence#
High-to-low. When in doubt, parenthesise.
Level |
Operator |
Associativity |
|---|---|---|
1 (highest) |
|
Left |
1 (highest) |
|
Left |
1 (highest) |
|
Left |
1 (highest) |
Function call |
Left |
2 |
|
Right |
3 |
Unary |
Right |
3 |
Unary |
Right |
3 |
|
Right |
4 |
|
Left |
4 |
|
Left |
4 |
|
Left |
4 |
|
Left |
4 |
|
Left |
5 |
|
Left |
5 |
|
Left |
6 |
|
Left |
6 |
|
Left |
7 |
|
Left |
8 |
|
Left |
9 |
|
Left |
10 |
Comparison |
Left (chained) |
10 |
Identity |
Left (chained) |
10 |
Membership |
Left (chained) |
11 |
|
Right |
12 |
|
Left |
13 |
|
Left |
14 |
Conditional |
Right |
15 |
|
Non-associative |
16 (lowest) |
|
Right |
References#
Types for truthiness and how operators interact with each type.
Variables for binding and the rebinding keywords.
OOP for the dunder methods every operator dispatches through.
Operator precedence in the Language Reference.
Data model for the full dunder catalog.