Slicing means accessing a sub‒part of an array(a range of elements).
Slicing
•
Slicing means accessing a sub‒part of an array(a range of elements).
array[start: end:
step]
where
start:
Index where slice begins.
end:
Index where slice ends.
step:
How many elements to skip
Following
python programs illustrate the use of slicing in NumPy.
import numpy as np
arr = np.array([10,20,30,40,50,60])
print("Displaying slice(elements from 1 to 3)")
print(arr[1:4])
print("Displaying first three elements as slice")
print(arr[:3])
print("Displaying every alternate element as slice")
print(arr[::2])
Displaying slice
(elements from 1 to 3)
[20 30 40]
Displaying first three
elements as slice
[10 20 30]
Displaying every
alternate element as slice
[10 30 50]
The
working of above code can be illustrated by following figure ‒

import numpy as np
arr = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
print("Slice second and third rows and first three
columns")
print(arr[1:3,0:3])
slice second and third
rows and first three columns
[[ 5 6 7]
[ 9 10 11]]
Following
figure illustrates how to slice a two dimensional array ‒

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - NumPy: Slicing
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation