Friday, July 24, 2026
HomeLanguagesTest whether the elements of a given NumPy array is zero or...

Test whether the elements of a given NumPy array is zero or not in Python

In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy.all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value.

Syntax: numpy.all( array )

Parameters: An array

Return: Boolean value (True or False)

The elements of a given NumPy array is zero or not in Python

Example 1:

Here we can see that the array is passes to the all() built-in function in Python and as there is no zero in the array it returns True.

Python




# import numpy library
import numpy as np
 
# create an array
x = np.array([34, 56, 89,
              23, 69, 980,
              567])
 
# print array
print(x)
 
# Test if none of the elements
# of the said array is zero
print(np.all(x))


Output:

[ 34  56  89  23  69 980 567]
True

Example 2:

Here we can see that the array is passes to the all function and as there is a zero present in the array it returns False.

Python




# import numpy library
import numpy as np
 
# create an array
x = np.array([1, 2, 3,
              4, 6, 7,
              8, 9, 10,
              0, 89, 67])
 
# print array
print(x)
 
# Test if none of the elements
# of the said array is zero
print(np.all(x))


Output:

[ 1  2  3  4  6  7  8  9 10  0 89 67]
False
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS