Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using np.any() and np.all() to Check Conditions in Arrays
📖 Scenario: Imagine you are analyzing a small dataset of daily temperatures recorded over a week. You want to check if any day was very hot or if all days were above freezing.
🎯 Goal: You will create a NumPy array of temperatures, set a temperature threshold, use np.any() and np.all() to check conditions, and print the results.
📋 What You'll Learn
Create a NumPy array called temperatures with the exact values: 12, 15, 22, 28, 19, 24, 30
Create a variable called hot_threshold and set it to 25
Use np.any() to check if any temperature is greater than hot_threshold and store the result in any_hot
Use np.all() to check if all temperatures are above 0 and store the result in all_above_freezing
Print the values of any_hot and all_above_freezing
💡 Why This Matters
🌍 Real World
Checking if any or all values in a dataset meet certain conditions is common in data analysis, like finding if any sales exceeded a target or if all sensor readings are within safe limits.
💼 Career
Data scientists and analysts often use <code>np.any()</code> and <code>np.all()</code> to quickly summarize data quality or detect important patterns in large datasets.
Progress0 / 4 steps
1
Create the temperatures array
Import NumPy as np and create a NumPy array called temperatures with these exact values: 12, 15, 22, 28, 19, 24, 30
NumPy
Hint
Use np.array() to create the array with the exact numbers inside square brackets.
2
Set the hot temperature threshold
Create a variable called hot_threshold and set it to the number 25
NumPy
Hint
Just assign the number 25 to the variable hot_threshold.
3
Check conditions with np.any() and np.all()
Use np.any() to check if any value in temperatures is greater than hot_threshold and save it in any_hot. Then use np.all() to check if all values in temperatures are greater than 0 and save it in all_above_freezing
NumPy
Hint
Use the comparison inside the parentheses of np.any() and np.all() to check the conditions.
4
Print the results
Print the values of any_hot and all_above_freezing on separate lines
NumPy
Hint
Use two print() statements, one for each variable.
Practice
(1/5)
1. What does np.any() do when applied to a NumPy array?
easy
A. Returns True if at least one element is True
B. Returns True only if all elements are True
C. Returns the sum of all elements
D. Returns the number of True elements
Solution
Step 1: Understand the function purpose
np.any() checks if any element in the array is True.
Step 2: Compare with other options
It does not require all elements to be True, only one is enough.
Final Answer:
Returns True if at least one element is True -> Option A
Quick Check:
np.any() = True if any True [OK]
Hint: np.any() means 'any True?' if yes, returns True [OK]
Common Mistakes:
Confusing np.any() with np.all()
Thinking it counts True elements
Assuming it returns False if one element is False
2. Which of the following is the correct syntax to check if all elements in a NumPy array arr are True?
easy
A. np.any(arr)
B. np.all(arr, axis=1)
C. np.all(arr)
D. arr.any()
Solution
Step 1: Identify the function for all True check
np.all(arr) returns True only if all elements are True.
Step 2: Check other options
np.any(arr) checks if any element is True, arr.any() checks if any element is True, np.all(arr, axis=1) checks along axis 1, not whole array.
Final Answer:
np.all(arr) -> Option C
Quick Check:
np.all() = all True check [OK]
Hint: Use np.all(arr) to check if everything is True [OK]
Common Mistakes:
Using np.any() instead of np.all()
Confusing axis parameter usage
Using arr.any() or arr.all() without knowing difference
3. Given the array arr = np.array([[True, False], [True, True]]), what is the output of np.any(arr, axis=0)?
medium
A. [True, True]
B. [True, False]
C. [False, True]
D. [False, False]
Solution
Step 1: Understand axis=0 operation
Axis 0 means checking down each column for any True value.
Step 2: Check each column
First column: True and True -> any True = True Second column: False and True -> any True = True
Final Answer:
[True, True] -> Option A
Quick Check:
np.any(arr, axis=0) = column-wise any True [OK]
Hint: axis=0 checks columns; any True in column returns True [OK]
Common Mistakes:
Mixing axis=0 with axis=1
Confusing np.any() with np.all()
Misreading array shape
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3, 0])
result = np.all(arr)
print(result)
medium
A. np.all() cannot be used on numeric arrays
B. np.all() returns False because 0 is treated as False
C. Syntax error in np.all() usage
D. np.all() returns True regardless of values
Solution
Step 1: Understand np.all() on numeric arrays
np.all() treats 0 as False and non-zero as True.
Step 2: Analyze the array values
Array has 0, which is False, so np.all(arr) returns False.
Final Answer:
np.all() returns False because 0 is treated as False -> Option B
Quick Check:
np.all([1,2,3,0]) = False due to zero [OK]
Hint: np.all() treats 0 as False, so result is False [OK]
Common Mistakes:
Thinking np.all() only works on booleans
Expecting True despite zero in array
Assuming syntax error
5. You have a 2D NumPy array data representing test results where True means pass and False means fail. How do you find which rows have all tests passed?
hard
A. np.any(data, axis=0)
B. np.any(data, axis=1)
C. np.all(data, axis=0)
D. np.all(data, axis=1)
Solution
Step 1: Understand the problem context
We want rows where all tests are passed (all True in that row).
Step 2: Use np.all() along rows
Using axis=1 checks each row. np.all(data, axis=1) returns True for rows with all True.
Final Answer:
np.all(data, axis=1) -> Option D
Quick Check:
np.all(axis=1) = all True per row [OK]
Hint: Use np.all(data, axis=1) to check all True in each row [OK]
Common Mistakes:
Using np.any() instead of np.all() for all pass check