🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.mod()

AI & DATA SCIENCE // np-mod

np.mod() computes the element-wise remainder of division between two arrays, and is exactly what the % operator calls on ndarrays, matching Python's own modulo sign convention.

Syntax

np.mod(x1, x2)
x1 % x2

Deep Dive Course

np.mod(a, b) returns the remainder of a divided by b for each pair of elements, and like Python's built-in %, the result always takes the sign of the divisor, b, not the dividend, a — so -7 mod 3 is 2, not -1, since the result's sign follows the positive divisor. This differs from the C/Java-style remainder operator, which instead takes the sign of the dividend, so translating modulo-based code from those languages into NumPy/Python can silently produce different results for negative operands.

1Understanding np.mod()

np.mod(a, b) returns the remainder of a divided by b for each pair of elements, and like Python's built-in %, the result always takes the sign of the divisor, b, not the dividend, a — so -7 mod 3 is 2, not -1, since the result's sign follows the positive divisor. This differs from the C/Java-style remainder operator, which instead takes the sign of the dividend, so translating modulo-based code from those languages into NumPy/Python can silently produce different results for negative operands.

💡

If you're porting an algorithm from C, Java, or JavaScript that relies on the sign of a modulo result, double-check it against Python's floor-division-based convention — negative operands can produce a different sign than you'd get in those other languages.

editor.html
import numpy as np

a = np.array([10, 11, 12])
b = np.array([3, 3, 3])
print(np.mod(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.mod() showing how it is used in production NumPy code.

editor.html
import numpy as np

print(np.mod(-7, 3))
print(np.fmod(-7, 3))
localhost:3000

3Best Practices

Follow these guidelines when working with np.mod():

1. Double-check modulo behavior with negative operands specifically when porting code from a language with a different remainder-sign convention

2. Use np.mod() (or %) for wraparound logic like cyclic indexing, since its always-positive-with-positive-divisor result is usually exactly what that needs

3. Use np.fmod() instead of np.mod() specifically when you need C-style, dividend-sign remainder behavior for compatibility with another system

⚠️

Tip: If you're porting an algorithm from C, Java, or JavaScript that relies on the sign of a modulo result, double-check it against Python's floor-division-based convention — negative operands can produce a different sign than you'd get in those other languages.

editor.html
import numpy as np

a = np.array([10, 11, 12])
b = np.array([3, 3, 3])
print(np.mod(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([10, 11, 12])
b = np.array([3, 3, 3])
print(np.mod(a, b))
Example 02Advanced Example
import numpy as np

print(np.mod(-7, 3))
print(np.fmod(-7, 3))

Best Practices

  • Double-check modulo behavior with negative operands specifically when porting code from a language with a different remainder-sign convention
  • Use np.mod() (or %) for wraparound logic like cyclic indexing, since its always-positive-with-positive-divisor result is usually exactly what that needs
  • Use np.fmod() instead of np.mod() specifically when you need C-style, dividend-sign remainder behavior for compatibility with another system

Interview Question

Why does np.mod(-7, 3) return 2, while some other languages' modulo operator would return -1 for the same inputs?

Hint: Think about which operand's sign the result follows.

NumPy's np.mod(), matching Python's own % operator, always returns a result with the same sign as the divisor, based on floor division — for -7 divided by 3, floor division gives -3, and -7 minus -3 times 3 is 2, a positive result matching the positive divisor. Languages like C and Java instead define their remainder operator to take the sign of the dividend, using truncating division, which for the same inputs produces -1. NumPy does provide np.fmod() specifically for that C-style, dividend-sign behavior when compatibility with such systems is needed.

Exercises

MediumPractice using np.mod() in a real scenario.
View Solution
import numpy as np

a = np.array([10, 11, 12])
b = np.array([3, 3, 3])
print(np.mod(a, b))

Frequently Asked Questions

Why does np.mod(-7, 3) return 2, while some other languages' modulo operator would return -1 for the same inputs?

NumPy's np.mod(), matching Python's own % operator, always returns a result with the same sign as the divisor, based on floor division — for -7 divided by 3, floor division gives -3, and -7 minus -3 times 3 is 2, a positive result matching the positive divisor. Languages like C and Java instead define their remainder operator to take the sign of the dividend, using truncating division, which for the same inputs produces -1. NumPy does provide np.fmod() specifically for that C-style, dividend-sign behavior when compatibility with such systems is needed.

Related Functions

np-dividearithmetic-operatorsnp-clip