0
0
NumPydata~10 mins

np.any() and np.all() in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if any element in the array is True.

NumPy
import numpy as np
arr = np.array([False, False, True, False])
result = np.[1](arr)
print(result)
Drag options to blanks, or click blank then click option'
Aall
Bsum
Cany
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.all() which checks if all elements are True.
Using sum() or mean() which return numeric values, not boolean.
2fill in blank
medium

Complete the code to check if all elements in the array are True.

NumPy
import numpy as np
arr = np.array([True, True, True])
result = np.[1](arr)
print(result)
Drag options to blanks, or click blank then click option'
Aall
Bany
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.any() which returns True if any element is True.
Using max() or min() which return numeric values.
3fill in blank
hard

Fix the error in the code to correctly check if any element is True along axis 0.

NumPy
import numpy as np
arr = np.array([[False, True], [False, False]])
result = np.any(arr, axis=[1])
print(result)
Drag options to blanks, or click blank then click option'
A0
B2
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 which checks rows instead of columns.
Using axis=2 which is invalid for 2D arrays.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if all characters are alphabetic.

NumPy
words = ['apple', 'banana', '123', 'pear']
lengths = {word: len(word) for word in words if word.[1]() and all(c.[2]() for c in word)}
print(lengths)
Drag options to blanks, or click blank then click option'
Aisalpha
Bisdigit
Dislower
Attempts:
3 left
💡 Hint
Common Mistakes
Using isdigit() which checks for digits, not letters.
Using islower() which checks for lowercase but not alphabetic.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if any character is uppercase and all characters are alphabetic.

NumPy
words = ['Apple', 'BANANA', '123', 'Pear']
result = {word.[1](): len(word) for word in words if any(c.[2]() for c in word) and all(c.[3]() for c in word)}
print(result)
Drag options to blanks, or click blank then click option'
Aupper
Bisupper
Cisalpha
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper() to convert words.
Using islower() or isdigit() instead of isupper() or isalpha().