%

The remainder — and in Python, -7 % 3 is 2, not -1. That difference powers clean wrap-around math.

Arithmetic operatorPython 1.0+Live demo
Common call
7 % 3
Returns
remainder, sign follows the divisor
Replaces
-7 % 3 == 2 (JS/C give -1)
Watch out
% on a string is old-style formatting — different feature
aaDividend.type: number · required % bbDivisor — zero raises. The result takes this sign.type: number · required
number

Demo

Live evaluation
Try:
Inputs
afloatdividend
bfloatdivisor
Output
7 % 3
1

Try the negative cases — the result always carries the divisor’s sign: -7 % 3 is 2, and 7 % -3 is -2. C-family languages do the opposite, which is why ported wrap-around code breaks.

Operands

NameTypeRequiredDescription
anumberyesDividend.
bnumberyesDivisor — zero raises. The result takes this sign.

Return value

numberThe remainder of a // b. Its sign follows the DIVISOR — the opposite of C and JavaScript.

Common patterns

Even / odd and divisibility
The classic use.
if n % 2 == 0:
    even()
Wrap-around indexing
Python’s divisor-signed % makes circular indexes just work — even negative ones.
next_player = (i + 1) % n_players
prev_player = (i - 1) % n_players  # works at i == 0!
Cycling through buckets
Every k-th item, clock arithmetic, striping.
bucket = hash(key) % n_buckets

Examples

1. Basic remainder
7 % 3
Returns
1
2. Negative dividend
-7 % 3
Returns
2
3. Negative divisor
7 % -3
Returns
-2
4. Divisibility test
10 % 2
Returns
0

Pitfalls

1. C/JS intuition breaks on negatives
Same expression, different answer across languages.
JS gives -1
// JavaScript
-7 % 3
-1
Python
# Python
-7 % 3
2 — sign follows the divisor
2. Modulo by zero raises
Same guard as division.
Raises
5 % 0
ZeroDivisionError: integer division or modulo by zero
Guard
r = a % b if b else 0
explicit choice
3. % on strings is formatting
The old printf-style operator — unrelated to arithmetic.
Different feature
"%s scored %d" % ("Ann", 9)
'Ann scored 9'
Modern form
f"{name} scored {score}"
f-strings supersede it

When to use

Use it
  • Divisibility and parity checks
  • Wrap-around / circular indexing (negatives included)
  • Hashing into buckets, striping work
Reach for something else
  • C-style remainder semantics → math.fmod
  • Quotient too → divmod()
  • String formatting → f-strings

Notes

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

FAQ

It keeps a % n in the range [0, n) for positive n — so circular structures (clocks, rings, buffers) never see a negative index. The invariant a == b*(a//b) + (a%b) ties it to floor division.

History

1.0
Core operator; divisor-signed semantics from the start.