Updated April 19, 2023

## Introduction to NumPy divide

Whenever there is a need to perform division among the elements of first array by the elements of the second array in python, we make use of the function divide in numpy and this process takes place with each element in the two arrays. And hence the two arrays must be of the same shape to perform division between the elements of the first array and the elements of the second array and as well know, division by zero is not possible and hence we need to make sure the second array whose elements are to be divisor of the elements of the first array are never zero.

**Syntax of NumPy divide function in Python:**

`numpy.divide(arrayname1,arrayname2,)`

Where,

arrayname1 is the name of the first array whose elements are to be divided by the elements of the second array represented by the name arrayname2 and both the arrays must be of the same shape to perform division between the elements of the first array and the elements of the second array.

### Working of NumPy divide Function

- Whenever there is a need to perform division among the elements of first array by the elements of the second array in python, we make use of the function divide in numpy.
- The process of division takes place with each element in the two arrays and hence the two arrays must be of the same shape to perform division between the elements of the first array and the elements of the second array.
- As well know, division by zero is not possible and hence we need to make sure the second array whose elements are to be divisor of the elements of the first array are never zero.
- The numpy divide function takes two parameters as arguments namely arrayname1 and arrayname2.
- arrayname1 is the name of the first array whose elements are to be divided by the elements of the second array represented by the name arrayname2 and both the arrays must be of the same shape to perform division between the elements of the first array and the elements of the second array.

### Examples of NumPy divide

Given below are the examples of NumPy divide:

#### Example #1

Python program to demonstrate NumPy divide function to create two arrays of the same shape and then use divide function to divide the elements of the first array by the elements of the second array.

**Code:**

```
#importing the package numpy
import numpy as n
#creating an array using the array function and storing it in the variable array1
array1 = n.array([[10,20,30,40]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print array1
print '\n'
#creating an array using the array function and storing it in the variable array2
array2 = n.array([[10,20,30,40]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print array2
print '\n'
#using divide function to divide the elements of the first array by the elements of the second array and storing it in a variable called resultarray
resultarray = n.divide(array1,array2)
#displaying the elements of the resultant array
print 'The elements of the resultant array after dividing the elements of the first array by the elements of the second array:'
print resultarray
print '\n'
```

**Output:**

**Explanation:**

- In the above program, we are importing the package called numpy using which we are able to make use of array function to create new arrays and divide function to divide the elements of the first array by the elements of the second array.
- Then an array called array1 is created using the array function whose elements are to be divided by the elements of another newly created array using array function called array2.
- Then divide function is used to divide the elements of the first array array1 by the elements of the second array array2. Then the elements of the resultant array are displayed.

#### Example #2

Python program to demonstrate NumPy divide function to create two arrays of the same shape and then use divide function to divide the elements of the first array by the elements of the second array.

**Code:**

```
#importing the package numpy
import numpy as n
#creating an array using the array function and storing it in the variable array1
array1 = n.array([[5,10,15,20]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print array1
print '\n'
#creating an array using the array function and storing it in the variable array2
array2 = n.array([[5,5,5,5]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print array2
print '\n'
#using divide function to divide the elements of the first array by the elements of the second array and storing it in a variable called resultarray
resultarray = n.divide(array1,array2)
#displaying the elements of the resultant array
print 'The elements of the resultant array after dividing the elements of the first array by the elements of the second array:'
print resultarray
print '\n'
```

**Output:**

**Explanation:**

- In the above program, we are importing the package called numpy using which we are able to make use of array function to create new arrays and divide function to divide the elements of the first array by the elements of the second array.
- Then an array called array1 is created using the array function whose elements are to be divided by the elements of another newly created array using array function called array2.
- Then divide function is used to divide the elements of the first array array1 by the elements of the second array array2. Then the elements of the resultant array are displayed.

### Recommended Articles

This is a guide to NumPy divide. Here we discuss the introduction, working of NumPy divide function along with examples respectively. You may also have a look at the following articles to learn more –