🚀 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.divide()

AI & DATA SCIENCE // np-divide

np.divide() performs true (floating-point) division between two arrays, element-wise, and is exactly what the / operator calls on ndarrays.

Syntax

np.divide(x1, x2)
x1 / x2

Deep Dive Course

np.divide(a, b) always returns a floating-point result, even when dividing two integer arrays evenly, matching Python 3's own true-division behavior for /. Dividing by zero doesn't raise a Python exception the way plain Python division does — instead, NumPy emits a RuntimeWarning and fills the corresponding position with inf, for a nonzero numerator, or nan, for zero divided by zero, letting the rest of the array's computation continue rather than halting the whole operation.

1Understanding np.divide()

np.divide(a, b) always returns a floating-point result, even when dividing two integer arrays evenly, matching Python 3's own true-division behavior for /. Dividing by zero doesn't raise a Python exception the way plain Python division does — instead, NumPy emits a RuntimeWarning and fills the corresponding position with inf, for a nonzero numerator, or nan, for zero divided by zero, letting the rest of the array's computation continue rather than halting the whole operation.

💡

Check for zero denominators explicitly before dividing, or use np.errstate() to control warning behavior, if inf/nan results from a division-by-zero would cause problems further down your computation.

editor.html
import numpy as np

a = np.array([10, 20, 30])
b = np.array([2, 5, 3])
print(np.divide(a, b))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

a = np.array([1.0, 2.0, 0.0])
b = np.array([0.0, 4.0, 0.0])
result = np.divide(a, b)
print(result)
localhost:3000

3Best Practices

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

1. Guard against zero denominators explicitly, or use np.where() to substitute a safe value, when division by zero is a realistic possibility

2. Use np.errstate(divide='ignore') deliberately, and briefly, when you intentionally expect and handle inf/nan results, to suppress the runtime warning noise

3. Prefer np.divide(a, b, out=..., where=condition) to skip computing division entirely for elements where it isn't valid or needed

⚠️

Tip: Check for zero denominators explicitly before dividing, or use np.errstate() to control warning behavior, if inf/nan results from a division-by-zero would cause problems further down your computation.

editor.html
import numpy as np

a = np.array([10, 20, 30])
b = np.array([2, 5, 3])
print(np.divide(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([10, 20, 30])
b = np.array([2, 5, 3])
print(np.divide(a, b))
Example 02Advanced Example
import numpy as np

a = np.array([1.0, 2.0, 0.0])
b = np.array([0.0, 4.0, 0.0])
result = np.divide(a, b)
print(result)

Best Practices

  • Guard against zero denominators explicitly, or use np.where() to substitute a safe value, when division by zero is a realistic possibility
  • Use np.errstate(divide='ignore') deliberately, and briefly, when you intentionally expect and handle inf/nan results, to suppress the runtime warning noise
  • Prefer np.divide(a, b, out=..., where=condition) to skip computing division entirely for elements where it isn't valid or needed

Interview Question

What happens when you divide by zero in a NumPy array, compared to dividing by zero with plain Python numbers?

Hint: Think about exceptions versus special floating-point values.

Plain Python division by zero raises a ZeroDivisionError immediately, halting execution at that line. NumPy's array division instead follows the IEEE-754 floating-point standard: dividing a nonzero number by zero produces inf, or -inf for a negative numerator, and dividing zero by zero produces nan, while also emitting a RuntimeWarning to flag that something numerically unusual happened — but it does not raise an exception, letting the rest of the array's elements finish computing normally.

Exercises

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

a = np.array([10, 20, 30])
b = np.array([2, 5, 3])
print(np.divide(a, b))

Frequently Asked Questions

What happens when you divide by zero in a NumPy array, compared to dividing by zero with plain Python numbers?

Plain Python division by zero raises a ZeroDivisionError immediately, halting execution at that line. NumPy's array division instead follows the IEEE-754 floating-point standard: dividing a nonzero number by zero produces inf, or -inf for a negative numerator, and dividing zero by zero produces nan, while also emitting a RuntimeWarning to flag that something numerically unusual happened — but it does not raise an exception, letting the rest of the array's elements finish computing normally.

Related Functions

np-multiplynp-modnp-isnan