Codectionary / Developer documentation / Python

Arithmetic Operators

Arithmetic operators calculate numeric results. Python provides addition (+), subtraction (-), multiplication (*), division (/), floor division (//), remainder (%) and exponentiation (**). The operand types determine the result type; the examples here use built-in integers and floats.

How it works

For integer operands, / returns a float even when the division is exact. // rounds the quotient down towards negative infinity: -7 // 3 is -3, not -2. With two integer operands its result is an integer; with a float operand it can be a float such as 3.0.

For integers and a nonzero divisor b, a == (a // b) * b + a % b. The remainder is zero or has the sign of b. This makes % useful for repeating cycles and testing divisibility, including negative integers.

Multiplication and division bind more tightly than addition and subtraction. Exponentiation binds more tightly than a unary minus on its left, so -2 ** 2 is -4 while (-2) ** 2 is 4. Use parentheses when the intended grouping is not obvious.

Common mistakes and boundaries

Division, floor division and remainder with a zero divisor raise ZeroDivisionError. Validate the divisor when zero is possible.

Binary floats approximate many decimal fractions. / does not promise an exact decimal result. Use math.isclose for approximate numeric comparisons and Decimal with an explicit rounding policy where decimal arithmetic is required.

Try it yourself

Predict -7 // 3 and -7 % 3, then verify the quotient/remainder identity. Compare int(-7 / 3) with -7 // 3 and explain why truncation and flooring differ.

Syntax

+  -  *  /  //  %  **

Examples

Basic Arithmetic

Addition, subtraction, multiplication, and true division.

print(10 + 3)   # 13
print(10 - 3)   # 7
print(10 * 3)   # 30
print(10 / 3)   # 3.3333333333333335  (a float for these integer operands)
  1. Integer addition, subtraction and multiplication remain integers. True division produces a floating-point approximation.
  2. Expected output: 13 7 30 3.3333333333333335

Floor Division and Modulus

Getting a whole-number quotient and the remainder of a division.

print(10 // 3)  # 3   (floor division - rounds the quotient down)
print(10 % 3)   # 1   (modulus - just the remainder)

# Common use: checking even/odd
number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")
  1. The positive integer quotient is 3 and the remainder is 1. A nonzero remainder when dividing by 2 identifies an odd integer.
  2. Expected output: 3 1 Odd

Exponentiation

Raising a number to a power with **.

print(2 ** 10)   # 1024
print(9 ** 0.5)  # 3.0  (square root via a fractional exponent)
print(5 ** 2)    # 25
  1. The ** operator raises the left operand to the right operand. A fractional exponent produces a float in the square-root example.
  2. Expected output: 1024 3.0 25

Best practices

  • Use // when flooring is the intended operation, including for negative values; it is not truncation towards zero.
  • Use parentheses to make calculations readable.
  • Handle a possible zero divisor before dividing.
  • Use Decimal when exact decimal representation and controlled rounding matter.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading