NumPy Tutorial · NumPy Tutorial

NumPy Array Slicing

Learn all about NumPy Array Slicing in this comprehensive tutorial.

5 min read intermediate
  • Slicing in python means taking elements from one given index to another given index.
  • Use the minus operator to refer to an index from the end:
  • Use the step value to determine the step of the slicing:

Slicing arrays

Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

python
Note: Note: The result includes the start index, but excludes the end index.
python
python

Negative Slicing

Use the minus operator to refer to an index from the end:

python

STEP

Use the step value to determine the step of the slicing:

python
python

Slicing 2-D Arrays

python
Note: Note: Remember that second element has index 1.
python
python

Module quiz

2 questions
1

Which of the following is true about NumPy Array Slicing?

2

What is the most common pitfall when working with NumPy Array Slicing?

Answer all questions to submit.