📖 Scenario: Imagine you work in a store that tracks daily sales of two products. You want to find days when certain sales conditions are met using logical operations.
🎯 Goal: You will create two NumPy arrays representing daily sales of two products. Then, you will use logical and, or, and not operations to find days matching specific sales conditions.
📋 What You'll Learn
Create two NumPy arrays named product_a_sales and product_b_sales with exact daily sales values.
Create a threshold variable named threshold with the exact value 50.
Use logical operations np.logical_and, np.logical_or, and np.logical_not on the sales arrays.
Print the final results arrays exactly as specified.
💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to find days when multiple products perform well or need attention.
💼 Career
Data analysts and scientists use logical operations on arrays to filter and analyze large datasets efficiently.
Progress0 / 4 steps
1
Create sales data arrays
Import NumPy as np. Create two NumPy arrays called product_a_sales and product_b_sales with these exact values: [30, 60, 45, 80, 55] and [20, 70, 50, 40, 65] respectively.
NumPy
Hint
Use np.array([...]) to create the arrays with the exact numbers given.
2
Set sales threshold
Create a variable called threshold and set it to the integer value 50.
NumPy
Hint
Just assign the number 50 to the variable named threshold.
3
Apply logical operations
Create three variables: both_above using np.logical_and to find days when both products have sales above threshold, either_above using np.logical_or to find days when either product has sales above threshold, and not_a_above using np.logical_not to find days when product A sales are NOT above threshold.
NumPy
Hint
Use comparisons like product_a_sales > threshold inside the logical functions.
4
Print the results
Print the variables both_above, either_above, and not_a_above each on a separate line in this order.
NumPy
Hint
Use three separate print() statements for each variable in the order given.
Practice
(1/5)
1. Which NumPy function is used to perform a logical AND operation element-wise on two arrays?
easy
A. numpy.logical_and
B. numpy.and_
C. numpy.bitwise_and
D. numpy.logical_or
Solution
Step 1: Understand logical AND operation in NumPy
Logical AND combines two boolean arrays and returns True only where both are True.
Step 2: Identify the correct NumPy function
numpy.logical_and is the function designed for element-wise logical AND on boolean arrays.
Final Answer:
numpy.logical_and -> Option A
Quick Check:
AND operation = numpy.logical_and [OK]
Hint: AND operation in NumPy uses logical_and function [OK]
Common Mistakes:
Confusing bitwise_and with logical_and
Using logical_or instead of logical_and
Using and_ which is not a NumPy function
2. Which of the following is the correct syntax to apply a logical NOT operation on a NumPy array a?
easy
A. numpy.logical_not[]
B. numpy.not(a)
C. numpy.logical_not(a)
D. not numpy.logical(a)
Solution
Step 1: Recall the correct function for logical NOT in NumPy
The function is numpy.logical_not(array) to invert boolean values element-wise.
Step 2: Check syntax correctness
numpy.logical_not(a) uses correct function and parentheses syntax; others are invalid or incorrect.
Final Answer:
numpy.logical_not(a) -> Option C
Quick Check:
NOT operation = numpy.logical_not(array) [OK]
Hint: Use logical_not with parentheses for NOT operation [OK]
Common Mistakes:
Using numpy.not which does not exist
Using square brackets instead of parentheses
Trying to negate with not keyword on arrays
3. What is the output of the following code?
import numpy as np
x = np.array([True, False, True])
y = np.array([False, False, True])
result = np.logical_or(x, y)
print(result)
medium
A. [False False False]
B. [False True True]
C. [True True True]
D. [True False True]
Solution
Step 1: Understand logical OR operation element-wise
logical_or returns True if either element in the pair is True.
Step 2: Apply logical_or to each pair in arrays x and y