In this python numpy program, we will check whether the NumPy array is empty.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Get the size of the array (i.e. total number of elements) using size.
- Determine whether the given array is empty or not using an if-else statement.
- If the size of the array is 0 then the array is empty else the array is not empty.
- Print the respective output.
import numpy as np
x = np.array([4,8,0])
if x.size == 0:
print("Array is empty")
else:
print("Array is not empty")
Output :
Array is not empty