Bird
Raised Fist0
NumPydata~10 mins

Boolean type in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Boolean type
Create Boolean variable
↓
Assign True or False
↓
Use in conditions or arrays
↓
Perform logical operations
↓
Get Boolean output or mask
This flow shows how Boolean variables are created, assigned, used in conditions or arrays, and combined with logical operations to produce Boolean outputs.
Execution Sample
NumPy
import numpy as np
b = np.array([True, False, True])
print(b)
print(b & np.array([False, False, True]))
Create a numpy Boolean array and perform element-wise AND operation with another Boolean array.
Execution Table
StepActionEvaluationResult
1Create numpy array bnp.array([True, False, True])[ True False True]
2Create second array for ANDnp.array([False, False, True])[False False True]
3Perform element-wise ANDb & second array[False False True]
4Print original array bprint(b)[ True False True]
5Print AND resultprint(b & second array)[False False True]
6End of executionExecution stops
💡 All steps executed; Boolean arrays created and combined with logical AND.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
bundefined[ True False True][ True False True][ True False True][ True False True]
second arrayundefinedundefined[False False True][False False True][False False True]
AND resultundefinedundefinedundefined[False False True][False False True]
Key Moments - 2 Insights
Why does the AND operation return an array of True/False instead of a single True/False?
Because both operands are numpy arrays, the & operator performs element-wise logical AND, producing a Boolean array as shown in execution_table step 3.
Can numpy Boolean arrays be used directly in if statements?
No, numpy Boolean arrays represent multiple values. Using them directly in if statements causes errors. Use methods like .any() or .all() to reduce to a single Boolean.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what is the result of the AND operation?
A[False False True]
B[True False True]
C[True True True]
D[False True False]
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
According to variable_tracker, what is the value of variable 'b' after step 1?
Aundefined
B[True False True]
C[False False True]
D[False True False]
💡 Hint
Look at the 'After Step 1' column for variable 'b' in variable_tracker.
If we change the second array to all True values, what would be the AND result at step 3?
A[True True True]
B[False False False]
C[True False True]
D[False True False]
💡 Hint
AND with all True values returns the original array, see execution_table step 3 logic.
Concept Snapshot
Boolean type in numpy:
- Use np.array([True, False]) to create Boolean arrays
- Supports element-wise logical operations (&, |, ~)
- Results are Boolean arrays
- Use .any() or .all() to reduce arrays to single Boolean
- Useful for masking and filtering data
Full Transcript
This lesson shows how to create and use Boolean types in numpy. We start by creating a Boolean array with True and False values. Then, we create a second Boolean array to combine with the first using the element-wise AND operator '&'. The result is a new Boolean array showing True only where both arrays had True. We print both the original and the result arrays. Variables 'b', 'second array', and 'AND result' change values step-by-step. Key points include understanding element-wise operations and that numpy Boolean arrays cannot be used directly in if statements without reduction. The quiz tests understanding of the AND operation result, variable values after steps, and effects of changing input arrays.

Practice

(1/5)
1. What does the bool type in numpy represent?
easy
A. Values that are either True or False
B. Integer numbers only
C. Floating point numbers
D. Text strings

Solution

  1. Step 1: Understand the boolean type meaning

    The boolean type stores only two possible values: True or False.
  2. Step 2: Compare with other types

    Integer, float, and string types store numbers or text, not boolean values.
  3. Final Answer:

    Values that are either True or False -> Option A
  4. Quick Check:

    Boolean type = True/False [OK]
Hint: Boolean means True or False only [OK]
Common Mistakes:
  • Confusing boolean with integer or float
  • Thinking boolean stores text
  • Assuming boolean can store multiple values
2. Which of the following is the correct way to create a numpy boolean array?
easy
A. np.array([1, 0, 1], dtype=str)
B. np.array([True, False, True], dtype=bool)
C. np.array([True, False, True], dtype=int)
D. np.array(['True', 'False'], dtype=bool)

Solution

  1. Step 1: Check dtype for boolean array

    To create a boolean array, use dtype=bool with True/False values.
  2. Step 2: Validate options

    np.array([True, False, True], dtype=bool) uses correct dtype and values. np.array([1, 0, 1], dtype=str) uses string dtype, not boolean. np.array([True, False, True], dtype=int) uses int dtype. np.array(['True', 'False'], dtype=bool) uses strings 'True', 'False' which are not boolean values.
  3. Final Answer:

    np.array([True, False, True], dtype=bool) -> Option B
  4. Quick Check:

    Boolean array needs dtype=bool [OK]
Hint: Use dtype=bool with True/False values [OK]
Common Mistakes:
  • Using string values instead of boolean
  • Setting wrong dtype like int or str
  • Confusing True/False strings with boolean
3. What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
print(mask)
medium
A. [3 4 5]
B. [True True True False False]
C. [False False False True True]
D. [1 1 1 0 0]

Solution

  1. Step 1: Evaluate the condition arr > 3

    For each element in arr, check if it is greater than 3: 1>3 False, 2>3 False, 3>3 False, 4>3 True, 5>3 True.
  2. Step 2: Understand the mask output

    The mask is a boolean array showing True where condition is met, False otherwise: [False False False True True].
  3. Final Answer:

    [False False False True True] -> Option C
  4. Quick Check:

    Condition mask = [False False False True True] [OK]
Hint: Compare array with condition to get boolean mask [OK]
Common Mistakes:
  • Confusing boolean mask with integer 1/0
  • Expecting filtered values instead of mask
  • Mixing up True and False positions
4. Find the error in this code snippet:
import numpy as np
arr = np.array([10, 20, 30])
mask = arr = 20
print(mask)
medium
A. print statement syntax error
B. Missing import statement for numpy
C. Array elements are not boolean
D. Using '=' instead of '==' for comparison

Solution

  1. Step 1: Identify the assignment vs comparison

    The code uses 'mask = arr = 20' which assigns 20 to arr and mask, not a comparison.
  2. Step 2: Correct the comparison operator

    To create a boolean mask, use 'mask = arr == 20' to compare each element with 20.
  3. Final Answer:

    Using '=' instead of '==' for comparison -> Option D
  4. Quick Check:

    Use '==' for comparison, '=' is assignment [OK]
Hint: Use '==' to compare, not '=' assignment [OK]
Common Mistakes:
  • Using single '=' instead of '=='
  • Confusing assignment with comparison
  • Expecting boolean mask but getting int
5. Given a numpy array data = np.array([5, 10, 15, 20, 25]), how can you use a boolean mask to select only values greater than 12 and less than 22?
hard
A. mask = (data > 12) & (data < 22); result = data[mask]
B. mask = data > 12 or data < 22; result = data[mask]
C. mask = data > 12 and data < 22; result = data[mask]
D. mask = (data > 12) | (data < 22); result = data[mask]

Solution

  1. Step 1: Create correct boolean mask with logical AND

    To select values greater than 12 and less than 22, use element-wise AND: (data > 12) & (data < 22).
  2. Step 2: Apply mask to data array

    Use the mask to index data: data[mask] returns values meeting both conditions.
  3. Final Answer:

    mask = (data > 12) & (data < 22); result = data[mask] -> Option A
  4. Quick Check:

    Use '&' for element-wise AND in numpy [OK]
Hint: Use '&' for AND, '|' for OR in numpy boolean masks [OK]
Common Mistakes:
  • Using 'and' or 'or' instead of '&' or '|'
  • Forgetting parentheses around conditions
  • Using '|' (OR) instead of '&' (AND) for both conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard