Bird
Raised Fist0
NumPydata~10 mins

Creating boolean arrays in NumPy - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a boolean array where elements are True if they are greater than 5.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
bool_arr = arr [1] 5
Drag options to blanks, or click blank then click option'
A==
B>
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' returns True only for elements equal to 5.
Using '<' or '<=' returns True for smaller or equal elements, not greater.
2fill in blank
medium

Complete the code to create a boolean array where elements are True if they are even numbers.

NumPy
import numpy as np
arr = np.array([1, 4, 7, 8, 10])
bool_arr = (arr [1] 2) == 0
Drag options to blanks, or click blank then click option'
A-
B+
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '%' does not check for even numbers.
Using '*' multiplies and does not help in checking evenness.
3fill in blank
hard

Fix the error in the code to create a boolean array where elements are True if less than or equal to 3.

NumPy
import numpy as np
arr = np.array([1, 4, 3, 2, 5])
bool_arr = arr [1] 3
Drag options to blanks, or click blank then click option'
A<
B=>
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' causes syntax errors.
Using '<' excludes elements equal to 3.
Using '==' only matches elements exactly equal to 3.
4fill in blank
hard

Fill both blanks to create a boolean array where elements are True if they are between 3 and 7 inclusive.

NumPy
import numpy as np
arr = np.array([2, 3, 5, 7, 8])
bool_arr = (arr [1] 3) & (arr [2] 7)
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' excludes boundary values.
Using '|' instead of '&' changes logic to 'or' instead of 'and'.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and boolean values indicating if word length is greater than 4.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
length_check = { [1]: len([2]) [3] 4 for [2] in words }
Drag options to blanks, or click blank then click option'
Aword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(word)' as key instead of 'word'.
Using '<' instead of '>' changes the condition.
Using different variable names for key and loop variable causes errors.

Practice

(1/5)
1. What does a boolean array in numpy represent?
easy
A. An array of integers only
B. An array of True/False values based on a condition
C. An array of strings
D. An array with only zeros

Solution

  1. Step 1: Understand boolean arrays

    A boolean array stores True or False for each element, usually from a condition.
  2. Step 2: Identify the correct description

    The description 'An array of True/False values based on a condition' matches what a boolean array represents. Others refer to integers, strings, or zeros.
  3. Final Answer:

    An array of True/False values based on a condition -> Option B
  4. Quick Check:

    Boolean array = True/False values [OK]
Hint: Boolean arrays always hold True or False values [OK]
Common Mistakes:
  • Thinking boolean arrays hold numbers or strings
  • Confusing boolean arrays with integer arrays
2. Which of the following is the correct way to create a boolean array from a numpy array arr to check where elements are greater than 5?
easy
A. bool_arr = arr > 5
B. bool_arr = arr == 5
C. bool_arr = arr >= 5
D. bool_arr = arr < 5

Solution

  1. Step 1: Understand the condition for boolean array

    We want True where elements are greater than 5, so the condition is arr > 5.
  2. Step 2: Match the correct syntax

    bool_arr = arr > 5 uses arr > 5, which is correct. arr >= 5 checks greater than or equal to 5, arr == 5 checks equality to 5, and arr < 5 checks less than 5.
  3. Final Answer:

    bool_arr = arr > 5 -> Option A
  4. Quick Check:

    Condition for > 5 is arr > 5 [OK]
Hint: Use comparison operators directly on arrays for boolean arrays [OK]
Common Mistakes:
  • Using == instead of > for greater than
  • Confusing >= with >
  • Using wrong comparison operator
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 7, 4, 9])
bool_arr = arr < 5
print(bool_arr)
medium
A. [True False True False]
B. [False True False True]
C. [True True True True]
D. [False False False False]

Solution

  1. Step 1: Evaluate the condition arr < 5 for each element

    Elements: 2 < 5 is True, 7 < 5 is False, 4 < 5 is True, 9 < 5 is False.
  2. Step 2: Form the boolean array

    The boolean array is [True, False, True, False].
  3. Final Answer:

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

    Check each element < 5 = [True False True False] [OK]
Hint: Check each element against condition to form True/False array [OK]
Common Mistakes:
  • Mixing up True and False values
  • Forgetting to use < operator correctly
4. Identify the error in this code that tries to create a boolean array for elements equal to 10:
import numpy as np
arr = np.array([10, 5, 10, 3])
bool_arr = arr = 10
print(bool_arr)
medium
A. Array should be a list, not np.array
B. Missing import statement for numpy
C. Using '=' instead of '==' for comparison
D. print statement syntax error

Solution

  1. Step 1: Check the comparison operator

    The code uses arr = 10 which assigns 10 to arr, not compares.
  2. Step 2: Correct operator for comparison

    To compare elements to 10, use arr == 10 to create a boolean array.
  3. Final Answer:

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

    Comparison needs '==' not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
  • Using single '=' instead of '=='
  • Confusing assignment with comparison
5. Given a numpy array data = np.array([3, 8, 1, 6, 0]), how can you create a boolean array that is True for elements greater than 2 and less than 7?
hard
A. bool_arr = (data > 2) && (data < 7)
B. bool_arr = data > 2 or data < 7
C. bool_arr = data > 2 and data < 7
D. bool_arr = (data > 2) & (data < 7)

Solution

  1. Step 1: Understand element-wise logical operations in numpy

    Use bitwise operators & and | for element-wise AND/OR, not 'and' or 'or'.
  2. Step 2: Apply correct syntax for combined condition

    bool_arr = (data > 2) & (data < 7) uses (data > 2) & (data < 7), which is correct for element-wise AND.
  3. Final Answer:

    bool_arr = (data > 2) & (data < 7) -> Option D
  4. Quick Check:

    Use & for element-wise AND in numpy [OK]
Hint: Use & for element-wise AND, not 'and' [OK]
Common Mistakes:
  • Using 'and' instead of '&' for element-wise AND
  • Using '&&' which is invalid in Python
  • Using 'or' instead of '&' for AND condition