29 Mar NumPy – Array Slicing
Slicing the array in a range from start to end is what we call Array Slicing. Slice (range) is passed in place of index i.e.
The above will slice two elements from index 1 to index 3 because it includes the start index, but excludes the end index.
The following is the syntax to slice NumPy arrays:
Here, the start is the beginning index (include), and the end is the last index (excluded)
Slicing Examples
The following Array Slicing examples we will run:
- Slicing from index 1 to 3 (3 is excluded)
- Slicing from index 2 to 5 (5 is excluded)
- Slicing from index 5 to last
- Slicing from the beginning to index 5 (5 is excluded)
Example: Slicing from index 1 to 3 (3 is excluded)
Output
Example: Slicing from index 2 to 5 (5 is excluded)
Output
Example: Slicing from index 5 to last
Output
Example: Slicing from beginning to index 5 (5 is excluded)
Output
Slice 1D arrays with STEP
We can also include steps while slicing. Step means a jump of elements. If the step is 2, that means 2 jumps for the next element.
Syntax
Example
Output
Slicing 2D arrays with STEP
The following are the examples of slicing Two-Dimensional arrays with steps:
Example: Slicing from index 2 to 5 (excluding 5)
Output
Example: Slicing for both the dimensions with step
Output
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments