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()

Parameters: An array

Return: A boolean value (True or False)

Let’s see the examples:



Example 1:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

[34,56,89,23,69,980,567]
True

Example 2:

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

False

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.