0
0
NumPydata~15 mins

np.any() and np.all() in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use two print() statements, one for each variable.