What is the result of modulo operator / percent sign in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the % operator returns the remainder after division, but with one important rule: the result has the same sign as the divisor. That makes Python’s modulo behavior different from what some developers expect from other languages. To use % correctly, it helps to understand its relationship to floor division and to test negative-number cases explicitly.
Basic Meaning of %
For positive integers, modulo behaves the way most people first learn it:
It answers the question, “what remainder is left after division?”
This is why modulo is so common for:
- even or odd checks,
- cyclic indexing,
- periodic scheduling,
- wrapping counters.
Python’s Defining Rule
Python defines modulo consistently with floor division:
That means the % result aligns with //, not with truncation toward zero. This is most visible with negative numbers.
Why?
Because:
Python floors the quotient, then computes the remainder from that quotient.
Same Sign as the Divisor
The easiest way to remember Python modulo is:
- the remainder has the same sign as the divisor.
So:
- '
10 % 3is positive because3is positive,' - '
10 % -3is negative because-3is negative.'
This rule makes arithmetic identities behave predictably and keeps % and // mathematically aligned.
Common Uses
Even or odd
Wrap around a fixed range
This is a common way to cycle through a list repeatedly.
Grouping by periodic pattern
Modulo is a natural tool for periodic behavior.
% with Floats
Python also allows modulo with floats:
This follows the same general rule, but floating-point arithmetic can introduce precision surprises. If exact decimal behavior matters, use decimal.Decimal or rethink whether floating-point modulo is the right model.
Use divmod When You Need Both Results
If you need both the quotient and the remainder, divmod is cleaner than computing them separately.
This also avoids repeating the same division logic twice.
% Is Not Only Arithmetic in Python
Historically, % is also used for old-style string formatting:
That usage is different from numeric modulo and can confuse beginners when they first encounter it. In new code, f-strings are usually clearer for formatting.
Common Pitfalls
- Assuming Python modulo behaves like languages that truncate division toward zero for negative numbers.
- Forgetting that the result takes the sign of the divisor, not the dividend.
- Using
%with floats without accounting for floating-point precision behavior. - Confusing arithmetic modulo with old-style string formatting syntax.
- Reimplementing quotient and remainder logic manually instead of using
divmodwhen both are needed.
Summary
- In Python,
%returns the remainder consistent with floor division. - The result has the same sign as the divisor.
- Negative-number cases such as
-10 % 3are often the ones that surprise people. - Modulo is useful for parity checks, cyclic indexing, and periodic grouping.
- Use
divmodwhen you need quotient and remainder together.
Related reading
- What is the right way to treat Python argparse.Namespace as a dictionary?
- What is the sequence of SessionRunHook's member function to be called?
- What is the source code of the this module doing?
- What is the standard way to add N seconds to datetime.time?
- What is the syntax to insert one list into another list in python?
- What is the threshold for the sklearn roc_auc_score
- What is the time complexity of heapq.nlargest?
- What is the time complexity of popping an element from a dict in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.