w3resource
NumPy Exercises

NumPy: Test whether none of the elements of a given array is zero

NumPy: Basic Exercise-3 with Solution

Write a NumPy program to test whether none of the elements of a given array is zero.

Sample Solution :

Python Code :

import numpy as np
x = np.array([1, 2, 3, 4])
print("Original array:")
print(x)
print("Test if none of the elements of the said array is zero:")
print(np.all(x))
x = np.array([0, 1, 2, 3])
print("Original array:")
print(x)
print("Test if none of the elements of the said array is zero:")
print(np.all(x))

Sample Output:

Original array:
[1 2 3 4]
Test if none of the elements of the said array is zero:
True
Original array:
[0 1 2 3]
Test if none of the elements of the said array is zero:
False                         

Pictorial Presentation:

NumPy: Test whether none of the elements of a given array is zero.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to get help on the add function.
Next: Write a NumPy program to test if any of the elements of a given array is non-zero.

What is the difficulty level of this exercise?