Slicing Syntax :
Python
array[start : stop : step] - start → index to begin (inclusive)
- stop → index to end (exclusive)
- step → step size (optional)
If omitted:
- start defaults to 0
- stop defaults to end of array
- step defaults to 1
Slicing a 1D Array :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr[1:4]) #output[20 30 40] Slicing with Step :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr[0:5:2]) #output [10 30 50] Slicing a 2D Array :
Python
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr[0:2]) #output [[1 2 3] [4 5 6]] Python
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr[0:2, 0:2]) #output [[1 2] [4 5]]