NUMPY BASIC EXERCISE 3


Create a program that returns True when all elements passed to the array are True.

Note: np.all() is a function that returns True when all elements of ndarray passed to the first parameter are True, and returns False otherwise.

Code

import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9,10])
print("Initial array:")
print(arr)
print("Applying the np.all() function")
print(np.all(arr))
arr = np.array([0,1,2,3,4,5,6,7,8,9,10])
print("Array with a zero:")
print(arr)
print("Applying the np.all() function again")
print(np.all(arr))

Output

Initial array:
[ 1  2  3  4  5  6  7  8  9 10]
Applying the np.all() function
True
Array with a zero:
[ 0  1  2  3  4  5  6  7  8  9 10]
Applying the np.all() function again
False

Code Editor

import numpy as np arr = np.array([1,2,3,4,5,6,7,8,9,10]) print("Initial array:") print(arr) print("Applying the np.all() function") print(np.all(arr)) arr = np.array([0,1,2,3,4,5,6,7,8,9,10]) print("Array with a zero:") print(arr) print("Applying the np.all() function again") print(np.all(arr))