Test that no element is zero

EasyNumPy Programs · 2 of 100

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

Advertisement
Input
[6, 8, 3, 5]
Output
True False

Approach

  1. np.all(arr) is True when every element is non-zero.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs