Programming Search⌘K

Featureintro

Arithmetic operators

Also called Python math operators, floor division, true division, modulo operator, power operator, matrix multiplication operator.

Python's arithmetic operators are +, -, *, /, //, %, ** and @. / always gives a float, // and % round toward minus infinity, and ** binds tighter than a unary minus on its left.

Language
Python

Synopsis

print(7 + 2, 7 - 2, 7 * 2)
print(7 / 2, 7 // 2, 7 % 2)
print(2 ** 10, 10 ** -2, -7 // 2, -7 % 2)
Output
9 5 14
3.5 3 1
1024 0.01 -4 1

Semantics

r1. Apart from the power operator, there are two priority levels: the multiplicative operators *, @, /, // and %, and the additive operators + and -. The unary operators -, + and ~ share one level above them.1 2

r2. With operands of different numeric types, the operand of the narrower type is widened to the type of the other. If both are complex, nothing is converted; if either one is a complex or a float, the other is converted to a float; otherwise both are integers. A complex and a real operand are combined by the usual formula, such as x + complex(u, v) = complex(x + u, v).3 Example: mixed-types.

r3. / and // give the quotient. "Division of integers yields a float, while floor division of integers results in an integer": // is mathematical division with the floor function applied. Division by zero raises ZeroDivisionError.1 Example: operators.

r4. % gives the remainder, and the result always has "the same sign as its second operand (or zero)". The two are connected by x == (x//y)*y + (x%y), and divmod(x, y) returns (x//y, x%y).1 Example: negative-division.

r5. ** binds more tightly than a unary operator on its left, and less tightly than one on its right, so -1**2 is -1. A chain of ** is evaluated from right to left. With int operands the result is an int, unless the exponent is negative: then the result is a float.4 Example: power-binding.

r6. * multiplies two numbers, or repeats a sequence when the other argument is an integer, and a negative factor gives an empty sequence. + adds two numbers, or concatenates two sequences of the same type.1 Example: sequences.

r7. @ is for matrix multiplication, and "No builtin Python types implement this operator". A class supports it with __matmul__() and __rmatmul__().1 Example: matmul.

Examples

The common type of rule r2.

for value in [3 + 4, 3 + 4.0, 3 + 4j, 6 / 3, 6 // 3, 6.0 // 3]:
    print(repr(value), type(value).__name__)
Output
7 int
7.0 float
(3+4j) complex
2.0 float
2 int
2.0 float

Floor division and modulo with negative numbers (rule r4). The identity holds in each row.

for x, y in [(7, 3), (-7, 3), (7, -3), (-7, -3)]:
    q, r = divmod(x, y)
    print(f"{x:3} {y:3} -> {q:3} {r:3}  {q * y + r == x}")
Output
  7   3 ->   2   1  True
 -7   3 ->  -3   2  True
  7  -3 ->  -3  -2  True
 -7  -3 ->   2  -1  True

Sequence repetition and concatenation (rule r6).

print([0] * 3, "ab" * 2, "ab" * -1 == "")
print([1, 2] + [3], "py" + "thon")
Output
[0, 0, 0] abab True
[1, 2, 3] python

A small class that defines @ (rule r7).

class Matrix:
    def __init__(self, rows):
        self.rows = rows

    def __matmul__(self, other):
        columns = list(zip(*other.rows))
        return Matrix([[sum(a * b for a, b in zip(row, col)) for col in columns] for row in self.rows])

a = Matrix([[1, 2], [3, 4]])
b = Matrix([[5, 6], [7, 8]])
print((a @ b).rows)
Output
[[19, 22], [43, 50]]

Pitfalls

-2 ** 2 is -4

The power binds before the unary minus on its left (rule r5). Put the negative base in parentheses.

This code runs but gives a wrong answer on purpose.

print(-2 ** 2)
print((-2) ** 2)
print(2 ** 3 ** 2)
Output
-4
4
512

2 ** 3 ** 2 is 2 ** 9, because the chain groups from the right.

/ on integers gives a float

7 / 2 is 3.5, and 6 / 3 is 2.0, not 2 (rule r3). Use // for an integer quotient.

Division by zero

/, // and % raise ZeroDivisionError for a zero right argument (rules r3 and r4).

This code raises ZeroDivisionError, on purpose.

total = 10
count = 0
print(total / count)

Comparison

Go uses truncated division for integers: x / y is "truncated towards zero", so -5 / 3 is -1 and -5 % 3 is -2.5 Python floors, so -5 // 3 is -2 and -5 % 3 is 1 (rule r4). The two languages agree when both operands are positive.

References

  1. The Python Language Reference, Python 3.15, 6.7 Binary arithmetic operations
  2. The Python Language Reference, Python 3.15, 6.6 Unary arithmetic and bitwise operations
  3. Built-in Types, Python 3.15 documentation, Numeric Types, int, float, complex
  4. The Python Language Reference, Python 3.15, 6.5 The power operator
  5. The Go Programming Language Specification, Integer operators

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source