Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

boolean-indexing

Boolean indexing

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Boolean indexing: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Boolean indexing in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Boolean indexing in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

A boolean array of the same shape (or broadcastable) selects elements where True. This is the idiomatic way to filter ndarrays.

Mask filtering

import numpy as np
a = np.array([1, -2, 3, -4, 5])
pos = a[a > 0]
print(pos)

Compound conditions

Combine with & (and), | (or), ~ (not)—each condition must be parenthesized: (a > 0) & (a < 10).

Assignment via mask

import numpy as np
a = np.array([1, 2, 3, 4, 5])
a[a % 2 == 0] = 0
print(a)

Important interview questions and answers

  1. Q: Why not use and/or keywords?
    A: Python and/or don't element-wise broadcast on arrays.
  2. Q: Boolean indexing copy?
    A: Returns a copy—assigning through mask modifies original in place.

Self-check

  1. Select elements between 2 and 8 inclusive.
  2. Set negative values to zero using a mask.

Pitfall: Use & and |, not Python and/or.

Interview prep

and vs &?

Use & | ~ with parentheses for element-wise boolean arrays.

Filter pattern?

arr[mask] selects True positions.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Mask dtype?
  • Fancy vs bool?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump