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 __matmul__ (NumPy and others).

Comparison

==

Value equality.

Comparison

!=

Value inequality.

Comparison

<

Less than.

Comparison

<=

Less than or equal.

Comparison

>

Greater than.

Comparison

>=

Greater than or equal.

Logical

and

Short-circuit; returns the first falsy operand or the last.

Logical

or

Short-circuit; returns the first truthy operand or the last.

Logical

not

Logical negation; always returns bool.

Bitwise

&

AND.

Bitwise

|

OR.

Bitwise

^

XOR.

Bitwise

~

NOT (two’s complement).

Bitwise

<<

Left shift.

Bitwise

>>

Arithmetic right shift.

Identity

is

Same object (compares identity, not value).

Identity

is not

Different object.

Membership

in

Membership.

Membership

not in

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.

2 + 35

-

Subtraction or unary negation.

5 - 23

*

Multiplication; sequence repetition.

"-" * 5"-----"

/

True division (always float).

7 / 23.5

//

Floor division.

7 // 23

%

Modulo; legacy string formatting.

7 % 21

**

Exponentiation.

2 ** 8256

@

Matrix multiply (NumPy and others).

A @ B

Comparison#

Operator

Effect

Example

==

Value equality (calls __eq__).

[1, 2] == [1, 2]True

!=

Value inequality.

"a" != "b"

<

Less than (calls __lt__).

1 < 2

<=

Less than or equal (calls __le__).

1 <= 1

>

Greater than (calls __gt__).

3 > 2

>=

Greater than or equal (calls __ge__).

3 >= 3

Comparison chaining (0 < x < 10) is real and idiomatic, not two C-style comparisons. Python evaluates the middle expression once.

Logical#

Operator

Effect

Example

and

Returns first falsy operand or the last (short-circuits).

a and b

or

Returns first truthy operand or the last (short-circuits).

a or default

not

Logical negation; always returns bool.

not x

For the underlying truthiness rules, see Types.

Bitwise#

Operator

Effect

Example

&

AND

0b1100 & 0b10100b1000

|

OR

0b1100 | 0b10100b1110

^

XOR

0b1100 ^ 0b10100b0110

~

NOT (two’s complement)

~5-6

<<

Left shift.

1 << 38

>>

Arithmetic right shift.

8 >> 22

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

is

Same object (not equal value).

x is None

is not

Different object.

x is not None

in

Membership.

"k" in d

not in

Negated membership.

x not in xs

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.

x = 1

+=

Augmented add.

x += 1

-=

Augmented subtract.

x -= 1

*=

Augmented multiply.

x *= 2

/=

Augmented true divide.

x /= 2

//=

Augmented floor divide.

x //= 2

%=

Augmented modulo.

x %= 2

**=

Augmented exponentiation.

x **= 2

&=

Augmented bitwise AND.

flags &= MASK

|=

Augmented bitwise OR.

flags |= READ

^=

Augmented bitwise XOR.

flags ^= TOGGLE

<<=

Augmented left shift.

x <<= 1

>>=

Augmented right shift.

x >>= 1

:=

Walrus; bind inside an expression.

if (n := len(xs)) > 5:

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

not

Right

12

and

Left

13

or

Left

14

Conditional a if c else b

Right

15

lambda

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.