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, 30Create a variable called
hot_threshold and set it to 25Use
np.any() to check if any temperature is greater than hot_threshold and store the result in any_hotUse
np.all() to check if all temperatures are above 0 and store the result in all_above_freezingPrint 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