NUMPY BASIC EXERCISE 4


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

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

Code

import numpy as np
arr = np.array([1, 0,0,0,1])
print("Initial array:")
print(arr)
print("Applying the np.any() function")
print(np.any(arr))
arr = np.array([0,0,0,0])
print("Array with all zeros:")
print(arr)
print("Applying the np.any() function again")
print(np.any(arr))

Output

Initial array:
[1 0 0 0 1]
Applying the np.any() function
True
Array with all zero:
[0 0 0 0]
Applying the np.any() function again
False

Code Editor

import numpy as np arr = np.array([1, 0,0,0,1]) print("Initial array:") print(arr) print("Applying the np.any() function") print(np.any(arr)) arr = np.array([0,0,0,0]) print("Array with all zeros:") print(arr) print("Applying the np.any() function again") print(np.any(arr))