Python
Modulo Operator
Percent Sign
Python Operators
Programming Basics

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.

Browse interview questions

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:

python
print(10 % 3)   # 1
print(14 % 5)   # 4
print(8 % 2)    # 0

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:

python
a % b == a - b * (a // b)

That means the % result aligns with //, not with truncation toward zero. This is most visible with negative numbers.

python
print(-10 % 3)   # 2
print(10 % -3)   # -2
print(-10 % -3)  # -1

Why?

Because:

python
print(-10 // 3)   # -4
print(10 // -3)   # -4

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 % 3 is positive because 3 is positive,'
  • '10 % -3 is negative because -3 is negative.'

This rule makes arithmetic identities behave predictably and keeps % and // mathematically aligned.

Common Uses

Even or odd

python
1def is_even(n):
2    return n % 2 == 0
3
4print(is_even(12))
5print(is_even(7))

Wrap around a fixed range

python
1items = ["A", "B", "C"]
2
3for i in range(7):
4    print(items[i % len(items)])

This is a common way to cycle through a list repeatedly.

Grouping by periodic pattern

python
for day in range(1, 15):
    print(day, "week slot", day % 7)

Modulo is a natural tool for periodic behavior.

% with Floats

Python also allows modulo with floats:

python
print(5.5 % 2.0)    # 1.5
print(-5.5 % 2.0)   # 0.5

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.

python
q, r = divmod(17, 5)
print(q, r)  # 3 2

This also avoids repeating the same division logic twice.

% Is Not Only Arithmetic in Python

Historically, % is also used for old-style string formatting:

python
name = "Ada"
print("Hello, %s" % name)

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 divmod when 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 % 3 are often the ones that surprise people.
  • Modulo is useful for parity checks, cyclic indexing, and periodic grouping.
  • Use divmod when you need quotient and remainder together.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.