Python Practice › NumPy Programs › Program 2
Test that no element is zero
Write a NumPy program to test whether none of the elements of an array is zero.
Input
[6, 8, 3, 5]
Output
True False
Approach
np.all(arr)is True when every element is non-zero.
Solution
import numpy as npprint(np.all(np.array([6, 8, 3, 5])), np.all(np.array([6, 0, 3])))Output
True False
Line by line
Line 2
Zero counts as False, so a single 0 makes all False.
Learn the concept
New to this topic? Read the lesson, then come back.