//

Whole-number division — with a floor, not a truncation: -7 // 2 is -4.

Arithmetic operatorPython 2.2+Live demo
Common call
7 // 2
Returns
floored quotient (int for ints)
Replaces
floors toward -∞: -7 // 2 == -4, not -3
Watch out
pairs with %: a == b * (a // b) + (a % b) always holds
aaDividend.type: number · required // bbDivisor — zero raises.type: number · required
int | float

Demo

Live evaluation
Try:
Inputs
afloatdividend
bfloatdivisor
Output
7 // 2
3

Compare 7 // 2 with -7 // 2: the floor goes DOWN in both cases (3 and -4). C, JS and Java truncate toward zero instead (-3) — this difference is the whole reason the page exists.

Operands

NameTypeRequiredDescription
anumberyesDividend.
bnumberyesDivisor — zero raises.

Return value

int | floatThe floored quotient: int for int operands, float for float ones. Floors toward negative infinity.

Common patterns

Index arithmetic
Midpoints and bucket indexes must be ints.
mid = (lo + hi) // 2
Units breakdown
// and % together convert totals into unit parts.
hours = seconds // 3600
minutes = (seconds % 3600) // 60
Both at once
divmod returns quotient and remainder in one call.
q, r = divmod(seconds, 60)

Examples

1. Positive operands
7 // 2
Returns
3
2. Floors toward -∞
-7 // 2
Returns
-4
3. Floats stay float
7.0 // 2
Returns
3.0

Pitfalls

1. Negative results floor DOWN
Coming from C/JS, -7 // 2 == -4 is the surprise — truncation would give -3.
C intuition
-7 // 2   # expecting -3?
-4
Truncation when wanted
import math
math.trunc(-7 / 2)
-3
2. Float operands give float results
// does not force int — it floors within the operand type.
Still a float
items[10.0 // 3]
TypeError: list indices must be integers
Fix
items[int(10.0 // 3)]
works

When to use

Use it
  • Integer quotients: paging, bucketing, midpoints
  • Unit conversions with % as the counterpart
Reach for something else
  • Fractional results wanted → /
  • Truncation toward zero → math.trunc(a / b)
  • Quotient and remainder together → divmod()

Notes

Complexity
O(1)
Return
int for int operands, float otherwise
CPython impl
Objects/abstract.c :: PyNumber_FloorDivide → __floordiv__
Memory
No allocation beyond the result
Thread-safe
Yes — pure computation

FAQ

So the identity a == b * (a // b) + (a % b) holds with a remainder whose sign follows the divisor — which makes modular arithmetic (clock math, indexing) work cleanly for negatives.

History

2.2
// introduced (PEP 238) alongside the true-division plan.