Do you want to **divide a NumPy array by a scalar**? In this Python blog, I will explain how **NumPy divide array by scalar in Python** using different methods and some illustrative examples. I will also explain what is **np.divide vs /**.

**To divide a NumPy array by a scalar in Python, we can use the / operator for simple element-wise division or numpy.divide for additional control over the operation. Handling division by zero is achieved with numpy.seterr. NumPy also supports element-wise division with broadcasting for arrays of different shapes, and in-place division using the /= operator.**

## NumPy Divide Array by Scalar in Python Methods

There are five different methods and use cases of NumPy divide array by scalar in Python:

- Basic array division by scalar
- Using numpy.divide
- Handling division by zero
- Element-wise division with broadcasting
- In-place division

Let’s see them one by one using some illustrative examples:

### 1. Divide NumPy array by scalar in Python using / operator

The simplest way NumPy divide array by scalar in Python is by using the **division operator /**. When we use this operator, each element in the array is divided by the scalar value. This operation is vectorized, meaning it’s efficiently implemented to work with arrays of any size.

Here is an example:

```
import numpy as np
populations = np.array([8.4, 3.9, 2.7, 2.3, 1.7])
result = populations / 2
print(result)
```

**Output:**

`[4.2 1.95 1.35 1.15 0.85]`

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

### 2. NumPy divide by scalar using np.divide()

A function called **np.divide** can also be used by NumPy divide array by scalar in Python. It is particularly useful when we need additional control over the operation, such as specifying the data type of the output.

Here’s a basic example to illustrate how this works:

```
import numpy as np
temperatures = np.array([68, 75, 59, 85, 72])
result = np.divide(temperatures, 2)
print(result)
```

**Output:**

`[34. 37.5 29.5 42.5 36. ]`

The output from running the code in PyCharm is visually represented in the screenshot below.

### 3. NumPy array divide all elements by scalar handling with Division by zero

When NumPy divide array by scalar in Python, there’s a potential risk of dividing by zero, which can raise an error or result in inf or nan values. To handle this, we can use **numpy.seterr** to control how NumPy handles such errors.

For instance:

```
import numpy as np
rainfall = np.array([49.9, 14.8, 36.9, 49.8, 8.2])
np.seterr(divide='ignore')
result = rainfall / 0
print(result)
```

**Output:**

`[inf inf inf inf inf]`

The screenshot below showcases the output generated by running the code within the PyCharm editor.

### 4. Divide array by scalar Python element-wise with broadcasting

In NumPy, we can also divide arrays of different shapes using broadcasting. For example, if we have a 2D array and a scalar, NumPy will automatically broadcast the scalar to match the shape of the array.

For example:

```
import numpy as np
distances = np.array([[0, 2446, 713, 1625, 2298],
[2446, 0, 1745, 1374, 372],
[713, 1745, 0, 925, 1447]])
result = distances / 2
print(result)
```

**Output:**

```
[[ 0. 1223. 356.5 812.5 1149. ]
[1223. 0. 872.5 687. 186. ]
[ 356.5 872.5 0. 462.5 723.5]]
```

Below is an image displaying the results of the code execution in the PyCharm environment.

### 5. NumPy divide all elements by scalar with in-place division

For memory efficiency, we might want to perform the division operation in place, modifying the original array. This can be done using the **/= operator**.

For instance:

```
import numpy as np
gdp_growth = np.array([2.5, 3.1, 2.8, 3.6, 3.0])
gdp_growth /= 2
print(gdp_growth)
```

**Output:**

`[1.25 1.55 1.4 1.8 1.5 ]`

The ensuing image captures the results produced by executing the code in PyCharm.

## What the np.divide vs / is

Below is a table comparing np.divide and the / operator for dividing an array in Python, particularly using NumPy. Both methods are commonly used for array division, but they have some differences in terms of functionality and usage.

Aspect | np.divide() | / (Division operator) |
---|---|---|

Syntax | np.divide(array, scalar) | array / scalar |

Flexibility | Offers additional parameters like out, where, and dtype. This allows for more control over the operation, like specifying the output data type or where the operation should be applied. | Simple and straightforward, used for basic division operations without extra parameters. |

Error Handling | Can handle errors like division by zero using parameters. It also allows setting up error handling globally using np.seterr. | Relies on the global error handling set by np.seterr. May result in inf, -inf, or nan without explicit error handling. |

Return Type | Always returns a new array unless the out parameter is specified. | Always returns a new array. |

Use Case | Preferred when you need more control over the division operation, such as defining the output type or handling divisions in a subset of the array. | Ideal for quick and simple division operations without the need for additional control. |

## Conclusion

Understanding different methods and use cases of how **NumPy divide array by scalar** in Python like / operator, numpy.divide, handling division by zero, element-wise division with broadcasting, and in-place division can help to solve one problem easily. Also, what is the main difference between np.divide() function and /(divide operator) too.

You may also like to read:

- np.median() function in Python
- np.count() function in Python
- np.diff() function in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.