0
0
NumPydata~10 mins

Logical operations (and, or, not) in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical operations (and, or, not)
Start with two boolean arrays
Apply 'and' operation element-wise
Apply 'or' operation element-wise
Apply 'not' operation element-wise
Get resulting boolean arrays
End
We start with two boolean arrays and apply logical 'and', 'or', and 'not' operations element-wise to get new boolean arrays.
Execution Sample
NumPy
import numpy as np

A = np.array([True, False, True, False])
B = np.array([True, True, False, False])

and_result = np.logical_and(A, B)
or_result = np.logical_or(A, B)
not_result = np.logical_not(A)
This code creates two boolean arrays and applies logical 'and', 'or', and 'not' operations element-wise.
Execution Table
StepOperationInput AInput BResult
1logical_and[True, False, True, False][True, True, False, False][True, False, False, False]
2logical_or[True, False, True, False][True, True, False, False][True, True, True, False]
3logical_not[True, False, True, False]N/A[False, True, False, True]
4EndN/AN/AAll operations complete
💡 All logical operations applied element-wise to arrays; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
A[True, False, True, False][True, False, True, False][True, False, True, False][True, False, True, False][True, False, True, False]
B[True, True, False, False][True, True, False, False][True, True, False, False][True, True, False, False][True, True, False, False]
and_resultN/A[True, False, False, False][True, False, False, False][True, False, False, False][True, False, False, False]
or_resultN/AN/A[True, True, True, False][True, True, True, False][True, True, True, False]
not_resultN/AN/AN/A[False, True, False, True][False, True, False, True]
Key Moments - 3 Insights
Why do we use np.logical_and instead of the Python 'and' operator?
Python 'and' does not work element-wise on arrays; np.logical_and applies 'and' to each pair of elements as shown in step 1 of the execution_table.
What happens if the input arrays have different lengths?
Numpy will raise an error because element-wise operations require arrays of the same shape, which is why inputs A and B have the same length in the execution_table.
Why does logical_not only take one input array?
Because 'not' is a unary operation that flips each element's boolean value individually, as shown in step 3 where only input A is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the result of logical_and for the third element?
AFalse
BTrue
CNone
DError
💡 Hint
Check the third element in the 'Result' column at step 1 in the execution_table.
At which step does the logical_not operation occur?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Operation' column in the execution_table to find where logical_not is applied.
If array B was changed to [False, False, False, False], what would be the result of logical_or at step 2?
A[True, True, True, True]
B[False, False, False, False]
C[True, False, True, False]
D[False, True, False, True]
💡 Hint
Logical_or returns True if either element is True; check variable_tracker for A and B values.
Concept Snapshot
Logical operations in numpy:
- Use np.logical_and(A, B) for element-wise AND
- Use np.logical_or(A, B) for element-wise OR
- Use np.logical_not(A) for element-wise NOT
- Inputs must be boolean arrays of same shape
- Results are boolean arrays of same shape
Full Transcript
This visual execution shows how numpy applies logical operations element-wise on boolean arrays. Starting with two arrays A and B, np.logical_and returns True only where both A and B are True. np.logical_or returns True where either A or B is True. np.logical_not flips each True to False and vice versa in array A. The execution table traces each step and the variable tracker shows how results build up. Key moments clarify why numpy functions are needed instead of Python's built-in operators. The quiz tests understanding of element-wise results and operation order.